2020-01-03 10:13:16 +08:00
|
|
|
// Copyright 2020 Kentaro Hibino. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
// Package asynqtest defines test helpers for asynq and its internal packages.
|
|
|
|
package asynqtest
|
|
|
|
|
|
|
|
import (
|
2021-09-02 20:56:02 +08:00
|
|
|
"context"
|
2021-03-21 04:42:13 +08:00
|
|
|
"encoding/json"
|
2020-08-28 21:04:17 +08:00
|
|
|
"math"
|
2019-12-29 12:12:14 +08:00
|
|
|
"sort"
|
|
|
|
"testing"
|
2021-05-19 12:00:53 +08:00
|
|
|
"time"
|
2019-12-29 12:12:14 +08:00
|
|
|
|
2021-09-02 20:56:02 +08:00
|
|
|
"github.com/go-redis/redis/v8"
|
2019-12-29 12:12:14 +08:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2019-12-30 01:41:00 +08:00
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2020-07-02 21:21:20 +08:00
|
|
|
"github.com/google/uuid"
|
2019-12-29 12:12:14 +08:00
|
|
|
"github.com/hibiken/asynq/internal/base"
|
2022-02-14 23:17:51 +08:00
|
|
|
"github.com/hibiken/asynq/internal/timeutil"
|
2019-12-29 12:12:14 +08:00
|
|
|
)
|
|
|
|
|
2020-08-28 21:04:17 +08:00
|
|
|
// EquateInt64Approx returns a Comparer option that treats int64 values
|
|
|
|
// to be equal if they are within the given margin.
|
|
|
|
func EquateInt64Approx(margin int64) cmp.Option {
|
|
|
|
return cmp.Comparer(func(a, b int64) bool {
|
2020-08-29 21:54:08 +08:00
|
|
|
return math.Abs(float64(a-b)) <= float64(margin)
|
2020-08-28 21:04:17 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
// SortMsgOpt is a cmp.Option to sort base.TaskMessage for comparing slice of task messages.
|
|
|
|
var SortMsgOpt = cmp.Transformer("SortTaskMessages", func(in []*base.TaskMessage) []*base.TaskMessage {
|
|
|
|
out := append([]*base.TaskMessage(nil), in...) // Copy input to avoid mutating it
|
|
|
|
sort.Slice(out, func(i, j int) bool {
|
2021-09-10 21:29:37 +08:00
|
|
|
return out[i].ID < out[j].ID
|
2019-12-29 12:12:14 +08:00
|
|
|
})
|
|
|
|
return out
|
|
|
|
})
|
|
|
|
|
|
|
|
// SortZSetEntryOpt is an cmp.Option to sort ZSetEntry for comparing slice of zset entries.
|
2020-07-13 21:29:41 +08:00
|
|
|
var SortZSetEntryOpt = cmp.Transformer("SortZSetEntries", func(in []base.Z) []base.Z {
|
|
|
|
out := append([]base.Z(nil), in...) // Copy input to avoid mutating it
|
2019-12-29 12:12:14 +08:00
|
|
|
sort.Slice(out, func(i, j int) bool {
|
2021-09-10 21:29:37 +08:00
|
|
|
return out[i].Message.ID < out[j].Message.ID
|
2019-12-29 12:12:14 +08:00
|
|
|
})
|
|
|
|
return out
|
|
|
|
})
|
|
|
|
|
2020-04-13 07:42:11 +08:00
|
|
|
// SortServerInfoOpt is a cmp.Option to sort base.ServerInfo for comparing slice of process info.
|
|
|
|
var SortServerInfoOpt = cmp.Transformer("SortServerInfo", func(in []*base.ServerInfo) []*base.ServerInfo {
|
|
|
|
out := append([]*base.ServerInfo(nil), in...) // Copy input to avoid mutating it
|
2020-02-02 14:22:48 +08:00
|
|
|
sort.Slice(out, func(i, j int) bool {
|
|
|
|
if out[i].Host != out[j].Host {
|
|
|
|
return out[i].Host < out[j].Host
|
|
|
|
}
|
|
|
|
return out[i].PID < out[j].PID
|
|
|
|
})
|
|
|
|
return out
|
|
|
|
})
|
|
|
|
|
2020-02-23 12:42:53 +08:00
|
|
|
// SortWorkerInfoOpt is a cmp.Option to sort base.WorkerInfo for comparing slice of worker info.
|
|
|
|
var SortWorkerInfoOpt = cmp.Transformer("SortWorkerInfo", func(in []*base.WorkerInfo) []*base.WorkerInfo {
|
|
|
|
out := append([]*base.WorkerInfo(nil), in...) // Copy input to avoid mutating it
|
|
|
|
sort.Slice(out, func(i, j int) bool {
|
2020-05-19 11:47:35 +08:00
|
|
|
return out[i].ID < out[j].ID
|
2020-02-23 12:42:53 +08:00
|
|
|
})
|
|
|
|
return out
|
|
|
|
})
|
|
|
|
|
2020-09-27 08:33:29 +08:00
|
|
|
// SortSchedulerEntryOpt is a cmp.Option to sort base.SchedulerEntry for comparing slice of entries.
|
|
|
|
var SortSchedulerEntryOpt = cmp.Transformer("SortSchedulerEntry", func(in []*base.SchedulerEntry) []*base.SchedulerEntry {
|
|
|
|
out := append([]*base.SchedulerEntry(nil), in...) // Copy input to avoid mutating it
|
|
|
|
sort.Slice(out, func(i, j int) bool {
|
|
|
|
return out[i].Spec < out[j].Spec
|
|
|
|
})
|
|
|
|
return out
|
|
|
|
})
|
|
|
|
|
|
|
|
// SortSchedulerEnqueueEventOpt is a cmp.Option to sort base.SchedulerEnqueueEvent for comparing slice of events.
|
|
|
|
var SortSchedulerEnqueueEventOpt = cmp.Transformer("SortSchedulerEnqueueEvent", func(in []*base.SchedulerEnqueueEvent) []*base.SchedulerEnqueueEvent {
|
|
|
|
out := append([]*base.SchedulerEnqueueEvent(nil), in...)
|
|
|
|
sort.Slice(out, func(i, j int) bool {
|
|
|
|
return out[i].EnqueuedAt.Unix() < out[j].EnqueuedAt.Unix()
|
|
|
|
})
|
|
|
|
return out
|
|
|
|
})
|
|
|
|
|
2020-02-23 06:30:24 +08:00
|
|
|
// SortStringSliceOpt is a cmp.Option to sort string slice.
|
|
|
|
var SortStringSliceOpt = cmp.Transformer("SortStringSlice", func(in []string) []string {
|
|
|
|
out := append([]string(nil), in...)
|
|
|
|
sort.Strings(out)
|
|
|
|
return out
|
|
|
|
})
|
|
|
|
|
2019-12-30 01:41:00 +08:00
|
|
|
// IgnoreIDOpt is an cmp.Option to ignore ID field in task messages when comparing.
|
|
|
|
var IgnoreIDOpt = cmpopts.IgnoreFields(base.TaskMessage{}, "ID")
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
// NewTaskMessage returns a new instance of TaskMessage given a task type and payload.
|
2021-03-21 04:42:13 +08:00
|
|
|
func NewTaskMessage(taskType string, payload []byte) *base.TaskMessage {
|
2020-08-19 12:21:05 +08:00
|
|
|
return NewTaskMessageWithQueue(taskType, payload, base.DefaultQueueName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTaskMessageWithQueue returns a new instance of TaskMessage given a
|
|
|
|
// task type, payload and queue name.
|
2021-03-21 04:42:13 +08:00
|
|
|
func NewTaskMessageWithQueue(taskType string, payload []byte, qname string) *base.TaskMessage {
|
2019-12-29 12:12:14 +08:00
|
|
|
return &base.TaskMessage{
|
2021-09-10 21:29:37 +08:00
|
|
|
ID: uuid.NewString(),
|
2020-06-17 12:11:54 +08:00
|
|
|
Type: taskType,
|
2020-08-19 12:21:05 +08:00
|
|
|
Queue: qname,
|
2020-06-17 12:11:54 +08:00
|
|
|
Retry: 25,
|
|
|
|
Payload: payload,
|
|
|
|
Timeout: 1800, // default timeout of 30 mins
|
|
|
|
Deadline: 0, // no deadline
|
2020-01-10 22:56:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 23:17:51 +08:00
|
|
|
// NewLeaseWithClock returns a new lease with the given expiration time and clock.
|
|
|
|
func NewLeaseWithClock(expirationTime time.Time, clock timeutil.Clock) *base.Lease {
|
|
|
|
l := base.NewLease(expirationTime)
|
|
|
|
l.Clock = clock
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2021-03-21 04:42:13 +08:00
|
|
|
// JSON serializes the given key-value pairs into stream of bytes in JSON.
|
|
|
|
func JSON(kv map[string]interface{}) []byte {
|
|
|
|
b, err := json.Marshal(kv)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2020-06-21 22:05:57 +08:00
|
|
|
// TaskMessageAfterRetry returns an updated copy of t after retry.
|
2021-06-03 21:58:07 +08:00
|
|
|
// It increments retry count and sets the error message and last_failed_at time.
|
|
|
|
func TaskMessageAfterRetry(t base.TaskMessage, errMsg string, failedAt time.Time) *base.TaskMessage {
|
2020-06-21 22:05:57 +08:00
|
|
|
t.Retried = t.Retried + 1
|
|
|
|
t.ErrorMsg = errMsg
|
2021-06-03 21:58:07 +08:00
|
|
|
t.LastFailedAt = failedAt.Unix()
|
2020-06-21 22:05:57 +08:00
|
|
|
return &t
|
|
|
|
}
|
|
|
|
|
|
|
|
// TaskMessageWithError returns an updated copy of t with the given error message.
|
2021-06-03 21:58:07 +08:00
|
|
|
func TaskMessageWithError(t base.TaskMessage, errMsg string, failedAt time.Time) *base.TaskMessage {
|
2020-06-21 22:05:57 +08:00
|
|
|
t.ErrorMsg = errMsg
|
2021-06-03 21:58:07 +08:00
|
|
|
t.LastFailedAt = failedAt.Unix()
|
2020-06-21 22:05:57 +08:00
|
|
|
return &t
|
|
|
|
}
|
|
|
|
|
2021-11-06 07:52:54 +08:00
|
|
|
// TaskMessageWithCompletedAt returns an updated copy of t after completion.
|
|
|
|
func TaskMessageWithCompletedAt(t base.TaskMessage, completedAt time.Time) *base.TaskMessage {
|
|
|
|
t.CompletedAt = completedAt.Unix()
|
|
|
|
return &t
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
// MustMarshal marshals given task message and returns a json string.
|
|
|
|
// Calling test will fail if marshaling errors out.
|
2019-12-31 12:10:34 +08:00
|
|
|
func MustMarshal(tb testing.TB, msg *base.TaskMessage) string {
|
|
|
|
tb.Helper()
|
2021-03-13 08:23:08 +08:00
|
|
|
data, err := base.EncodeMessage(msg)
|
2019-12-29 12:12:14 +08:00
|
|
|
if err != nil {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Fatal(err)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustUnmarshal unmarshals given string into task message struct.
|
|
|
|
// Calling test will fail if unmarshaling errors out.
|
2019-12-31 12:10:34 +08:00
|
|
|
func MustUnmarshal(tb testing.TB, data string) *base.TaskMessage {
|
|
|
|
tb.Helper()
|
2021-03-13 08:23:08 +08:00
|
|
|
msg, err := base.DecodeMessage([]byte(data))
|
2019-12-29 12:12:14 +08:00
|
|
|
if err != nil {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Fatal(err)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
2021-03-13 08:23:08 +08:00
|
|
|
return msg
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FlushDB deletes all the keys of the currently selected DB.
|
2020-08-28 20:40:16 +08:00
|
|
|
func FlushDB(tb testing.TB, r redis.UniversalClient) {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
2020-08-29 21:54:08 +08:00
|
|
|
switch r := r.(type) {
|
|
|
|
case *redis.Client:
|
2021-09-02 20:56:02 +08:00
|
|
|
if err := r.FlushDB(context.Background()).Err(); err != nil {
|
2020-08-29 21:54:08 +08:00
|
|
|
tb.Fatal(err)
|
|
|
|
}
|
|
|
|
case *redis.ClusterClient:
|
2021-09-02 20:56:02 +08:00
|
|
|
err := r.ForEachMaster(context.Background(), func(ctx context.Context, c *redis.Client) error {
|
|
|
|
if err := c.FlushAll(ctx).Err(); err != nil {
|
2020-08-29 21:54:08 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
tb.Fatal(err)
|
|
|
|
}
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 22:03:43 +08:00
|
|
|
// SeedPendingQueue initializes the specified queue with the given messages.
|
|
|
|
func SeedPendingQueue(tb testing.TB, r redis.UniversalClient, msgs []*base.TaskMessage, qname string) {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
2021-09-02 20:56:02 +08:00
|
|
|
r.SAdd(context.Background(), base.AllQueues, qname)
|
2021-05-12 11:43:01 +08:00
|
|
|
seedRedisList(tb, r, base.PendingKey(qname), msgs, base.TaskStatePending)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
2020-09-06 03:43:15 +08:00
|
|
|
// SeedActiveQueue initializes the active queue with the given messages.
|
|
|
|
func SeedActiveQueue(tb testing.TB, r redis.UniversalClient, msgs []*base.TaskMessage, qname string) {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
2021-09-02 20:56:02 +08:00
|
|
|
r.SAdd(context.Background(), base.AllQueues, qname)
|
2021-05-12 11:43:01 +08:00
|
|
|
seedRedisList(tb, r, base.ActiveKey(qname), msgs, base.TaskStateActive)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SeedScheduledQueue initializes the scheduled queue with the given messages.
|
2020-08-28 20:40:16 +08:00
|
|
|
func SeedScheduledQueue(tb testing.TB, r redis.UniversalClient, entries []base.Z, qname string) {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
2021-09-02 20:56:02 +08:00
|
|
|
r.SAdd(context.Background(), base.AllQueues, qname)
|
2021-05-12 11:43:01 +08:00
|
|
|
seedRedisZSet(tb, r, base.ScheduledKey(qname), entries, base.TaskStateScheduled)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SeedRetryQueue initializes the retry queue with the given messages.
|
2020-08-28 20:40:16 +08:00
|
|
|
func SeedRetryQueue(tb testing.TB, r redis.UniversalClient, entries []base.Z, qname string) {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
2021-09-02 20:56:02 +08:00
|
|
|
r.SAdd(context.Background(), base.AllQueues, qname)
|
2021-05-12 11:43:01 +08:00
|
|
|
seedRedisZSet(tb, r, base.RetryKey(qname), entries, base.TaskStateRetry)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
// SeedArchivedQueue initializes the archived queue with the given messages.
|
|
|
|
func SeedArchivedQueue(tb testing.TB, r redis.UniversalClient, entries []base.Z, qname string) {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
2021-09-02 20:56:02 +08:00
|
|
|
r.SAdd(context.Background(), base.AllQueues, qname)
|
2021-05-12 11:43:01 +08:00
|
|
|
seedRedisZSet(tb, r, base.ArchivedKey(qname), entries, base.TaskStateArchived)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
2022-02-11 11:01:05 +08:00
|
|
|
// SeedLease initializes the lease set with the given entries.
|
|
|
|
func SeedLease(tb testing.TB, r redis.UniversalClient, entries []base.Z, qname string) {
|
|
|
|
tb.Helper()
|
|
|
|
r.SAdd(context.Background(), base.AllQueues, qname)
|
|
|
|
seedRedisZSet(tb, r, base.LeaseKey(qname), entries, base.TaskStateActive)
|
|
|
|
}
|
|
|
|
|
2021-11-06 07:52:54 +08:00
|
|
|
// SeedCompletedQueue initializes the completed set witht the given entries.
|
|
|
|
func SeedCompletedQueue(tb testing.TB, r redis.UniversalClient, entries []base.Z, qname string) {
|
|
|
|
tb.Helper()
|
|
|
|
r.SAdd(context.Background(), base.AllQueues, qname)
|
|
|
|
seedRedisZSet(tb, r, base.CompletedKey(qname), entries, base.TaskStateCompleted)
|
|
|
|
}
|
|
|
|
|
2020-09-05 22:03:43 +08:00
|
|
|
// SeedAllPendingQueues initializes all of the specified queues with the given messages.
|
2020-08-08 21:48:49 +08:00
|
|
|
//
|
2020-09-05 22:03:43 +08:00
|
|
|
// pending maps a queue name to a list of messages.
|
|
|
|
func SeedAllPendingQueues(tb testing.TB, r redis.UniversalClient, pending map[string][]*base.TaskMessage) {
|
2021-03-13 08:23:08 +08:00
|
|
|
tb.Helper()
|
2020-09-05 22:03:43 +08:00
|
|
|
for q, msgs := range pending {
|
|
|
|
SeedPendingQueue(tb, r, msgs, q)
|
2020-08-08 21:48:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 03:43:15 +08:00
|
|
|
// SeedAllActiveQueues initializes all of the specified active queues with the given messages.
|
|
|
|
func SeedAllActiveQueues(tb testing.TB, r redis.UniversalClient, active map[string][]*base.TaskMessage) {
|
2021-03-13 08:23:08 +08:00
|
|
|
tb.Helper()
|
2020-09-06 03:43:15 +08:00
|
|
|
for q, msgs := range active {
|
|
|
|
SeedActiveQueue(tb, r, msgs, q)
|
2020-08-08 21:48:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SeedAllScheduledQueues initializes all of the specified scheduled queues with the given entries.
|
2020-08-28 20:40:16 +08:00
|
|
|
func SeedAllScheduledQueues(tb testing.TB, r redis.UniversalClient, scheduled map[string][]base.Z) {
|
2021-03-13 08:23:08 +08:00
|
|
|
tb.Helper()
|
2020-08-08 21:48:49 +08:00
|
|
|
for q, entries := range scheduled {
|
|
|
|
SeedScheduledQueue(tb, r, entries, q)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SeedAllRetryQueues initializes all of the specified retry queues with the given entries.
|
2020-08-28 20:40:16 +08:00
|
|
|
func SeedAllRetryQueues(tb testing.TB, r redis.UniversalClient, retry map[string][]base.Z) {
|
2021-03-13 08:23:08 +08:00
|
|
|
tb.Helper()
|
2020-08-08 21:48:49 +08:00
|
|
|
for q, entries := range retry {
|
|
|
|
SeedRetryQueue(tb, r, entries, q)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
// SeedAllArchivedQueues initializes all of the specified archived queues with the given entries.
|
|
|
|
func SeedAllArchivedQueues(tb testing.TB, r redis.UniversalClient, archived map[string][]base.Z) {
|
2021-03-13 08:23:08 +08:00
|
|
|
tb.Helper()
|
2021-01-13 03:01:21 +08:00
|
|
|
for q, entries := range archived {
|
|
|
|
SeedArchivedQueue(tb, r, entries, q)
|
2020-08-08 21:48:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 11:01:05 +08:00
|
|
|
// SeedAllLease initializes all of the lease sets with the given entries.
|
2022-02-13 01:48:07 +08:00
|
|
|
func SeedAllLease(tb testing.TB, r redis.UniversalClient, lease map[string][]base.Z) {
|
2022-02-11 11:01:05 +08:00
|
|
|
tb.Helper()
|
2022-02-13 01:48:07 +08:00
|
|
|
for q, entries := range lease {
|
2022-02-11 11:01:05 +08:00
|
|
|
SeedLease(tb, r, entries, q)
|
2020-08-08 21:48:49 +08:00
|
|
|
}
|
2020-06-18 22:10:57 +08:00
|
|
|
}
|
|
|
|
|
2021-11-06 07:52:54 +08:00
|
|
|
// SeedAllCompletedQueues initializes all of the completed queues with the given entries.
|
|
|
|
func SeedAllCompletedQueues(tb testing.TB, r redis.UniversalClient, completed map[string][]base.Z) {
|
|
|
|
tb.Helper()
|
|
|
|
for q, entries := range completed {
|
|
|
|
SeedCompletedQueue(tb, r, entries, q)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-26 21:58:33 +08:00
|
|
|
func seedRedisList(tb testing.TB, c redis.UniversalClient, key string,
|
2021-05-12 11:43:01 +08:00
|
|
|
msgs []*base.TaskMessage, state base.TaskState) {
|
2021-03-13 08:23:08 +08:00
|
|
|
tb.Helper()
|
|
|
|
for _, msg := range msgs {
|
|
|
|
encoded := MustMarshal(tb, msg)
|
2021-09-10 21:29:37 +08:00
|
|
|
if err := c.LPush(context.Background(), key, msg.ID).Err(); err != nil {
|
2021-03-13 08:23:08 +08:00
|
|
|
tb.Fatal(err)
|
|
|
|
}
|
2021-09-10 21:29:37 +08:00
|
|
|
key := base.TaskKey(msg.Queue, msg.ID)
|
2021-03-13 08:23:08 +08:00
|
|
|
data := map[string]interface{}{
|
2021-06-09 21:06:43 +08:00
|
|
|
"msg": encoded,
|
|
|
|
"state": state.String(),
|
|
|
|
"unique_key": msg.UniqueKey,
|
2022-03-06 22:04:56 +08:00
|
|
|
"group": msg.GroupKey,
|
2021-03-13 08:23:08 +08:00
|
|
|
}
|
2021-09-02 20:56:02 +08:00
|
|
|
if err := c.HSet(context.Background(), key, data).Err(); err != nil {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Fatal(err)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
2021-06-09 21:06:43 +08:00
|
|
|
if len(msg.UniqueKey) > 0 {
|
2021-09-10 21:29:37 +08:00
|
|
|
err := c.SetNX(context.Background(), msg.UniqueKey, msg.ID, 1*time.Minute).Err()
|
2021-06-09 21:06:43 +08:00
|
|
|
if err != nil {
|
|
|
|
tb.Fatalf("Failed to set unique lock in redis: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-26 21:58:33 +08:00
|
|
|
func seedRedisZSet(tb testing.TB, c redis.UniversalClient, key string,
|
2021-05-12 11:43:01 +08:00
|
|
|
items []base.Z, state base.TaskState) {
|
2021-03-13 08:23:08 +08:00
|
|
|
tb.Helper()
|
2019-12-29 12:12:14 +08:00
|
|
|
for _, item := range items {
|
2021-03-13 08:23:08 +08:00
|
|
|
msg := item.Message
|
|
|
|
encoded := MustMarshal(tb, msg)
|
2021-09-10 21:29:37 +08:00
|
|
|
z := &redis.Z{Member: msg.ID, Score: float64(item.Score)}
|
2021-09-02 20:56:02 +08:00
|
|
|
if err := c.ZAdd(context.Background(), key, z).Err(); err != nil {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Fatal(err)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
2021-09-10 21:29:37 +08:00
|
|
|
key := base.TaskKey(msg.Queue, msg.ID)
|
2021-03-13 08:23:08 +08:00
|
|
|
data := map[string]interface{}{
|
2021-06-09 21:06:43 +08:00
|
|
|
"msg": encoded,
|
|
|
|
"state": state.String(),
|
|
|
|
"unique_key": msg.UniqueKey,
|
2022-03-06 22:04:56 +08:00
|
|
|
"group": msg.GroupKey,
|
2021-03-13 08:23:08 +08:00
|
|
|
}
|
2021-09-02 20:56:02 +08:00
|
|
|
if err := c.HSet(context.Background(), key, data).Err(); err != nil {
|
2021-03-13 08:23:08 +08:00
|
|
|
tb.Fatal(err)
|
|
|
|
}
|
2021-06-09 21:06:43 +08:00
|
|
|
if len(msg.UniqueKey) > 0 {
|
2021-09-10 21:29:37 +08:00
|
|
|
err := c.SetNX(context.Background(), msg.UniqueKey, msg.ID, 1*time.Minute).Err()
|
2021-06-09 21:06:43 +08:00
|
|
|
if err != nil {
|
|
|
|
tb.Fatalf("Failed to set unique lock in redis: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 22:03:43 +08:00
|
|
|
// GetPendingMessages returns all pending messages in the given queue.
|
2021-04-26 21:58:33 +08:00
|
|
|
// It also asserts the state field of the task.
|
2020-09-05 22:03:43 +08:00
|
|
|
func GetPendingMessages(tb testing.TB, r redis.UniversalClient, qname string) []*base.TaskMessage {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
2021-05-12 11:43:01 +08:00
|
|
|
return getMessagesFromList(tb, r, qname, base.PendingKey, base.TaskStatePending)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
2020-09-06 03:43:15 +08:00
|
|
|
// GetActiveMessages returns all active messages in the given queue.
|
2021-04-26 21:58:33 +08:00
|
|
|
// It also asserts the state field of the task.
|
2020-09-06 03:43:15 +08:00
|
|
|
func GetActiveMessages(tb testing.TB, r redis.UniversalClient, qname string) []*base.TaskMessage {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
2021-05-12 11:43:01 +08:00
|
|
|
return getMessagesFromList(tb, r, qname, base.ActiveKey, base.TaskStateActive)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
2020-08-07 21:31:02 +08:00
|
|
|
// GetScheduledMessages returns all scheduled task messages in the given queue.
|
2021-04-26 21:58:33 +08:00
|
|
|
// It also asserts the state field of the task.
|
2020-08-28 20:40:16 +08:00
|
|
|
func GetScheduledMessages(tb testing.TB, r redis.UniversalClient, qname string) []*base.TaskMessage {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
2021-05-12 11:43:01 +08:00
|
|
|
return getMessagesFromZSet(tb, r, qname, base.ScheduledKey, base.TaskStateScheduled)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
2020-08-07 21:31:02 +08:00
|
|
|
// GetRetryMessages returns all retry messages in the given queue.
|
2021-04-26 21:58:33 +08:00
|
|
|
// It also asserts the state field of the task.
|
2020-08-28 20:40:16 +08:00
|
|
|
func GetRetryMessages(tb testing.TB, r redis.UniversalClient, qname string) []*base.TaskMessage {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
2021-05-12 11:43:01 +08:00
|
|
|
return getMessagesFromZSet(tb, r, qname, base.RetryKey, base.TaskStateRetry)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
// GetArchivedMessages returns all archived messages in the given queue.
|
2021-04-26 21:58:33 +08:00
|
|
|
// It also asserts the state field of the task.
|
2021-01-13 03:01:21 +08:00
|
|
|
func GetArchivedMessages(tb testing.TB, r redis.UniversalClient, qname string) []*base.TaskMessage {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
2021-05-12 11:43:01 +08:00
|
|
|
return getMessagesFromZSet(tb, r, qname, base.ArchivedKey, base.TaskStateArchived)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
2021-11-06 07:52:54 +08:00
|
|
|
// GetCompletedMessages returns all completed task messages in the given queue.
|
|
|
|
// It also asserts the state field of the task.
|
|
|
|
func GetCompletedMessages(tb testing.TB, r redis.UniversalClient, qname string) []*base.TaskMessage {
|
|
|
|
tb.Helper()
|
|
|
|
return getMessagesFromZSet(tb, r, qname, base.CompletedKey, base.TaskStateCompleted)
|
|
|
|
}
|
|
|
|
|
2020-08-07 21:31:02 +08:00
|
|
|
// GetScheduledEntries returns all scheduled messages and its score in the given queue.
|
2021-04-26 21:58:33 +08:00
|
|
|
// It also asserts the state field of the task.
|
2020-08-28 20:40:16 +08:00
|
|
|
func GetScheduledEntries(tb testing.TB, r redis.UniversalClient, qname string) []base.Z {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
2021-05-12 11:43:01 +08:00
|
|
|
return getMessagesFromZSetWithScores(tb, r, qname, base.ScheduledKey, base.TaskStateScheduled)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
2020-08-07 21:31:02 +08:00
|
|
|
// GetRetryEntries returns all retry messages and its score in the given queue.
|
2021-04-26 21:58:33 +08:00
|
|
|
// It also asserts the state field of the task.
|
2020-08-28 20:40:16 +08:00
|
|
|
func GetRetryEntries(tb testing.TB, r redis.UniversalClient, qname string) []base.Z {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
2021-05-12 11:43:01 +08:00
|
|
|
return getMessagesFromZSetWithScores(tb, r, qname, base.RetryKey, base.TaskStateRetry)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
// GetArchivedEntries returns all archived messages and its score in the given queue.
|
2021-04-26 21:58:33 +08:00
|
|
|
// It also asserts the state field of the task.
|
2021-01-13 03:01:21 +08:00
|
|
|
func GetArchivedEntries(tb testing.TB, r redis.UniversalClient, qname string) []base.Z {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
2021-05-12 11:43:01 +08:00
|
|
|
return getMessagesFromZSetWithScores(tb, r, qname, base.ArchivedKey, base.TaskStateArchived)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
2022-02-08 22:47:31 +08:00
|
|
|
// GetLeaseEntries returns all task IDs and its score in the lease set for the given queue.
|
|
|
|
// It also asserts the state field of the task.
|
|
|
|
func GetLeaseEntries(tb testing.TB, r redis.UniversalClient, qname string) []base.Z {
|
|
|
|
tb.Helper()
|
|
|
|
return getMessagesFromZSetWithScores(tb, r, qname, base.LeaseKey, base.TaskStateActive)
|
|
|
|
}
|
|
|
|
|
2021-11-06 07:52:54 +08:00
|
|
|
// GetCompletedEntries returns all completed messages and its score in the given queue.
|
|
|
|
// It also asserts the state field of the task.
|
|
|
|
func GetCompletedEntries(tb testing.TB, r redis.UniversalClient, qname string) []base.Z {
|
|
|
|
tb.Helper()
|
|
|
|
return getMessagesFromZSetWithScores(tb, r, qname, base.CompletedKey, base.TaskStateCompleted)
|
|
|
|
}
|
|
|
|
|
2022-03-05 22:41:44 +08:00
|
|
|
// GetGroupEntries returns all scheduled messages and its score in the given queue.
|
|
|
|
// It also asserts the state field of the task.
|
|
|
|
func GetGroupEntries(tb testing.TB, r redis.UniversalClient, qname, groupKey string) []base.Z {
|
|
|
|
tb.Helper()
|
|
|
|
return getMessagesFromZSetWithScores(tb, r, qname,
|
|
|
|
func(qname string) string { return base.GroupKey(qname, groupKey) }, base.TaskStateAggregating)
|
|
|
|
}
|
|
|
|
|
2021-03-13 08:23:08 +08:00
|
|
|
// Retrieves all messages stored under `keyFn(qname)` key in redis list.
|
2021-04-26 21:58:33 +08:00
|
|
|
func getMessagesFromList(tb testing.TB, r redis.UniversalClient, qname string,
|
2021-05-12 11:43:01 +08:00
|
|
|
keyFn func(qname string) string, state base.TaskState) []*base.TaskMessage {
|
2021-03-13 08:23:08 +08:00
|
|
|
tb.Helper()
|
2021-09-02 20:56:02 +08:00
|
|
|
ids := r.LRange(context.Background(), keyFn(qname), 0, -1).Val()
|
2021-03-13 08:23:08 +08:00
|
|
|
var msgs []*base.TaskMessage
|
|
|
|
for _, id := range ids {
|
2021-04-26 21:58:33 +08:00
|
|
|
taskKey := base.TaskKey(qname, id)
|
2021-09-02 20:56:02 +08:00
|
|
|
data := r.HGet(context.Background(), taskKey, "msg").Val()
|
2021-03-13 08:23:08 +08:00
|
|
|
msgs = append(msgs, MustUnmarshal(tb, data))
|
2021-09-02 20:56:02 +08:00
|
|
|
if gotState := r.HGet(context.Background(), taskKey, "state").Val(); gotState != state.String() {
|
2021-05-12 11:43:01 +08:00
|
|
|
tb.Errorf("task (id=%q) is in %q state, want %v", id, gotState, state)
|
2021-04-26 21:58:33 +08:00
|
|
|
}
|
2021-03-13 08:23:08 +08:00
|
|
|
}
|
|
|
|
return msgs
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
2021-03-13 08:23:08 +08:00
|
|
|
// Retrieves all messages stored under `keyFn(qname)` key in redis zset (sorted-set).
|
2021-04-26 21:58:33 +08:00
|
|
|
func getMessagesFromZSet(tb testing.TB, r redis.UniversalClient, qname string,
|
2021-05-12 11:43:01 +08:00
|
|
|
keyFn func(qname string) string, state base.TaskState) []*base.TaskMessage {
|
2021-03-13 08:23:08 +08:00
|
|
|
tb.Helper()
|
2021-09-02 20:56:02 +08:00
|
|
|
ids := r.ZRange(context.Background(), keyFn(qname), 0, -1).Val()
|
2021-03-13 08:23:08 +08:00
|
|
|
var msgs []*base.TaskMessage
|
|
|
|
for _, id := range ids {
|
2021-04-26 21:58:33 +08:00
|
|
|
taskKey := base.TaskKey(qname, id)
|
2021-09-02 20:56:02 +08:00
|
|
|
msg := r.HGet(context.Background(), taskKey, "msg").Val()
|
2021-03-13 08:23:08 +08:00
|
|
|
msgs = append(msgs, MustUnmarshal(tb, msg))
|
2021-09-02 20:56:02 +08:00
|
|
|
if gotState := r.HGet(context.Background(), taskKey, "state").Val(); gotState != state.String() {
|
2021-05-12 11:43:01 +08:00
|
|
|
tb.Errorf("task (id=%q) is in %q state, want %v", id, gotState, state)
|
2021-04-26 21:58:33 +08:00
|
|
|
}
|
2021-03-13 08:23:08 +08:00
|
|
|
}
|
|
|
|
return msgs
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
2021-03-13 08:23:08 +08:00
|
|
|
// Retrieves all messages along with their scores stored under `keyFn(qname)` key in redis zset (sorted-set).
|
2021-04-26 21:58:33 +08:00
|
|
|
func getMessagesFromZSetWithScores(tb testing.TB, r redis.UniversalClient,
|
2021-05-12 11:43:01 +08:00
|
|
|
qname string, keyFn func(qname string) string, state base.TaskState) []base.Z {
|
2021-03-13 08:23:08 +08:00
|
|
|
tb.Helper()
|
2021-09-02 20:56:02 +08:00
|
|
|
zs := r.ZRangeWithScores(context.Background(), keyFn(qname), 0, -1).Val()
|
2021-03-13 08:23:08 +08:00
|
|
|
var res []base.Z
|
|
|
|
for _, z := range zs {
|
2021-04-26 21:58:33 +08:00
|
|
|
taskID := z.Member.(string)
|
|
|
|
taskKey := base.TaskKey(qname, taskID)
|
2021-09-02 20:56:02 +08:00
|
|
|
msg := r.HGet(context.Background(), taskKey, "msg").Val()
|
2021-03-13 08:23:08 +08:00
|
|
|
res = append(res, base.Z{Message: MustUnmarshal(tb, msg), Score: int64(z.Score)})
|
2021-09-02 20:56:02 +08:00
|
|
|
if gotState := r.HGet(context.Background(), taskKey, "state").Val(); gotState != state.String() {
|
2021-05-12 11:43:01 +08:00
|
|
|
tb.Errorf("task (id=%q) is in %q state, want %v", taskID, gotState, state)
|
2021-04-26 21:58:33 +08:00
|
|
|
}
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
2021-03-13 08:23:08 +08:00
|
|
|
return res
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|