2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-11-14 11:31:18 +08:00
asynq/internal/rdb/helpers_test.go

157 lines
3.9 KiB
Go
Raw Normal View History

2019-12-13 11:49:41 +08:00
package rdb
import (
"encoding/json"
"math/rand"
"sort"
"testing"
"time"
"github.com/go-redis/redis/v7"
"github.com/google/go-cmp/cmp"
2019-12-22 23:15:45 +08:00
"github.com/hibiken/asynq/internal/base"
2019-12-13 11:49:41 +08:00
"github.com/rs/xid"
)
// This file defines test helpers for the rdb package testing.
func init() {
rand.Seed(time.Now().UnixNano())
}
// TODO(hibiken): Get Redis address and db number from ENV variables.
func setup(t *testing.T) *RDB {
t.Helper()
r := NewRDB(redis.NewClient(&redis.Options{
Addr: "localhost:6379",
DB: 13,
}))
// Start each test with a clean slate.
flushDB(t, r)
return r
}
func flushDB(t *testing.T, r *RDB) {
t.Helper()
if err := r.client.FlushDB().Err(); err != nil {
t.Fatal(err)
}
}
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-13 11:49:41 +08:00
sort.Slice(out, func(i, j int) bool {
return out[i].ID.String() < out[j].ID.String()
})
return out
})
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
func newTaskMessage(taskType string, payload map[string]interface{}) *base.TaskMessage {
return &base.TaskMessage{
2019-12-13 11:49:41 +08:00
ID: xid.New(),
Type: taskType,
Queue: "default",
Retry: 25,
Payload: payload,
}
}
2019-12-22 23:15:45 +08:00
func mustMarshal(t *testing.T, msg *base.TaskMessage) string {
2019-12-13 11:49:41 +08:00
t.Helper()
data, err := json.Marshal(msg)
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-12-13 11:49:41 +08:00
t.Helper()
2019-12-22 23:15:45 +08:00
var msg base.TaskMessage
2019-12-13 11:49:41 +08:00
err := json.Unmarshal([]byte(data), &msg)
if err != nil {
t.Fatal(err)
}
return &msg
}
2019-12-22 23:15:45 +08:00
func mustMarshalSlice(t *testing.T, msgs []*base.TaskMessage) []string {
2019-12-13 11:49:41 +08:00
t.Helper()
var data []string
for _, m := range msgs {
data = append(data, mustMarshal(t, m))
}
return data
}
2019-12-22 23:15:45 +08:00
func mustUnmarshalSlice(t *testing.T, data []string) []*base.TaskMessage {
2019-12-13 11:49:41 +08:00
t.Helper()
2019-12-22 23:15:45 +08:00
var msgs []*base.TaskMessage
2019-12-13 11:49:41 +08:00
for _, s := range data {
msgs = append(msgs, mustUnmarshal(t, s))
}
return msgs
}
2019-12-22 23:15:45 +08:00
func seedRedisList(t *testing.T, c *redis.Client, key string, msgs []*base.TaskMessage) {
2019-12-13 11:49:41 +08:00
data := mustMarshalSlice(t, msgs)
for _, s := range data {
if err := c.LPush(key, s).Err(); err != nil {
t.Fatal(err)
}
}
}
func seedRedisZSet(t *testing.T, c *redis.Client, key string, items []sortedSetEntry) {
for _, item := range items {
z := &redis.Z{Member: mustMarshal(t, item.msg), Score: float64(item.score)}
if err := c.ZAdd(key, z).Err(); err != nil {
t.Fatal(err)
}
}
}
// 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-13 11:49:41 +08:00
score int64
}
// seedDefaultQueue initializes the default queue with the given messages.
2019-12-22 23:15:45 +08:00
func seedDefaultQueue(t *testing.T, r *RDB, msgs []*base.TaskMessage) {
2019-12-13 11:49:41 +08:00
t.Helper()
2019-12-22 23:15:45 +08:00
seedRedisList(t, r.client, base.DefaultQueue, msgs)
2019-12-13 11:49:41 +08:00
}
// seedInProgressQueue initializes the in-progress queue with the given messages.
2019-12-22 23:15:45 +08:00
func seedInProgressQueue(t *testing.T, r *RDB, msgs []*base.TaskMessage) {
2019-12-13 11:49:41 +08:00
t.Helper()
2019-12-22 23:15:45 +08:00
seedRedisList(t, r.client, base.InProgressQueue, msgs)
2019-12-13 11:49:41 +08:00
}
// seedScheduledQueue initializes the scheduled queue with the given messages.
func seedScheduledQueue(t *testing.T, r *RDB, entries []sortedSetEntry) {
t.Helper()
2019-12-22 23:15:45 +08:00
seedRedisZSet(t, r.client, base.ScheduledQueue, entries)
2019-12-13 11:49:41 +08:00
}
// seedRetryQueue initializes the retry queue with the given messages.
func seedRetryQueue(t *testing.T, r *RDB, entries []sortedSetEntry) {
t.Helper()
2019-12-22 23:15:45 +08:00
seedRedisZSet(t, r.client, base.RetryQueue, entries)
2019-12-13 11:49:41 +08:00
}
// seedDeadQueue initializes the dead queue with the given messages.
func seedDeadQueue(t *testing.T, r *RDB, entries []sortedSetEntry) {
t.Helper()
2019-12-22 23:15:45 +08:00
seedRedisZSet(t, r.client, base.DeadQueue, entries)
2019-12-13 11:49:41 +08:00
}