-
Notifications
You must be signed in to change notification settings - Fork 42
/
lock_e2e_test.go
365 lines (334 loc) · 8.63 KB
/
lock_e2e_test.go
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright 2021 gotomicro
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build e2e
package rlock
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type ClientE2ESuite struct {
suite.Suite
rdb redis.Cmdable
}
func (s *ClientE2ESuite) SetupSuite() {
s.rdb = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
// 确保测试的目标 Redis 已经启动成功了
for s.rdb.Ping(context.Background()).Err() != nil {
}
}
func TestClientE2E(t *testing.T) {
suite.Run(t, &ClientE2ESuite{})
}
func (s *ClientE2ESuite) TestLock() {
t := s.T()
rdb := s.rdb
testCases := []struct {
name string
key string
expiration time.Duration
retry RetryStrategy
timeout time.Duration
client *Client
wantLock *Lock
wantErr error
before func()
after func()
}{
{
// 一次加锁成功
name: "locked",
key: "locked-key",
retry: &FixIntervalRetry{Interval: time.Second, Max: 3},
timeout: time.Second,
expiration: time.Minute,
client: NewClient(rdb),
before: func() {},
after: func() {
res, err := rdb.Del(context.Background(), "locked-key").Result()
require.NoError(t, err)
require.Equal(t, int64(1), res)
},
},
{
// 加锁不成功
name: "failed",
key: "failed-key",
retry: &FixIntervalRetry{Interval: time.Second, Max: 3},
timeout: time.Second,
expiration: time.Minute,
client: NewClient(rdb),
before: func() {
res, err := rdb.Set(context.Background(), "failed-key", "123", time.Minute).Result()
require.NoError(t, err)
require.Equal(t, "OK", res)
},
after: func() {
res, err := rdb.Get(context.Background(), "failed-key").Result()
require.NoError(t, err)
require.Equal(t, "123", res)
delRes, err := rdb.Del(context.Background(), "failed-key").Result()
require.NoError(t, err)
require.Equal(t, int64(1), delRes)
},
wantErr: context.DeadlineExceeded,
},
{
// 已经加锁,再加锁就是刷新超时时间
name: "already-locked",
key: "already-key",
retry: &FixIntervalRetry{Interval: time.Second, Max: 3},
timeout: time.Second,
expiration: time.Minute,
client: func() *Client {
client := NewClient(rdb)
client.valuer = func() string {
return "123"
}
return client
}(),
before: func() {
res, err := rdb.Set(context.Background(), "already-key", "123", time.Minute).Result()
require.NoError(t, err)
require.Equal(t, "OK", res)
},
after: func() {
res, err := rdb.Get(context.Background(), "already-key").Result()
require.NoError(t, err)
require.Equal(t, "123", res)
delRes, err := rdb.Del(context.Background(), "already-key").Result()
require.NoError(t, err)
require.Equal(t, int64(1), delRes)
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tc.before()
l, err := tc.client.Lock(context.Background(), tc.key, tc.expiration, tc.retry, tc.timeout)
assert.True(t, errors.Is(err, tc.wantErr))
if err != nil {
return
}
assert.Equal(t, tc.key, l.key)
assert.Equal(t, tc.expiration, l.expiration)
assert.NotEmpty(t, l.value)
tc.after()
})
}
}
func (s *ClientE2ESuite) TestTryLock() {
t := s.T()
rdb := s.rdb
client := NewClient(rdb)
testCases := []struct {
name string
key string
expiration time.Duration
wantLock *Lock
wantErr error
before func()
after func()
}{
{
// 加锁成功
name: "locked",
key: "locked-key",
expiration: time.Minute,
before: func() {},
after: func() {
res, err := rdb.Del(context.Background(), "locked-key").Result()
require.NoError(t, err)
require.Equal(t, int64(1), res)
},
wantLock: &Lock{
key: "locked-key",
expiration: time.Minute,
},
},
{
// 模拟并发竞争失败
name: "failed",
key: "failed-key",
expiration: time.Minute,
before: func() {
// 假设已经有人设置了分布式锁
val, err := rdb.Set(context.Background(), "failed-key", "123", time.Minute).Result()
require.NoError(t, err)
require.Equal(t, "OK", val)
},
after: func() {
res, err := rdb.Del(context.Background(), "failed-key").Result()
require.NoError(t, err)
require.Equal(t, int64(1), res)
},
wantErr: ErrFailedToPreemptLock,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tc.before()
l, err := client.TryLock(context.Background(), tc.key, tc.expiration)
assert.Equal(t, tc.wantErr, err)
if err != nil {
return
}
assert.Equal(t, tc.key, l.key)
assert.Equal(t, tc.expiration, l.expiration)
assert.NotEmpty(t, l.value)
tc.after()
})
}
}
func (s *ClientE2ESuite) TestRefresh() {
t := s.T()
rdb := s.rdb
testCases := []struct {
name string
timeout time.Duration
lock *Lock
wantErr error
before func()
after func()
}{
{
name: "refresh success",
lock: &Lock{
key: "refresh-key",
value: "123",
expiration: time.Minute,
unlock: make(chan struct{}, 1),
client: rdb,
},
before: func() {
// 设置一个比较短的过期时间
res, err := rdb.SetNX(context.Background(),
"refresh-key", "123", time.Second*10).Result()
require.NoError(t, err)
assert.True(t, res)
},
after: func() {
res, err := rdb.TTL(context.Background(), "refresh-key").Result()
require.NoError(t, err)
// 刷新完过期时间
assert.True(t, res.Seconds() > 50)
// 清理数据
rdb.Del(context.Background(), "refresh-key")
},
timeout: time.Minute,
},
{
// 锁被人持有
name: "refresh failed",
lock: &Lock{
key: "refresh-key",
value: "123",
expiration: time.Minute,
unlock: make(chan struct{}, 1),
client: rdb,
},
before: func() {
// 设置一个比较短的过期时间
res, err := rdb.SetNX(context.Background(),
"refresh-key", "456", time.Second*10).Result()
require.NoError(t, err)
assert.True(t, res)
},
after: func() {
res, err := rdb.Get(context.Background(), "refresh-key").Result()
require.NoError(t, err)
require.Equal(t, "456", res)
rdb.Del(context.Background(), "refresh-key")
},
timeout: time.Minute,
wantErr: ErrLockNotHold,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tc.before()
ctx, cancel := context.WithTimeout(context.Background(), tc.timeout)
err := tc.lock.Refresh(ctx)
cancel()
assert.Equal(t, tc.wantErr, err)
tc.after()
})
}
}
func (s *ClientE2ESuite) TestAutoRefresh() {
t := s.T()
rdb := s.rdb
client := NewClient(rdb)
l, err := client.TryLock(context.Background(), "auto-refresh-key", time.Second)
require.NoError(t, err)
go func() {
err = l.AutoRefresh(time.Millisecond*800, time.Millisecond*300)
require.NoError(t, err)
}()
// 模拟业务
time.Sleep(time.Second * 2)
// 因为自动刷新了,所以 key 还在
res, err := rdb.Exists(context.Background(), "auto-refresh-key").Result()
require.NoError(t, err)
require.Equal(t, int64(1), res)
err = l.Unlock(context.Background())
require.NoError(t, err)
}
func (s *ClientE2ESuite) TestUnLock() {
t := s.T()
rdb := s.rdb
client := NewClient(rdb)
testCases := []struct {
name string
lock *Lock
before func()
after func()
wantLock *Lock
wantErr error
}{
{
name: "unlocked",
lock: func() *Lock {
l, err := client.TryLock(context.Background(), "unlocked-key", time.Minute)
require.NoError(t, err)
return l
}(),
before: func() {},
after: func() {
res, err := rdb.Exists(context.Background(), "unlocked-key").Result()
require.NoError(t, err)
require.Equal(t, 1, res)
},
},
{
name: "lock not hold",
lock: newLock(rdb, "not-hold-key", "123", time.Minute),
wantErr: ErrLockNotHold,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.lock.Unlock(context.Background())
require.Equal(t, tc.wantErr, err)
})
}
}