2020-01-03 10:13:16 +08:00
|
|
|
// Copyright 2020 Kentaro Hibino. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
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"
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
"github.com/go-redis/redis/v7"
|
2019-11-20 23:01:24 +08:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2019-12-29 12:12:14 +08:00
|
|
|
h "github.com/hibiken/asynq/internal/asynqtest"
|
2019-12-22 23:15:45 +08:00
|
|
|
"github.com/hibiken/asynq/internal/base"
|
2019-11-20 23:01:24 +08:00
|
|
|
)
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
// 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.
|
|
|
|
h.FlushDB(t, r.client)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
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-22 23:15:45 +08:00
|
|
|
msg *base.TaskMessage
|
2019-11-26 10:55:17 +08:00
|
|
|
}{
|
2019-12-29 12:12:14 +08:00
|
|
|
{h.NewTaskMessage("send_email", map[string]interface{}{"to": "exampleuser@gmail.com", "from": "noreply@example.com"})},
|
|
|
|
{h.NewTaskMessage("generate_csv", map[string]interface{}{})},
|
|
|
|
{h.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-29 12:12:14 +08:00
|
|
|
h.FlushDB(t, r.client) // clean up db before each test case.
|
2019-12-13 11:49:41 +08:00
|
|
|
|
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-29 12:12:14 +08:00
|
|
|
gotEnqueued := h.GetEnqueuedMessages(t, r.client)
|
|
|
|
if len(gotEnqueued) != 1 {
|
|
|
|
t.Errorf("%q has length %d, want 1", base.DefaultQueue, len(gotEnqueued))
|
2019-11-26 10:55:17 +08:00
|
|
|
continue
|
|
|
|
}
|
2019-12-29 12:12:14 +08:00
|
|
|
if diff := cmp.Diff(tc.msg, gotEnqueued[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-29 12:12:14 +08:00
|
|
|
t1 := h.NewTaskMessage("send_email", map[string]interface{}{"subject": "hello!"})
|
2019-11-26 12:57:53 +08:00
|
|
|
tests := []struct {
|
2019-12-28 12:37:15 +08:00
|
|
|
enqueued []*base.TaskMessage
|
|
|
|
want *base.TaskMessage
|
|
|
|
err error
|
|
|
|
wantInProgress []*base.TaskMessage
|
2019-11-26 12:57:53 +08:00
|
|
|
}{
|
2019-12-28 12:37:15 +08:00
|
|
|
{
|
|
|
|
enqueued: []*base.TaskMessage{t1},
|
|
|
|
want: t1,
|
|
|
|
err: nil,
|
|
|
|
wantInProgress: []*base.TaskMessage{t1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
enqueued: []*base.TaskMessage{},
|
|
|
|
want: nil,
|
|
|
|
err: ErrDequeueTimeout,
|
|
|
|
wantInProgress: []*base.TaskMessage{},
|
|
|
|
},
|
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-29 12:12:14 +08:00
|
|
|
h.FlushDB(t, r.client) // clean up db before each test case
|
|
|
|
h.SeedDefaultQueue(t, r.client, tc.enqueued)
|
2019-12-13 11:49:41 +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 {
|
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-28 12:37:15 +08:00
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
gotInProgress := h.GetInProgressMessages(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantInProgress, gotInProgress, h.SortMsgOpt); diff != "" {
|
2019-12-28 12:37:15 +08:00
|
|
|
t.Errorf("mismatch found in %q: (-want,+got):\n%s", base.InProgressQueue, diff)
|
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-29 12:12:14 +08:00
|
|
|
t1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
t2 := h.NewTaskMessage("export_csv", nil)
|
2019-11-28 22:50:05 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2019-12-22 23:15:45 +08:00
|
|
|
inProgress []*base.TaskMessage // initial state of the in-progress list
|
|
|
|
target *base.TaskMessage // task to remove
|
|
|
|
wantInProgress []*base.TaskMessage // final state of the in-progress list
|
2019-11-28 22:50:05 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-22 23:15:45 +08:00
|
|
|
inProgress: []*base.TaskMessage{t1, t2},
|
2019-12-13 11:49:41 +08:00
|
|
|
target: t1,
|
2019-12-22 23:15:45 +08:00
|
|
|
wantInProgress: []*base.TaskMessage{t2},
|
2019-11-28 22:50:05 +08:00
|
|
|
},
|
|
|
|
{
|
2019-12-22 23:15:45 +08:00
|
|
|
inProgress: []*base.TaskMessage{t1},
|
2019-12-13 11:49:41 +08:00
|
|
|
target: t1,
|
2019-12-22 23:15:45 +08:00
|
|
|
wantInProgress: []*base.TaskMessage{},
|
2019-11-28 22:50:05 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2019-12-29 12:12:14 +08:00
|
|
|
h.FlushDB(t, r.client) // clean up db before each test case
|
|
|
|
h.SeedInProgressQueue(t, r.client, 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-29 12:12:14 +08:00
|
|
|
gotInProgress := h.GetInProgressMessages(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantInProgress, gotInProgress, h.SortMsgOpt); diff != "" {
|
2019-12-28 12:37:15 +08:00
|
|
|
t.Errorf("mismatch found in %q: (-want, +got):\n%s", base.InProgressQueue, diff)
|
2019-11-28 23:47:12 +08:00
|
|
|
continue
|
|
|
|
}
|
2019-12-23 21:33:48 +08:00
|
|
|
|
|
|
|
processedKey := base.ProcessedKey(time.Now())
|
|
|
|
gotProcessed := r.client.Get(processedKey).Val()
|
|
|
|
if gotProcessed != "1" {
|
|
|
|
t.Errorf("GET %q = %q, want 1", processedKey, gotProcessed)
|
|
|
|
}
|
|
|
|
|
|
|
|
gotTTL := r.client.TTL(processedKey).Val()
|
|
|
|
if gotTTL > statsTTL {
|
|
|
|
t.Errorf("TTL %q = %v, want less than or equal to %v", processedKey, gotTTL, statsTTL)
|
|
|
|
}
|
2019-11-28 23:47:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-18 12:07:17 +08:00
|
|
|
func TestRequeue(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-29 12:12:14 +08:00
|
|
|
t1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
t2 := h.NewTaskMessage("export_csv", nil)
|
2019-12-18 12:07:17 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2019-12-22 23:15:45 +08:00
|
|
|
enqueued []*base.TaskMessage // initial state of the default queue
|
|
|
|
inProgress []*base.TaskMessage // initial state of the in-progress list
|
|
|
|
target *base.TaskMessage // task to requeue
|
|
|
|
wantEnqueued []*base.TaskMessage // final state of the default queue
|
|
|
|
wantInProgress []*base.TaskMessage // final state of the in-progress list
|
2019-12-18 12:07:17 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-22 23:15:45 +08:00
|
|
|
enqueued: []*base.TaskMessage{},
|
|
|
|
inProgress: []*base.TaskMessage{t1, t2},
|
2019-12-18 12:07:17 +08:00
|
|
|
target: t1,
|
2019-12-22 23:15:45 +08:00
|
|
|
wantEnqueued: []*base.TaskMessage{t1},
|
|
|
|
wantInProgress: []*base.TaskMessage{t2},
|
2019-12-18 12:07:17 +08:00
|
|
|
},
|
|
|
|
{
|
2019-12-22 23:15:45 +08:00
|
|
|
enqueued: []*base.TaskMessage{t1},
|
|
|
|
inProgress: []*base.TaskMessage{t2},
|
2019-12-18 12:07:17 +08:00
|
|
|
target: t2,
|
2019-12-22 23:15:45 +08:00
|
|
|
wantEnqueued: []*base.TaskMessage{t1, t2},
|
|
|
|
wantInProgress: []*base.TaskMessage{},
|
2019-12-18 12:07:17 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2019-12-29 12:12:14 +08:00
|
|
|
h.FlushDB(t, r.client) // clean up db before each test case
|
|
|
|
h.SeedDefaultQueue(t, r.client, tc.enqueued)
|
|
|
|
h.SeedInProgressQueue(t, r.client, tc.inProgress)
|
2019-12-18 12:07:17 +08:00
|
|
|
|
|
|
|
err := r.Requeue(tc.target)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("(*RDB).Requeue(task) = %v, want nil", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
gotEnqueued := h.GetEnqueuedMessages(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantEnqueued, gotEnqueued, h.SortMsgOpt); diff != "" {
|
2019-12-22 23:15:45 +08:00
|
|
|
t.Errorf("mismatch found in %q: (-want, +got):\n%s", base.DefaultQueue, diff)
|
2019-12-18 12:07:17 +08:00
|
|
|
}
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
gotInProgress := h.GetInProgressMessages(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantInProgress, gotInProgress, h.SortMsgOpt); diff != "" {
|
2019-12-22 23:15:45 +08:00
|
|
|
t.Errorf("mismatch found in %q: (-want, +got):\n%s", base.InProgressQueue, diff)
|
2019-12-18 12:07:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 12:37:15 +08:00
|
|
|
func TestSchedule(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-29 12:12:14 +08:00
|
|
|
t1 := h.NewTaskMessage("send_email", map[string]interface{}{"subject": "hello"})
|
2019-12-28 12:37:15 +08:00
|
|
|
tests := []struct {
|
|
|
|
msg *base.TaskMessage
|
|
|
|
processAt time.Time
|
|
|
|
}{
|
|
|
|
{t1, time.Now().Add(15 * time.Minute)},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2019-12-29 12:12:14 +08:00
|
|
|
h.FlushDB(t, r.client) // clean up db before each test case
|
2019-12-28 12:37:15 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
gotScheduled := h.GetScheduledEntries(t, r.client)
|
|
|
|
if len(gotScheduled) != 1 {
|
|
|
|
t.Errorf("%s inserted %d items to %q, want 1 items inserted", desc, len(gotScheduled), base.ScheduledQueue)
|
2019-12-28 12:37:15 +08:00
|
|
|
continue
|
|
|
|
}
|
2019-12-29 12:12:14 +08:00
|
|
|
if gotScheduled[0].Score != tc.processAt.Unix() {
|
|
|
|
t.Errorf("%s inserted an item with score %d, want %d", desc, gotScheduled[0].Score, tc.processAt.Unix())
|
2019-12-28 12:37:15 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetry(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-29 12:12:14 +08:00
|
|
|
t1 := h.NewTaskMessage("send_email", map[string]interface{}{"subject": "Hola!"})
|
|
|
|
t2 := h.NewTaskMessage("gen_thumbnail", map[string]interface{}{"path": "some/path/to/image.jpg"})
|
|
|
|
t3 := h.NewTaskMessage("reindex", nil)
|
2019-12-28 12:37:15 +08:00
|
|
|
t1.Retried = 10
|
|
|
|
errMsg := "SMTP server is not responding"
|
|
|
|
t1AfterRetry := &base.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 []*base.TaskMessage
|
2019-12-29 12:12:14 +08:00
|
|
|
retry []h.ZSetEntry
|
2019-12-28 12:37:15 +08:00
|
|
|
msg *base.TaskMessage
|
|
|
|
processAt time.Time
|
|
|
|
errMsg string
|
|
|
|
wantInProgress []*base.TaskMessage
|
2019-12-29 12:12:14 +08:00
|
|
|
wantRetry []h.ZSetEntry
|
2019-12-28 12:37:15 +08:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
inProgress: []*base.TaskMessage{t1, t2},
|
2019-12-29 12:12:14 +08:00
|
|
|
retry: []h.ZSetEntry{
|
|
|
|
{
|
|
|
|
Msg: t3,
|
|
|
|
Score: now.Add(time.Minute).Unix(),
|
|
|
|
},
|
2019-12-28 12:37:15 +08:00
|
|
|
},
|
|
|
|
msg: t1,
|
|
|
|
processAt: now.Add(5 * time.Minute),
|
|
|
|
errMsg: errMsg,
|
|
|
|
wantInProgress: []*base.TaskMessage{t2},
|
2019-12-29 12:12:14 +08:00
|
|
|
wantRetry: []h.ZSetEntry{
|
|
|
|
{
|
|
|
|
Msg: t1AfterRetry,
|
|
|
|
Score: now.Add(5 * time.Minute).Unix(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Msg: t3,
|
|
|
|
Score: now.Add(time.Minute).Unix(),
|
|
|
|
},
|
2019-12-28 12:37:15 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2019-12-29 12:12:14 +08:00
|
|
|
h.FlushDB(t, r.client)
|
|
|
|
h.SeedInProgressQueue(t, r.client, tc.inProgress)
|
|
|
|
h.SeedRetryQueue(t, r.client, tc.retry)
|
2019-12-28 12:37:15 +08:00
|
|
|
|
|
|
|
err := r.Retry(tc.msg, tc.processAt, tc.errMsg)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("(*RDB).Retry = %v, want nil", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
gotInProgress := h.GetInProgressMessages(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantInProgress, gotInProgress, h.SortMsgOpt); diff != "" {
|
2019-12-28 12:37:15 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.InProgressQueue, diff)
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
gotRetry := h.GetRetryEntries(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantRetry, gotRetry, h.SortZSetEntryOpt); diff != "" {
|
2019-12-28 12:37:15 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.RetryQueue, diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
processedKey := base.ProcessedKey(time.Now())
|
|
|
|
gotProcessed := r.client.Get(processedKey).Val()
|
|
|
|
if gotProcessed != "1" {
|
|
|
|
t.Errorf("GET %q = %q, want 1", processedKey, gotProcessed)
|
|
|
|
}
|
|
|
|
gotTTL := r.client.TTL(processedKey).Val()
|
|
|
|
if gotTTL > statsTTL {
|
|
|
|
t.Errorf("TTL %q = %v, want less than or equal to %v", processedKey, gotTTL, statsTTL)
|
|
|
|
}
|
|
|
|
|
|
|
|
failureKey := base.FailureKey(time.Now())
|
|
|
|
gotFailure := r.client.Get(failureKey).Val()
|
|
|
|
if gotFailure != "1" {
|
|
|
|
t.Errorf("GET %q = %q, want 1", failureKey, gotFailure)
|
|
|
|
}
|
|
|
|
gotTTL = r.client.TTL(processedKey).Val()
|
|
|
|
if gotTTL > statsTTL {
|
|
|
|
t.Errorf("TTL %q = %v, want less than or equal to %v", failureKey, gotTTL, statsTTL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-31 01:22:25 +08:00
|
|
|
|
2019-11-28 23:47:12 +08:00
|
|
|
func TestKill(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-29 12:12:14 +08:00
|
|
|
t1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
t2 := h.NewTaskMessage("reindex", nil)
|
|
|
|
t3 := h.NewTaskMessage("generate_csv", nil)
|
2019-12-16 09:16:13 +08:00
|
|
|
errMsg := "SMTP server not responding"
|
2019-12-22 23:15:45 +08:00
|
|
|
t1AfterKill := &base.TaskMessage{
|
2019-12-16 09:16:13 +08:00
|
|
|
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-22 23:15:45 +08:00
|
|
|
inProgress []*base.TaskMessage
|
2019-12-29 12:12:14 +08:00
|
|
|
dead []h.ZSetEntry
|
2019-12-22 23:15:45 +08:00
|
|
|
target *base.TaskMessage // task to kill
|
|
|
|
wantInProgress []*base.TaskMessage
|
2019-12-29 12:12:14 +08:00
|
|
|
wantDead []h.ZSetEntry
|
2019-11-28 23:47:12 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-22 23:15:45 +08:00
|
|
|
inProgress: []*base.TaskMessage{t1, t2},
|
2019-12-29 12:12:14 +08:00
|
|
|
dead: []h.ZSetEntry{
|
|
|
|
{
|
|
|
|
Msg: t3,
|
|
|
|
Score: now.Add(-time.Hour).Unix(),
|
|
|
|
},
|
2019-12-16 09:16:13 +08:00
|
|
|
},
|
|
|
|
target: t1,
|
2019-12-22 23:15:45 +08:00
|
|
|
wantInProgress: []*base.TaskMessage{t2},
|
2019-12-29 12:12:14 +08:00
|
|
|
wantDead: []h.ZSetEntry{
|
|
|
|
{
|
|
|
|
Msg: t1AfterKill,
|
|
|
|
Score: now.Unix(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Msg: t3,
|
|
|
|
Score: now.Add(-time.Hour).Unix(),
|
|
|
|
},
|
2019-12-16 09:16:13 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-12-22 23:15:45 +08:00
|
|
|
inProgress: []*base.TaskMessage{t1, t2, t3},
|
2019-12-29 12:12:14 +08:00
|
|
|
dead: []h.ZSetEntry{},
|
2019-12-16 09:16:13 +08:00
|
|
|
target: t1,
|
2019-12-22 23:15:45 +08:00
|
|
|
wantInProgress: []*base.TaskMessage{t2, t3},
|
2019-12-29 12:12:14 +08:00
|
|
|
wantDead: []h.ZSetEntry{
|
|
|
|
{
|
|
|
|
Msg: t1AfterKill,
|
|
|
|
Score: now.Unix(),
|
|
|
|
},
|
2019-12-16 09:16:13 +08:00
|
|
|
},
|
2019-11-28 23:47:12 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2019-12-29 12:12:14 +08:00
|
|
|
h.FlushDB(t, r.client) // clean up db before each test case
|
|
|
|
h.SeedInProgressQueue(t, r.client, tc.inProgress)
|
|
|
|
h.SeedDeadQueue(t, r.client, 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-29 12:12:14 +08:00
|
|
|
gotInProgress := h.GetInProgressMessages(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantInProgress, gotInProgress, h.SortMsgOpt); diff != "" {
|
2019-12-29 02:51:37 +08:00
|
|
|
t.Errorf("mismatch found in %q: (-want, +got)\n%s", base.InProgressQueue, diff)
|
2019-12-16 09:16:13 +08:00
|
|
|
}
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
gotDead := h.GetDeadEntries(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantDead, gotDead, h.SortZSetEntryOpt); diff != "" {
|
2019-12-22 23:15:45 +08:00
|
|
|
t.Errorf("mismatch found in %q after calling (*RDB).Kill: (-want, +got):\n%s", base.DeadQueue, diff)
|
2019-11-28 22:50:05 +08:00
|
|
|
}
|
2019-12-23 21:33:48 +08:00
|
|
|
|
|
|
|
processedKey := base.ProcessedKey(time.Now())
|
|
|
|
gotProcessed := r.client.Get(processedKey).Val()
|
|
|
|
if gotProcessed != "1" {
|
|
|
|
t.Errorf("GET %q = %q, want 1", processedKey, gotProcessed)
|
|
|
|
}
|
|
|
|
gotTTL := r.client.TTL(processedKey).Val()
|
|
|
|
if gotTTL > statsTTL {
|
|
|
|
t.Errorf("TTL %q = %v, want less than or equal to %v", processedKey, gotTTL, statsTTL)
|
|
|
|
}
|
|
|
|
|
|
|
|
failureKey := base.FailureKey(time.Now())
|
|
|
|
gotFailure := r.client.Get(failureKey).Val()
|
|
|
|
if gotFailure != "1" {
|
|
|
|
t.Errorf("GET %q = %q, want 1", failureKey, gotFailure)
|
|
|
|
}
|
|
|
|
gotTTL = r.client.TTL(processedKey).Val()
|
|
|
|
if gotTTL > statsTTL {
|
|
|
|
t.Errorf("TTL %q = %v, want less than or equal to %v", failureKey, gotTTL, statsTTL)
|
|
|
|
}
|
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-29 12:12:14 +08:00
|
|
|
t1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
t2 := h.NewTaskMessage("export_csv", nil)
|
|
|
|
t3 := h.NewTaskMessage("sync_stuff", nil)
|
2019-11-24 07:09:50 +08:00
|
|
|
|
2019-11-27 01:57:53 +08:00
|
|
|
tests := []struct {
|
2019-12-22 23:15:45 +08:00
|
|
|
inProgress []*base.TaskMessage
|
|
|
|
enqueued []*base.TaskMessage
|
2019-12-19 10:55:08 +08:00
|
|
|
want int64
|
2019-12-22 23:15:45 +08:00
|
|
|
wantInProgress []*base.TaskMessage
|
|
|
|
wantEnqueued []*base.TaskMessage
|
2019-11-27 01:57:53 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-22 23:15:45 +08:00
|
|
|
inProgress: []*base.TaskMessage{t1, t2, t3},
|
|
|
|
enqueued: []*base.TaskMessage{},
|
2019-12-19 10:55:08 +08:00
|
|
|
want: 3,
|
2019-12-22 23:15:45 +08:00
|
|
|
wantInProgress: []*base.TaskMessage{},
|
|
|
|
wantEnqueued: []*base.TaskMessage{t1, t2, t3},
|
2019-11-27 01:57:53 +08:00
|
|
|
},
|
|
|
|
{
|
2019-12-22 23:15:45 +08:00
|
|
|
inProgress: []*base.TaskMessage{},
|
|
|
|
enqueued: []*base.TaskMessage{t1, t2, t3},
|
2019-12-19 10:55:08 +08:00
|
|
|
want: 0,
|
2019-12-22 23:15:45 +08:00
|
|
|
wantInProgress: []*base.TaskMessage{},
|
|
|
|
wantEnqueued: []*base.TaskMessage{t1, t2, t3},
|
2019-11-27 01:57:53 +08:00
|
|
|
},
|
|
|
|
{
|
2019-12-22 23:15:45 +08:00
|
|
|
inProgress: []*base.TaskMessage{t2, t3},
|
|
|
|
enqueued: []*base.TaskMessage{t1},
|
2019-12-19 10:55:08 +08:00
|
|
|
want: 2,
|
2019-12-22 23:15:45 +08:00
|
|
|
wantInProgress: []*base.TaskMessage{},
|
|
|
|
wantEnqueued: []*base.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-29 12:12:14 +08:00
|
|
|
h.FlushDB(t, r.client) // clean up db before each test case
|
|
|
|
h.SeedInProgressQueue(t, r.client, tc.inProgress)
|
|
|
|
h.SeedDefaultQueue(t, r.client, tc.enqueued)
|
2019-11-27 01:57:53 +08:00
|
|
|
|
2019-12-19 10:55:08 +08:00
|
|
|
got, err := r.RestoreUnfinished()
|
|
|
|
if got != tc.want || err != nil {
|
|
|
|
t.Errorf("(*RDB).RestoreUnfinished() = %v %v, want %v nil", got, err, tc.want)
|
2019-11-27 01:57:53 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
gotInProgress := h.GetInProgressMessages(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantInProgress, gotInProgress, h.SortMsgOpt); diff != "" {
|
2019-12-29 02:51:37 +08:00
|
|
|
t.Errorf("mismatch found in %q: (-want, +got):\n%s", base.InProgressQueue, diff)
|
2019-11-27 01:57:53 +08:00
|
|
|
}
|
2019-12-29 12:12:14 +08:00
|
|
|
|
|
|
|
gotEnqueued := h.GetEnqueuedMessages(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantEnqueued, gotEnqueued, h.SortMsgOpt); diff != "" {
|
2019-12-29 02:51:37 +08:00
|
|
|
t.Errorf("mismatch found in %q: (-want, +got):\n%s", base.DefaultQueue, 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-29 12:12:14 +08:00
|
|
|
t1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
t2 := h.NewTaskMessage("generate_csv", nil)
|
|
|
|
t3 := h.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-29 12:12:14 +08:00
|
|
|
scheduled []h.ZSetEntry
|
|
|
|
retry []h.ZSetEntry
|
2019-12-22 23:15:45 +08:00
|
|
|
wantQueued []*base.TaskMessage
|
|
|
|
wantScheduled []*base.TaskMessage
|
|
|
|
wantRetry []*base.TaskMessage
|
2019-11-26 22:38:11 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled: []h.ZSetEntry{
|
|
|
|
{Msg: t1, Score: secondAgo.Unix()},
|
|
|
|
{Msg: t2, Score: secondAgo.Unix()},
|
|
|
|
},
|
|
|
|
retry: []h.ZSetEntry{
|
|
|
|
{Msg: t3, Score: secondAgo.Unix()}},
|
2019-12-22 23:15:45 +08:00
|
|
|
wantQueued: []*base.TaskMessage{t1, t2, t3},
|
|
|
|
wantScheduled: []*base.TaskMessage{},
|
|
|
|
wantRetry: []*base.TaskMessage{},
|
2019-11-26 22:38:11 +08:00
|
|
|
},
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled: []h.ZSetEntry{
|
|
|
|
{Msg: t1, Score: hourFromNow.Unix()},
|
|
|
|
{Msg: t2, Score: secondAgo.Unix()}},
|
|
|
|
retry: []h.ZSetEntry{
|
|
|
|
{Msg: t3, Score: secondAgo.Unix()}},
|
2019-12-22 23:15:45 +08:00
|
|
|
wantQueued: []*base.TaskMessage{t2, t3},
|
|
|
|
wantScheduled: []*base.TaskMessage{t1},
|
|
|
|
wantRetry: []*base.TaskMessage{},
|
2019-11-26 22:38:11 +08:00
|
|
|
},
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled: []h.ZSetEntry{
|
|
|
|
{Msg: t1, Score: hourFromNow.Unix()},
|
|
|
|
{Msg: t2, Score: hourFromNow.Unix()}},
|
|
|
|
retry: []h.ZSetEntry{
|
|
|
|
{Msg: t3, Score: hourFromNow.Unix()}},
|
2019-12-22 23:15:45 +08:00
|
|
|
wantQueued: []*base.TaskMessage{},
|
|
|
|
wantScheduled: []*base.TaskMessage{t1, t2},
|
|
|
|
wantRetry: []*base.TaskMessage{t3},
|
2019-11-26 22:38:11 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2019-12-29 12:12:14 +08:00
|
|
|
h.FlushDB(t, r.client) // clean up db before each test case
|
|
|
|
h.SeedScheduledQueue(t, r.client, tc.scheduled)
|
|
|
|
h.SeedRetryQueue(t, r.client, 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-29 12:12:14 +08:00
|
|
|
|
|
|
|
gotEnqueued := h.GetEnqueuedMessages(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantQueued, gotEnqueued, h.SortMsgOpt); diff != "" {
|
2019-12-22 23:15:45 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.DefaultQueue, diff)
|
2019-11-26 22:38:11 +08:00
|
|
|
}
|
2019-12-29 12:12:14 +08:00
|
|
|
|
|
|
|
gotScheduled := h.GetScheduledMessages(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantScheduled, gotScheduled, h.SortMsgOpt); diff != "" {
|
2019-12-22 23:15:45 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.ScheduledQueue, diff)
|
2019-12-13 11:49:41 +08:00
|
|
|
}
|
2019-12-29 12:12:14 +08:00
|
|
|
|
|
|
|
gotRetry := h.GetRetryMessages(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantRetry, gotRetry, h.SortMsgOpt); diff != "" {
|
2019-12-22 23:15:45 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.RetryQueue, diff)
|
2019-11-26 22:38:11 +08:00
|
|
|
}
|
2019-11-25 23:09:39 +08:00
|
|
|
}
|
|
|
|
}
|