2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-26 07:42:17 +08:00

Clean up rdb package tests

This commit is contained in:
Ken Hibino 2019-12-12 19:49:41 -08:00
parent 9410a65338
commit 04836194f9
4 changed files with 450 additions and 627 deletions

View File

@ -5,7 +5,7 @@ import "github.com/go-redis/redis/v7"
/* /*
TODOs: TODOs:
- [P0] asynqmon kill <taskID>, asynqmon killall <qname> - [P0] asynqmon kill <taskID>, asynqmon killall <qname>
- [P0] Test refactor - helpers to initialize queues and read queue contents - [P0] Assigning int or any number type to Payload will be converted to float64 in handler
- [P0] Redis Memory Usage, Connection info in stats - [P0] Redis Memory Usage, Connection info in stats
- [P0] Processed, Failed count for today - [P0] Processed, Failed count for today
- [P0] Go docs + CONTRIBUTION.md + Github issue template + License comment - [P0] Go docs + CONTRIBUTION.md + Github issue template + License comment

View File

@ -0,0 +1,147 @@
package rdb
import (
"encoding/json"
"math/rand"
"sort"
"testing"
"time"
"github.com/go-redis/redis/v7"
"github.com/google/go-cmp/cmp"
"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)
}
}
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 newTaskMessage(taskType string, payload map[string]interface{}) *TaskMessage {
return &TaskMessage{
ID: xid.New(),
Type: taskType,
Queue: "default",
Retry: 25,
Payload: payload,
}
}
func mustMarshal(t *testing.T, msg *TaskMessage) string {
t.Helper()
data, err := json.Marshal(msg)
if err != nil {
t.Fatal(err)
}
return string(data)
}
func mustUnmarshal(t *testing.T, data string) *TaskMessage {
t.Helper()
var msg TaskMessage
err := json.Unmarshal([]byte(data), &msg)
if err != nil {
t.Fatal(err)
}
return &msg
}
func mustMarshalSlice(t *testing.T, msgs []*TaskMessage) []string {
t.Helper()
var data []string
for _, m := range msgs {
data = append(data, mustMarshal(t, m))
}
return data
}
func mustUnmarshalSlice(t *testing.T, data []string) []*TaskMessage {
t.Helper()
var msgs []*TaskMessage
for _, s := range data {
msgs = append(msgs, mustUnmarshal(t, s))
}
return msgs
}
func seedRedisList(t *testing.T, c *redis.Client, key string, msgs []*TaskMessage) {
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 {
msg *TaskMessage
score int64
}
// seedDefaultQueue initializes the default queue with the given messages.
func seedDefaultQueue(t *testing.T, r *RDB, msgs []*TaskMessage) {
t.Helper()
seedRedisList(t, r.client, defaultQ, msgs)
}
// seedInProgressQueue initializes the in-progress queue with the given messages.
func seedInProgressQueue(t *testing.T, r *RDB, msgs []*TaskMessage) {
t.Helper()
seedRedisList(t, r.client, inProgressQ, msgs)
}
// seedScheduledQueue initializes the scheduled queue with the given messages.
func seedScheduledQueue(t *testing.T, r *RDB, entries []sortedSetEntry) {
t.Helper()
seedRedisZSet(t, r.client, scheduledQ, entries)
}
// seedRetryQueue initializes the retry queue with the given messages.
func seedRetryQueue(t *testing.T, r *RDB, entries []sortedSetEntry) {
t.Helper()
seedRedisZSet(t, r.client, retryQ, entries)
}
// seedDeadQueue initializes the dead queue with the given messages.
func seedDeadQueue(t *testing.T, r *RDB, entries []sortedSetEntry) {
t.Helper()
seedRedisZSet(t, r.client, deadQ, entries)
}

View File

@ -5,7 +5,6 @@ import (
"testing" "testing"
"time" "time"
"github.com/go-redis/redis/v7"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"github.com/rs/xid" "github.com/rs/xid"
) )
@ -58,17 +57,19 @@ func TestCurrentStats(t *testing.T) {
tests := []struct { tests := []struct {
enqueued []*TaskMessage enqueued []*TaskMessage
inProgress []*TaskMessage inProgress []*TaskMessage
scheduled []*TaskMessage scheduled []sortedSetEntry
retry []*TaskMessage retry []sortedSetEntry
dead []*TaskMessage dead []sortedSetEntry
want *Stats want *Stats
}{ }{
{ {
enqueued: []*TaskMessage{m1}, enqueued: []*TaskMessage{m1},
inProgress: []*TaskMessage{m2}, inProgress: []*TaskMessage{m2},
scheduled: []*TaskMessage{m3, m4}, scheduled: []sortedSetEntry{
retry: []*TaskMessage{}, {m3, time.Now().Add(time.Hour).Unix()},
dead: []*TaskMessage{}, {m4, time.Now().Unix()}},
retry: []sortedSetEntry{},
dead: []sortedSetEntry{},
want: &Stats{ want: &Stats{
Enqueued: 1, Enqueued: 1,
InProgress: 1, InProgress: 1,
@ -81,9 +82,13 @@ func TestCurrentStats(t *testing.T) {
{ {
enqueued: []*TaskMessage{}, enqueued: []*TaskMessage{},
inProgress: []*TaskMessage{}, inProgress: []*TaskMessage{},
scheduled: []*TaskMessage{m3, m4}, scheduled: []sortedSetEntry{
retry: []*TaskMessage{m1}, {m3, time.Now().Unix()},
dead: []*TaskMessage{m2}, {m4, time.Now().Unix()}},
retry: []sortedSetEntry{
{m1, time.Now().Add(time.Minute).Unix()}},
dead: []sortedSetEntry{
{m2, time.Now().Add(-time.Hour).Unix()}},
want: &Stats{ want: &Stats{
Enqueued: 0, Enqueued: 0,
InProgress: 0, InProgress: 0,
@ -96,36 +101,12 @@ func TestCurrentStats(t *testing.T) {
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedDefaultQueue(t, r, tc.enqueued)
t.Fatal(err) seedInProgressQueue(t, r, tc.inProgress)
} seedScheduledQueue(t, r, tc.scheduled)
// initialize the queues. seedRetryQueue(t, r, tc.retry)
for _, msg := range tc.enqueued { seedDeadQueue(t, r, tc.dead)
if err := r.Enqueue(msg); err != nil {
t.Fatal(err)
}
}
for _, msg := range tc.inProgress {
if err := r.client.LPush(inProgressQ, mustMarshal(t, msg)).Err(); err != nil {
t.Fatal(err)
}
}
for _, msg := range tc.scheduled {
if err := r.Schedule(msg, time.Now().Add(time.Hour)); err != nil {
t.Fatal(err)
}
}
for _, msg := range tc.retry {
if err := r.RetryLater(msg, time.Now().Add(time.Hour)); err != nil {
t.Fatal(err)
}
}
for _, msg := range tc.dead {
if err := r.Kill(msg); err != nil {
t.Fatal(err)
}
}
got, err := r.CurrentStats() got, err := r.CurrentStats()
if err != nil { if err != nil {
@ -163,16 +144,9 @@ func TestListEnqueued(t *testing.T) {
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedDefaultQueue(t, r, tc.enqueued)
t.Fatal(err)
}
// initialize the list
for _, msg := range tc.enqueued {
if err := r.Enqueue(msg); err != nil {
t.Fatal(err)
}
}
got, err := r.ListEnqueued() got, err := r.ListEnqueued()
if err != nil { if err != nil {
t.Errorf("r.ListEnqueued() = %v, %v, want %v, nil", got, err, tc.want) t.Errorf("r.ListEnqueued() = %v, %v, want %v, nil", got, err, tc.want)
@ -200,30 +174,23 @@ func TestListInProgress(t *testing.T) {
t1 := &InProgressTask{ID: m1.ID, Type: m1.Type, Payload: m1.Payload} t1 := &InProgressTask{ID: m1.ID, Type: m1.Type, Payload: m1.Payload}
t2 := &InProgressTask{ID: m2.ID, Type: m2.Type, Payload: m2.Payload} t2 := &InProgressTask{ID: m2.ID, Type: m2.Type, Payload: m2.Payload}
tests := []struct { tests := []struct {
enqueued []*TaskMessage inProgress []*TaskMessage
want []*InProgressTask want []*InProgressTask
}{ }{
{ {
enqueued: []*TaskMessage{m1, m2}, inProgress: []*TaskMessage{m1, m2},
want: []*InProgressTask{t1, t2}, want: []*InProgressTask{t1, t2},
}, },
{ {
enqueued: []*TaskMessage{}, inProgress: []*TaskMessage{},
want: []*InProgressTask{}, want: []*InProgressTask{},
}, },
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedInProgressQueue(t, r, tc.inProgress)
t.Fatal(err)
}
// initialize the list
for _, msg := range tc.enqueued {
if err := r.client.LPush(inProgressQ, mustMarshal(t, msg)).Err(); err != nil {
t.Fatal(err)
}
}
got, err := r.ListInProgress() got, err := r.ListInProgress()
if err != nil { if err != nil {
t.Errorf("r.ListInProgress() = %v, %v, want %v, nil", got, err, tc.want) t.Errorf("r.ListInProgress() = %v, %v, want %v, nil", got, err, tc.want)
@ -252,40 +219,26 @@ func TestListScheduled(t *testing.T) {
t1 := &ScheduledTask{ID: m1.ID, Type: m1.Type, Payload: m1.Payload, ProcessAt: p1, Score: p1.Unix()} t1 := &ScheduledTask{ID: m1.ID, Type: m1.Type, Payload: m1.Payload, ProcessAt: p1, Score: p1.Unix()}
t2 := &ScheduledTask{ID: m2.ID, Type: m2.Type, Payload: m2.Payload, ProcessAt: p2, Score: p2.Unix()} t2 := &ScheduledTask{ID: m2.ID, Type: m2.Type, Payload: m2.Payload, ProcessAt: p2, Score: p2.Unix()}
type scheduledEntry struct {
msg *TaskMessage
processAt time.Time
}
tests := []struct { tests := []struct {
scheduled []scheduledEntry scheduled []sortedSetEntry
want []*ScheduledTask want []*ScheduledTask
}{ }{
{ {
scheduled: []scheduledEntry{ scheduled: []sortedSetEntry{
{m1, p1}, {m1, p1.Unix()},
{m2, p2}, {m2, p2.Unix()},
}, },
want: []*ScheduledTask{t1, t2}, want: []*ScheduledTask{t1, t2},
}, },
{ {
scheduled: []scheduledEntry{}, scheduled: []sortedSetEntry{},
want: []*ScheduledTask{}, want: []*ScheduledTask{},
}, },
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedScheduledQueue(t, r, tc.scheduled)
t.Fatal(err)
}
// initialize the scheduled queue
for _, s := range tc.scheduled {
err := r.Schedule(s.msg, s.processAt)
if err != nil {
t.Fatal(err)
}
}
got, err := r.ListScheduled() got, err := r.ListScheduled()
if err != nil { if err != nil {
@ -329,47 +282,47 @@ func TestListRetry(t *testing.T) {
} }
p1 := time.Now().Add(5 * time.Minute) p1 := time.Now().Add(5 * time.Minute)
p2 := time.Now().Add(24 * time.Hour) p2 := time.Now().Add(24 * time.Hour)
t1 := &RetryTask{ID: m1.ID, Type: m1.Type, Payload: m1.Payload, t1 := &RetryTask{
ProcessAt: p1, ErrorMsg: m1.ErrorMsg, Retried: m1.Retried, ID: m1.ID,
Retry: m1.Retry, Score: p1.Unix()} Type: m1.Type,
t2 := &RetryTask{ID: m2.ID, Type: m2.Type, Payload: m2.Payload, Payload: m1.Payload,
ProcessAt: p2, ErrorMsg: m2.ErrorMsg, Retried: m2.Retried, ProcessAt: p1,
Retry: m2.Retry, Score: p2.Unix()} ErrorMsg: m1.ErrorMsg,
Retried: m1.Retried,
type retryEntry struct { Retry: m1.Retry,
msg *TaskMessage Score: p1.Unix(),
processAt time.Time }
t2 := &RetryTask{
ID: m2.ID,
Type: m2.Type,
Payload: m2.Payload,
ProcessAt: p2,
ErrorMsg: m2.ErrorMsg,
Retried: m2.Retried,
Retry: m2.Retry,
Score: p2.Unix(),
} }
tests := []struct { tests := []struct {
dead []retryEntry retry []sortedSetEntry
want []*RetryTask want []*RetryTask
}{ }{
{ {
dead: []retryEntry{ retry: []sortedSetEntry{
{m1, p1}, {m1, p1.Unix()},
{m2, p2}, {m2, p2.Unix()},
}, },
want: []*RetryTask{t1, t2}, want: []*RetryTask{t1, t2},
}, },
{ {
dead: []retryEntry{}, retry: []sortedSetEntry{},
want: []*RetryTask{}, want: []*RetryTask{},
}, },
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedRetryQueue(t, r, tc.retry)
t.Fatal(err)
}
// initialize the scheduled queue
for _, d := range tc.dead {
r.client.ZAdd(retryQ, &redis.Z{
Member: mustMarshal(t, d.msg),
Score: float64(d.processAt.Unix()),
})
}
got, err := r.ListRetry() got, err := r.ListRetry()
if err != nil { if err != nil {
@ -409,45 +362,43 @@ func TestListDead(t *testing.T) {
} }
f1 := time.Now().Add(-5 * time.Minute) f1 := time.Now().Add(-5 * time.Minute)
f2 := time.Now().Add(-24 * time.Hour) f2 := time.Now().Add(-24 * time.Hour)
t1 := &DeadTask{ID: m1.ID, Type: m1.Type, Payload: m1.Payload, t1 := &DeadTask{
LastFailedAt: f1, ErrorMsg: m1.ErrorMsg, Score: f1.Unix()} ID: m1.ID,
t2 := &DeadTask{ID: m2.ID, Type: m2.Type, Payload: m2.Payload, Type: m1.Type,
LastFailedAt: f2, ErrorMsg: m2.ErrorMsg, Score: f2.Unix()} Payload: m1.Payload,
LastFailedAt: f1,
type deadEntry struct { ErrorMsg: m1.ErrorMsg,
msg *TaskMessage Score: f1.Unix(),
lastFailedAt time.Time }
t2 := &DeadTask{
ID: m2.ID,
Type: m2.Type,
Payload: m2.Payload,
LastFailedAt: f2,
ErrorMsg: m2.ErrorMsg,
Score: f2.Unix(),
} }
tests := []struct { tests := []struct {
dead []deadEntry dead []sortedSetEntry
want []*DeadTask want []*DeadTask
}{ }{
{ {
dead: []deadEntry{ dead: []sortedSetEntry{
{m1, f1}, {m1, f1.Unix()},
{m2, f2}, {m2, f2.Unix()},
}, },
want: []*DeadTask{t1, t2}, want: []*DeadTask{t1, t2},
}, },
{ {
dead: []deadEntry{}, dead: []sortedSetEntry{},
want: []*DeadTask{}, want: []*DeadTask{},
}, },
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedDeadQueue(t, r, tc.dead)
t.Fatal(err)
}
// initialize the scheduled queue
for _, d := range tc.dead {
r.client.ZAdd(deadQ, &redis.Z{
Member: mustMarshal(t, d.msg),
Score: float64(d.lastFailedAt.Unix()),
})
}
got, err := r.ListDead() got, err := r.ListDead()
if err != nil { if err != nil {
@ -473,17 +424,13 @@ var timeCmpOpt = EquateApproxTime(time.Second)
func TestEnqueueDeadTask(t *testing.T) { func TestEnqueueDeadTask(t *testing.T) {
r := setup(t) r := setup(t)
t1 := newTaskMessage("send_email", nil) t1 := newTaskMessage("send_email", nil)
t2 := newTaskMessage("gen_thumbnail", nil) t2 := newTaskMessage("gen_thumbnail", nil)
s1 := time.Now().Add(-5 * time.Minute).Unix() s1 := time.Now().Add(-5 * time.Minute).Unix()
s2 := time.Now().Add(-time.Hour).Unix() s2 := time.Now().Add(-time.Hour).Unix()
type deadEntry struct {
msg *TaskMessage
score int64
}
tests := []struct { tests := []struct {
dead []deadEntry dead []sortedSetEntry
score int64 score int64
id xid.ID id xid.ID
want error // expected return value from calling EnqueueDeadTask want error // expected return value from calling EnqueueDeadTask
@ -491,7 +438,7 @@ func TestEnqueueDeadTask(t *testing.T) {
wantEnqueued []*TaskMessage wantEnqueued []*TaskMessage
}{ }{
{ {
dead: []deadEntry{ dead: []sortedSetEntry{
{t1, s1}, {t1, s1},
{t2, s2}, {t2, s2},
}, },
@ -502,7 +449,7 @@ func TestEnqueueDeadTask(t *testing.T) {
wantEnqueued: []*TaskMessage{t2}, wantEnqueued: []*TaskMessage{t2},
}, },
{ {
dead: []deadEntry{ dead: []sortedSetEntry{
{t1, s1}, {t1, s1},
{t2, s2}, {t2, s2},
}, },
@ -515,30 +462,19 @@ func TestEnqueueDeadTask(t *testing.T) {
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedDeadQueue(t, r, tc.dead)
t.Fatal(err)
}
// initialize dead queue
for _, d := range tc.dead {
err := r.client.ZAdd(deadQ, &redis.Z{Member: mustMarshal(t, d.msg), Score: float64(d.score)}).Err()
if err != nil {
t.Fatal(err)
}
}
got := r.EnqueueDeadTask(tc.id, tc.score) got := r.EnqueueDeadTask(tc.id, tc.score)
if got != tc.want { if got != tc.want {
t.Errorf("r.EnqueueDeadTask(%s, %d) = %v, want %v", tc.id, tc.score, got, tc.want) t.Errorf("r.EnqueueDeadTask(%s, %d) = %v, want %v", tc.id, tc.score, got, tc.want)
continue continue
} }
gotEnqueuedRaw := r.client.LRange(defaultQ, 0, -1).Val() gotEnqueuedRaw := r.client.LRange(defaultQ, 0, -1).Val()
gotEnqueued := mustUnmarshalSlice(t, gotEnqueuedRaw) gotEnqueued := mustUnmarshalSlice(t, gotEnqueuedRaw)
if diff := cmp.Diff(tc.wantEnqueued, gotEnqueued, sortMsgOpt); diff != "" { if diff := cmp.Diff(tc.wantEnqueued, gotEnqueued, sortMsgOpt); diff != "" {
t.Errorf("mismatch found in %q; (-want, +got)\n%s", defaultQ, diff) t.Errorf("mismatch found in %q; (-want, +got)\n%s", defaultQ, diff)
} }
gotDeadRaw := r.client.ZRange(deadQ, 0, -1).Val() gotDeadRaw := r.client.ZRange(deadQ, 0, -1).Val()
gotDead := mustUnmarshalSlice(t, gotDeadRaw) gotDead := mustUnmarshalSlice(t, gotDeadRaw)
if diff := cmp.Diff(tc.wantDead, gotDead, sortMsgOpt); diff != "" { if diff := cmp.Diff(tc.wantDead, gotDead, sortMsgOpt); diff != "" {
@ -554,12 +490,8 @@ func TestEnqueueRetryTask(t *testing.T) {
t2 := newTaskMessage("gen_thumbnail", nil) t2 := newTaskMessage("gen_thumbnail", nil)
s1 := time.Now().Add(-5 * time.Minute).Unix() s1 := time.Now().Add(-5 * time.Minute).Unix()
s2 := time.Now().Add(-time.Hour).Unix() s2 := time.Now().Add(-time.Hour).Unix()
type retryEntry struct {
msg *TaskMessage
score int64
}
tests := []struct { tests := []struct {
dead []retryEntry retry []sortedSetEntry
score int64 score int64
id xid.ID id xid.ID
want error // expected return value from calling EnqueueRetryTask want error // expected return value from calling EnqueueRetryTask
@ -567,7 +499,7 @@ func TestEnqueueRetryTask(t *testing.T) {
wantEnqueued []*TaskMessage wantEnqueued []*TaskMessage
}{ }{
{ {
dead: []retryEntry{ retry: []sortedSetEntry{
{t1, s1}, {t1, s1},
{t2, s2}, {t2, s2},
}, },
@ -578,7 +510,7 @@ func TestEnqueueRetryTask(t *testing.T) {
wantEnqueued: []*TaskMessage{t2}, wantEnqueued: []*TaskMessage{t2},
}, },
{ {
dead: []retryEntry{ retry: []sortedSetEntry{
{t1, s1}, {t1, s1},
{t2, s2}, {t2, s2},
}, },
@ -591,30 +523,20 @@ func TestEnqueueRetryTask(t *testing.T) {
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil {
t.Fatal(err) seedRetryQueue(t, r, tc.retry) // initialize retry queue
}
// initialize retry queue
for _, d := range tc.dead {
err := r.client.ZAdd(retryQ, &redis.Z{Member: mustMarshal(t, d.msg), Score: float64(d.score)}).Err()
if err != nil {
t.Fatal(err)
}
}
got := r.EnqueueRetryTask(tc.id, tc.score) got := r.EnqueueRetryTask(tc.id, tc.score)
if got != tc.want { if got != tc.want {
t.Errorf("r.EnqueueRetryTask(%s, %d) = %v, want %v", tc.id, tc.score, got, tc.want) t.Errorf("r.EnqueueRetryTask(%s, %d) = %v, want %v", tc.id, tc.score, got, tc.want)
continue continue
} }
gotEnqueuedRaw := r.client.LRange(defaultQ, 0, -1).Val() gotEnqueuedRaw := r.client.LRange(defaultQ, 0, -1).Val()
gotEnqueued := mustUnmarshalSlice(t, gotEnqueuedRaw) gotEnqueued := mustUnmarshalSlice(t, gotEnqueuedRaw)
if diff := cmp.Diff(tc.wantEnqueued, gotEnqueued, sortMsgOpt); diff != "" { if diff := cmp.Diff(tc.wantEnqueued, gotEnqueued, sortMsgOpt); diff != "" {
t.Errorf("mismatch found in %q; (-want, +got)\n%s", defaultQ, diff) t.Errorf("mismatch found in %q; (-want, +got)\n%s", defaultQ, diff)
} }
gotRetryRaw := r.client.ZRange(retryQ, 0, -1).Val() gotRetryRaw := r.client.ZRange(retryQ, 0, -1).Val()
gotRetry := mustUnmarshalSlice(t, gotRetryRaw) gotRetry := mustUnmarshalSlice(t, gotRetryRaw)
if diff := cmp.Diff(tc.wantRetry, gotRetry, sortMsgOpt); diff != "" { if diff := cmp.Diff(tc.wantRetry, gotRetry, sortMsgOpt); diff != "" {
@ -625,17 +547,13 @@ func TestEnqueueRetryTask(t *testing.T) {
func TestEnqueueScheduledTask(t *testing.T) { func TestEnqueueScheduledTask(t *testing.T) {
r := setup(t) r := setup(t)
t1 := newTaskMessage("send_email", nil) t1 := newTaskMessage("send_email", nil)
t2 := newTaskMessage("gen_thumbnail", nil) t2 := newTaskMessage("gen_thumbnail", nil)
s1 := time.Now().Add(-5 * time.Minute).Unix() s1 := time.Now().Add(-5 * time.Minute).Unix()
s2 := time.Now().Add(-time.Hour).Unix() s2 := time.Now().Add(-time.Hour).Unix()
type scheduledEntry struct {
msg *TaskMessage
score int64
}
tests := []struct { tests := []struct {
dead []scheduledEntry scheduled []sortedSetEntry
score int64 score int64
id xid.ID id xid.ID
want error // expected return value from calling EnqueueScheduledTask want error // expected return value from calling EnqueueScheduledTask
@ -643,7 +561,7 @@ func TestEnqueueScheduledTask(t *testing.T) {
wantEnqueued []*TaskMessage wantEnqueued []*TaskMessage
}{ }{
{ {
dead: []scheduledEntry{ scheduled: []sortedSetEntry{
{t1, s1}, {t1, s1},
{t2, s2}, {t2, s2},
}, },
@ -654,7 +572,7 @@ func TestEnqueueScheduledTask(t *testing.T) {
wantEnqueued: []*TaskMessage{t2}, wantEnqueued: []*TaskMessage{t2},
}, },
{ {
dead: []scheduledEntry{ scheduled: []sortedSetEntry{
{t1, s1}, {t1, s1},
{t2, s2}, {t2, s2},
}, },
@ -667,30 +585,19 @@ func TestEnqueueScheduledTask(t *testing.T) {
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedScheduledQueue(t, r, tc.scheduled)
t.Fatal(err)
}
// initialize scheduled queue
for _, d := range tc.dead {
err := r.client.ZAdd(scheduledQ, &redis.Z{Member: mustMarshal(t, d.msg), Score: float64(d.score)}).Err()
if err != nil {
t.Fatal(err)
}
}
got := r.EnqueueScheduledTask(tc.id, tc.score) got := r.EnqueueScheduledTask(tc.id, tc.score)
if got != tc.want { if got != tc.want {
t.Errorf("r.EnqueueRetryTask(%s, %d) = %v, want %v", tc.id, tc.score, got, tc.want) t.Errorf("r.EnqueueRetryTask(%s, %d) = %v, want %v", tc.id, tc.score, got, tc.want)
continue continue
} }
gotEnqueuedRaw := r.client.LRange(defaultQ, 0, -1).Val() gotEnqueuedRaw := r.client.LRange(defaultQ, 0, -1).Val()
gotEnqueued := mustUnmarshalSlice(t, gotEnqueuedRaw) gotEnqueued := mustUnmarshalSlice(t, gotEnqueuedRaw)
if diff := cmp.Diff(tc.wantEnqueued, gotEnqueued, sortMsgOpt); diff != "" { if diff := cmp.Diff(tc.wantEnqueued, gotEnqueued, sortMsgOpt); diff != "" {
t.Errorf("mismatch found in %q; (-want, +got)\n%s", defaultQ, diff) t.Errorf("mismatch found in %q; (-want, +got)\n%s", defaultQ, diff)
} }
gotScheduledRaw := r.client.ZRange(scheduledQ, 0, -1).Val() gotScheduledRaw := r.client.ZRange(scheduledQ, 0, -1).Val()
gotScheduled := mustUnmarshalSlice(t, gotScheduledRaw) gotScheduled := mustUnmarshalSlice(t, gotScheduledRaw)
if diff := cmp.Diff(tc.wantScheduled, gotScheduled, sortMsgOpt); diff != "" { if diff := cmp.Diff(tc.wantScheduled, gotScheduled, sortMsgOpt); diff != "" {
@ -706,56 +613,49 @@ func TestEnqueueAllScheduledTasks(t *testing.T) {
t3 := newTaskMessage("reindex", nil) t3 := newTaskMessage("reindex", nil)
tests := []struct { tests := []struct {
description string desc string
scheduled []*TaskMessage scheduled []sortedSetEntry
want int64 want int64
wantEnqueued []*TaskMessage wantEnqueued []*TaskMessage
}{ }{
{ {
description: "with tasks in scheduled queue", desc: "with tasks in scheduled queue",
scheduled: []*TaskMessage{t1, t2, t3}, scheduled: []sortedSetEntry{
{t1, time.Now().Add(time.Hour).Unix()},
{t2, time.Now().Add(time.Hour).Unix()},
{t3, time.Now().Add(time.Hour).Unix()},
},
want: 3, want: 3,
wantEnqueued: []*TaskMessage{t1, t2, t3}, wantEnqueued: []*TaskMessage{t1, t2, t3},
}, },
{ {
description: "with empty scheduled queue", desc: "with empty scheduled queue",
scheduled: []*TaskMessage{}, scheduled: []sortedSetEntry{},
want: 0, want: 0,
wantEnqueued: []*TaskMessage{}, wantEnqueued: []*TaskMessage{},
}, },
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedScheduledQueue(t, r, tc.scheduled)
t.Fatal(err)
}
// initialize scheduled queue
for _, msg := range tc.scheduled {
err := r.client.ZAdd(scheduledQ, &redis.Z{
Member: mustMarshal(t, msg),
Score: float64(time.Now().Add(time.Hour).Unix())}).Err()
if err != nil {
t.Fatal(err)
}
}
got, err := r.EnqueueAllScheduledTasks() got, err := r.EnqueueAllScheduledTasks()
if err != nil { if err != nil {
t.Errorf("%s; r.EnqueueAllScheduledTasks = %v, %v; want %v, nil", t.Errorf("%s; r.EnqueueAllScheduledTasks = %v, %v; want %v, nil",
tc.description, got, err, tc.want) tc.desc, got, err, tc.want)
continue continue
} }
if got != tc.want { if got != tc.want {
t.Errorf("%s; r.EnqueueAllScheduledTasks = %v, %v; want %v, nil", t.Errorf("%s; r.EnqueueAllScheduledTasks = %v, %v; want %v, nil",
tc.description, got, err, tc.want) tc.desc, got, err, tc.want)
} }
gotEnqueuedRaw := r.client.LRange(defaultQ, 0, -1).Val() gotEnqueuedRaw := r.client.LRange(defaultQ, 0, -1).Val()
gotEnqueued := mustUnmarshalSlice(t, gotEnqueuedRaw) gotEnqueued := mustUnmarshalSlice(t, gotEnqueuedRaw)
if diff := cmp.Diff(tc.wantEnqueued, gotEnqueued, sortMsgOpt); diff != "" { if diff := cmp.Diff(tc.wantEnqueued, gotEnqueued, sortMsgOpt); diff != "" {
t.Errorf("%s; mismatch found in %q; (-want, +got)\n%s", tc.description, defaultQ, diff) t.Errorf("%s; mismatch found in %q; (-want, +got)\n%s", tc.desc, defaultQ, diff)
} }
} }
} }
@ -768,38 +668,31 @@ func TestEnqueueAllRetryTasks(t *testing.T) {
tests := []struct { tests := []struct {
description string description string
retry []*TaskMessage retry []sortedSetEntry
want int64 want int64
wantEnqueued []*TaskMessage wantEnqueued []*TaskMessage
}{ }{
{ {
description: "with tasks in retry queue", description: "with tasks in retry queue",
retry: []*TaskMessage{t1, t2, t3}, retry: []sortedSetEntry{
{t1, time.Now().Add(time.Hour).Unix()},
{t2, time.Now().Add(time.Hour).Unix()},
{t3, time.Now().Add(time.Hour).Unix()},
},
want: 3, want: 3,
wantEnqueued: []*TaskMessage{t1, t2, t3}, wantEnqueued: []*TaskMessage{t1, t2, t3},
}, },
{ {
description: "with empty retry queue", description: "with empty retry queue",
retry: []*TaskMessage{}, retry: []sortedSetEntry{},
want: 0, want: 0,
wantEnqueued: []*TaskMessage{}, wantEnqueued: []*TaskMessage{},
}, },
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedRetryQueue(t, r, tc.retry)
t.Fatal(err)
}
// initialize retry queue
for _, msg := range tc.retry {
err := r.client.ZAdd(retryQ, &redis.Z{
Member: mustMarshal(t, msg),
Score: float64(time.Now().Add(time.Hour).Unix())}).Err()
if err != nil {
t.Fatal(err)
}
}
got, err := r.EnqueueAllRetryTasks() got, err := r.EnqueueAllRetryTasks()
if err != nil { if err != nil {
@ -828,56 +721,49 @@ func TestEnqueueAllDeadTasks(t *testing.T) {
t3 := newTaskMessage("reindex", nil) t3 := newTaskMessage("reindex", nil)
tests := []struct { tests := []struct {
description string desc string
dead []*TaskMessage dead []sortedSetEntry
want int64 want int64
wantEnqueued []*TaskMessage wantEnqueued []*TaskMessage
}{ }{
{ {
description: "with tasks in dead queue", desc: "with tasks in dead queue",
dead: []*TaskMessage{t1, t2, t3}, dead: []sortedSetEntry{
{t1, time.Now().Add(-time.Minute).Unix()},
{t2, time.Now().Add(-time.Minute).Unix()},
{t3, time.Now().Add(-time.Minute).Unix()},
},
want: 3, want: 3,
wantEnqueued: []*TaskMessage{t1, t2, t3}, wantEnqueued: []*TaskMessage{t1, t2, t3},
}, },
{ {
description: "with empty dead queue", desc: "with empty dead queue",
dead: []*TaskMessage{}, dead: []sortedSetEntry{},
want: 0, want: 0,
wantEnqueued: []*TaskMessage{}, wantEnqueued: []*TaskMessage{},
}, },
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedDeadQueue(t, r, tc.dead)
t.Fatal(err)
}
// initialize dead queue
for _, msg := range tc.dead {
err := r.client.ZAdd(deadQ, &redis.Z{
Member: mustMarshal(t, msg),
Score: float64(time.Now().Add(time.Hour).Unix())}).Err()
if err != nil {
t.Fatal(err)
}
}
got, err := r.EnqueueAllDeadTasks() got, err := r.EnqueueAllDeadTasks()
if err != nil { if err != nil {
t.Errorf("%s; r.EnqueueAllDeadTasks = %v, %v; want %v, nil", t.Errorf("%s; r.EnqueueAllDeadTasks = %v, %v; want %v, nil",
tc.description, got, err, tc.want) tc.desc, got, err, tc.want)
continue continue
} }
if got != tc.want { if got != tc.want {
t.Errorf("%s; r.EnqueueAllDeadTasks = %v, %v; want %v, nil", t.Errorf("%s; r.EnqueueAllDeadTasks = %v, %v; want %v, nil",
tc.description, got, err, tc.want) tc.desc, got, err, tc.want)
} }
gotEnqueuedRaw := r.client.LRange(defaultQ, 0, -1).Val() gotEnqueuedRaw := r.client.LRange(defaultQ, 0, -1).Val()
gotEnqueued := mustUnmarshalSlice(t, gotEnqueuedRaw) gotEnqueued := mustUnmarshalSlice(t, gotEnqueuedRaw)
if diff := cmp.Diff(tc.wantEnqueued, gotEnqueued, sortMsgOpt); diff != "" { if diff := cmp.Diff(tc.wantEnqueued, gotEnqueued, sortMsgOpt); diff != "" {
t.Errorf("%s; mismatch found in %q; (-want, +got)\n%s", tc.description, defaultQ, diff) t.Errorf("%s; mismatch found in %q; (-want, +got)\n%s", tc.desc, defaultQ, diff)
} }
} }
} }
@ -889,19 +775,15 @@ func TestDeleteDeadTask(t *testing.T) {
t1 := time.Now().Add(-5 * time.Minute) t1 := time.Now().Add(-5 * time.Minute)
t2 := time.Now().Add(-time.Hour) t2 := time.Now().Add(-time.Hour)
type deadEntry struct {
msg *TaskMessage
score int64
}
tests := []struct { tests := []struct {
dead []deadEntry dead []sortedSetEntry
id xid.ID id xid.ID
score int64 score int64
want error want error
wantDead []*TaskMessage wantDead []*TaskMessage
}{ }{
{ {
dead: []deadEntry{ dead: []sortedSetEntry{
{m1, t1.Unix()}, {m1, t1.Unix()},
{m2, t2.Unix()}, {m2, t2.Unix()},
}, },
@ -911,7 +793,7 @@ func TestDeleteDeadTask(t *testing.T) {
wantDead: []*TaskMessage{m2}, wantDead: []*TaskMessage{m2},
}, },
{ {
dead: []deadEntry{ dead: []sortedSetEntry{
{m1, t1.Unix()}, {m1, t1.Unix()},
{m2, t2.Unix()}, {m2, t2.Unix()},
}, },
@ -921,7 +803,7 @@ func TestDeleteDeadTask(t *testing.T) {
wantDead: []*TaskMessage{m1, m2}, wantDead: []*TaskMessage{m1, m2},
}, },
{ {
dead: []deadEntry{}, dead: []sortedSetEntry{},
id: m1.ID, id: m1.ID,
score: t1.Unix(), score: t1.Unix(),
want: ErrTaskNotFound, want: ErrTaskNotFound,
@ -930,20 +812,8 @@ func TestDeleteDeadTask(t *testing.T) {
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedDeadQueue(t, r, tc.dead)
t.Fatal(err)
}
// initialize dead queue.
for _, d := range tc.dead {
err := r.client.ZAdd(deadQ, &redis.Z{
Member: mustMarshal(t, d.msg),
Score: float64(d.score),
}).Err()
if err != nil {
t.Fatal(err)
}
}
got := r.DeleteDeadTask(tc.id, tc.score) got := r.DeleteDeadTask(tc.id, tc.score)
if got != tc.want { if got != tc.want {
@ -966,19 +836,15 @@ func TestDeleteRetryTask(t *testing.T) {
t1 := time.Now().Add(5 * time.Minute) t1 := time.Now().Add(5 * time.Minute)
t2 := time.Now().Add(time.Hour) t2 := time.Now().Add(time.Hour)
type retryEntry struct {
msg *TaskMessage
score int64
}
tests := []struct { tests := []struct {
retry []retryEntry retry []sortedSetEntry
id xid.ID id xid.ID
score int64 score int64
want error want error
wantRetry []*TaskMessage wantRetry []*TaskMessage
}{ }{
{ {
retry: []retryEntry{ retry: []sortedSetEntry{
{m1, t1.Unix()}, {m1, t1.Unix()},
{m2, t2.Unix()}, {m2, t2.Unix()},
}, },
@ -988,7 +854,7 @@ func TestDeleteRetryTask(t *testing.T) {
wantRetry: []*TaskMessage{m2}, wantRetry: []*TaskMessage{m2},
}, },
{ {
retry: []retryEntry{ retry: []sortedSetEntry{
{m1, t1.Unix()}, {m1, t1.Unix()},
}, },
id: m2.ID, id: m2.ID,
@ -999,20 +865,8 @@ func TestDeleteRetryTask(t *testing.T) {
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedRetryQueue(t, r, tc.retry)
t.Fatal(err)
}
// initialize retry queue.
for _, e := range tc.retry {
err := r.client.ZAdd(retryQ, &redis.Z{
Member: mustMarshal(t, e.msg),
Score: float64(e.score),
}).Err()
if err != nil {
t.Fatal(err)
}
}
got := r.DeleteRetryTask(tc.id, tc.score) got := r.DeleteRetryTask(tc.id, tc.score)
if got != tc.want { if got != tc.want {
@ -1035,19 +889,15 @@ func TestDeleteScheduledTask(t *testing.T) {
t1 := time.Now().Add(5 * time.Minute) t1 := time.Now().Add(5 * time.Minute)
t2 := time.Now().Add(time.Hour) t2 := time.Now().Add(time.Hour)
type scheduledEntry struct {
msg *TaskMessage
score int64
}
tests := []struct { tests := []struct {
scheduled []scheduledEntry scheduled []sortedSetEntry
id xid.ID id xid.ID
score int64 score int64
want error want error
wantScheduled []*TaskMessage wantScheduled []*TaskMessage
}{ }{
{ {
scheduled: []scheduledEntry{ scheduled: []sortedSetEntry{
{m1, t1.Unix()}, {m1, t1.Unix()},
{m2, t2.Unix()}, {m2, t2.Unix()},
}, },
@ -1057,7 +907,7 @@ func TestDeleteScheduledTask(t *testing.T) {
wantScheduled: []*TaskMessage{m2}, wantScheduled: []*TaskMessage{m2},
}, },
{ {
scheduled: []scheduledEntry{ scheduled: []sortedSetEntry{
{m1, t1.Unix()}, {m1, t1.Unix()},
}, },
id: m2.ID, id: m2.ID,
@ -1068,20 +918,8 @@ func TestDeleteScheduledTask(t *testing.T) {
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedScheduledQueue(t, r, tc.scheduled)
t.Fatal(err)
}
// initialize scheduled queue.
for _, e := range tc.scheduled {
err := r.client.ZAdd(scheduledQ, &redis.Z{
Member: mustMarshal(t, e.msg),
Score: float64(e.score),
}).Err()
if err != nil {
t.Fatal(err)
}
}
got := r.DeleteScheduledTask(tc.id, tc.score) got := r.DeleteScheduledTask(tc.id, tc.score)
if got != tc.want { if got != tc.want {
@ -1104,30 +942,22 @@ func TestDeleteAllDeadTasks(t *testing.T) {
m3 := newTaskMessage("gen_thumbnail", nil) m3 := newTaskMessage("gen_thumbnail", nil)
tests := []struct { tests := []struct {
initDead []*TaskMessage dead []sortedSetEntry
wantDead []*TaskMessage wantDead []*TaskMessage
}{ }{
{ {
initDead: []*TaskMessage{m1, m2, m3}, dead: []sortedSetEntry{
{m1, time.Now().Unix()},
{m2, time.Now().Unix()},
{m3, time.Now().Unix()},
},
wantDead: []*TaskMessage{}, wantDead: []*TaskMessage{},
}, },
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedDeadQueue(t, r, tc.dead)
t.Fatal(err)
}
// initialize dead queue.
for _, msg := range tc.initDead {
err := r.client.ZAdd(deadQ, &redis.Z{
Member: mustMarshal(t, msg),
Score: float64(time.Now().Unix()),
}).Err()
if err != nil {
t.Fatal(err)
}
}
err := r.DeleteAllDeadTasks() err := r.DeleteAllDeadTasks()
if err != nil { if err != nil {
@ -1149,30 +979,22 @@ func TestDeleteAllRetryTasks(t *testing.T) {
m3 := newTaskMessage("gen_thumbnail", nil) m3 := newTaskMessage("gen_thumbnail", nil)
tests := []struct { tests := []struct {
initRetry []*TaskMessage retry []sortedSetEntry
wantRetry []*TaskMessage wantRetry []*TaskMessage
}{ }{
{ {
initRetry: []*TaskMessage{m1, m2, m3}, retry: []sortedSetEntry{
{m1, time.Now().Unix()},
{m2, time.Now().Unix()},
{m3, time.Now().Unix()},
},
wantRetry: []*TaskMessage{}, wantRetry: []*TaskMessage{},
}, },
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedRetryQueue(t, r, tc.retry)
t.Fatal(err)
}
// initialize retry queue.
for _, msg := range tc.initRetry {
err := r.client.ZAdd(retryQ, &redis.Z{
Member: mustMarshal(t, msg),
Score: float64(time.Now().Unix()),
}).Err()
if err != nil {
t.Fatal(err)
}
}
err := r.DeleteAllRetryTasks() err := r.DeleteAllRetryTasks()
if err != nil { if err != nil {
@ -1194,30 +1016,22 @@ func TestDeleteAllScheduledTasks(t *testing.T) {
m3 := newTaskMessage("gen_thumbnail", nil) m3 := newTaskMessage("gen_thumbnail", nil)
tests := []struct { tests := []struct {
initScheduled []*TaskMessage scheduled []sortedSetEntry
wantScheduled []*TaskMessage wantScheduled []*TaskMessage
}{ }{
{ {
initScheduled: []*TaskMessage{m1, m2, m3}, scheduled: []sortedSetEntry{
{m1, time.Now().Add(time.Minute).Unix()},
{m2, time.Now().Add(time.Minute).Unix()},
{m3, time.Now().Add(time.Minute).Unix()},
},
wantScheduled: []*TaskMessage{}, wantScheduled: []*TaskMessage{},
}, },
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedScheduledQueue(t, r, tc.scheduled)
t.Fatal(err)
}
// initialize scheduled queue.
for _, msg := range tc.initScheduled {
err := r.client.ZAdd(scheduledQ, &redis.Z{
Member: mustMarshal(t, msg),
Score: float64(time.Now().Unix()),
}).Err()
if err != nil {
t.Fatal(err)
}
}
err := r.DeleteAllScheduledTasks() err := r.DeleteAllScheduledTasks()
if err != nil { if err != nil {

View File

@ -1,91 +1,13 @@
package rdb package rdb
import ( import (
"encoding/json"
"fmt" "fmt"
"math/rand"
"sort"
"testing" "testing"
"time" "time"
"github.com/go-redis/redis/v7"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"github.com/rs/xid"
) )
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.
if err := r.client.FlushDB().Err(); err != nil {
t.Fatal(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 newTaskMessage(taskType string, payload map[string]interface{}) *TaskMessage {
return &TaskMessage{
ID: xid.New(),
Type: taskType,
Queue: "default",
Retry: 25,
Payload: payload,
}
}
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
}
func TestEnqueue(t *testing.T) { func TestEnqueue(t *testing.T) {
r := setup(t) r := setup(t)
tests := []struct { tests := []struct {
@ -97,21 +19,19 @@ func TestEnqueue(t *testing.T) {
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case.
if err := r.client.FlushDB().Err(); err != nil {
t.Fatal(err)
}
err := r.Enqueue(tc.msg) err := r.Enqueue(tc.msg)
if err != nil { if err != nil {
t.Error(err) t.Errorf("(*RDB).Enqueue = %v, want nil", err)
continue continue
} }
res := r.client.LRange(defaultQ, 0, -1).Val() res := r.client.LRange(defaultQ, 0, -1).Val()
if len(res) != 1 { if len(res) != 1 {
t.Errorf("LIST %q has length %d, want 1", defaultQ, len(res)) t.Errorf("%q has length %d, want 1", defaultQ, len(res))
continue continue
} }
if diff := cmp.Diff(*tc.msg, *mustUnmarshal(t, res[0])); diff != "" { if diff := cmp.Diff(tc.msg, mustUnmarshal(t, res[0])); diff != "" {
t.Errorf("persisted data differed from the original input (-want, +got)\n%s", diff) t.Errorf("persisted data differed from the original input (-want, +got)\n%s", diff)
} }
} }
@ -121,23 +41,19 @@ func TestDequeue(t *testing.T) {
r := setup(t) r := setup(t)
t1 := newTaskMessage("send_email", map[string]interface{}{"subject": "hello!"}) t1 := newTaskMessage("send_email", map[string]interface{}{"subject": "hello!"})
tests := []struct { tests := []struct {
queued []*TaskMessage enqueued []*TaskMessage
want *TaskMessage want *TaskMessage
err error err error
inProgress int64 // length of "in-progress" tasks after dequeue inProgress int64 // length of "in-progress" tasks after dequeue
}{ }{
{queued: []*TaskMessage{t1}, want: t1, err: nil, inProgress: 1}, {enqueued: []*TaskMessage{t1}, want: t1, err: nil, inProgress: 1},
{queued: []*TaskMessage{}, want: nil, err: ErrDequeueTimeout, inProgress: 0}, {enqueued: []*TaskMessage{}, want: nil, err: ErrDequeueTimeout, inProgress: 0},
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedDefaultQueue(t, r, tc.enqueued)
t.Fatal(err)
}
for _, m := range tc.queued {
r.Enqueue(m)
}
got, err := r.Dequeue(time.Second) got, err := r.Dequeue(time.Second)
if !cmp.Equal(got, tc.want) || err != tc.err { if !cmp.Equal(got, tc.want) || err != tc.err {
t.Errorf("(*RDB).Dequeue(time.Second) = %v, %v; want %v, %v", t.Errorf("(*RDB).Dequeue(time.Second) = %v, %v; want %v, %v",
@ -145,7 +61,7 @@ func TestDequeue(t *testing.T) {
continue continue
} }
if l := r.client.LLen(inProgressQ).Val(); l != tc.inProgress { if l := r.client.LLen(inProgressQ).Val(); l != tc.inProgress {
t.Errorf("LIST %q has length %d, want %d", inProgressQ, l, tc.inProgress) t.Errorf("%q has length %d, want %d", inProgressQ, l, tc.inProgress)
} }
} }
} }
@ -156,54 +72,41 @@ func TestDone(t *testing.T) {
t2 := newTaskMessage("export_csv", nil) t2 := newTaskMessage("export_csv", nil)
tests := []struct { tests := []struct {
initial []*TaskMessage // initial state of the in-progress list inProgress []*TaskMessage // initial state of the in-progress list
target *TaskMessage // task to remove target *TaskMessage // task to remove
final []*TaskMessage // final state of the in-progress list wantInProgress []*TaskMessage // final state of the in-progress list
}{ }{
{ {
initial: []*TaskMessage{t1, t2}, inProgress: []*TaskMessage{t1, t2},
target: t1, target: t1,
final: []*TaskMessage{t2}, wantInProgress: []*TaskMessage{t2},
}, },
{ {
initial: []*TaskMessage{t2}, inProgress: []*TaskMessage{t2},
target: t1, target: t1,
final: []*TaskMessage{t2}, wantInProgress: []*TaskMessage{t2},
}, },
{ {
initial: []*TaskMessage{t1}, inProgress: []*TaskMessage{t1},
target: t1, target: t1,
final: []*TaskMessage{}, wantInProgress: []*TaskMessage{},
}, },
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedInProgressQueue(t, r, tc.inProgress)
t.Fatal(err)
}
// set up initial state
for _, task := range tc.initial {
err := r.client.LPush(inProgressQ, mustMarshal(t, task)).Err()
if err != nil {
t.Fatal(err)
}
}
err := r.Done(tc.target) err := r.Done(tc.target)
if err != nil { if err != nil {
t.Error(err) t.Errorf("(*RDB).Done(task) = %v, want nil", err)
continue continue
} }
var got []*TaskMessage
data := r.client.LRange(inProgressQ, 0, -1).Val() data := r.client.LRange(inProgressQ, 0, -1).Val()
for _, s := range data { gotInProgress := mustUnmarshalSlice(t, data)
got = append(got, mustUnmarshal(t, s)) if diff := cmp.Diff(tc.wantInProgress, gotInProgress, sortMsgOpt); diff != "" {
} t.Errorf("mismatch found in %q after calling (*RDB).Done: (-want, +got):\n%s", inProgressQ, diff)
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)
continue continue
} }
} }
@ -215,29 +118,20 @@ func TestKill(t *testing.T) {
// TODO(hibiken): add test cases for trimming // TODO(hibiken): add test cases for trimming
tests := []struct { tests := []struct {
initial []*TaskMessage // inital state of "dead" set dead []sortedSetEntry // inital state of dead queue
target *TaskMessage // task to kill target *TaskMessage // task to kill
want []*TaskMessage // final state of "dead" set wantDead []*TaskMessage // final state of dead queue
}{ }{
{ {
initial: []*TaskMessage{}, dead: []sortedSetEntry{},
target: t1, target: t1,
want: []*TaskMessage{t1}, wantDead: []*TaskMessage{t1},
}, },
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedDeadQueue(t, r, tc.dead)
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()
if err != nil {
t.Fatal(err)
}
}
err := r.Kill(tc.target) err := r.Kill(tc.target)
if err != nil { if err != nil {
@ -245,10 +139,10 @@ func TestKill(t *testing.T) {
continue continue
} }
actual := r.client.ZRange(deadQ, 0, -1).Val() data := r.client.ZRange(deadQ, 0, -1).Val()
got := mustUnmarshalSlice(t, actual) gotDead := mustUnmarshalSlice(t, data)
if diff := cmp.Diff(tc.want, got, sortMsgOpt); diff != "" { if diff := cmp.Diff(tc.wantDead, gotDead, sortMsgOpt); diff != "" {
t.Errorf("mismatch found in %q after calling (*rdb).kill: (-want, +got):\n%s", deadQ, diff) t.Errorf("mismatch found in %q after calling (*RDB).Kill: (-want, +got):\n%s", deadQ, diff)
continue continue
} }
} }
@ -261,59 +155,49 @@ func TestRestoreUnfinished(t *testing.T) {
t3 := newTaskMessage("sync_stuff", nil) t3 := newTaskMessage("sync_stuff", nil)
tests := []struct { tests := []struct {
beforeSrc []*TaskMessage inProgress []*TaskMessage
beforeDst []*TaskMessage enqueued []*TaskMessage
afterSrc []*TaskMessage wantInProgress []*TaskMessage
afterDst []*TaskMessage wantEnqueued []*TaskMessage
}{ }{
{ {
beforeSrc: []*TaskMessage{t1, t2, t3}, inProgress: []*TaskMessage{t1, t2, t3},
beforeDst: []*TaskMessage{}, enqueued: []*TaskMessage{},
afterSrc: []*TaskMessage{}, wantInProgress: []*TaskMessage{},
afterDst: []*TaskMessage{t1, t2, t3}, wantEnqueued: []*TaskMessage{t1, t2, t3},
}, },
{ {
beforeSrc: []*TaskMessage{}, inProgress: []*TaskMessage{},
beforeDst: []*TaskMessage{t1, t2, t3}, enqueued: []*TaskMessage{t1, t2, t3},
afterSrc: []*TaskMessage{}, wantInProgress: []*TaskMessage{},
afterDst: []*TaskMessage{t1, t2, t3}, wantEnqueued: []*TaskMessage{t1, t2, t3},
}, },
{ {
beforeSrc: []*TaskMessage{t2, t3}, inProgress: []*TaskMessage{t2, t3},
beforeDst: []*TaskMessage{t1}, enqueued: []*TaskMessage{t1},
afterSrc: []*TaskMessage{}, wantInProgress: []*TaskMessage{},
afterDst: []*TaskMessage{t1, t2, t3}, wantEnqueued: []*TaskMessage{t1, t2, t3},
}, },
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedInProgressQueue(t, r, tc.inProgress)
t.Error(err) seedDefaultQueue(t, r, tc.enqueued)
continue
}
// seed src list.
for _, msg := range tc.beforeSrc {
r.client.LPush(inProgressQ, mustMarshal(t, msg))
}
// seed dst list.
for _, msg := range tc.beforeDst {
r.client.LPush(defaultQ, mustMarshal(t, msg))
}
if err := r.RestoreUnfinished(); err != nil { if err := r.RestoreUnfinished(); err != nil {
t.Errorf("(*RDB).RestoreUnfinished() = %v, want nil", err) t.Errorf("(*RDB).RestoreUnfinished() = %v, want nil", err)
continue continue
} }
src := r.client.LRange(inProgressQ, 0, -1).Val() gotInProgressRaw := r.client.LRange(inProgressQ, 0, -1).Val()
gotSrc := mustUnmarshalSlice(t, src) gotInProgress := mustUnmarshalSlice(t, gotInProgressRaw)
if diff := cmp.Diff(tc.afterSrc, gotSrc, sortMsgOpt); diff != "" { if diff := cmp.Diff(tc.wantInProgress, gotInProgress, sortMsgOpt); diff != "" {
t.Errorf("mismatch found in %q (-want, +got)\n%s", inProgressQ, diff) t.Errorf("mismatch found in %q (-want, +got)\n%s", inProgressQ, diff)
} }
dst := r.client.LRange(defaultQ, 0, -1).Val() gotEnqueuedRaw := r.client.LRange(defaultQ, 0, -1).Val()
gotDst := mustUnmarshalSlice(t, dst) gotEnqueued := mustUnmarshalSlice(t, gotEnqueuedRaw)
if diff := cmp.Diff(tc.afterDst, gotDst, sortMsgOpt); diff != "" { if diff := cmp.Diff(tc.wantEnqueued, gotEnqueued, sortMsgOpt); diff != "" {
t.Errorf("mismatch found in %q (-want, +got)\n%s", defaultQ, diff) t.Errorf("mismatch found in %q (-want, +got)\n%s", defaultQ, diff)
} }
} }
@ -328,38 +212,38 @@ func TestCheckAndEnqueue(t *testing.T) {
hourFromNow := time.Now().Add(time.Hour) hourFromNow := time.Now().Add(time.Hour)
tests := []struct { tests := []struct {
initScheduled []*redis.Z // tasks to be processed later scheduled []sortedSetEntry
initRetry []*redis.Z // tasks to be retired later retry []sortedSetEntry
wantQueued []*TaskMessage // queue after calling forward wantQueued []*TaskMessage
wantScheduled []*TaskMessage // tasks in scheduled queue after calling the method wantScheduled []*TaskMessage
wantRetry []*TaskMessage // tasks in retry queue after calling the method wantRetry []*TaskMessage
}{ }{
{ {
initScheduled: []*redis.Z{ scheduled: []sortedSetEntry{
&redis.Z{Member: mustMarshal(t, t1), Score: float64(secondAgo.Unix())}, {t1, secondAgo.Unix()},
&redis.Z{Member: mustMarshal(t, t2), Score: float64(secondAgo.Unix())}}, {t2, secondAgo.Unix()}},
initRetry: []*redis.Z{ retry: []sortedSetEntry{
&redis.Z{Member: mustMarshal(t, t3), Score: float64(secondAgo.Unix())}}, {t3, secondAgo.Unix()}},
wantQueued: []*TaskMessage{t1, t2, t3}, wantQueued: []*TaskMessage{t1, t2, t3},
wantScheduled: []*TaskMessage{}, wantScheduled: []*TaskMessage{},
wantRetry: []*TaskMessage{}, wantRetry: []*TaskMessage{},
}, },
{ {
initScheduled: []*redis.Z{ scheduled: []sortedSetEntry{
&redis.Z{Member: mustMarshal(t, t1), Score: float64(hourFromNow.Unix())}, {t1, hourFromNow.Unix()},
&redis.Z{Member: mustMarshal(t, t2), Score: float64(secondAgo.Unix())}}, {t2, secondAgo.Unix()}},
initRetry: []*redis.Z{ retry: []sortedSetEntry{
&redis.Z{Member: mustMarshal(t, t3), Score: float64(secondAgo.Unix())}}, {t3, secondAgo.Unix()}},
wantQueued: []*TaskMessage{t2, t3}, wantQueued: []*TaskMessage{t2, t3},
wantScheduled: []*TaskMessage{t1}, wantScheduled: []*TaskMessage{t1},
wantRetry: []*TaskMessage{}, wantRetry: []*TaskMessage{},
}, },
{ {
initScheduled: []*redis.Z{ scheduled: []sortedSetEntry{
&redis.Z{Member: mustMarshal(t, t1), Score: float64(hourFromNow.Unix())}, {t1, hourFromNow.Unix()},
&redis.Z{Member: mustMarshal(t, t2), Score: float64(hourFromNow.Unix())}}, {t2, hourFromNow.Unix()}},
initRetry: []*redis.Z{ retry: []sortedSetEntry{
&redis.Z{Member: mustMarshal(t, t3), Score: float64(hourFromNow.Unix())}}, {t3, hourFromNow.Unix()}},
wantQueued: []*TaskMessage{}, wantQueued: []*TaskMessage{},
wantScheduled: []*TaskMessage{t1, t2}, wantScheduled: []*TaskMessage{t1, t2},
wantRetry: []*TaskMessage{t3}, wantRetry: []*TaskMessage{t3},
@ -367,18 +251,9 @@ func TestCheckAndEnqueue(t *testing.T) {
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // clean up db before each test case
if err := r.client.FlushDB().Err(); err != nil { seedScheduledQueue(t, r, tc.scheduled)
t.Fatal(err) seedRetryQueue(t, r, tc.retry)
}
if err := r.client.ZAdd(scheduledQ, tc.initScheduled...).Err(); err != nil {
t.Error(err)
continue
}
if err := r.client.ZAdd(retryQ, tc.initRetry...).Err(); err != nil {
t.Error(err)
continue
}
err := r.CheckAndEnqueue() err := r.CheckAndEnqueue()
if err != nil { if err != nil {
@ -388,12 +263,17 @@ func TestCheckAndEnqueue(t *testing.T) {
queued := r.client.LRange(defaultQ, 0, -1).Val() queued := r.client.LRange(defaultQ, 0, -1).Val()
gotQueued := mustUnmarshalSlice(t, queued) gotQueued := mustUnmarshalSlice(t, queued)
if diff := cmp.Diff(tc.wantQueued, gotQueued, sortMsgOpt); diff != "" { 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) t.Errorf("mismatch found in %q; (-want, +got)\n%s", defaultQ, diff)
} }
scheduled := r.client.ZRangeByScore(scheduledQ, &redis.ZRangeBy{Min: "-inf", Max: "+inf"}).Val() scheduled := r.client.ZRange(scheduledQ, 0, -1).Val()
gotScheduled := mustUnmarshalSlice(t, scheduled) gotScheduled := mustUnmarshalSlice(t, scheduled)
if diff := cmp.Diff(tc.wantScheduled, gotScheduled, sortMsgOpt); diff != "" { if diff := cmp.Diff(tc.wantScheduled, gotScheduled, sortMsgOpt); diff != "" {
t.Errorf("%q has %d tasks, want %d tasks; (-want, +got)\n%s", scheduled, len(gotScheduled), len(tc.wantScheduled), diff) t.Errorf("mismatch found in %q; (-want, +got)\n%s", scheduledQ, diff)
}
retry := r.client.ZRange(retryQ, 0, -1).Val()
gotRetry := mustUnmarshalSlice(t, retry)
if diff := cmp.Diff(tc.wantRetry, gotRetry, sortMsgOpt); diff != "" {
t.Errorf("mismatch found in %q; (-want, +got)\n%s", retryQ, diff)
} }
} }
} }
@ -411,29 +291,20 @@ func TestSchedule(t *testing.T) {
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // 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)
if err != nil {
t.Error(err)
continue
}
res, err := r.client.ZRangeWithScores(scheduledQ, 0, -1).Result()
if err != nil {
t.Error(err)
continue
}
desc := fmt.Sprintf("(*RDB).Schedule(%v, %v)", tc.msg, tc.processAt) desc := fmt.Sprintf("(*RDB).Schedule(%v, %v)", tc.msg, tc.processAt)
err := r.Schedule(tc.msg, tc.processAt)
if err != nil {
t.Errorf("%s = %v, want nil", desc, err)
continue
}
res := r.client.ZRangeWithScores(scheduledQ, 0, -1).Val()
if len(res) != 1 { if len(res) != 1 {
t.Errorf("%s inserted %d items to %q, want 1 items inserted", desc, len(res), scheduledQ) t.Errorf("%s inserted %d items to %q, want 1 items inserted", desc, len(res), scheduledQ)
continue continue
} }
if res[0].Score != float64(tc.processAt.Unix()) { 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())) t.Errorf("%s inserted an item with score %f, want %f", desc, res[0].Score, float64(tc.processAt.Unix()))
continue continue
@ -454,29 +325,20 @@ func TestRetryLater(t *testing.T) {
} }
for _, tc := range tests { for _, tc := range tests {
// clean up db before each test case. flushDB(t, r) // 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) desc := fmt.Sprintf("(*RDB).RetryLater(%v, %v)", tc.msg, tc.processAt)
err := r.RetryLater(tc.msg, tc.processAt)
if err != nil {
t.Errorf("%s = %v, want nil", desc, err)
continue
}
res := r.client.ZRangeWithScores(retryQ, 0, -1).Val()
if len(res) != 1 { if len(res) != 1 {
t.Errorf("%s inserted %d items to %q, want 1 items inserted", desc, len(res), retryQ) t.Errorf("%s inserted %d items to %q, want 1 items inserted", desc, len(res), retryQ)
continue continue
} }
if res[0].Score != float64(tc.processAt.Unix()) { 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())) t.Errorf("%s inserted an item with score %f, want %f", desc, res[0].Score, float64(tc.processAt.Unix()))
continue continue