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 (
|
|
|
|
"encoding/json"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/go-redis/redis/v7"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return out[i].ID.String() < out[j].ID.String()
|
|
|
|
})
|
|
|
|
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 {
|
2020-07-13 21:29:41 +08:00
|
|
|
return out[i].Message.ID.String() < out[j].Message.ID.String()
|
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-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.
|
|
|
|
func NewTaskMessage(taskType string, payload map[string]interface{}) *base.TaskMessage {
|
|
|
|
return &base.TaskMessage{
|
2020-07-02 21:21:20 +08:00
|
|
|
ID: uuid.New(),
|
2020-06-17 12:11:54 +08:00
|
|
|
Type: taskType,
|
|
|
|
Queue: base.DefaultQueueName,
|
|
|
|
Retry: 25,
|
|
|
|
Payload: payload,
|
|
|
|
Timeout: 1800, // default timeout of 30 mins
|
|
|
|
Deadline: 0, // no deadline
|
2020-01-10 22:56:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTaskMessageWithQueue returns a new instance of TaskMessage given a
|
|
|
|
// task type, payload and queue name.
|
|
|
|
func NewTaskMessageWithQueue(taskType string, payload map[string]interface{}, qname string) *base.TaskMessage {
|
|
|
|
return &base.TaskMessage{
|
2020-07-02 21:21:20 +08:00
|
|
|
ID: uuid.New(),
|
2020-01-10 22:56:51 +08:00
|
|
|
Type: taskType,
|
|
|
|
Queue: qname,
|
2019-12-29 12:12:14 +08:00
|
|
|
Retry: 25,
|
|
|
|
Payload: payload,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 22:05:57 +08:00
|
|
|
// TaskMessageAfterRetry returns an updated copy of t after retry.
|
|
|
|
// It increments retry count and sets the error message.
|
|
|
|
func TaskMessageAfterRetry(t base.TaskMessage, errMsg string) *base.TaskMessage {
|
|
|
|
t.Retried = t.Retried + 1
|
|
|
|
t.ErrorMsg = errMsg
|
|
|
|
return &t
|
|
|
|
}
|
|
|
|
|
|
|
|
// TaskMessageWithError returns an updated copy of t with the given error message.
|
|
|
|
func TaskMessageWithError(t base.TaskMessage, errMsg string) *base.TaskMessage {
|
|
|
|
t.ErrorMsg = errMsg
|
|
|
|
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()
|
2019-12-29 12:12:14 +08:00
|
|
|
data, err := json.Marshal(msg)
|
|
|
|
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()
|
2019-12-29 12:12:14 +08:00
|
|
|
var msg base.TaskMessage
|
|
|
|
err := json.Unmarshal([]byte(data), &msg)
|
|
|
|
if err != nil {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Fatal(err)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
return &msg
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustMarshalSlice marshals a slice of task messages and return a slice of
|
|
|
|
// json strings. Calling test will fail if marshaling errors out.
|
2019-12-31 12:10:34 +08:00
|
|
|
func MustMarshalSlice(tb testing.TB, msgs []*base.TaskMessage) []string {
|
|
|
|
tb.Helper()
|
2019-12-29 12:12:14 +08:00
|
|
|
var data []string
|
|
|
|
for _, m := range msgs {
|
2019-12-31 12:10:34 +08:00
|
|
|
data = append(data, MustMarshal(tb, m))
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustUnmarshalSlice unmarshals a slice of strings into a slice of task message structs.
|
|
|
|
// Calling test will fail if marshaling errors out.
|
2019-12-31 12:10:34 +08:00
|
|
|
func MustUnmarshalSlice(tb testing.TB, data []string) []*base.TaskMessage {
|
|
|
|
tb.Helper()
|
2019-12-29 12:12:14 +08:00
|
|
|
var msgs []*base.TaskMessage
|
|
|
|
for _, s := range data {
|
2019-12-31 12:10:34 +08:00
|
|
|
msgs = append(msgs, MustUnmarshal(tb, s))
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
return msgs
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlushDB deletes all the keys of the currently selected DB.
|
2019-12-31 12:10:34 +08:00
|
|
|
func FlushDB(tb testing.TB, r *redis.Client) {
|
|
|
|
tb.Helper()
|
2019-12-29 12:12:14 +08:00
|
|
|
if err := r.FlushDB().Err(); err != nil {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Fatal(err)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-07 22:28:34 +08:00
|
|
|
// SeedEnqueuedQueue initializes the specified queue with the given messages.
|
|
|
|
//
|
|
|
|
// If queue name option is not passed, it defaults to the default queue.
|
|
|
|
func SeedEnqueuedQueue(tb testing.TB, r *redis.Client, msgs []*base.TaskMessage, queueOpt ...string) {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
2020-01-07 22:28:34 +08:00
|
|
|
queue := base.DefaultQueue
|
|
|
|
if len(queueOpt) > 0 {
|
|
|
|
queue = base.QueueKey(queueOpt[0])
|
|
|
|
}
|
2020-01-10 22:56:51 +08:00
|
|
|
r.SAdd(base.AllQueues, queue)
|
2020-01-07 22:28:34 +08:00
|
|
|
seedRedisList(tb, r, queue, msgs)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
2020-07-13 21:29:41 +08:00
|
|
|
// SeedAllEnqueuedQueues initializes all of the specified queues with the given messages.
|
|
|
|
//
|
2020-07-26 22:01:19 +08:00
|
|
|
// enqueued maps a queue name to a list of messages.
|
2020-07-13 21:29:41 +08:00
|
|
|
func SeedAllEnqueuedQueues(tb testing.TB, r *redis.Client, enqueued map[string][]*base.TaskMessage) {
|
|
|
|
for q, msgs := range enqueued {
|
|
|
|
SeedEnqueuedQueue(tb, r, msgs, q)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
// SeedInProgressQueue initializes the in-progress queue with the given messages.
|
2019-12-31 12:10:34 +08:00
|
|
|
func SeedInProgressQueue(tb testing.TB, r *redis.Client, msgs []*base.TaskMessage) {
|
|
|
|
tb.Helper()
|
|
|
|
seedRedisList(tb, r, base.InProgressQueue, msgs)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SeedScheduledQueue initializes the scheduled queue with the given messages.
|
2020-07-13 21:29:41 +08:00
|
|
|
func SeedScheduledQueue(tb testing.TB, r *redis.Client, entries []base.Z) {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
|
|
|
seedRedisZSet(tb, r, base.ScheduledQueue, entries)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SeedRetryQueue initializes the retry queue with the given messages.
|
2020-07-13 21:29:41 +08:00
|
|
|
func SeedRetryQueue(tb testing.TB, r *redis.Client, entries []base.Z) {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
|
|
|
seedRedisZSet(tb, r, base.RetryQueue, entries)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SeedDeadQueue initializes the dead queue with the given messages.
|
2020-07-13 21:29:41 +08:00
|
|
|
func SeedDeadQueue(tb testing.TB, r *redis.Client, entries []base.Z) {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
|
|
|
seedRedisZSet(tb, r, base.DeadQueue, entries)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
2020-06-18 22:10:57 +08:00
|
|
|
// SeedDeadlines initializes the deadlines set with the given entries.
|
2020-07-13 21:29:41 +08:00
|
|
|
func SeedDeadlines(tb testing.TB, r *redis.Client, entries []base.Z) {
|
2020-06-18 22:10:57 +08:00
|
|
|
tb.Helper()
|
|
|
|
seedRedisZSet(tb, r, base.KeyDeadlines, entries)
|
|
|
|
}
|
|
|
|
|
2019-12-31 12:10:34 +08:00
|
|
|
func seedRedisList(tb testing.TB, c *redis.Client, key string, msgs []*base.TaskMessage) {
|
|
|
|
data := MustMarshalSlice(tb, msgs)
|
2019-12-29 12:12:14 +08:00
|
|
|
for _, s := range data {
|
|
|
|
if err := c.LPush(key, s).Err(); err != nil {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Fatal(err)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-13 21:29:41 +08:00
|
|
|
func seedRedisZSet(tb testing.TB, c *redis.Client, key string, items []base.Z) {
|
2019-12-29 12:12:14 +08:00
|
|
|
for _, item := range items {
|
2020-07-13 21:29:41 +08:00
|
|
|
z := &redis.Z{Member: MustMarshal(tb, item.Message), Score: float64(item.Score)}
|
2019-12-29 12:12:14 +08:00
|
|
|
if err := c.ZAdd(key, z).Err(); err != nil {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Fatal(err)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-06 22:53:40 +08:00
|
|
|
// GetEnqueuedMessages returns all task messages in the specified queue.
|
|
|
|
//
|
|
|
|
// If queue name option is not passed, it defaults to the default queue.
|
|
|
|
func GetEnqueuedMessages(tb testing.TB, r *redis.Client, queueOpt ...string) []*base.TaskMessage {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
2020-01-06 22:53:40 +08:00
|
|
|
queue := base.DefaultQueue
|
|
|
|
if len(queueOpt) > 0 {
|
|
|
|
queue = base.QueueKey(queueOpt[0])
|
|
|
|
}
|
|
|
|
return getListMessages(tb, r, queue)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetInProgressMessages returns all task messages in the in-progress queue.
|
2019-12-31 12:10:34 +08:00
|
|
|
func GetInProgressMessages(tb testing.TB, r *redis.Client) []*base.TaskMessage {
|
|
|
|
tb.Helper()
|
|
|
|
return getListMessages(tb, r, base.InProgressQueue)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetScheduledMessages returns all task messages in the scheduled queue.
|
2019-12-31 12:10:34 +08:00
|
|
|
func GetScheduledMessages(tb testing.TB, r *redis.Client) []*base.TaskMessage {
|
|
|
|
tb.Helper()
|
|
|
|
return getZSetMessages(tb, r, base.ScheduledQueue)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRetryMessages returns all task messages in the retry queue.
|
2019-12-31 12:10:34 +08:00
|
|
|
func GetRetryMessages(tb testing.TB, r *redis.Client) []*base.TaskMessage {
|
|
|
|
tb.Helper()
|
|
|
|
return getZSetMessages(tb, r, base.RetryQueue)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDeadMessages returns all task messages in the dead queue.
|
2019-12-31 12:10:34 +08:00
|
|
|
func GetDeadMessages(tb testing.TB, r *redis.Client) []*base.TaskMessage {
|
|
|
|
tb.Helper()
|
|
|
|
return getZSetMessages(tb, r, base.DeadQueue)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetScheduledEntries returns all task messages and its score in the scheduled queue.
|
2020-07-13 21:29:41 +08:00
|
|
|
func GetScheduledEntries(tb testing.TB, r *redis.Client) []base.Z {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
|
|
|
return getZSetEntries(tb, r, base.ScheduledQueue)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRetryEntries returns all task messages and its score in the retry queue.
|
2020-07-13 21:29:41 +08:00
|
|
|
func GetRetryEntries(tb testing.TB, r *redis.Client) []base.Z {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
|
|
|
return getZSetEntries(tb, r, base.RetryQueue)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDeadEntries returns all task messages and its score in the dead queue.
|
2020-07-13 21:29:41 +08:00
|
|
|
func GetDeadEntries(tb testing.TB, r *redis.Client) []base.Z {
|
2019-12-31 12:10:34 +08:00
|
|
|
tb.Helper()
|
|
|
|
return getZSetEntries(tb, r, base.DeadQueue)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
2020-06-17 21:46:54 +08:00
|
|
|
// GetDeadlinesEntries returns all task messages and its score in the deadlines set.
|
2020-07-13 21:29:41 +08:00
|
|
|
func GetDeadlinesEntries(tb testing.TB, r *redis.Client) []base.Z {
|
2020-06-17 21:46:54 +08:00
|
|
|
tb.Helper()
|
|
|
|
return getZSetEntries(tb, r, base.KeyDeadlines)
|
|
|
|
}
|
|
|
|
|
2019-12-31 12:10:34 +08:00
|
|
|
func getListMessages(tb testing.TB, r *redis.Client, list string) []*base.TaskMessage {
|
2019-12-29 12:12:14 +08:00
|
|
|
data := r.LRange(list, 0, -1).Val()
|
2019-12-31 12:10:34 +08:00
|
|
|
return MustUnmarshalSlice(tb, data)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
2019-12-31 12:10:34 +08:00
|
|
|
func getZSetMessages(tb testing.TB, r *redis.Client, zset string) []*base.TaskMessage {
|
2019-12-29 12:12:14 +08:00
|
|
|
data := r.ZRange(zset, 0, -1).Val()
|
2019-12-31 12:10:34 +08:00
|
|
|
return MustUnmarshalSlice(tb, data)
|
2019-12-29 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
2020-07-13 21:29:41 +08:00
|
|
|
func getZSetEntries(tb testing.TB, r *redis.Client, zset string) []base.Z {
|
2019-12-29 12:12:14 +08:00
|
|
|
data := r.ZRangeWithScores(zset, 0, -1).Val()
|
2020-07-13 21:29:41 +08:00
|
|
|
var entries []base.Z
|
2019-12-29 12:12:14 +08:00
|
|
|
for _, z := range data {
|
2020-07-13 21:29:41 +08:00
|
|
|
entries = append(entries, base.Z{
|
|
|
|
Message: MustUnmarshal(tb, z.Member.(string)),
|
|
|
|
Score: int64(z.Score),
|
2019-12-29 12:12:14 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return entries
|
|
|
|
}
|