2019-11-30 12:53:29 +08:00
|
|
|
package asynq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-12-05 09:23:11 +08:00
|
|
|
"math/rand"
|
2019-11-30 12:53:29 +08:00
|
|
|
"sort"
|
|
|
|
"testing"
|
2019-12-05 09:23:11 +08:00
|
|
|
"time"
|
2019-11-30 12:53:29 +08:00
|
|
|
|
2019-12-04 13:01:26 +08:00
|
|
|
"github.com/go-redis/redis/v7"
|
2019-11-30 12:53:29 +08:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2019-12-21 23:42:32 +08:00
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2019-12-22 23:15:45 +08:00
|
|
|
"github.com/hibiken/asynq/internal/base"
|
2019-12-11 23:38:24 +08:00
|
|
|
"github.com/rs/xid"
|
2019-11-30 12:53:29 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// This file defines test helper functions used by
|
|
|
|
// other test files.
|
|
|
|
|
2019-12-05 09:23:11 +08:00
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
2019-12-21 23:42:32 +08:00
|
|
|
// scheduledEntry represents an item in redis sorted set (aka ZSET).
|
|
|
|
type sortedSetEntry struct {
|
2019-12-22 23:15:45 +08:00
|
|
|
msg *base.TaskMessage
|
2019-12-21 23:42:32 +08:00
|
|
|
score int64
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:01:26 +08:00
|
|
|
func setup(t *testing.T) *redis.Client {
|
|
|
|
t.Helper()
|
|
|
|
r := redis.NewClient(&redis.Options{
|
|
|
|
Addr: "localhost:6379",
|
2019-12-04 23:38:55 +08:00
|
|
|
DB: 14,
|
2019-11-30 12:53:29 +08:00
|
|
|
})
|
2019-12-04 13:01:26 +08:00
|
|
|
// Start each test with a clean slate.
|
|
|
|
if err := r.FlushDB().Err(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
2019-11-30 12:53:29 +08:00
|
|
|
|
|
|
|
var sortTaskOpt = cmp.Transformer("SortMsg", func(in []*Task) []*Task {
|
|
|
|
out := append([]*Task(nil), in...) // Copy input to avoid mutating it
|
|
|
|
sort.Slice(out, func(i, j int) bool {
|
|
|
|
return out[i].Type < out[j].Type
|
|
|
|
})
|
|
|
|
return out
|
|
|
|
})
|
|
|
|
|
2019-12-22 23:15:45 +08:00
|
|
|
var sortMsgOpt = cmp.Transformer("SortMsg", func(in []*base.TaskMessage) []*base.TaskMessage {
|
|
|
|
out := append([]*base.TaskMessage(nil), in...) // Copy input to avoid mutating it
|
2019-12-04 13:01:26 +08:00
|
|
|
sort.Slice(out, func(i, j int) bool {
|
|
|
|
return out[i].ID.String() < out[j].ID.String()
|
2019-11-30 12:53:29 +08:00
|
|
|
})
|
2019-12-04 13:01:26 +08:00
|
|
|
return out
|
|
|
|
})
|
2019-12-21 23:42:32 +08:00
|
|
|
|
|
|
|
var sortZSetEntryOpt = cmp.Transformer("SortZSetEntry", func(in []sortedSetEntry) []sortedSetEntry {
|
|
|
|
out := append([]sortedSetEntry(nil), in...) // Copy input to avoid mutating it
|
|
|
|
sort.Slice(out, func(i, j int) bool {
|
|
|
|
return out[i].msg.ID.String() < out[j].msg.ID.String()
|
|
|
|
})
|
|
|
|
return out
|
|
|
|
})
|
|
|
|
|
2019-12-22 23:15:45 +08:00
|
|
|
var ignoreIDOpt = cmpopts.IgnoreFields(base.TaskMessage{}, "ID")
|
2019-11-30 12:53:29 +08:00
|
|
|
|
2019-12-22 23:15:45 +08:00
|
|
|
func randomTask(taskType, qname string, payload map[string]interface{}) *base.TaskMessage {
|
|
|
|
return &base.TaskMessage{
|
2019-12-11 23:38:24 +08:00
|
|
|
ID: xid.New(),
|
2019-11-30 12:53:29 +08:00
|
|
|
Type: taskType,
|
|
|
|
Queue: qname,
|
2019-12-04 13:01:26 +08:00
|
|
|
Retry: defaultMaxRetry,
|
2019-11-30 12:53:29 +08:00
|
|
|
Payload: make(map[string]interface{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 23:15:45 +08:00
|
|
|
func mustMarshal(t *testing.T, task *base.TaskMessage) string {
|
2019-11-30 12:53:29 +08:00
|
|
|
t.Helper()
|
|
|
|
data, err := json.Marshal(task)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
2019-12-22 23:15:45 +08:00
|
|
|
func mustUnmarshal(t *testing.T, data string) *base.TaskMessage {
|
2019-11-30 12:53:29 +08:00
|
|
|
t.Helper()
|
2019-12-22 23:15:45 +08:00
|
|
|
var task base.TaskMessage
|
2019-11-30 12:53:29 +08:00
|
|
|
err := json.Unmarshal([]byte(data), &task)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return &task
|
|
|
|
}
|
|
|
|
|
2019-12-22 23:15:45 +08:00
|
|
|
func mustMarshalSlice(t *testing.T, tasks []*base.TaskMessage) []string {
|
2019-11-30 12:53:29 +08:00
|
|
|
t.Helper()
|
|
|
|
var data []string
|
|
|
|
for _, task := range tasks {
|
|
|
|
data = append(data, mustMarshal(t, task))
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2019-12-22 23:15:45 +08:00
|
|
|
func mustUnmarshalSlice(t *testing.T, data []string) []*base.TaskMessage {
|
2019-11-30 12:53:29 +08:00
|
|
|
t.Helper()
|
2019-12-22 23:15:45 +08:00
|
|
|
var tasks []*base.TaskMessage
|
2019-11-30 12:53:29 +08:00
|
|
|
for _, s := range data {
|
|
|
|
tasks = append(tasks, mustUnmarshal(t, s))
|
|
|
|
}
|
|
|
|
return tasks
|
|
|
|
}
|