6
6
package asynqtest
7
7
8
8
import (
9
+ "context"
9
10
"encoding/json"
10
11
"math"
11
12
"sort"
12
13
"testing"
13
14
"time"
14
15
15
- "github.com/go-redis/redis/v7 "
16
+ "github.com/go-redis/redis/v8 "
16
17
"github.com/google/go-cmp/cmp"
17
18
"github.com/google/go-cmp/cmp/cmpopts"
18
19
"github.com/google/uuid"
@@ -165,12 +166,12 @@ func FlushDB(tb testing.TB, r redis.UniversalClient) {
165
166
tb .Helper ()
166
167
switch r := r .(type ) {
167
168
case * redis.Client :
168
- if err := r .FlushDB ().Err (); err != nil {
169
+ if err := r .FlushDB (context . Background () ).Err (); err != nil {
169
170
tb .Fatal (err )
170
171
}
171
172
case * redis.ClusterClient :
172
- err := r .ForEachMaster (func (c * redis.Client ) error {
173
- if err := c .FlushAll ().Err (); err != nil {
173
+ err := r .ForEachMaster (context . Background (), func (ctx context. Context , c * redis.Client ) error {
174
+ if err := c .FlushAll (ctx ).Err (); err != nil {
174
175
return err
175
176
}
176
177
return nil
@@ -184,42 +185,42 @@ func FlushDB(tb testing.TB, r redis.UniversalClient) {
184
185
// SeedPendingQueue initializes the specified queue with the given messages.
185
186
func SeedPendingQueue (tb testing.TB , r redis.UniversalClient , msgs []* base.TaskMessage , qname string ) {
186
187
tb .Helper ()
187
- r .SAdd (base .AllQueues , qname )
188
+ r .SAdd (context . Background (), base .AllQueues , qname )
188
189
seedRedisList (tb , r , base .PendingKey (qname ), msgs , base .TaskStatePending )
189
190
}
190
191
191
192
// SeedActiveQueue initializes the active queue with the given messages.
192
193
func SeedActiveQueue (tb testing.TB , r redis.UniversalClient , msgs []* base.TaskMessage , qname string ) {
193
194
tb .Helper ()
194
- r .SAdd (base .AllQueues , qname )
195
+ r .SAdd (context . Background (), base .AllQueues , qname )
195
196
seedRedisList (tb , r , base .ActiveKey (qname ), msgs , base .TaskStateActive )
196
197
}
197
198
198
199
// SeedScheduledQueue initializes the scheduled queue with the given messages.
199
200
func SeedScheduledQueue (tb testing.TB , r redis.UniversalClient , entries []base.Z , qname string ) {
200
201
tb .Helper ()
201
- r .SAdd (base .AllQueues , qname )
202
+ r .SAdd (context . Background (), base .AllQueues , qname )
202
203
seedRedisZSet (tb , r , base .ScheduledKey (qname ), entries , base .TaskStateScheduled )
203
204
}
204
205
205
206
// SeedRetryQueue initializes the retry queue with the given messages.
206
207
func SeedRetryQueue (tb testing.TB , r redis.UniversalClient , entries []base.Z , qname string ) {
207
208
tb .Helper ()
208
- r .SAdd (base .AllQueues , qname )
209
+ r .SAdd (context . Background (), base .AllQueues , qname )
209
210
seedRedisZSet (tb , r , base .RetryKey (qname ), entries , base .TaskStateRetry )
210
211
}
211
212
212
213
// SeedArchivedQueue initializes the archived queue with the given messages.
213
214
func SeedArchivedQueue (tb testing.TB , r redis.UniversalClient , entries []base.Z , qname string ) {
214
215
tb .Helper ()
215
- r .SAdd (base .AllQueues , qname )
216
+ r .SAdd (context . Background (), base .AllQueues , qname )
216
217
seedRedisZSet (tb , r , base .ArchivedKey (qname ), entries , base .TaskStateArchived )
217
218
}
218
219
219
220
// SeedDeadlines initializes the deadlines set with the given entries.
220
221
func SeedDeadlines (tb testing.TB , r redis.UniversalClient , entries []base.Z , qname string ) {
221
222
tb .Helper ()
222
- r .SAdd (base .AllQueues , qname )
223
+ r .SAdd (context . Background (), base .AllQueues , qname )
223
224
seedRedisZSet (tb , r , base .DeadlinesKey (qname ), entries , base .TaskStateActive )
224
225
}
225
226
@@ -278,7 +279,7 @@ func seedRedisList(tb testing.TB, c redis.UniversalClient, key string,
278
279
tb .Helper ()
279
280
for _ , msg := range msgs {
280
281
encoded := MustMarshal (tb , msg )
281
- if err := c .LPush (key , msg .ID .String ()).Err (); err != nil {
282
+ if err := c .LPush (context . Background (), key , msg .ID .String ()).Err (); err != nil {
282
283
tb .Fatal (err )
283
284
}
284
285
key := base .TaskKey (msg .Queue , msg .ID .String ())
@@ -289,11 +290,11 @@ func seedRedisList(tb testing.TB, c redis.UniversalClient, key string,
289
290
"deadline" : msg .Deadline ,
290
291
"unique_key" : msg .UniqueKey ,
291
292
}
292
- if err := c .HSet (key , data ).Err (); err != nil {
293
+ if err := c .HSet (context . Background (), key , data ).Err (); err != nil {
293
294
tb .Fatal (err )
294
295
}
295
296
if len (msg .UniqueKey ) > 0 {
296
- err := c .SetNX (msg .UniqueKey , msg .ID .String (), 1 * time .Minute ).Err ()
297
+ err := c .SetNX (context . Background (), msg .UniqueKey , msg .ID .String (), 1 * time .Minute ).Err ()
297
298
if err != nil {
298
299
tb .Fatalf ("Failed to set unique lock in redis: %v" , err )
299
300
}
@@ -308,7 +309,7 @@ func seedRedisZSet(tb testing.TB, c redis.UniversalClient, key string,
308
309
msg := item .Message
309
310
encoded := MustMarshal (tb , msg )
310
311
z := & redis.Z {Member : msg .ID .String (), Score : float64 (item .Score )}
311
- if err := c .ZAdd (key , z ).Err (); err != nil {
312
+ if err := c .ZAdd (context . Background (), key , z ).Err (); err != nil {
312
313
tb .Fatal (err )
313
314
}
314
315
key := base .TaskKey (msg .Queue , msg .ID .String ())
@@ -319,11 +320,11 @@ func seedRedisZSet(tb testing.TB, c redis.UniversalClient, key string,
319
320
"deadline" : msg .Deadline ,
320
321
"unique_key" : msg .UniqueKey ,
321
322
}
322
- if err := c .HSet (key , data ).Err (); err != nil {
323
+ if err := c .HSet (context . Background (), key , data ).Err (); err != nil {
323
324
tb .Fatal (err )
324
325
}
325
326
if len (msg .UniqueKey ) > 0 {
326
- err := c .SetNX (msg .UniqueKey , msg .ID .String (), 1 * time .Minute ).Err ()
327
+ err := c .SetNX (context . Background (), msg .UniqueKey , msg .ID .String (), 1 * time .Minute ).Err ()
327
328
if err != nil {
328
329
tb .Fatalf ("Failed to set unique lock in redis: %v" , err )
329
330
}
@@ -398,13 +399,13 @@ func GetDeadlinesEntries(tb testing.TB, r redis.UniversalClient, qname string) [
398
399
func getMessagesFromList (tb testing.TB , r redis.UniversalClient , qname string ,
399
400
keyFn func (qname string ) string , state base.TaskState ) []* base.TaskMessage {
400
401
tb .Helper ()
401
- ids := r .LRange (keyFn (qname ), 0 , - 1 ).Val ()
402
+ ids := r .LRange (context . Background (), keyFn (qname ), 0 , - 1 ).Val ()
402
403
var msgs []* base.TaskMessage
403
404
for _ , id := range ids {
404
405
taskKey := base .TaskKey (qname , id )
405
- data := r .HGet (taskKey , "msg" ).Val ()
406
+ data := r .HGet (context . Background (), taskKey , "msg" ).Val ()
406
407
msgs = append (msgs , MustUnmarshal (tb , data ))
407
- if gotState := r .HGet (taskKey , "state" ).Val (); gotState != state .String () {
408
+ if gotState := r .HGet (context . Background (), taskKey , "state" ).Val (); gotState != state .String () {
408
409
tb .Errorf ("task (id=%q) is in %q state, want %v" , id , gotState , state )
409
410
}
410
411
}
@@ -415,13 +416,13 @@ func getMessagesFromList(tb testing.TB, r redis.UniversalClient, qname string,
415
416
func getMessagesFromZSet (tb testing.TB , r redis.UniversalClient , qname string ,
416
417
keyFn func (qname string ) string , state base.TaskState ) []* base.TaskMessage {
417
418
tb .Helper ()
418
- ids := r .ZRange (keyFn (qname ), 0 , - 1 ).Val ()
419
+ ids := r .ZRange (context . Background (), keyFn (qname ), 0 , - 1 ).Val ()
419
420
var msgs []* base.TaskMessage
420
421
for _ , id := range ids {
421
422
taskKey := base .TaskKey (qname , id )
422
- msg := r .HGet (taskKey , "msg" ).Val ()
423
+ msg := r .HGet (context . Background (), taskKey , "msg" ).Val ()
423
424
msgs = append (msgs , MustUnmarshal (tb , msg ))
424
- if gotState := r .HGet (taskKey , "state" ).Val (); gotState != state .String () {
425
+ if gotState := r .HGet (context . Background (), taskKey , "state" ).Val (); gotState != state .String () {
425
426
tb .Errorf ("task (id=%q) is in %q state, want %v" , id , gotState , state )
426
427
}
427
428
}
@@ -432,14 +433,14 @@ func getMessagesFromZSet(tb testing.TB, r redis.UniversalClient, qname string,
432
433
func getMessagesFromZSetWithScores (tb testing.TB , r redis.UniversalClient ,
433
434
qname string , keyFn func (qname string ) string , state base.TaskState ) []base.Z {
434
435
tb .Helper ()
435
- zs := r .ZRangeWithScores (keyFn (qname ), 0 , - 1 ).Val ()
436
+ zs := r .ZRangeWithScores (context . Background (), keyFn (qname ), 0 , - 1 ).Val ()
436
437
var res []base.Z
437
438
for _ , z := range zs {
438
439
taskID := z .Member .(string )
439
440
taskKey := base .TaskKey (qname , taskID )
440
- msg := r .HGet (taskKey , "msg" ).Val ()
441
+ msg := r .HGet (context . Background (), taskKey , "msg" ).Val ()
441
442
res = append (res , base.Z {Message : MustUnmarshal (tb , msg ), Score : int64 (z .Score )})
442
- if gotState := r .HGet (taskKey , "state" ).Val (); gotState != state .String () {
443
+ if gotState := r .HGet (context . Background (), taskKey , "state" ).Val (); gotState != state .String () {
443
444
tb .Errorf ("task (id=%q) is in %q state, want %v" , taskID , gotState , state )
444
445
}
445
446
}
0 commit comments