-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathttl_grace_test.go
More file actions
156 lines (137 loc) · 5.23 KB
/
ttl_grace_test.go
File metadata and controls
156 lines (137 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package setddblock_test
import (
"context"
"testing"
"time"
"strconv"
"github.com/Songmu/flextime"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/mashiike/setddblock"
"github.com/stretchr/testify/require"
)
func ensureTableWithTTL(t *testing.T, client *dynamodb.Client, tableName string) {
t.Helper()
// Describe
_, err := client.DescribeTable(context.Background(), &dynamodb.DescribeTableInput{TableName: aws.String(tableName)})
if err == nil {
return
}
// Create
_, err = client.CreateTable(context.Background(), &dynamodb.CreateTableInput{
TableName: aws.String(tableName),
AttributeDefinitions: []types.AttributeDefinition{
{AttributeName: aws.String("ID"), AttributeType: types.ScalarAttributeTypeS},
},
KeySchema: []types.KeySchemaElement{
{AttributeName: aws.String("ID"), KeyType: types.KeyTypeHash},
},
BillingMode: types.BillingModePayPerRequest,
})
require.NoError(t, err)
// Wait a moment for table to become active (simple sleep is fine here)
time.Sleep(500 * time.Millisecond)
// Enable TTL on attribute `ttl`
_, err = client.UpdateTimeToLive(context.Background(), &dynamodb.UpdateTimeToLiveInput{
TableName: aws.String(tableName),
TimeToLiveSpecification: &types.TimeToLiveSpecification{
AttributeName: aws.String("ttl"),
Enabled: aws.Bool(true),
},
})
require.NoError(t, err)
}
func newDynamoClient(t *testing.T, endpoint string) *dynamodb.Client {
t.Helper()
cfg, err := config.LoadDefaultConfig(context.Background())
require.NoError(t, err)
return dynamodb.NewFromConfig(cfg, func(o *dynamodb.Options) {
o.BaseEndpoint = aws.String(endpoint)
})
}
// Test that when ExpireGracePeriod is set and current time exceeds TTL+grace,
// we forcibly reacquire the lock on first attempt.
func TestForceAcquireAfterTTLWithGrace(t *testing.T) {
endpoint := checkDDBLocalEndpoint(t)
client := newDynamoClient(t, endpoint)
tableName := "test"
itemID := "ttl_grace_item"
ensureTableWithTTL(t, client, tableName)
// Fix base time
base := time.Now().Truncate(time.Second)
restore := flextime.Fix(base)
t.Cleanup(func() { restore() })
// Simulate an existing lock item with TTL in the future (base+20s)
lease := 10 * time.Second
ttl := base.Add(20 * time.Second).Unix()
_, err := client.PutItem(context.Background(), &dynamodb.PutItemInput{
TableName: aws.String(tableName),
Item: map[string]types.AttributeValue{
"ID": &types.AttributeValueMemberS{Value: itemID},
"LeaseDuration": &types.AttributeValueMemberN{Value: "10000"}, // 10s in ms
"Revision": &types.AttributeValueMemberS{Value: "rev-1"},
"ttl": &types.AttributeValueMemberN{Value: strconv.FormatInt(ttl, 10)},
},
ConditionExpression: aws.String("attribute_not_exists(ID)"),
})
require.NoError(t, err)
// Advance time beyond TTL+grace (grace=5s => base+25s)
flextime.Fix(base.Add(30 * time.Second))
// Attempt to acquire with grace configured; should force acquire immediately.
locker, err := setddblock.New(
"ddb://"+tableName+"/"+itemID,
setddblock.WithEndpoint(endpoint),
setddblock.WithLeaseDuration(lease),
setddblock.WithExpireGracePeriod(5*time.Second),
setddblock.WithNoPanic(),
)
require.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
granted, err := locker.LockWithErr(ctx)
require.NoError(t, err)
require.True(t, granted, "lock should be forcibly reacquired after TTL+grace")
_ = locker.UnlockWithErr(context.Background())
}
// Without ExpireGracePeriod and with Delay(false), lock should NOT be granted
// even if current time is past TTL (we don't force acquire without grace).
func TestNoForceAcquireWithoutGrace(t *testing.T) {
endpoint := checkDDBLocalEndpoint(t)
client := newDynamoClient(t, endpoint)
tableName := "test"
itemID := "ttl_nograce_item"
ensureTableWithTTL(t, client, tableName)
base := time.Now().Truncate(time.Second)
restore := flextime.Fix(base)
t.Cleanup(func() { restore() })
// Existing lock with TTL at base+20s
ttl := base.Add(20 * time.Second).Unix()
_, err := client.PutItem(context.Background(), &dynamodb.PutItemInput{
TableName: aws.String(tableName),
Item: map[string]types.AttributeValue{
"ID": &types.AttributeValueMemberS{Value: itemID},
"LeaseDuration": &types.AttributeValueMemberN{Value: "10000"},
"Revision": &types.AttributeValueMemberS{Value: "rev-1"},
"ttl": &types.AttributeValueMemberN{Value: strconv.FormatInt(ttl, 10)},
},
ConditionExpression: aws.String("attribute_not_exists(ID)"),
})
require.NoError(t, err)
// Move time past TTL+5s
flextime.Fix(base.Add(30 * time.Second))
locker, err := setddblock.New(
"ddb://"+tableName+"/"+itemID,
setddblock.WithEndpoint(endpoint),
setddblock.WithLeaseDuration(10*time.Second),
setddblock.WithDelay(false), // do not wait; return immediately when not granted
setddblock.WithNoPanic(),
)
require.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
granted, err := locker.LockWithErr(ctx)
require.NoError(t, err)
require.False(t, granted, "without grace, should not force acquire immediately")
}