2019-12-04 13:01:26 +08:00
|
|
|
package rdb
|
2019-11-20 23:01:24 +08:00
|
|
|
|
|
|
|
import (
|
2019-11-27 23:16:16 +08:00
|
|
|
"fmt"
|
2019-11-20 23:01:24 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
)
|
|
|
|
|
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-12-13 11:45:03 +08:00
|
|
|
{msg: newTaskMessage("send_email", map[string]interface{}{"to": "exampleuser@gmail.com", "from": "noreply@example.com"})},
|
|
|
|
{msg: newTaskMessage("generate_csv", map[string]interface{}{})},
|
|
|
|
{msg: newTaskMessage("sync", nil)},
|
2019-11-20 23:01:24 +08:00
|
|
|
}
|
|
|
|
|
2019-11-26 10:55:17 +08:00
|
|
|
for _, tc := range tests {
|
2019-12-13 11:49:41 +08:00
|
|
|
flushDB(t, r) // clean up db before each test case.
|
|
|
|
|
2019-12-04 13:01:26 +08:00
|
|
|
err := r.Enqueue(tc.msg)
|
2019-11-26 10:55:17 +08:00
|
|
|
if err != nil {
|
2019-12-13 11:49:41 +08:00
|
|
|
t.Errorf("(*RDB).Enqueue = %v, want nil", err)
|
2019-11-28 12:05:31 +08:00
|
|
|
continue
|
2019-11-26 10:55:17 +08:00
|
|
|
}
|
2019-12-05 09:23:11 +08:00
|
|
|
res := r.client.LRange(defaultQ, 0, -1).Val()
|
2019-11-26 10:55:17 +08:00
|
|
|
if len(res) != 1 {
|
2019-12-13 11:49:41 +08:00
|
|
|
t.Errorf("%q has length %d, want 1", defaultQ, len(res))
|
2019-11-26 10:55:17 +08:00
|
|
|
continue
|
|
|
|
}
|
2019-12-13 11:49:41 +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-12-13 11:45:03 +08:00
|
|
|
t1 := newTaskMessage("send_email", map[string]interface{}{"subject": "hello!"})
|
2019-11-26 12:57:53 +08:00
|
|
|
tests := []struct {
|
2019-12-13 11:49:41 +08:00
|
|
|
enqueued []*TaskMessage
|
2019-12-04 13:01:26 +08:00
|
|
|
want *TaskMessage
|
2019-11-26 12:57:53 +08:00
|
|
|
err error
|
|
|
|
inProgress int64 // length of "in-progress" tasks after dequeue
|
|
|
|
}{
|
2019-12-13 11:49:41 +08:00
|
|
|
{enqueued: []*TaskMessage{t1}, want: t1, err: nil, inProgress: 1},
|
|
|
|
{enqueued: []*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 {
|
2019-12-13 11:49:41 +08:00
|
|
|
flushDB(t, r) // clean up db before each test case
|
|
|
|
seedDefaultQueue(t, r, tc.enqueued)
|
|
|
|
|
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 {
|
2019-12-04 22:45:30 +08:00
|
|
|
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
|
|
|
|
}
|
2019-12-05 09:23:11 +08:00
|
|
|
if l := r.client.LLen(inProgressQ).Val(); l != tc.inProgress {
|
2019-12-13 11:49:41 +08:00
|
|
|
t.Errorf("%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-11-24 07:09:50 +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)
|
2019-12-13 11:45:03 +08:00
|
|
|
t1 := newTaskMessage("send_email", nil)
|
|
|
|
t2 := newTaskMessage("export_csv", nil)
|
2019-11-28 22:50:05 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2019-12-13 11:49:41 +08:00
|
|
|
inProgress []*TaskMessage // initial state of the in-progress list
|
|
|
|
target *TaskMessage // task to remove
|
|
|
|
wantInProgress []*TaskMessage // final state of the in-progress list
|
2019-11-28 22:50:05 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-13 11:49:41 +08:00
|
|
|
inProgress: []*TaskMessage{t1, t2},
|
|
|
|
target: t1,
|
|
|
|
wantInProgress: []*TaskMessage{t2},
|
2019-11-28 22:50:05 +08:00
|
|
|
},
|
|
|
|
{
|
2019-12-13 11:49:41 +08:00
|
|
|
inProgress: []*TaskMessage{t2},
|
|
|
|
target: t1,
|
|
|
|
wantInProgress: []*TaskMessage{t2},
|
2019-11-28 22:50:05 +08:00
|
|
|
},
|
|
|
|
{
|
2019-12-13 11:49:41 +08:00
|
|
|
inProgress: []*TaskMessage{t1},
|
|
|
|
target: t1,
|
|
|
|
wantInProgress: []*TaskMessage{},
|
2019-11-28 22:50:05 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2019-12-13 11:49:41 +08:00
|
|
|
flushDB(t, r) // clean up db before each test case
|
|
|
|
seedInProgressQueue(t, r, tc.inProgress)
|
2019-11-28 22:50:05 +08:00
|
|
|
|
2019-12-04 22:33:05 +08:00
|
|
|
err := r.Done(tc.target)
|
2019-11-28 22:50:05 +08:00
|
|
|
if err != nil {
|
2019-12-13 11:49:41 +08:00
|
|
|
t.Errorf("(*RDB).Done(task) = %v, want nil", err)
|
2019-11-28 22:50:05 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-05 09:23:11 +08:00
|
|
|
data := r.client.LRange(inProgressQ, 0, -1).Val()
|
2019-12-13 11:49:41 +08:00
|
|
|
gotInProgress := mustUnmarshalSlice(t, data)
|
|
|
|
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)
|
2019-11-28 23:47:12 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKill(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-13 11:45:03 +08:00
|
|
|
t1 := newTaskMessage("send_email", nil)
|
2019-12-16 09:16:13 +08:00
|
|
|
t2 := newTaskMessage("reindex", nil)
|
|
|
|
t3 := newTaskMessage("generate_csv", nil)
|
|
|
|
errMsg := "SMTP server not responding"
|
|
|
|
t1AfterKill := &TaskMessage{
|
|
|
|
ID: t1.ID,
|
|
|
|
Type: t1.Type,
|
|
|
|
Payload: t1.Payload,
|
|
|
|
Queue: t1.Queue,
|
|
|
|
Retry: t1.Retry,
|
|
|
|
Retried: t1.Retried,
|
|
|
|
ErrorMsg: errMsg,
|
|
|
|
}
|
|
|
|
now := time.Now()
|
2019-11-28 23:47:12 +08:00
|
|
|
|
|
|
|
// TODO(hibiken): add test cases for trimming
|
|
|
|
tests := []struct {
|
2019-12-16 09:16:13 +08:00
|
|
|
inProgress []*TaskMessage
|
|
|
|
dead []sortedSetEntry
|
|
|
|
target *TaskMessage // task to kill
|
|
|
|
wantInProgress []*TaskMessage
|
|
|
|
wantDead []sortedSetEntry
|
2019-11-28 23:47:12 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-16 09:16:13 +08:00
|
|
|
inProgress: []*TaskMessage{t1, t2},
|
|
|
|
dead: []sortedSetEntry{
|
|
|
|
{t3, now.Add(-time.Hour).Unix()},
|
|
|
|
},
|
|
|
|
target: t1,
|
|
|
|
wantInProgress: []*TaskMessage{t2},
|
|
|
|
wantDead: []sortedSetEntry{
|
|
|
|
{t1AfterKill, now.Unix()},
|
|
|
|
{t3, now.Add(-time.Hour).Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inProgress: []*TaskMessage{t1, t2, t3},
|
|
|
|
dead: []sortedSetEntry{},
|
|
|
|
target: t1,
|
|
|
|
wantInProgress: []*TaskMessage{t2, t3},
|
|
|
|
wantDead: []sortedSetEntry{
|
|
|
|
{t1AfterKill, now.Unix()},
|
|
|
|
},
|
2019-11-28 23:47:12 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2019-12-13 11:49:41 +08:00
|
|
|
flushDB(t, r) // clean up db before each test case
|
2019-12-16 09:16:13 +08:00
|
|
|
seedInProgressQueue(t, r, tc.inProgress)
|
2019-12-13 11:49:41 +08:00
|
|
|
seedDeadQueue(t, r, tc.dead)
|
2019-11-28 23:47:12 +08:00
|
|
|
|
2019-12-16 09:16:13 +08:00
|
|
|
err := r.Kill(tc.target, errMsg)
|
2019-11-28 23:47:12 +08:00
|
|
|
if err != nil {
|
2019-12-16 09:16:13 +08:00
|
|
|
t.Errorf("(*RDB).Kill(%v, %v) = %v, want nil", tc.target, errMsg, err)
|
2019-11-28 23:47:12 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-16 09:16:13 +08:00
|
|
|
gotInProgressRaw := r.client.LRange(inProgressQ, 0, -1).Val()
|
|
|
|
gotInProgress := mustUnmarshalSlice(t, gotInProgressRaw)
|
|
|
|
if diff := cmp.Diff(tc.wantInProgress, gotInProgress, sortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", inProgressQ, diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
var gotDead []sortedSetEntry
|
|
|
|
data := r.client.ZRangeWithScores(deadQ, 0, -1).Val()
|
|
|
|
for _, z := range data {
|
|
|
|
gotDead = append(gotDead, sortedSetEntry{
|
|
|
|
msg: mustUnmarshal(t, z.Member.(string)),
|
|
|
|
score: int64(z.Score),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
cmpOpt := cmp.AllowUnexported(sortedSetEntry{})
|
|
|
|
if diff := cmp.Diff(tc.wantDead, gotDead, cmpOpt, sortZSetEntryOpt); diff != "" {
|
2019-12-13 11:49:41 +08:00
|
|
|
t.Errorf("mismatch found in %q after calling (*RDB).Kill: (-want, +got):\n%s", deadQ, diff)
|
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-12-13 11:45:03 +08:00
|
|
|
t1 := newTaskMessage("send_email", nil)
|
|
|
|
t2 := newTaskMessage("export_csv", nil)
|
|
|
|
t3 := newTaskMessage("sync_stuff", nil)
|
2019-11-24 07:09:50 +08:00
|
|
|
|
2019-11-27 01:57:53 +08:00
|
|
|
tests := []struct {
|
2019-12-13 11:49:41 +08:00
|
|
|
inProgress []*TaskMessage
|
|
|
|
enqueued []*TaskMessage
|
|
|
|
wantInProgress []*TaskMessage
|
|
|
|
wantEnqueued []*TaskMessage
|
2019-11-27 01:57:53 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-13 11:49:41 +08:00
|
|
|
inProgress: []*TaskMessage{t1, t2, t3},
|
|
|
|
enqueued: []*TaskMessage{},
|
|
|
|
wantInProgress: []*TaskMessage{},
|
|
|
|
wantEnqueued: []*TaskMessage{t1, t2, t3},
|
2019-11-27 01:57:53 +08:00
|
|
|
},
|
|
|
|
{
|
2019-12-13 11:49:41 +08:00
|
|
|
inProgress: []*TaskMessage{},
|
|
|
|
enqueued: []*TaskMessage{t1, t2, t3},
|
|
|
|
wantInProgress: []*TaskMessage{},
|
|
|
|
wantEnqueued: []*TaskMessage{t1, t2, t3},
|
2019-11-27 01:57:53 +08:00
|
|
|
},
|
|
|
|
{
|
2019-12-13 11:49:41 +08:00
|
|
|
inProgress: []*TaskMessage{t2, t3},
|
|
|
|
enqueued: []*TaskMessage{t1},
|
|
|
|
wantInProgress: []*TaskMessage{},
|
|
|
|
wantEnqueued: []*TaskMessage{t1, t2, t3},
|
2019-11-27 01:57:53 +08:00
|
|
|
},
|
2019-11-24 07:09:50 +08:00
|
|
|
}
|
2019-11-27 01:57:53 +08:00
|
|
|
|
|
|
|
for _, tc := range tests {
|
2019-12-13 11:49:41 +08:00
|
|
|
flushDB(t, r) // clean up db before each test case
|
|
|
|
seedInProgressQueue(t, r, tc.inProgress)
|
|
|
|
seedDefaultQueue(t, r, tc.enqueued)
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-13 11:49:41 +08:00
|
|
|
gotInProgressRaw := r.client.LRange(inProgressQ, 0, -1).Val()
|
|
|
|
gotInProgress := mustUnmarshalSlice(t, gotInProgressRaw)
|
|
|
|
if diff := cmp.Diff(tc.wantInProgress, gotInProgress, sortMsgOpt); diff != "" {
|
2019-12-05 09:23:11 +08:00
|
|
|
t.Errorf("mismatch found in %q (-want, +got)\n%s", inProgressQ, diff)
|
2019-11-27 01:57:53 +08:00
|
|
|
}
|
2019-12-13 11:49:41 +08:00
|
|
|
gotEnqueuedRaw := r.client.LRange(defaultQ, 0, -1).Val()
|
|
|
|
gotEnqueued := mustUnmarshalSlice(t, gotEnqueuedRaw)
|
|
|
|
if diff := cmp.Diff(tc.wantEnqueued, gotEnqueued, sortMsgOpt); diff != "" {
|
2019-12-05 09:23:11 +08:00
|
|
|
t.Errorf("mismatch found in %q (-want, +got)\n%s", defaultQ, diff)
|
2019-11-27 01:57:53 +08:00
|
|
|
}
|
2019-11-24 07:09:50 +08:00
|
|
|
}
|
|
|
|
}
|
2019-11-25 23:09:39 +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-13 11:45:03 +08:00
|
|
|
t1 := newTaskMessage("send_email", nil)
|
|
|
|
t2 := newTaskMessage("generate_csv", nil)
|
|
|
|
t3 := newTaskMessage("gen_thumbnail", 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-13 11:49:41 +08:00
|
|
|
scheduled []sortedSetEntry
|
|
|
|
retry []sortedSetEntry
|
|
|
|
wantQueued []*TaskMessage
|
|
|
|
wantScheduled []*TaskMessage
|
|
|
|
wantRetry []*TaskMessage
|
2019-11-26 22:38:11 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-13 11:49:41 +08:00
|
|
|
scheduled: []sortedSetEntry{
|
|
|
|
{t1, secondAgo.Unix()},
|
|
|
|
{t2, secondAgo.Unix()}},
|
|
|
|
retry: []sortedSetEntry{
|
|
|
|
{t3, secondAgo.Unix()}},
|
2019-12-04 23:14:37 +08:00
|
|
|
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-13 11:49:41 +08:00
|
|
|
scheduled: []sortedSetEntry{
|
|
|
|
{t1, hourFromNow.Unix()},
|
|
|
|
{t2, secondAgo.Unix()}},
|
|
|
|
retry: []sortedSetEntry{
|
|
|
|
{t3, secondAgo.Unix()}},
|
2019-12-04 23:14:37 +08:00
|
|
|
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-13 11:49:41 +08:00
|
|
|
scheduled: []sortedSetEntry{
|
|
|
|
{t1, hourFromNow.Unix()},
|
|
|
|
{t2, hourFromNow.Unix()}},
|
|
|
|
retry: []sortedSetEntry{
|
|
|
|
{t3, 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 {
|
2019-12-13 11:49:41 +08:00
|
|
|
flushDB(t, r) // clean up db before each test case
|
|
|
|
seedScheduledQueue(t, r, tc.scheduled)
|
|
|
|
seedRetryQueue(t, r, tc.retry)
|
2019-11-25 23:09:39 +08:00
|
|
|
|
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
|
|
|
|
}
|
2019-12-05 09:23:11 +08:00
|
|
|
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 != "" {
|
2019-12-13 11:49:41 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", defaultQ, diff)
|
2019-11-26 22:38:11 +08:00
|
|
|
}
|
2019-12-13 11:49:41 +08:00
|
|
|
scheduled := r.client.ZRange(scheduledQ, 0, -1).Val()
|
2019-11-28 23:17:07 +08:00
|
|
|
gotScheduled := mustUnmarshalSlice(t, scheduled)
|
|
|
|
if diff := cmp.Diff(tc.wantScheduled, gotScheduled, sortMsgOpt); diff != "" {
|
2019-12-13 11:49:41 +08:00
|
|
|
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)
|
2019-11-26 22:38:11 +08:00
|
|
|
}
|
2019-11-25 23:09:39 +08:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}{
|
|
|
|
{
|
2019-12-13 11:45:03 +08:00
|
|
|
newTaskMessage("send_email", map[string]interface{}{"subject": "hello"}),
|
2019-11-27 23:16:16 +08:00
|
|
|
time.Now().Add(15 * time.Minute),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2019-12-13 11:49:41 +08:00
|
|
|
flushDB(t, r) // clean up db before each test case
|
2019-11-27 23:16:16 +08:00
|
|
|
|
2019-12-13 11:49:41 +08:00
|
|
|
desc := fmt.Sprintf("(*RDB).Schedule(%v, %v)", tc.msg, tc.processAt)
|
2019-12-04 22:45:30 +08:00
|
|
|
err := r.Schedule(tc.msg, tc.processAt)
|
2019-11-27 23:16:16 +08:00
|
|
|
if err != nil {
|
2019-12-13 11:49:41 +08:00
|
|
|
t.Errorf("%s = %v, want nil", desc, err)
|
2019-11-27 23:16:16 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-13 11:49:41 +08:00
|
|
|
res := r.client.ZRangeWithScores(scheduledQ, 0, -1).Val()
|
2019-11-27 23:16:16 +08:00
|
|
|
if len(res) != 1 {
|
2019-12-05 09:23:11 +08:00
|
|
|
t.Errorf("%s inserted %d items to %q, want 1 items inserted", desc, len(res), scheduledQ)
|
2019-12-04 22:45:30 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-16 08:15:07 +08:00
|
|
|
func TestRetry(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
t1 := newTaskMessage("send_email", map[string]interface{}{"subject": "Hola!"})
|
|
|
|
t2 := newTaskMessage("gen_thumbnail", map[string]interface{}{"path": "some/path/to/image.jpg"})
|
|
|
|
t3 := newTaskMessage("reindex", nil)
|
|
|
|
t1.Retried = 10
|
|
|
|
errMsg := "SMTP server is not responding"
|
|
|
|
t1AfterRetry := &TaskMessage{
|
|
|
|
ID: t1.ID,
|
|
|
|
Type: t1.Type,
|
|
|
|
Payload: t1.Payload,
|
|
|
|
Queue: t1.Queue,
|
|
|
|
Retry: t1.Retry,
|
|
|
|
Retried: t1.Retried + 1,
|
|
|
|
ErrorMsg: errMsg,
|
|
|
|
}
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
inProgress []*TaskMessage
|
|
|
|
retry []sortedSetEntry
|
|
|
|
msg *TaskMessage
|
|
|
|
processAt time.Time
|
|
|
|
errMsg string
|
|
|
|
wantInProgress []*TaskMessage
|
|
|
|
wantRetry []sortedSetEntry
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
inProgress: []*TaskMessage{t1, t2},
|
|
|
|
retry: []sortedSetEntry{
|
|
|
|
{t3, now.Add(time.Minute).Unix()},
|
|
|
|
},
|
|
|
|
msg: t1,
|
|
|
|
processAt: now.Add(5 * time.Minute),
|
|
|
|
errMsg: errMsg,
|
|
|
|
wantInProgress: []*TaskMessage{t2},
|
|
|
|
wantRetry: []sortedSetEntry{
|
|
|
|
{t1AfterRetry, now.Add(5 * time.Minute).Unix()},
|
|
|
|
{t3, now.Add(time.Minute).Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
flushDB(t, r)
|
|
|
|
seedInProgressQueue(t, r, tc.inProgress)
|
|
|
|
seedRetryQueue(t, r, tc.retry)
|
|
|
|
|
|
|
|
err := r.Retry(tc.msg, tc.processAt, tc.errMsg)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("(*RDB).Retry = %v, want nil", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
gotInProgressRaw := r.client.LRange(inProgressQ, 0, -1).Val()
|
|
|
|
gotInProgress := mustUnmarshalSlice(t, gotInProgressRaw)
|
|
|
|
if diff := cmp.Diff(tc.wantInProgress, gotInProgress, sortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", inProgressQ, diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
gotRetryRaw := r.client.ZRangeWithScores(retryQ, 0, -1).Val()
|
|
|
|
var gotRetry []sortedSetEntry
|
|
|
|
for _, z := range gotRetryRaw {
|
|
|
|
gotRetry = append(gotRetry, sortedSetEntry{
|
|
|
|
msg: mustUnmarshal(t, z.Member.(string)),
|
|
|
|
score: int64(z.Score),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
cmpOpt := cmp.AllowUnexported(sortedSetEntry{})
|
|
|
|
if diff := cmp.Diff(tc.wantRetry, gotRetry, cmpOpt, sortZSetEntryOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", retryQ, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|