2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-27 00:02:19 +08:00
asynq/internal/rdb/rdb_test.go

487 lines
12 KiB
Go
Raw Normal View History

2019-12-04 13:01:26 +08:00
package rdb
2019-11-20 23:01:24 +08:00
import (
2019-12-04 13:01:26 +08:00
"encoding/json"
2019-11-27 23:16:16 +08:00
"fmt"
2019-12-04 13:01:26 +08:00
"math/rand"
"sort"
2019-11-20 23:01:24 +08:00
"testing"
"time"
"github.com/go-redis/redis/v7"
"github.com/google/go-cmp/cmp"
"github.com/rs/xid"
2019-11-20 23:01:24 +08:00
)
2019-12-04 13:01:26 +08:00
func init() {
rand.Seed(time.Now().UnixNano())
}
func setup(t *testing.T) *RDB {
t.Helper()
r := NewRDB(redis.NewClient(&redis.Options{
Addr: "localhost:6379",
DB: 13,
2019-12-04 13:01:26 +08:00
}))
// Start each test with a clean slate.
if err := r.client.FlushDB().Err(); err != nil {
panic(err)
}
return r
}
var sortMsgOpt = cmp.Transformer("SortMsg", func(in []*TaskMessage) []*TaskMessage {
out := append([]*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
})
func randomTask(taskType, qname string, payload map[string]interface{}) *TaskMessage {
return &TaskMessage{
ID: xid.New(),
2019-12-04 13:01:26 +08:00
Type: taskType,
Queue: qname,
Retry: 25,
Payload: make(map[string]interface{}),
}
}
func mustMarshal(t *testing.T, task *TaskMessage) string {
t.Helper()
data, err := json.Marshal(task)
if err != nil {
t.Fatal(err)
}
return string(data)
}
func mustUnmarshal(t *testing.T, data string) *TaskMessage {
t.Helper()
var task TaskMessage
err := json.Unmarshal([]byte(data), &task)
if err != nil {
t.Fatal(err)
}
return &task
}
func mustMarshalSlice(t *testing.T, tasks []*TaskMessage) []string {
t.Helper()
var data []string
for _, task := range tasks {
data = append(data, mustMarshal(t, task))
}
return data
}
func mustUnmarshalSlice(t *testing.T, data []string) []*TaskMessage {
t.Helper()
var tasks []*TaskMessage
for _, s := range data {
tasks = append(tasks, mustUnmarshal(t, s))
}
return tasks
}
2019-11-26 11:58:24 +08:00
func TestEnqueue(t *testing.T) {
2019-11-26 10:55:17 +08:00
r := setup(t)
tests := []struct {
2019-12-04 13:01:26 +08:00
msg *TaskMessage
2019-11-26 10:55:17 +08:00
}{
2019-11-26 12:57:53 +08:00
{msg: randomTask("send_email", "default",
map[string]interface{}{"to": "exampleuser@gmail.com", "from": "noreply@example.com"})},
{msg: randomTask("generate_csv", "default",
map[string]interface{}{})},
2019-11-26 10:55:17 +08:00
{msg: randomTask("sync", "default", nil)},
2019-11-20 23:01:24 +08:00
}
2019-11-26 10:55:17 +08:00
for _, tc := range tests {
2019-11-26 12:57:53 +08:00
// clean up db before each test case.
2019-11-27 22:41:54 +08:00
if err := r.client.FlushDB().Err(); err != nil {
2019-11-26 12:57:53 +08:00
t.Fatal(err)
}
2019-12-04 13:01:26 +08:00
err := r.Enqueue(tc.msg)
2019-11-26 10:55:17 +08:00
if err != nil {
t.Error(err)
2019-11-28 12:05:31 +08:00
continue
2019-11-26 10:55:17 +08:00
}
res := r.client.LRange(defaultQ, 0, -1).Val()
2019-11-26 10:55:17 +08:00
if len(res) != 1 {
t.Errorf("LIST %q has length %d, want 1", defaultQ, len(res))
2019-11-26 10:55:17 +08:00
continue
}
2019-11-28 23:17:07 +08:00
if diff := cmp.Diff(*tc.msg, *mustUnmarshal(t, res[0])); diff != "" {
2019-11-26 10:55:17 +08:00
t.Errorf("persisted data differed from the original input (-want, +got)\n%s", diff)
}
2019-11-20 23:01:24 +08:00
}
}
2019-11-26 12:57:53 +08:00
func TestDequeue(t *testing.T) {
2019-11-26 10:55:17 +08:00
r := setup(t)
2019-11-26 12:57:53 +08:00
t1 := randomTask("send_email", "default", map[string]interface{}{"subject": "hello!"})
tests := []struct {
2019-12-04 13:01:26 +08:00
queued []*TaskMessage
want *TaskMessage
2019-11-26 12:57:53 +08:00
err error
inProgress int64 // length of "in-progress" tasks after dequeue
}{
2019-12-04 13:01:26 +08:00
{queued: []*TaskMessage{t1}, want: t1, err: nil, inProgress: 1},
{queued: []*TaskMessage{}, want: nil, err: ErrDequeueTimeout, inProgress: 0},
2019-11-22 13:45:27 +08:00
}
2019-11-20 23:01:24 +08:00
2019-11-26 12:57:53 +08:00
for _, tc := range tests {
// clean up db before each test case.
2019-11-27 22:41:54 +08:00
if err := r.client.FlushDB().Err(); err != nil {
2019-11-26 12:57:53 +08:00
t.Fatal(err)
}
for _, m := range tc.queued {
2019-12-04 13:01:26 +08:00
r.Enqueue(m)
2019-11-26 12:57:53 +08:00
}
2019-12-04 22:25:58 +08:00
got, err := r.Dequeue(time.Second)
2019-11-26 12:57:53 +08:00
if !cmp.Equal(got, tc.want) || err != tc.err {
t.Errorf("(*RDB).Dequeue(time.Second) = %v, %v; want %v, %v",
got, err, tc.want, tc.err)
2019-11-26 12:57:53 +08:00
continue
}
if l := r.client.LLen(inProgressQ).Val(); l != tc.inProgress {
t.Errorf("LIST %q has length %d, want %d", inProgressQ, l, tc.inProgress)
2019-11-26 12:57:53 +08:00
}
2019-11-20 23:01:24 +08:00
}
}
2019-12-04 22:33:05 +08:00
func TestDone(t *testing.T) {
2019-11-28 22:50:05 +08:00
r := setup(t)
t1 := randomTask("send_email", "default", nil)
t2 := randomTask("export_csv", "csv", nil)
tests := []struct {
2019-12-04 22:33:05 +08:00
initial []*TaskMessage // initial state of the in-progress list
2019-12-04 13:01:26 +08:00
target *TaskMessage // task to remove
2019-12-04 22:33:05 +08:00
final []*TaskMessage // final state of the in-progress list
2019-11-28 22:50:05 +08:00
}{
{
2019-12-04 13:01:26 +08:00
initial: []*TaskMessage{t1, t2},
2019-11-28 22:50:05 +08:00
target: t1,
2019-12-04 13:01:26 +08:00
final: []*TaskMessage{t2},
2019-11-28 22:50:05 +08:00
},
{
2019-12-04 13:01:26 +08:00
initial: []*TaskMessage{t2},
2019-11-28 22:50:05 +08:00
target: t1,
2019-12-04 13:01:26 +08:00
final: []*TaskMessage{t2},
2019-11-28 22:50:05 +08:00
},
{
2019-12-04 13:01:26 +08:00
initial: []*TaskMessage{t1},
2019-11-28 22:50:05 +08:00
target: t1,
2019-12-04 13:01:26 +08:00
final: []*TaskMessage{},
2019-11-28 22:50:05 +08:00
},
}
for _, tc := range tests {
// clean up db before each test case.
if err := r.client.FlushDB().Err(); err != nil {
t.Fatal(err)
}
// set up initial state
for _, task := range tc.initial {
err := r.client.LPush(inProgressQ, mustMarshal(t, task)).Err()
2019-11-28 22:50:05 +08:00
if err != nil {
t.Fatal(err)
}
}
2019-12-04 22:33:05 +08:00
err := r.Done(tc.target)
2019-11-28 22:50:05 +08:00
if err != nil {
t.Error(err)
continue
}
2019-12-04 13:01:26 +08:00
var got []*TaskMessage
data := r.client.LRange(inProgressQ, 0, -1).Val()
2019-11-28 22:50:05 +08:00
for _, s := range data {
got = append(got, mustUnmarshal(t, s))
}
if diff := cmp.Diff(tc.final, got, sortMsgOpt); diff != "" {
t.Errorf("mismatch found in %q after calling (*rdb).remove: (-want, +got):\n%s", defaultQ, diff)
2019-11-28 23:47:12 +08:00
continue
}
}
}
func TestKill(t *testing.T) {
r := setup(t)
t1 := randomTask("send_email", "default", nil)
// TODO(hibiken): add test cases for trimming
tests := []struct {
2019-12-04 13:01:26 +08:00
initial []*TaskMessage // inital state of "dead" set
target *TaskMessage // task to kill
want []*TaskMessage // final state of "dead" set
2019-11-28 23:47:12 +08:00
}{
{
2019-12-04 13:01:26 +08:00
initial: []*TaskMessage{},
2019-11-28 23:47:12 +08:00
target: t1,
2019-12-04 13:01:26 +08:00
want: []*TaskMessage{t1},
2019-11-28 23:47:12 +08:00
},
}
for _, tc := range tests {
// clean up db before each test case.
if err := r.client.FlushDB().Err(); err != nil {
t.Fatal(err)
}
// set up initial state
for _, task := range tc.initial {
err := r.client.ZAdd(deadQ, &redis.Z{Member: mustMarshal(t, task), Score: float64(time.Now().Unix())}).Err()
2019-11-28 23:47:12 +08:00
if err != nil {
t.Fatal(err)
}
}
2019-12-04 13:01:26 +08:00
err := r.Kill(tc.target)
2019-11-28 23:47:12 +08:00
if err != nil {
t.Error(err)
continue
}
actual := r.client.ZRange(deadQ, 0, -1).Val()
2019-11-28 23:47:12 +08:00
got := mustUnmarshalSlice(t, actual)
if diff := cmp.Diff(tc.want, got, sortMsgOpt); diff != "" {
t.Errorf("mismatch found in %q after calling (*rdb).kill: (-want, +got):\n%s", deadQ, diff)
2019-11-28 23:47:12 +08:00
continue
2019-11-28 22:50:05 +08:00
}
}
}
2019-12-04 22:50:52 +08:00
func TestRestoreUnfinished(t *testing.T) {
2019-11-26 10:55:17 +08:00
r := setup(t)
2019-11-27 01:57:53 +08:00
t1 := randomTask("send_email", "default", nil)
t2 := randomTask("export_csv", "csv", nil)
t3 := randomTask("sync_stuff", "sync", nil)
2019-11-27 01:57:53 +08:00
tests := []struct {
2019-12-04 13:01:26 +08:00
beforeSrc []*TaskMessage
beforeDst []*TaskMessage
afterSrc []*TaskMessage
afterDst []*TaskMessage
2019-11-27 01:57:53 +08:00
}{
{
2019-12-04 13:01:26 +08:00
beforeSrc: []*TaskMessage{t1, t2, t3},
beforeDst: []*TaskMessage{},
afterSrc: []*TaskMessage{},
afterDst: []*TaskMessage{t1, t2, t3},
2019-11-27 01:57:53 +08:00
},
{
2019-12-04 13:01:26 +08:00
beforeSrc: []*TaskMessage{},
beforeDst: []*TaskMessage{t1, t2, t3},
afterSrc: []*TaskMessage{},
afterDst: []*TaskMessage{t1, t2, t3},
2019-11-27 01:57:53 +08:00
},
{
2019-12-04 13:01:26 +08:00
beforeSrc: []*TaskMessage{t2, t3},
beforeDst: []*TaskMessage{t1},
afterSrc: []*TaskMessage{},
afterDst: []*TaskMessage{t1, t2, t3},
2019-11-27 01:57:53 +08:00
},
}
2019-11-27 01:57:53 +08:00
for _, tc := range tests {
// clean up db before each test case.
2019-11-27 22:41:54 +08:00
if err := r.client.FlushDB().Err(); err != nil {
2019-11-27 01:57:53 +08:00
t.Error(err)
continue
}
// seed src list.
for _, msg := range tc.beforeSrc {
r.client.LPush(inProgressQ, mustMarshal(t, msg))
2019-11-27 01:57:53 +08:00
}
// seed dst list.
for _, msg := range tc.beforeDst {
r.client.LPush(defaultQ, mustMarshal(t, msg))
2019-11-27 01:57:53 +08:00
}
2019-12-04 22:50:52 +08:00
if err := r.RestoreUnfinished(); err != nil {
t.Errorf("(*RDB).RestoreUnfinished() = %v, want nil", err)
2019-11-27 01:57:53 +08:00
continue
}
src := r.client.LRange(inProgressQ, 0, -1).Val()
2019-11-28 23:17:07 +08:00
gotSrc := mustUnmarshalSlice(t, src)
if diff := cmp.Diff(tc.afterSrc, gotSrc, sortMsgOpt); diff != "" {
t.Errorf("mismatch found in %q (-want, +got)\n%s", inProgressQ, diff)
2019-11-27 01:57:53 +08:00
}
dst := r.client.LRange(defaultQ, 0, -1).Val()
2019-11-28 23:17:07 +08:00
gotDst := mustUnmarshalSlice(t, dst)
if diff := cmp.Diff(tc.afterDst, gotDst, sortMsgOpt); diff != "" {
t.Errorf("mismatch found in %q (-want, +got)\n%s", defaultQ, diff)
2019-11-27 01:57:53 +08:00
}
}
}
2019-12-04 23:28:57 +08:00
func TestCheckAndEnqueue(t *testing.T) {
2019-11-26 10:55:17 +08:00
r := setup(t)
2019-12-04 13:01:26 +08:00
t1 := randomTask("send_email", "default", nil)
t2 := randomTask("generate_csv", "default", nil)
2019-12-04 23:14:37 +08:00
t3 := randomTask("gen_thumbnail", "default", nil)
2019-11-26 22:38:11 +08:00
secondAgo := time.Now().Add(-time.Second)
hourFromNow := time.Now().Add(time.Hour)
tests := []struct {
2019-12-04 23:14:37 +08:00
initScheduled []*redis.Z // tasks to be processed later
initRetry []*redis.Z // tasks to be retired later
2019-12-04 13:01:26 +08:00
wantQueued []*TaskMessage // queue after calling forward
2019-12-04 23:28:57 +08:00
wantScheduled []*TaskMessage // tasks in scheduled queue after calling the method
wantRetry []*TaskMessage // tasks in retry queue after calling the method
2019-11-26 22:38:11 +08:00
}{
{
2019-12-04 23:14:37 +08:00
initScheduled: []*redis.Z{
2019-11-28 23:17:07 +08:00
&redis.Z{Member: mustMarshal(t, t1), Score: float64(secondAgo.Unix())},
&redis.Z{Member: mustMarshal(t, t2), Score: float64(secondAgo.Unix())}},
2019-12-04 23:14:37 +08:00
initRetry: []*redis.Z{
&redis.Z{Member: mustMarshal(t, t3), Score: float64(secondAgo.Unix())}},
wantQueued: []*TaskMessage{t1, t2, t3},
2019-12-04 13:01:26 +08:00
wantScheduled: []*TaskMessage{},
2019-12-04 23:14:37 +08:00
wantRetry: []*TaskMessage{},
2019-11-26 22:38:11 +08:00
},
{
2019-12-04 23:14:37 +08:00
initScheduled: []*redis.Z{
2019-11-28 23:17:07 +08:00
&redis.Z{Member: mustMarshal(t, t1), Score: float64(hourFromNow.Unix())},
&redis.Z{Member: mustMarshal(t, t2), Score: float64(secondAgo.Unix())}},
2019-12-04 23:14:37 +08:00
initRetry: []*redis.Z{
&redis.Z{Member: mustMarshal(t, t3), Score: float64(secondAgo.Unix())}},
wantQueued: []*TaskMessage{t2, t3},
2019-12-04 13:01:26 +08:00
wantScheduled: []*TaskMessage{t1},
2019-12-04 23:14:37 +08:00
wantRetry: []*TaskMessage{},
2019-11-26 22:38:11 +08:00
},
{
2019-12-04 23:14:37 +08:00
initScheduled: []*redis.Z{
2019-11-28 23:17:07 +08:00
&redis.Z{Member: mustMarshal(t, t1), Score: float64(hourFromNow.Unix())},
&redis.Z{Member: mustMarshal(t, t2), Score: float64(hourFromNow.Unix())}},
2019-12-04 23:14:37 +08:00
initRetry: []*redis.Z{
&redis.Z{Member: mustMarshal(t, t3), Score: float64(hourFromNow.Unix())}},
2019-12-04 13:01:26 +08:00
wantQueued: []*TaskMessage{},
wantScheduled: []*TaskMessage{t1, t2},
2019-12-04 23:14:37 +08:00
wantRetry: []*TaskMessage{t3},
2019-11-26 22:38:11 +08:00
},
}
for _, tc := range tests {
// clean up db before each test case.
2019-11-27 22:41:54 +08:00
if err := r.client.FlushDB().Err(); err != nil {
2019-11-26 22:38:11 +08:00
t.Fatal(err)
}
if err := r.client.ZAdd(scheduledQ, tc.initScheduled...).Err(); err != nil {
2019-12-04 23:14:37 +08:00
t.Error(err)
continue
}
if err := r.client.ZAdd(retryQ, tc.initRetry...).Err(); err != nil {
2019-11-26 22:38:11 +08:00
t.Error(err)
continue
}
2019-12-04 23:28:57 +08:00
err := r.CheckAndEnqueue()
2019-11-26 22:38:11 +08:00
if err != nil {
2019-12-04 23:14:37 +08:00
t.Errorf("(*RDB).CheckScheduled() = %v, want nil", err)
2019-11-26 22:38:11 +08:00
continue
}
queued := r.client.LRange(defaultQ, 0, -1).Val()
2019-11-28 23:17:07 +08:00
gotQueued := mustUnmarshalSlice(t, queued)
if diff := cmp.Diff(tc.wantQueued, gotQueued, sortMsgOpt); diff != "" {
t.Errorf("%q has %d tasks, want %d tasks; (-want, +got)\n%s", defaultQ, len(gotQueued), len(tc.wantQueued), diff)
2019-11-26 22:38:11 +08:00
}
scheduled := r.client.ZRangeByScore(scheduledQ, &redis.ZRangeBy{Min: "-inf", Max: "+inf"}).Val()
2019-11-28 23:17:07 +08:00
gotScheduled := mustUnmarshalSlice(t, scheduled)
if diff := cmp.Diff(tc.wantScheduled, gotScheduled, sortMsgOpt); diff != "" {
2019-11-26 22:38:11 +08:00
t.Errorf("%q has %d tasks, want %d tasks; (-want, +got)\n%s", scheduled, len(gotScheduled), len(tc.wantScheduled), diff)
}
}
}
2019-11-27 23:16:16 +08:00
func TestSchedule(t *testing.T) {
r := setup(t)
tests := []struct {
2019-12-04 13:01:26 +08:00
msg *TaskMessage
2019-11-27 23:16:16 +08:00
processAt time.Time
}{
{
randomTask("send_email", "default", map[string]interface{}{"subject": "hello"}),
time.Now().Add(15 * time.Minute),
},
}
for _, tc := range tests {
// clean up db before each test case.
if err := r.client.FlushDB().Err(); err != nil {
t.Fatal(err)
}
err := r.Schedule(tc.msg, tc.processAt)
2019-11-27 23:16:16 +08:00
if err != nil {
t.Error(err)
continue
}
res, err := r.client.ZRangeWithScores(scheduledQ, 0, -1).Result()
2019-11-27 23:16:16 +08:00
if err != nil {
t.Error(err)
continue
}
desc := fmt.Sprintf("(*RDB).Schedule(%v, %v)", tc.msg, tc.processAt)
2019-11-27 23:16:16 +08:00
if len(res) != 1 {
t.Errorf("%s inserted %d items to %q, want 1 items inserted", desc, len(res), scheduledQ)
continue
}
if res[0].Score != float64(tc.processAt.Unix()) {
t.Errorf("%s inserted an item with score %f, want %f", desc, res[0].Score, float64(tc.processAt.Unix()))
continue
}
}
}
func TestRetryLater(t *testing.T) {
r := setup(t)
tests := []struct {
msg *TaskMessage
processAt time.Time
}{
{
randomTask("send_email", "default", map[string]interface{}{"subject": "hello"}),
time.Now().Add(15 * time.Minute),
},
}
for _, tc := range tests {
// clean up db before each test case.
if err := r.client.FlushDB().Err(); err != nil {
t.Fatal(err)
}
err := r.RetryLater(tc.msg, tc.processAt)
if err != nil {
t.Error(err)
continue
}
res, err := r.client.ZRangeWithScores(retryQ, 0, -1).Result()
if err != nil {
t.Error(err)
continue
}
desc := fmt.Sprintf("(*RDB).RetryLater(%v, %v)", tc.msg, tc.processAt)
if len(res) != 1 {
t.Errorf("%s inserted %d items to %q, want 1 items inserted", desc, len(res), retryQ)
2019-11-27 23:16:16 +08:00
continue
}
if res[0].Score != float64(tc.processAt.Unix()) {
t.Errorf("%s inserted an item with score %f, want %f", desc, res[0].Score, float64(tc.processAt.Unix()))
continue
}
}
}