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-05 12:30:37 +08:00
|
|
|
package rdb
|
|
|
|
|
|
|
|
import (
|
2020-01-24 23:19:58 +08:00
|
|
|
"fmt"
|
2019-12-05 12:30:37 +08:00
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-01-08 13:22:19 +08:00
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
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-12-11 23:38:24 +08:00
|
|
|
"github.com/rs/xid"
|
2019-12-05 12:30:37 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCurrentStats(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-29 12:12:14 +08:00
|
|
|
m1 := h.NewTaskMessage("send_email", map[string]interface{}{"subject": "hello"})
|
|
|
|
m2 := h.NewTaskMessage("reindex", nil)
|
|
|
|
m3 := h.NewTaskMessage("gen_thumbnail", map[string]interface{}{"src": "some/path/to/img"})
|
|
|
|
m4 := h.NewTaskMessage("sync", nil)
|
2020-01-09 23:01:44 +08:00
|
|
|
m5 := h.NewTaskMessage("important_notification", nil)
|
|
|
|
m5.Queue = "critical"
|
|
|
|
m6 := h.NewTaskMessage("minor_notification", nil)
|
|
|
|
m6.Queue = "low"
|
2019-12-26 12:17:00 +08:00
|
|
|
now := time.Now()
|
|
|
|
|
2019-12-05 12:30:37 +08:00
|
|
|
tests := []struct {
|
2020-01-09 23:01:44 +08:00
|
|
|
enqueued map[string][]*base.TaskMessage
|
2019-12-22 23:15:45 +08:00
|
|
|
inProgress []*base.TaskMessage
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled []h.ZSetEntry
|
|
|
|
retry []h.ZSetEntry
|
|
|
|
dead []h.ZSetEntry
|
2019-12-26 12:17:00 +08:00
|
|
|
processed int
|
|
|
|
failed int
|
2020-01-09 23:01:44 +08:00
|
|
|
allQueues []interface{}
|
2019-12-05 12:30:37 +08:00
|
|
|
want *Stats
|
|
|
|
}{
|
|
|
|
{
|
2020-01-09 23:01:44 +08:00
|
|
|
enqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {m1},
|
|
|
|
"critical": {m5},
|
|
|
|
"low": {m6},
|
|
|
|
},
|
2019-12-22 23:15:45 +08:00
|
|
|
inProgress: []*base.TaskMessage{m2},
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m3, Score: float64(now.Add(time.Hour).Unix())},
|
|
|
|
{Msg: m4, Score: float64(now.Unix())}},
|
2019-12-29 12:12:14 +08:00
|
|
|
retry: []h.ZSetEntry{},
|
|
|
|
dead: []h.ZSetEntry{},
|
2019-12-26 12:17:00 +08:00
|
|
|
processed: 120,
|
|
|
|
failed: 2,
|
2020-01-09 23:01:44 +08:00
|
|
|
allQueues: []interface{}{base.DefaultQueue, base.QueueKey("critical"), base.QueueKey("low")},
|
2019-12-05 12:30:37 +08:00
|
|
|
want: &Stats{
|
2020-01-09 23:01:44 +08:00
|
|
|
Enqueued: 3,
|
2019-12-05 12:30:37 +08:00
|
|
|
InProgress: 1,
|
|
|
|
Scheduled: 2,
|
|
|
|
Retry: 0,
|
|
|
|
Dead: 0,
|
2019-12-26 12:17:00 +08:00
|
|
|
Processed: 120,
|
|
|
|
Failed: 2,
|
|
|
|
Timestamp: now,
|
2020-01-09 23:01:44 +08:00
|
|
|
Queues: map[string]int{base.DefaultQueueName: 1, "critical": 1, "low": 1},
|
2019-12-05 12:30:37 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-09 23:01:44 +08:00
|
|
|
enqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {},
|
|
|
|
},
|
2019-12-22 23:15:45 +08:00
|
|
|
inProgress: []*base.TaskMessage{},
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m3, Score: float64(now.Unix())},
|
|
|
|
{Msg: m4, Score: float64(now.Unix())}},
|
2019-12-29 12:12:14 +08:00
|
|
|
retry: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(now.Add(time.Minute).Unix())}},
|
2019-12-29 12:12:14 +08:00
|
|
|
dead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m2, Score: float64(now.Add(-time.Hour).Unix())}},
|
2019-12-26 12:17:00 +08:00
|
|
|
processed: 90,
|
|
|
|
failed: 10,
|
2020-01-09 23:01:44 +08:00
|
|
|
allQueues: []interface{}{base.DefaultQueue},
|
2019-12-05 12:30:37 +08:00
|
|
|
want: &Stats{
|
|
|
|
Enqueued: 0,
|
|
|
|
InProgress: 0,
|
|
|
|
Scheduled: 2,
|
|
|
|
Retry: 1,
|
|
|
|
Dead: 1,
|
2019-12-26 12:17:00 +08:00
|
|
|
Processed: 90,
|
|
|
|
Failed: 10,
|
|
|
|
Timestamp: now,
|
2020-01-09 23:01:44 +08:00
|
|
|
Queues: map[string]int{base.DefaultQueueName: 0},
|
2019-12-05 12:30:37 +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
|
2020-01-09 23:01:44 +08:00
|
|
|
for qname, msgs := range tc.enqueued {
|
|
|
|
h.SeedEnqueuedQueue(t, r.client, msgs, qname)
|
|
|
|
}
|
2019-12-29 12:12:14 +08:00
|
|
|
h.SeedInProgressQueue(t, r.client, tc.inProgress)
|
|
|
|
h.SeedScheduledQueue(t, r.client, tc.scheduled)
|
|
|
|
h.SeedRetryQueue(t, r.client, tc.retry)
|
|
|
|
h.SeedDeadQueue(t, r.client, tc.dead)
|
2019-12-26 12:17:00 +08:00
|
|
|
processedKey := base.ProcessedKey(now)
|
|
|
|
failedKey := base.FailureKey(now)
|
|
|
|
r.client.Set(processedKey, tc.processed, 0)
|
|
|
|
r.client.Set(failedKey, tc.failed, 0)
|
2020-01-09 23:01:44 +08:00
|
|
|
r.client.SAdd(base.AllQueues, tc.allQueues...)
|
2019-12-05 12:30:37 +08:00
|
|
|
|
|
|
|
got, err := r.CurrentStats()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("r.CurrentStats() = %v, %v, want %v, nil", got, err, tc.want)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := cmp.Diff(tc.want, got, timeCmpOpt); diff != "" {
|
|
|
|
t.Errorf("r.CurrentStats() = %v, %v, want %v, nil; (-want, +got)\n%s", got, err, tc.want, diff)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2019-12-23 01:09:57 +08:00
|
|
|
}
|
|
|
|
|
2019-12-26 13:29:20 +08:00
|
|
|
func TestCurrentStatsWithoutData(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
|
|
|
|
want := &Stats{
|
|
|
|
Enqueued: 0,
|
|
|
|
InProgress: 0,
|
|
|
|
Scheduled: 0,
|
|
|
|
Retry: 0,
|
|
|
|
Dead: 0,
|
|
|
|
Processed: 0,
|
|
|
|
Failed: 0,
|
|
|
|
Timestamp: time.Now(),
|
2020-01-10 23:06:26 +08:00
|
|
|
Queues: map[string]int{},
|
2019-12-26 13:29:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
got, err := r.CurrentStats()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("r.CurrentStats() = %v, %v, want %+v, nil", got, err, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := cmp.Diff(want, got, timeCmpOpt); diff != "" {
|
|
|
|
t.Errorf("r.CurrentStats() = %v, %v, want %+v, nil; (-want, +got)\n%s", got, err, want, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 01:41:05 +08:00
|
|
|
func TestHistoricalStats(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
now := time.Now().UTC()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
n int // number of days
|
|
|
|
}{
|
|
|
|
{90},
|
|
|
|
{7},
|
|
|
|
{0},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r.client)
|
|
|
|
|
|
|
|
// populate last n days data
|
|
|
|
for i := 0; i < tc.n; i++ {
|
|
|
|
ts := now.Add(-time.Duration(i) * 24 * time.Hour)
|
|
|
|
processedKey := base.ProcessedKey(ts)
|
|
|
|
failedKey := base.FailureKey(ts)
|
|
|
|
r.client.Set(processedKey, (i+1)*1000, 0)
|
|
|
|
r.client.Set(failedKey, (i+1)*10, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
got, err := r.HistoricalStats(tc.n)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("RDB.HistoricalStats(%v) returned error: %v", tc.n, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(got) != tc.n {
|
|
|
|
t.Errorf("RDB.HistorycalStats(%v) returned %d daily stats, want %d", tc.n, len(got), tc.n)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < tc.n; i++ {
|
|
|
|
want := &DailyStats{
|
|
|
|
Processed: (i + 1) * 1000,
|
|
|
|
Failed: (i + 1) * 10,
|
|
|
|
Time: now.Add(-time.Duration(i) * 24 * time.Hour),
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(want, got[i], timeCmpOpt); diff != "" {
|
|
|
|
t.Errorf("RDB.HistoricalStats %d days ago data; got %+v, want %+v; (-want,+got):\n%s", i, got[i], want, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-23 01:09:57 +08:00
|
|
|
func TestRedisInfo(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
|
|
|
|
info, err := r.RedisInfo()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("RDB.RedisInfo() returned error: %v", err)
|
|
|
|
}
|
2019-12-05 12:30:37 +08:00
|
|
|
|
2019-12-23 01:09:57 +08:00
|
|
|
wantKeys := []string{
|
|
|
|
"redis_version",
|
|
|
|
"uptime_in_days",
|
|
|
|
"connected_clients",
|
|
|
|
"used_memory_human",
|
|
|
|
"used_memory_peak_human",
|
|
|
|
"used_memory_peak_perc",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range wantKeys {
|
|
|
|
if _, ok := info[key]; !ok {
|
|
|
|
t.Errorf("RDB.RedisInfo() = %v is missing entry for %q", info, key)
|
|
|
|
}
|
|
|
|
}
|
2019-12-05 12:30:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestListEnqueued(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
m1 := h.NewTaskMessage("send_email", map[string]interface{}{"subject": "hello"})
|
|
|
|
m2 := h.NewTaskMessage("reindex", nil)
|
2020-01-10 22:56:51 +08:00
|
|
|
m3 := h.NewTaskMessageWithQueue("important_notification", nil, "critical")
|
|
|
|
m4 := h.NewTaskMessageWithQueue("minor_notification", nil, "low")
|
2020-01-10 23:06:26 +08:00
|
|
|
t1 := &EnqueuedTask{ID: m1.ID, Type: m1.Type, Payload: m1.Payload, Queue: m1.Queue}
|
|
|
|
t2 := &EnqueuedTask{ID: m2.ID, Type: m2.Type, Payload: m2.Payload, Queue: m2.Queue}
|
|
|
|
t3 := &EnqueuedTask{ID: m3.ID, Type: m3.Type, Payload: m3.Payload, Queue: m3.Queue}
|
2019-12-05 12:30:37 +08:00
|
|
|
tests := []struct {
|
2020-01-10 22:56:51 +08:00
|
|
|
enqueued map[string][]*base.TaskMessage
|
2020-01-24 23:19:58 +08:00
|
|
|
qname string
|
2019-12-05 12:30:37 +08:00
|
|
|
want []*EnqueuedTask
|
|
|
|
}{
|
|
|
|
{
|
2020-01-10 22:56:51 +08:00
|
|
|
enqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {m1, m2},
|
|
|
|
},
|
2020-01-24 23:19:58 +08:00
|
|
|
qname: base.DefaultQueueName,
|
|
|
|
want: []*EnqueuedTask{t1, t2},
|
2020-01-10 22:56:51 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
enqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {},
|
|
|
|
},
|
2020-01-24 23:19:58 +08:00
|
|
|
qname: base.DefaultQueueName,
|
|
|
|
want: []*EnqueuedTask{},
|
2019-12-05 12:30:37 +08:00
|
|
|
},
|
|
|
|
{
|
2020-01-10 22:56:51 +08:00
|
|
|
enqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {m1, m2},
|
|
|
|
"critical": {m3},
|
|
|
|
"low": {m4},
|
|
|
|
},
|
2020-01-24 23:19:58 +08:00
|
|
|
qname: base.DefaultQueueName,
|
|
|
|
want: []*EnqueuedTask{t1, t2},
|
2020-01-11 13:32:15 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
enqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {m1, m2},
|
|
|
|
"critical": {m3},
|
|
|
|
"low": {m4},
|
|
|
|
},
|
2020-01-24 23:19:58 +08:00
|
|
|
qname: "critical",
|
|
|
|
want: []*EnqueuedTask{t3},
|
2019-12-05 12:30:37 +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
|
2020-01-10 22:56:51 +08:00
|
|
|
for qname, msgs := range tc.enqueued {
|
|
|
|
h.SeedEnqueuedQueue(t, r.client, msgs, qname)
|
|
|
|
}
|
2019-12-13 11:49:41 +08:00
|
|
|
|
2020-01-24 23:19:58 +08:00
|
|
|
got, err := r.ListEnqueued(tc.qname, Pagination{Size: 20, Page: 0})
|
|
|
|
op := fmt.Sprintf("r.ListEnqueued(%q, Pagination{Size: 20, Page: 0})", tc.qname)
|
2019-12-05 12:30:37 +08:00
|
|
|
if err != nil {
|
2020-01-24 23:19:58 +08:00
|
|
|
t.Errorf("%s = %v, %v, want %v, nil", op, got, err, tc.want)
|
2019-12-05 12:30:37 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
sortOpt := cmp.Transformer("SortMsg", func(in []*EnqueuedTask) []*EnqueuedTask {
|
|
|
|
out := append([]*EnqueuedTask(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
|
|
|
|
})
|
|
|
|
if diff := cmp.Diff(tc.want, got, sortOpt); diff != "" {
|
2020-01-24 23:19:58 +08:00
|
|
|
t.Errorf("%s = %v, %v, want %v, nil; (-want, +got)\n%s", op, got, err, tc.want, diff)
|
2019-12-05 12:30:37 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-24 23:19:58 +08:00
|
|
|
func TestListEnqueuedPagination(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
var msgs []*base.TaskMessage
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
msg := h.NewTaskMessage(fmt.Sprintf("task %d", i), nil)
|
|
|
|
msgs = append(msgs, msg)
|
|
|
|
}
|
|
|
|
// create 100 tasks in default queue
|
|
|
|
h.SeedEnqueuedQueue(t, r.client, msgs)
|
|
|
|
|
|
|
|
msgs = []*base.TaskMessage(nil) // empty list
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
msg := h.NewTaskMessage(fmt.Sprintf("custom %d", i), nil)
|
|
|
|
msgs = append(msgs, msg)
|
|
|
|
}
|
|
|
|
// create 100 tasks in custom queue
|
|
|
|
h.SeedEnqueuedQueue(t, r.client, msgs, "custom")
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
qname string
|
2020-02-13 14:23:25 +08:00
|
|
|
page int
|
|
|
|
size int
|
2020-01-24 23:19:58 +08:00
|
|
|
wantSize int
|
|
|
|
wantFirst string
|
|
|
|
wantLast string
|
|
|
|
}{
|
|
|
|
{"first page", "default", 0, 20, 20, "task 0", "task 19"},
|
|
|
|
{"second page", "default", 1, 20, 20, "task 20", "task 39"},
|
|
|
|
{"different page size", "default", 2, 30, 30, "task 60", "task 89"},
|
|
|
|
{"last page", "default", 3, 30, 10, "task 90", "task 99"},
|
|
|
|
{"out of range", "default", 4, 30, 0, "", ""},
|
|
|
|
{"second page with custom queue", "custom", 1, 20, 20, "custom 20", "custom 39"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
got, err := r.ListEnqueued(tc.qname, Pagination{Size: tc.size, Page: tc.page})
|
|
|
|
op := fmt.Sprintf("r.ListEnqueued(%q, Pagination{Size: %d, Page: %d})", tc.qname, tc.size, tc.page)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s; %s returned error %v", tc.desc, op, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(got) != tc.wantSize {
|
|
|
|
t.Errorf("%s; %s returned a list of size %d, want %d", tc.desc, op, len(got), tc.wantSize)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.wantSize == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
first := got[0]
|
|
|
|
if first.Type != tc.wantFirst {
|
|
|
|
t.Errorf("%s; %s returned a list with first message %q, want %q",
|
|
|
|
tc.desc, op, first.Type, tc.wantFirst)
|
|
|
|
}
|
|
|
|
|
|
|
|
last := got[len(got)-1]
|
|
|
|
if last.Type != tc.wantLast {
|
|
|
|
t.Errorf("%s; %s returned a list with the last message %q, want %q",
|
|
|
|
tc.desc, op, last.Type, tc.wantLast)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-05 12:30:37 +08:00
|
|
|
|
|
|
|
func TestListInProgress(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
m1 := h.NewTaskMessage("send_email", map[string]interface{}{"subject": "hello"})
|
|
|
|
m2 := h.NewTaskMessage("reindex", nil)
|
2019-12-05 12:30:37 +08:00
|
|
|
t1 := &InProgressTask{ID: m1.ID, Type: m1.Type, Payload: m1.Payload}
|
|
|
|
t2 := &InProgressTask{ID: m2.ID, Type: m2.Type, Payload: m2.Payload}
|
|
|
|
tests := []struct {
|
2019-12-22 23:15:45 +08:00
|
|
|
inProgress []*base.TaskMessage
|
2019-12-13 11:49:41 +08:00
|
|
|
want []*InProgressTask
|
2019-12-05 12:30:37 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-22 23:15:45 +08:00
|
|
|
inProgress: []*base.TaskMessage{m1, m2},
|
2019-12-13 11:49:41 +08:00
|
|
|
want: []*InProgressTask{t1, t2},
|
2019-12-05 12:30:37 +08:00
|
|
|
},
|
|
|
|
{
|
2019-12-22 23:15:45 +08:00
|
|
|
inProgress: []*base.TaskMessage{},
|
2019-12-13 11:49:41 +08:00
|
|
|
want: []*InProgressTask{},
|
2019-12-05 12:30:37 +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-12-13 11:49:41 +08:00
|
|
|
|
2020-01-24 23:19:58 +08:00
|
|
|
got, err := r.ListInProgress(Pagination{Size: 20, Page: 0})
|
|
|
|
op := "r.ListInProgress(Pagination{Size: 20, Page: 0})"
|
2019-12-05 12:30:37 +08:00
|
|
|
if err != nil {
|
2020-01-24 23:19:58 +08:00
|
|
|
t.Errorf("%s = %v, %v, want %v, nil", op, got, err, tc.want)
|
2019-12-05 12:30:37 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
sortOpt := cmp.Transformer("SortMsg", func(in []*InProgressTask) []*InProgressTask {
|
|
|
|
out := append([]*InProgressTask(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
|
|
|
|
})
|
|
|
|
if diff := cmp.Diff(tc.want, got, sortOpt); diff != "" {
|
2020-01-24 23:19:58 +08:00
|
|
|
t.Errorf("%s = %v, %v, want %v, nil; (-want, +got)\n%s", op, got, err, tc.want, diff)
|
2019-12-05 12:30:37 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 23:19:58 +08:00
|
|
|
func TestListInProgressPagination(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
var msgs []*base.TaskMessage
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
msg := h.NewTaskMessage(fmt.Sprintf("task %d", i), nil)
|
|
|
|
msgs = append(msgs, msg)
|
|
|
|
}
|
|
|
|
h.SeedInProgressQueue(t, r.client, msgs)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
2020-02-13 14:23:25 +08:00
|
|
|
page int
|
|
|
|
size int
|
2020-01-24 23:19:58 +08:00
|
|
|
wantSize int
|
|
|
|
wantFirst string
|
|
|
|
wantLast string
|
|
|
|
}{
|
|
|
|
{"first page", 0, 20, 20, "task 0", "task 19"},
|
|
|
|
{"second page", 1, 20, 20, "task 20", "task 39"},
|
|
|
|
{"different page size", 2, 30, 30, "task 60", "task 89"},
|
|
|
|
{"last page", 3, 30, 10, "task 90", "task 99"},
|
|
|
|
{"out of range", 4, 30, 0, "", ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
got, err := r.ListInProgress(Pagination{Size: tc.size, Page: tc.page})
|
|
|
|
op := fmt.Sprintf("r.ListInProgress(Pagination{Size: %d, Page: %d})", tc.size, tc.page)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s; %s returned error %v", tc.desc, op, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(got) != tc.wantSize {
|
|
|
|
t.Errorf("%s; %s returned list of size %d, want %d", tc.desc, op, len(got), tc.wantSize)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.wantSize == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
first := got[0]
|
|
|
|
if first.Type != tc.wantFirst {
|
|
|
|
t.Errorf("%s; %s returned a list with first message %q, want %q",
|
|
|
|
tc.desc, op, first.Type, tc.wantFirst)
|
|
|
|
}
|
|
|
|
|
|
|
|
last := got[len(got)-1]
|
|
|
|
if last.Type != tc.wantLast {
|
|
|
|
t.Errorf("%s; %s returned a list with the last message %q, want %q",
|
|
|
|
tc.desc, op, last.Type, tc.wantLast)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 12:30:37 +08:00
|
|
|
func TestListScheduled(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-29 12:12:14 +08:00
|
|
|
m1 := h.NewTaskMessage("send_email", map[string]interface{}{"subject": "hello"})
|
|
|
|
m2 := h.NewTaskMessage("reindex", nil)
|
2019-12-05 12:30:37 +08:00
|
|
|
p1 := time.Now().Add(30 * time.Minute)
|
|
|
|
p2 := time.Now().Add(24 * time.Hour)
|
2020-01-10 23:06:26 +08:00
|
|
|
t1 := &ScheduledTask{ID: m1.ID, Type: m1.Type, Payload: m1.Payload, ProcessAt: p1, Score: p1.Unix(), Queue: m1.Queue}
|
|
|
|
t2 := &ScheduledTask{ID: m2.ID, Type: m2.Type, Payload: m2.Payload, ProcessAt: p2, Score: p2.Unix(), Queue: m2.Queue}
|
2019-12-05 12:30:37 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled []h.ZSetEntry
|
2019-12-05 12:30:37 +08:00
|
|
|
want []*ScheduledTask
|
|
|
|
}{
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(p1.Unix())},
|
|
|
|
{Msg: m2, Score: float64(p2.Unix())},
|
2019-12-05 12:30:37 +08:00
|
|
|
},
|
|
|
|
want: []*ScheduledTask{t1, t2},
|
|
|
|
},
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled: []h.ZSetEntry{},
|
2019-12-05 12:30:37 +08:00
|
|
|
want: []*ScheduledTask{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2019-12-05 12:30:37 +08:00
|
|
|
|
2020-01-24 23:19:58 +08:00
|
|
|
got, err := r.ListScheduled(Pagination{Size: 20, Page: 0})
|
|
|
|
op := "r.ListScheduled(Pagination{Size: 20, Page: 0})"
|
2019-12-05 12:30:37 +08:00
|
|
|
if err != nil {
|
2020-01-24 23:19:58 +08:00
|
|
|
t.Errorf("%s = %v, %v, want %v, nil", op, got, err, tc.want)
|
2019-12-05 12:30:37 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
sortOpt := cmp.Transformer("SortMsg", func(in []*ScheduledTask) []*ScheduledTask {
|
|
|
|
out := append([]*ScheduledTask(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
|
|
|
|
})
|
|
|
|
if diff := cmp.Diff(tc.want, got, sortOpt, timeCmpOpt); diff != "" {
|
2020-01-24 23:19:58 +08:00
|
|
|
t.Errorf("%s = %v, %v, want %v, nil; (-want, +got)\n%s", op, got, err, tc.want, diff)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListScheduledPagination(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
// create 100 tasks with an increasing number of wait time.
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
msg := h.NewTaskMessage(fmt.Sprintf("task %d", i), nil)
|
|
|
|
if err := r.Schedule(msg, time.Now().Add(time.Duration(i)*time.Second)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
2020-02-13 14:23:25 +08:00
|
|
|
page int
|
|
|
|
size int
|
2020-01-24 23:19:58 +08:00
|
|
|
wantSize int
|
|
|
|
wantFirst string
|
|
|
|
wantLast string
|
|
|
|
}{
|
|
|
|
{"first page", 0, 20, 20, "task 0", "task 19"},
|
|
|
|
{"second page", 1, 20, 20, "task 20", "task 39"},
|
|
|
|
{"different page size", 2, 30, 30, "task 60", "task 89"},
|
|
|
|
{"last page", 3, 30, 10, "task 90", "task 99"},
|
|
|
|
{"out of range", 4, 30, 0, "", ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
got, err := r.ListScheduled(Pagination{Size: tc.size, Page: tc.page})
|
|
|
|
op := fmt.Sprintf("r.ListScheduled(Pagination{Size: %d, Page: %d})", tc.size, tc.page)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s; %s returned error %v", tc.desc, op, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(got) != tc.wantSize {
|
|
|
|
t.Errorf("%s; %s returned list of size %d, want %d", tc.desc, op, len(got), tc.wantSize)
|
2019-12-05 12:30:37 +08:00
|
|
|
continue
|
|
|
|
}
|
2020-01-24 23:19:58 +08:00
|
|
|
|
|
|
|
if tc.wantSize == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
first := got[0]
|
|
|
|
if first.Type != tc.wantFirst {
|
|
|
|
t.Errorf("%s; %s returned a list with first message %q, want %q",
|
|
|
|
tc.desc, op, first.Type, tc.wantFirst)
|
|
|
|
}
|
|
|
|
|
|
|
|
last := got[len(got)-1]
|
|
|
|
if last.Type != tc.wantLast {
|
|
|
|
t.Errorf("%s; %s returned a list with the last message %q, want %q",
|
|
|
|
tc.desc, op, last.Type, tc.wantLast)
|
|
|
|
}
|
2019-12-05 12:30:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListRetry(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-22 23:15:45 +08:00
|
|
|
m1 := &base.TaskMessage{
|
2019-12-11 23:38:24 +08:00
|
|
|
ID: xid.New(),
|
2019-12-05 12:30:37 +08:00
|
|
|
Type: "send_email",
|
|
|
|
Queue: "default",
|
|
|
|
Payload: map[string]interface{}{"subject": "hello"},
|
|
|
|
ErrorMsg: "email server not responding",
|
|
|
|
Retry: 25,
|
|
|
|
Retried: 10,
|
|
|
|
}
|
2019-12-22 23:15:45 +08:00
|
|
|
m2 := &base.TaskMessage{
|
2019-12-11 23:38:24 +08:00
|
|
|
ID: xid.New(),
|
2019-12-05 12:30:37 +08:00
|
|
|
Type: "reindex",
|
|
|
|
Queue: "default",
|
|
|
|
Payload: nil,
|
|
|
|
ErrorMsg: "search engine not responding",
|
|
|
|
Retry: 25,
|
|
|
|
Retried: 2,
|
|
|
|
}
|
|
|
|
p1 := time.Now().Add(5 * time.Minute)
|
|
|
|
p2 := time.Now().Add(24 * time.Hour)
|
2019-12-13 11:49:41 +08:00
|
|
|
t1 := &RetryTask{
|
|
|
|
ID: m1.ID,
|
|
|
|
Type: m1.Type,
|
|
|
|
Payload: m1.Payload,
|
|
|
|
ProcessAt: p1,
|
|
|
|
ErrorMsg: m1.ErrorMsg,
|
|
|
|
Retried: m1.Retried,
|
|
|
|
Retry: m1.Retry,
|
|
|
|
Score: p1.Unix(),
|
2020-01-10 23:06:26 +08:00
|
|
|
Queue: m1.Queue,
|
2019-12-13 11:49:41 +08:00
|
|
|
}
|
|
|
|
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(),
|
2020-01-10 23:06:26 +08:00
|
|
|
Queue: m1.Queue,
|
2019-12-05 12:30:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
2019-12-29 12:12:14 +08:00
|
|
|
retry []h.ZSetEntry
|
2019-12-13 11:49:41 +08:00
|
|
|
want []*RetryTask
|
2019-12-05 12:30:37 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
retry: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(p1.Unix())},
|
|
|
|
{Msg: m2, Score: float64(p2.Unix())},
|
2019-12-05 12:30:37 +08:00
|
|
|
},
|
|
|
|
want: []*RetryTask{t1, t2},
|
|
|
|
},
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
retry: []h.ZSetEntry{},
|
2019-12-13 11:49:41 +08:00
|
|
|
want: []*RetryTask{},
|
2019-12-05 12:30:37 +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.SeedRetryQueue(t, r.client, tc.retry)
|
2019-12-05 12:30:37 +08:00
|
|
|
|
2020-01-24 23:19:58 +08:00
|
|
|
got, err := r.ListRetry(Pagination{Size: 20, Page: 0})
|
|
|
|
op := "r.ListRetry(Pagination{Size: 20, Page: 0})"
|
2019-12-05 12:30:37 +08:00
|
|
|
if err != nil {
|
2020-01-24 23:19:58 +08:00
|
|
|
t.Errorf("%s = %v, %v, want %v, nil", op, got, err, tc.want)
|
2019-12-05 12:30:37 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
sortOpt := cmp.Transformer("SortMsg", func(in []*RetryTask) []*RetryTask {
|
|
|
|
out := append([]*RetryTask(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
|
|
|
|
})
|
|
|
|
|
|
|
|
if diff := cmp.Diff(tc.want, got, sortOpt, timeCmpOpt); diff != "" {
|
2020-01-24 23:19:58 +08:00
|
|
|
t.Errorf("%s = %v, %v, want %v, nil; (-want, +got)\n%s", op, got, err, tc.want, diff)
|
2019-12-05 12:30:37 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 23:19:58 +08:00
|
|
|
func TestListRetryPagination(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
// create 100 tasks with an increasing number of wait time.
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
msg := h.NewTaskMessage(fmt.Sprintf("task %d", i), nil)
|
|
|
|
if err := r.Retry(msg, time.Now().Add(time.Duration(i)*time.Second), "error"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
2020-02-13 14:23:25 +08:00
|
|
|
page int
|
|
|
|
size int
|
2020-01-24 23:19:58 +08:00
|
|
|
wantSize int
|
|
|
|
wantFirst string
|
|
|
|
wantLast string
|
|
|
|
}{
|
|
|
|
{"first page", 0, 20, 20, "task 0", "task 19"},
|
|
|
|
{"second page", 1, 20, 20, "task 20", "task 39"},
|
|
|
|
{"different page size", 2, 30, 30, "task 60", "task 89"},
|
|
|
|
{"last page", 3, 30, 10, "task 90", "task 99"},
|
|
|
|
{"out of range", 4, 30, 0, "", ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
got, err := r.ListRetry(Pagination{Size: tc.size, Page: tc.page})
|
|
|
|
op := fmt.Sprintf("r.ListRetry(Pagination{Size: %d, Page: %d})", tc.size, tc.page)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s; %s returned error %v", tc.desc, op, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(got) != tc.wantSize {
|
|
|
|
t.Errorf("%s; %s returned list of size %d, want %d", tc.desc, op, len(got), tc.wantSize)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.wantSize == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
first := got[0]
|
|
|
|
if first.Type != tc.wantFirst {
|
|
|
|
t.Errorf("%s; %s returned a list with first message %q, want %q",
|
|
|
|
tc.desc, op, first.Type, tc.wantFirst)
|
|
|
|
}
|
|
|
|
|
|
|
|
last := got[len(got)-1]
|
|
|
|
if last.Type != tc.wantLast {
|
|
|
|
t.Errorf("%s; %s returned a list with the last message %q, want %q",
|
|
|
|
tc.desc, op, last.Type, tc.wantLast)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 12:30:37 +08:00
|
|
|
func TestListDead(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-22 23:15:45 +08:00
|
|
|
m1 := &base.TaskMessage{
|
2019-12-11 23:38:24 +08:00
|
|
|
ID: xid.New(),
|
2019-12-05 12:30:37 +08:00
|
|
|
Type: "send_email",
|
|
|
|
Queue: "default",
|
|
|
|
Payload: map[string]interface{}{"subject": "hello"},
|
|
|
|
ErrorMsg: "email server not responding",
|
|
|
|
}
|
2019-12-22 23:15:45 +08:00
|
|
|
m2 := &base.TaskMessage{
|
2019-12-11 23:38:24 +08:00
|
|
|
ID: xid.New(),
|
2019-12-05 12:30:37 +08:00
|
|
|
Type: "reindex",
|
|
|
|
Queue: "default",
|
|
|
|
Payload: nil,
|
|
|
|
ErrorMsg: "search engine not responding",
|
|
|
|
}
|
|
|
|
f1 := time.Now().Add(-5 * time.Minute)
|
|
|
|
f2 := time.Now().Add(-24 * time.Hour)
|
2019-12-13 11:49:41 +08:00
|
|
|
t1 := &DeadTask{
|
|
|
|
ID: m1.ID,
|
|
|
|
Type: m1.Type,
|
|
|
|
Payload: m1.Payload,
|
|
|
|
LastFailedAt: f1,
|
|
|
|
ErrorMsg: m1.ErrorMsg,
|
|
|
|
Score: f1.Unix(),
|
2020-01-10 23:06:26 +08:00
|
|
|
Queue: m1.Queue,
|
2019-12-13 11:49:41 +08:00
|
|
|
}
|
|
|
|
t2 := &DeadTask{
|
|
|
|
ID: m2.ID,
|
|
|
|
Type: m2.Type,
|
|
|
|
Payload: m2.Payload,
|
|
|
|
LastFailedAt: f2,
|
|
|
|
ErrorMsg: m2.ErrorMsg,
|
|
|
|
Score: f2.Unix(),
|
2020-01-10 23:06:26 +08:00
|
|
|
Queue: m2.Queue,
|
2019-12-05 12:30:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
2019-12-29 12:12:14 +08:00
|
|
|
dead []h.ZSetEntry
|
2019-12-05 12:30:37 +08:00
|
|
|
want []*DeadTask
|
|
|
|
}{
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
dead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(f1.Unix())},
|
|
|
|
{Msg: m2, Score: float64(f2.Unix())},
|
2019-12-05 12:30:37 +08:00
|
|
|
},
|
|
|
|
want: []*DeadTask{t1, t2},
|
|
|
|
},
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
dead: []h.ZSetEntry{},
|
2019-12-05 12:30:37 +08:00
|
|
|
want: []*DeadTask{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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.SeedDeadQueue(t, r.client, tc.dead)
|
2019-12-05 12:30:37 +08:00
|
|
|
|
2020-01-24 23:19:58 +08:00
|
|
|
got, err := r.ListDead(Pagination{Size: 20, Page: 0})
|
|
|
|
op := "r.ListDead(Pagination{Size: 20, Page: 0})"
|
2019-12-05 12:30:37 +08:00
|
|
|
if err != nil {
|
2020-01-24 23:19:58 +08:00
|
|
|
t.Errorf("%s = %v, %v, want %v, nil", op, got, err, tc.want)
|
2019-12-05 12:30:37 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
sortOpt := cmp.Transformer("SortMsg", func(in []*DeadTask) []*DeadTask {
|
|
|
|
out := append([]*DeadTask(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
|
|
|
|
})
|
|
|
|
if diff := cmp.Diff(tc.want, got, sortOpt, timeCmpOpt); diff != "" {
|
2020-01-24 23:19:58 +08:00
|
|
|
t.Errorf("%s = %v, %v, want %v, nil; (-want, +got)\n%s", op, got, err, tc.want, diff)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListDeadPagination(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
var entries []h.ZSetEntry
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
msg := h.NewTaskMessage(fmt.Sprintf("task %d", i), nil)
|
|
|
|
entries = append(entries, h.ZSetEntry{Msg: msg, Score: float64(i)})
|
|
|
|
}
|
|
|
|
h.SeedDeadQueue(t, r.client, entries)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
2020-02-13 14:23:25 +08:00
|
|
|
page int
|
|
|
|
size int
|
2020-01-24 23:19:58 +08:00
|
|
|
wantSize int
|
|
|
|
wantFirst string
|
|
|
|
wantLast string
|
|
|
|
}{
|
|
|
|
{"first page", 0, 20, 20, "task 0", "task 19"},
|
|
|
|
{"second page", 1, 20, 20, "task 20", "task 39"},
|
|
|
|
{"different page size", 2, 30, 30, "task 60", "task 89"},
|
|
|
|
{"last page", 3, 30, 10, "task 90", "task 99"},
|
|
|
|
{"out of range", 4, 30, 0, "", ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
got, err := r.ListDead(Pagination{Size: tc.size, Page: tc.page})
|
|
|
|
op := fmt.Sprintf("r.ListDead(Pagination{Size: %d, Page: %d})", tc.size, tc.page)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s; %s returned error %v", tc.desc, op, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(got) != tc.wantSize {
|
|
|
|
t.Errorf("%s; %s returned list of size %d, want %d", tc.desc, op, len(got), tc.wantSize)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.wantSize == 0 {
|
2019-12-05 12:30:37 +08:00
|
|
|
continue
|
|
|
|
}
|
2020-01-24 23:19:58 +08:00
|
|
|
|
|
|
|
first := got[0]
|
|
|
|
if first.Type != tc.wantFirst {
|
|
|
|
t.Errorf("%s; %s returned a list with first message %q, want %q",
|
|
|
|
tc.desc, op, first.Type, tc.wantFirst)
|
|
|
|
}
|
|
|
|
|
|
|
|
last := got[len(got)-1]
|
|
|
|
if last.Type != tc.wantLast {
|
|
|
|
t.Errorf("%s; %s returned a list with the last message %q, want %q",
|
|
|
|
tc.desc, op, last.Type, tc.wantLast)
|
|
|
|
}
|
2019-12-05 12:30:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 13:22:19 +08:00
|
|
|
var timeCmpOpt = cmpopts.EquateApproxTime(time.Second)
|
2019-12-05 12:30:37 +08:00
|
|
|
|
2019-12-10 11:33:07 +08:00
|
|
|
func TestEnqueueDeadTask(t *testing.T) {
|
2019-12-08 22:46:04 +08:00
|
|
|
r := setup(t)
|
2019-12-29 12:12:14 +08:00
|
|
|
t1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
t2 := h.NewTaskMessage("gen_thumbnail", nil)
|
2020-01-08 23:16:21 +08:00
|
|
|
t3 := h.NewTaskMessage("send_notification", nil)
|
|
|
|
t3.Queue = "critical"
|
2019-12-10 12:37:30 +08:00
|
|
|
s1 := time.Now().Add(-5 * time.Minute).Unix()
|
|
|
|
s2 := time.Now().Add(-time.Hour).Unix()
|
2019-12-13 11:49:41 +08:00
|
|
|
|
2019-12-08 22:46:04 +08:00
|
|
|
tests := []struct {
|
2019-12-29 12:12:14 +08:00
|
|
|
dead []h.ZSetEntry
|
2019-12-10 12:37:30 +08:00
|
|
|
score int64
|
2019-12-11 23:38:24 +08:00
|
|
|
id xid.ID
|
2019-12-10 11:33:07 +08:00
|
|
|
want error // expected return value from calling EnqueueDeadTask
|
2019-12-22 23:15:45 +08:00
|
|
|
wantDead []*base.TaskMessage
|
2020-01-08 23:16:21 +08:00
|
|
|
wantEnqueued map[string][]*base.TaskMessage
|
2019-12-08 22:46:04 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
dead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: t1, Score: float64(s1)},
|
|
|
|
{Msg: t2, Score: float64(s2)},
|
2019-12-08 22:46:04 +08:00
|
|
|
},
|
2020-01-08 23:16:21 +08:00
|
|
|
score: s2,
|
|
|
|
id: t2.ID,
|
|
|
|
want: nil,
|
|
|
|
wantDead: []*base.TaskMessage{t1},
|
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {t2},
|
|
|
|
},
|
2019-12-08 22:46:04 +08:00
|
|
|
},
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
dead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: t1, Score: float64(s1)},
|
|
|
|
{Msg: t2, Score: float64(s2)},
|
2019-12-08 22:46:04 +08:00
|
|
|
},
|
2020-01-08 23:16:21 +08:00
|
|
|
score: 123,
|
|
|
|
id: t2.ID,
|
|
|
|
want: ErrTaskNotFound,
|
|
|
|
wantDead: []*base.TaskMessage{t1, t2},
|
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: t1, Score: float64(s1)},
|
|
|
|
{Msg: t2, Score: float64(s2)},
|
|
|
|
{Msg: t3, Score: float64(s1)},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
|
|
|
score: s1,
|
|
|
|
id: t3.ID,
|
|
|
|
want: nil,
|
|
|
|
wantDead: []*base.TaskMessage{t1, t2},
|
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {},
|
|
|
|
"critical": {t3},
|
|
|
|
},
|
2019-12-08 22:46:04 +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.SeedDeadQueue(t, r.client, tc.dead)
|
2019-12-08 22:46:04 +08:00
|
|
|
|
2019-12-10 11:33:07 +08:00
|
|
|
got := r.EnqueueDeadTask(tc.id, tc.score)
|
2019-12-08 22:46:04 +08:00
|
|
|
if got != tc.want {
|
2019-12-10 12:37:30 +08:00
|
|
|
t.Errorf("r.EnqueueDeadTask(%s, %d) = %v, want %v", tc.id, tc.score, got, tc.want)
|
2019-12-08 22:46:04 +08:00
|
|
|
continue
|
|
|
|
}
|
2019-12-29 12:12:14 +08:00
|
|
|
|
2020-01-08 23:16:21 +08:00
|
|
|
for qname, want := range tc.wantEnqueued {
|
|
|
|
gotEnqueued := h.GetEnqueuedMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotEnqueued, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.QueueKey(qname), diff)
|
|
|
|
}
|
2019-12-08 22:46:04 +08:00
|
|
|
}
|
2019-12-29 12:12:14 +08:00
|
|
|
|
|
|
|
gotDead := h.GetDeadMessages(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantDead, gotDead, h.SortMsgOpt); diff != "" {
|
2019-12-22 23:15:45 +08:00
|
|
|
t.Errorf("mismatch found in %q, (-want, +got)\n%s", base.DeadQueue, diff)
|
2019-12-08 22:46:04 +08:00
|
|
|
}
|
2019-12-05 12:30:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 11:33:07 +08:00
|
|
|
func TestEnqueueRetryTask(t *testing.T) {
|
2019-12-08 22:46:04 +08:00
|
|
|
r := setup(t)
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
t1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
t2 := h.NewTaskMessage("gen_thumbnail", nil)
|
2020-01-08 23:16:21 +08:00
|
|
|
t3 := h.NewTaskMessage("send_notification", nil)
|
|
|
|
t3.Queue = "low"
|
2019-12-10 12:37:30 +08:00
|
|
|
s1 := time.Now().Add(-5 * time.Minute).Unix()
|
|
|
|
s2 := time.Now().Add(-time.Hour).Unix()
|
2019-12-08 22:46:04 +08:00
|
|
|
tests := []struct {
|
2019-12-29 12:12:14 +08:00
|
|
|
retry []h.ZSetEntry
|
2019-12-10 12:37:30 +08:00
|
|
|
score int64
|
2019-12-11 23:38:24 +08:00
|
|
|
id xid.ID
|
2019-12-10 11:33:07 +08:00
|
|
|
want error // expected return value from calling EnqueueRetryTask
|
2019-12-22 23:15:45 +08:00
|
|
|
wantRetry []*base.TaskMessage
|
2020-01-08 23:16:21 +08:00
|
|
|
wantEnqueued map[string][]*base.TaskMessage
|
2019-12-08 22:46:04 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
retry: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: t1, Score: float64(s1)},
|
|
|
|
{Msg: t2, Score: float64(s2)},
|
2019-12-08 22:46:04 +08:00
|
|
|
},
|
2020-01-08 23:16:21 +08:00
|
|
|
score: s2,
|
|
|
|
id: t2.ID,
|
|
|
|
want: nil,
|
|
|
|
wantRetry: []*base.TaskMessage{t1},
|
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {t2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
retry: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: t1, Score: float64(s1)},
|
|
|
|
{Msg: t2, Score: float64(s2)},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
|
|
|
score: 123,
|
|
|
|
id: t2.ID,
|
|
|
|
want: ErrTaskNotFound,
|
|
|
|
wantRetry: []*base.TaskMessage{t1, t2},
|
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {},
|
|
|
|
},
|
2019-12-08 22:46:04 +08:00
|
|
|
},
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
retry: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: t1, Score: float64(s1)},
|
|
|
|
{Msg: t2, Score: float64(s2)},
|
|
|
|
{Msg: t3, Score: float64(s2)},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
|
|
|
score: s2,
|
|
|
|
id: t3.ID,
|
|
|
|
want: nil,
|
|
|
|
wantRetry: []*base.TaskMessage{t1, t2},
|
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {},
|
|
|
|
"low": {t3},
|
2019-12-08 22:46:04 +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.SeedRetryQueue(t, r.client, tc.retry) // initialize retry queue
|
2019-12-08 22:46:04 +08:00
|
|
|
|
2019-12-10 11:33:07 +08:00
|
|
|
got := r.EnqueueRetryTask(tc.id, tc.score)
|
2019-12-08 22:46:04 +08:00
|
|
|
if got != tc.want {
|
2019-12-10 12:37:30 +08:00
|
|
|
t.Errorf("r.EnqueueRetryTask(%s, %d) = %v, want %v", tc.id, tc.score, got, tc.want)
|
2019-12-08 22:46:04 +08:00
|
|
|
continue
|
|
|
|
}
|
2019-12-29 12:12:14 +08:00
|
|
|
|
2020-01-08 23:16:21 +08:00
|
|
|
for qname, want := range tc.wantEnqueued {
|
|
|
|
gotEnqueued := h.GetEnqueuedMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotEnqueued, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.QueueKey(qname), diff)
|
|
|
|
}
|
2019-12-08 22:46:04 +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-12-08 22:46:04 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-05 12:30:37 +08:00
|
|
|
}
|
|
|
|
|
2019-12-10 11:33:07 +08:00
|
|
|
func TestEnqueueScheduledTask(t *testing.T) {
|
2019-12-08 22:46:04 +08:00
|
|
|
r := setup(t)
|
2019-12-29 12:12:14 +08:00
|
|
|
t1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
t2 := h.NewTaskMessage("gen_thumbnail", nil)
|
2020-01-08 23:16:21 +08:00
|
|
|
t3 := h.NewTaskMessage("send_notification", nil)
|
|
|
|
t3.Queue = "notifications"
|
2019-12-10 12:37:30 +08:00
|
|
|
s1 := time.Now().Add(-5 * time.Minute).Unix()
|
|
|
|
s2 := time.Now().Add(-time.Hour).Unix()
|
2019-12-13 11:49:41 +08:00
|
|
|
|
2019-12-08 22:46:04 +08:00
|
|
|
tests := []struct {
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled []h.ZSetEntry
|
2019-12-10 12:37:30 +08:00
|
|
|
score int64
|
2019-12-11 23:38:24 +08:00
|
|
|
id xid.ID
|
2019-12-10 11:33:07 +08:00
|
|
|
want error // expected return value from calling EnqueueScheduledTask
|
2019-12-22 23:15:45 +08:00
|
|
|
wantScheduled []*base.TaskMessage
|
2020-01-08 23:16:21 +08:00
|
|
|
wantEnqueued map[string][]*base.TaskMessage
|
2019-12-08 22:46:04 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: t1, Score: float64(s1)},
|
|
|
|
{Msg: t2, Score: float64(s2)},
|
2019-12-08 22:46:04 +08:00
|
|
|
},
|
|
|
|
score: s2,
|
2019-12-10 12:37:30 +08:00
|
|
|
id: t2.ID,
|
2019-12-08 22:46:04 +08:00
|
|
|
want: nil,
|
2019-12-22 23:15:45 +08:00
|
|
|
wantScheduled: []*base.TaskMessage{t1},
|
2020-01-08 23:16:21 +08:00
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {t2},
|
|
|
|
},
|
2019-12-08 22:46:04 +08:00
|
|
|
},
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: t1, Score: float64(s1)},
|
|
|
|
{Msg: t2, Score: float64(s2)},
|
2019-12-08 22:46:04 +08:00
|
|
|
},
|
2019-12-10 12:37:30 +08:00
|
|
|
score: 123,
|
|
|
|
id: t2.ID,
|
2019-12-08 22:46:04 +08:00
|
|
|
want: ErrTaskNotFound,
|
2019-12-22 23:15:45 +08:00
|
|
|
wantScheduled: []*base.TaskMessage{t1, t2},
|
2020-01-08 23:16:21 +08:00
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
scheduled: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: t1, Score: float64(s1)},
|
|
|
|
{Msg: t2, Score: float64(s2)},
|
|
|
|
{Msg: t3, Score: float64(s1)},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
|
|
|
score: s1,
|
|
|
|
id: t3.ID,
|
|
|
|
want: nil,
|
|
|
|
wantScheduled: []*base.TaskMessage{t1, t2},
|
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {},
|
|
|
|
"notifications": {t3},
|
|
|
|
},
|
2019-12-08 22:46:04 +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)
|
2019-12-08 22:46:04 +08:00
|
|
|
|
2019-12-10 11:33:07 +08:00
|
|
|
got := r.EnqueueScheduledTask(tc.id, tc.score)
|
2019-12-08 22:46:04 +08:00
|
|
|
if got != tc.want {
|
2019-12-10 12:37:30 +08:00
|
|
|
t.Errorf("r.EnqueueRetryTask(%s, %d) = %v, want %v", tc.id, tc.score, got, tc.want)
|
2019-12-08 22:46:04 +08:00
|
|
|
continue
|
|
|
|
}
|
2019-12-29 12:12:14 +08:00
|
|
|
|
2020-01-08 23:16:21 +08:00
|
|
|
for qname, want := range tc.wantEnqueued {
|
|
|
|
gotEnqueued := h.GetEnqueuedMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotEnqueued, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.QueueKey(qname), diff)
|
|
|
|
}
|
2019-12-08 22:46:04 +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-08 22:46:04 +08:00
|
|
|
}
|
2019-12-05 12:30:37 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-11 12:28:31 +08:00
|
|
|
|
|
|
|
func TestEnqueueAllScheduledTasks(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-29 12:12:14 +08:00
|
|
|
t1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
t2 := h.NewTaskMessage("gen_thumbnail", nil)
|
|
|
|
t3 := h.NewTaskMessage("reindex", nil)
|
2020-01-08 23:16:21 +08:00
|
|
|
t4 := h.NewTaskMessage("important_notification", nil)
|
|
|
|
t4.Queue = "critical"
|
|
|
|
t5 := h.NewTaskMessage("minor_notification", nil)
|
|
|
|
t5.Queue = "low"
|
2019-12-11 12:28:31 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2019-12-13 11:49:41 +08:00
|
|
|
desc string
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled []h.ZSetEntry
|
2019-12-11 13:38:25 +08:00
|
|
|
want int64
|
2020-01-08 23:16:21 +08:00
|
|
|
wantEnqueued map[string][]*base.TaskMessage
|
2019-12-11 12:28:31 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-13 11:49:41 +08:00
|
|
|
desc: "with tasks in scheduled queue",
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: t1, Score: float64(time.Now().Add(time.Hour).Unix())},
|
|
|
|
{Msg: t2, Score: float64(time.Now().Add(time.Hour).Unix())},
|
|
|
|
{Msg: t3, Score: float64(time.Now().Add(time.Hour).Unix())},
|
2019-12-13 11:49:41 +08:00
|
|
|
},
|
2020-01-08 23:16:21 +08:00
|
|
|
want: 3,
|
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {t1, t2, t3},
|
|
|
|
},
|
2019-12-11 12:28:31 +08:00
|
|
|
},
|
|
|
|
{
|
2020-01-08 23:16:21 +08:00
|
|
|
desc: "with empty scheduled queue",
|
|
|
|
scheduled: []h.ZSetEntry{},
|
|
|
|
want: 0,
|
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "with custom queues",
|
|
|
|
scheduled: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: t1, Score: float64(time.Now().Add(time.Hour).Unix())},
|
|
|
|
{Msg: t2, Score: float64(time.Now().Add(time.Hour).Unix())},
|
|
|
|
{Msg: t3, Score: float64(time.Now().Add(time.Hour).Unix())},
|
|
|
|
{Msg: t4, Score: float64(time.Now().Add(time.Hour).Unix())},
|
|
|
|
{Msg: t5, Score: float64(time.Now().Add(time.Hour).Unix())},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
|
|
|
want: 5,
|
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {t1, t2, t3},
|
|
|
|
"critical": {t4},
|
|
|
|
"low": {t5},
|
|
|
|
},
|
2019-12-11 12:28:31 +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)
|
2019-12-11 12:28:31 +08:00
|
|
|
|
2019-12-11 13:38:25 +08:00
|
|
|
got, err := r.EnqueueAllScheduledTasks()
|
2019-12-11 12:28:31 +08:00
|
|
|
if err != nil {
|
2019-12-11 13:38:25 +08:00
|
|
|
t.Errorf("%s; r.EnqueueAllScheduledTasks = %v, %v; want %v, nil",
|
2019-12-13 11:49:41 +08:00
|
|
|
tc.desc, got, err, tc.want)
|
2019-12-11 13:38:25 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if got != tc.want {
|
|
|
|
t.Errorf("%s; r.EnqueueAllScheduledTasks = %v, %v; want %v, nil",
|
2019-12-13 11:49:41 +08:00
|
|
|
tc.desc, got, err, tc.want)
|
2019-12-11 12:28:31 +08:00
|
|
|
}
|
|
|
|
|
2020-01-08 23:16:21 +08:00
|
|
|
for qname, want := range tc.wantEnqueued {
|
|
|
|
gotEnqueued := h.GetEnqueuedMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotEnqueued, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("%s; mismatch found in %q; (-want, +got)\n%s", tc.desc, base.QueueKey(qname), diff)
|
|
|
|
}
|
2019-12-11 12:28:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnqueueAllRetryTasks(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-29 12:12:14 +08:00
|
|
|
t1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
t2 := h.NewTaskMessage("gen_thumbnail", nil)
|
|
|
|
t3 := h.NewTaskMessage("reindex", nil)
|
2020-01-08 23:16:21 +08:00
|
|
|
t4 := h.NewTaskMessage("important_notification", nil)
|
|
|
|
t4.Queue = "critical"
|
|
|
|
t5 := h.NewTaskMessage("minor_notification", nil)
|
|
|
|
t5.Queue = "low"
|
2019-12-11 12:28:31 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-01-08 23:16:21 +08:00
|
|
|
desc string
|
2019-12-29 12:12:14 +08:00
|
|
|
retry []h.ZSetEntry
|
2019-12-11 13:38:25 +08:00
|
|
|
want int64
|
2020-01-08 23:16:21 +08:00
|
|
|
wantEnqueued map[string][]*base.TaskMessage
|
2019-12-11 12:28:31 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-01-08 23:16:21 +08:00
|
|
|
desc: "with tasks in retry queue",
|
2019-12-29 12:12:14 +08:00
|
|
|
retry: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: t1, Score: float64(time.Now().Add(time.Hour).Unix())},
|
|
|
|
{Msg: t2, Score: float64(time.Now().Add(time.Hour).Unix())},
|
|
|
|
{Msg: t3, Score: float64(time.Now().Add(time.Hour).Unix())},
|
2019-12-13 11:49:41 +08:00
|
|
|
},
|
2020-01-08 23:16:21 +08:00
|
|
|
want: 3,
|
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {t1, t2, t3},
|
|
|
|
},
|
2019-12-11 12:28:31 +08:00
|
|
|
},
|
|
|
|
{
|
2020-01-08 23:16:21 +08:00
|
|
|
desc: "with empty retry queue",
|
|
|
|
retry: []h.ZSetEntry{},
|
|
|
|
want: 0,
|
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "with custom queues",
|
|
|
|
retry: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: t1, Score: float64(time.Now().Add(time.Hour).Unix())},
|
|
|
|
{Msg: t2, Score: float64(time.Now().Add(time.Hour).Unix())},
|
|
|
|
{Msg: t3, Score: float64(time.Now().Add(time.Hour).Unix())},
|
|
|
|
{Msg: t4, Score: float64(time.Now().Add(time.Hour).Unix())},
|
|
|
|
{Msg: t5, Score: float64(time.Now().Add(time.Hour).Unix())},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
|
|
|
want: 5,
|
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {t1, t2, t3},
|
|
|
|
"critical": {t4},
|
|
|
|
"low": {t5},
|
|
|
|
},
|
2019-12-11 12:28:31 +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.SeedRetryQueue(t, r.client, tc.retry)
|
2019-12-11 12:28:31 +08:00
|
|
|
|
2019-12-11 13:38:25 +08:00
|
|
|
got, err := r.EnqueueAllRetryTasks()
|
2019-12-11 12:28:31 +08:00
|
|
|
if err != nil {
|
2019-12-11 13:38:25 +08:00
|
|
|
t.Errorf("%s; r.EnqueueAllRetryTasks = %v, %v; want %v, nil",
|
2020-01-08 23:16:21 +08:00
|
|
|
tc.desc, got, err, tc.want)
|
2019-12-11 13:38:25 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if got != tc.want {
|
|
|
|
t.Errorf("%s; r.EnqueueAllRetryTasks = %v, %v; want %v, nil",
|
2020-01-08 23:16:21 +08:00
|
|
|
tc.desc, got, err, tc.want)
|
2019-12-11 12:28:31 +08:00
|
|
|
}
|
|
|
|
|
2020-01-08 23:16:21 +08:00
|
|
|
for qname, want := range tc.wantEnqueued {
|
|
|
|
gotEnqueued := h.GetEnqueuedMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotEnqueued, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("%s; mismatch found in %q; (-want, +got)\n%s", tc.desc, base.QueueKey(qname), diff)
|
|
|
|
}
|
2019-12-11 12:28:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnqueueAllDeadTasks(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-29 12:12:14 +08:00
|
|
|
t1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
t2 := h.NewTaskMessage("gen_thumbnail", nil)
|
|
|
|
t3 := h.NewTaskMessage("reindex", nil)
|
2020-01-08 23:16:21 +08:00
|
|
|
t4 := h.NewTaskMessage("important_notification", nil)
|
|
|
|
t4.Queue = "critical"
|
|
|
|
t5 := h.NewTaskMessage("minor_notification", nil)
|
|
|
|
t5.Queue = "low"
|
2019-12-11 12:28:31 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2019-12-13 11:49:41 +08:00
|
|
|
desc string
|
2019-12-29 12:12:14 +08:00
|
|
|
dead []h.ZSetEntry
|
2019-12-11 13:38:25 +08:00
|
|
|
want int64
|
2020-01-08 23:16:21 +08:00
|
|
|
wantEnqueued map[string][]*base.TaskMessage
|
2019-12-11 12:28:31 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-13 11:49:41 +08:00
|
|
|
desc: "with tasks in dead queue",
|
2019-12-29 12:12:14 +08:00
|
|
|
dead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: t1, Score: float64(time.Now().Add(-time.Minute).Unix())},
|
|
|
|
{Msg: t2, Score: float64(time.Now().Add(-time.Minute).Unix())},
|
|
|
|
{Msg: t3, Score: float64(time.Now().Add(-time.Minute).Unix())},
|
2019-12-13 11:49:41 +08:00
|
|
|
},
|
2020-01-08 23:16:21 +08:00
|
|
|
want: 3,
|
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {t1, t2, t3},
|
|
|
|
},
|
2019-12-11 12:28:31 +08:00
|
|
|
},
|
|
|
|
{
|
2020-01-08 23:16:21 +08:00
|
|
|
desc: "with empty dead queue",
|
|
|
|
dead: []h.ZSetEntry{},
|
|
|
|
want: 0,
|
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "with custom queues",
|
|
|
|
dead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: t1, Score: float64(time.Now().Add(-time.Minute).Unix())},
|
|
|
|
{Msg: t2, Score: float64(time.Now().Add(-time.Minute).Unix())},
|
|
|
|
{Msg: t3, Score: float64(time.Now().Add(-time.Minute).Unix())},
|
|
|
|
{Msg: t4, Score: float64(time.Now().Add(-time.Minute).Unix())},
|
|
|
|
{Msg: t5, Score: float64(time.Now().Add(-time.Minute).Unix())},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
|
|
|
want: 5,
|
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
base.DefaultQueueName: {t1, t2, t3},
|
|
|
|
"critical": {t4},
|
|
|
|
"low": {t5},
|
|
|
|
},
|
2019-12-11 12:28:31 +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.SeedDeadQueue(t, r.client, tc.dead)
|
2019-12-11 12:28:31 +08:00
|
|
|
|
2019-12-11 13:38:25 +08:00
|
|
|
got, err := r.EnqueueAllDeadTasks()
|
2019-12-11 12:28:31 +08:00
|
|
|
if err != nil {
|
2019-12-11 13:38:25 +08:00
|
|
|
t.Errorf("%s; r.EnqueueAllDeadTasks = %v, %v; want %v, nil",
|
2019-12-13 11:49:41 +08:00
|
|
|
tc.desc, got, err, tc.want)
|
2019-12-11 13:38:25 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if got != tc.want {
|
|
|
|
t.Errorf("%s; r.EnqueueAllDeadTasks = %v, %v; want %v, nil",
|
2019-12-13 11:49:41 +08:00
|
|
|
tc.desc, got, err, tc.want)
|
2019-12-11 12:28:31 +08:00
|
|
|
}
|
|
|
|
|
2020-01-08 23:16:21 +08:00
|
|
|
for qname, want := range tc.wantEnqueued {
|
|
|
|
gotEnqueued := h.GetEnqueuedMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotEnqueued, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("%s; mismatch found in %q; (-want, +got)\n%s", tc.desc, base.QueueKey(qname), diff)
|
|
|
|
}
|
2019-12-11 12:28:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-12 11:56:19 +08:00
|
|
|
|
2019-12-26 23:17:26 +08:00
|
|
|
func TestKillRetryTask(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-29 12:12:14 +08:00
|
|
|
m1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
m2 := h.NewTaskMessage("reindex", nil)
|
2019-12-26 23:17:26 +08:00
|
|
|
t1 := time.Now().Add(time.Minute)
|
|
|
|
t2 := time.Now().Add(time.Hour)
|
|
|
|
|
|
|
|
tests := []struct {
|
2019-12-29 12:12:14 +08:00
|
|
|
retry []h.ZSetEntry
|
|
|
|
dead []h.ZSetEntry
|
2019-12-26 23:17:26 +08:00
|
|
|
id xid.ID
|
|
|
|
score int64
|
|
|
|
want error
|
2019-12-29 12:12:14 +08:00
|
|
|
wantRetry []h.ZSetEntry
|
|
|
|
wantDead []h.ZSetEntry
|
2019-12-26 23:17:26 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
retry: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(t1.Unix())},
|
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
2019-12-29 12:12:14 +08:00
|
|
|
dead: []h.ZSetEntry{},
|
2019-12-26 23:17:26 +08:00
|
|
|
id: m1.ID,
|
|
|
|
score: t1.Unix(),
|
|
|
|
want: nil,
|
2019-12-29 12:12:14 +08:00
|
|
|
wantRetry: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
2019-12-29 12:12:14 +08:00
|
|
|
wantDead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(time.Now().Unix())},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
retry: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(t1.Unix())},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
2019-12-29 12:12:14 +08:00
|
|
|
dead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
|
|
|
id: m2.ID,
|
|
|
|
score: t2.Unix(),
|
|
|
|
want: ErrTaskNotFound,
|
2019-12-29 12:12:14 +08:00
|
|
|
wantRetry: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(t1.Unix())},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
2019-12-29 12:12:14 +08:00
|
|
|
wantDead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2019-12-29 12:12:14 +08:00
|
|
|
h.FlushDB(t, r.client)
|
|
|
|
h.SeedRetryQueue(t, r.client, tc.retry)
|
|
|
|
h.SeedDeadQueue(t, r.client, tc.dead)
|
2019-12-26 23:17:26 +08:00
|
|
|
|
|
|
|
got := r.KillRetryTask(tc.id, tc.score)
|
|
|
|
if got != tc.want {
|
|
|
|
t.Errorf("(*RDB).KillRetryTask(%v, %v) = %v, want %v",
|
|
|
|
tc.id, tc.score, got, tc.want)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
gotRetry := h.GetRetryEntries(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantRetry, gotRetry, h.SortZSetEntryOpt, timeCmpOpt); diff != "" {
|
2019-12-26 23:17:26 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want,+got)\n%s",
|
|
|
|
base.RetryQueue, diff)
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
gotDead := h.GetDeadEntries(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantDead, gotDead, h.SortZSetEntryOpt, timeCmpOpt); diff != "" {
|
2019-12-26 23:17:26 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want,+got)\n%s",
|
|
|
|
base.DeadQueue, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKillScheduledTask(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-29 12:12:14 +08:00
|
|
|
m1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
m2 := h.NewTaskMessage("reindex", nil)
|
2019-12-26 23:17:26 +08:00
|
|
|
t1 := time.Now().Add(time.Minute)
|
|
|
|
t2 := time.Now().Add(time.Hour)
|
|
|
|
|
|
|
|
tests := []struct {
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled []h.ZSetEntry
|
|
|
|
dead []h.ZSetEntry
|
2019-12-26 23:17:26 +08:00
|
|
|
id xid.ID
|
|
|
|
score int64
|
|
|
|
want error
|
2019-12-29 12:12:14 +08:00
|
|
|
wantScheduled []h.ZSetEntry
|
|
|
|
wantDead []h.ZSetEntry
|
2019-12-26 23:17:26 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(t1.Unix())},
|
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
2019-12-29 12:12:14 +08:00
|
|
|
dead: []h.ZSetEntry{},
|
2019-12-26 23:17:26 +08:00
|
|
|
id: m1.ID,
|
|
|
|
score: t1.Unix(),
|
|
|
|
want: nil,
|
2019-12-29 12:12:14 +08:00
|
|
|
wantScheduled: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
2019-12-29 12:12:14 +08:00
|
|
|
wantDead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(time.Now().Unix())},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(t1.Unix())},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
2019-12-29 12:12:14 +08:00
|
|
|
dead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
|
|
|
id: m2.ID,
|
|
|
|
score: t2.Unix(),
|
|
|
|
want: ErrTaskNotFound,
|
2019-12-29 12:12:14 +08:00
|
|
|
wantScheduled: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(t1.Unix())},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
2019-12-29 12:12:14 +08:00
|
|
|
wantDead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2019-12-29 12:12:14 +08:00
|
|
|
h.FlushDB(t, r.client)
|
|
|
|
h.SeedScheduledQueue(t, r.client, tc.scheduled)
|
|
|
|
h.SeedDeadQueue(t, r.client, tc.dead)
|
2019-12-26 23:17:26 +08:00
|
|
|
|
|
|
|
got := r.KillScheduledTask(tc.id, tc.score)
|
|
|
|
if got != tc.want {
|
|
|
|
t.Errorf("(*RDB).KillScheduledTask(%v, %v) = %v, want %v",
|
|
|
|
tc.id, tc.score, got, tc.want)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
gotScheduled := h.GetScheduledEntries(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantScheduled, gotScheduled, h.SortZSetEntryOpt, timeCmpOpt); diff != "" {
|
2019-12-26 23:17:26 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want,+got)\n%s",
|
|
|
|
base.ScheduledQueue, diff)
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
gotDead := h.GetDeadEntries(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantDead, gotDead, h.SortZSetEntryOpt, timeCmpOpt); diff != "" {
|
2019-12-27 22:22:33 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want,+got)\n%s",
|
|
|
|
base.DeadQueue, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKillAllRetryTasks(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-29 12:12:14 +08:00
|
|
|
m1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
m2 := h.NewTaskMessage("reindex", nil)
|
2019-12-27 22:22:33 +08:00
|
|
|
t1 := time.Now().Add(time.Minute)
|
|
|
|
t2 := time.Now().Add(time.Hour)
|
|
|
|
|
|
|
|
tests := []struct {
|
2019-12-29 12:12:14 +08:00
|
|
|
retry []h.ZSetEntry
|
|
|
|
dead []h.ZSetEntry
|
2019-12-27 22:22:33 +08:00
|
|
|
want int64
|
2019-12-29 12:12:14 +08:00
|
|
|
wantRetry []h.ZSetEntry
|
|
|
|
wantDead []h.ZSetEntry
|
2019-12-27 22:22:33 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
retry: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(t1.Unix())},
|
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
2019-12-29 12:12:14 +08:00
|
|
|
dead: []h.ZSetEntry{},
|
2019-12-27 22:22:33 +08:00
|
|
|
want: 2,
|
2019-12-29 12:12:14 +08:00
|
|
|
wantRetry: []h.ZSetEntry{},
|
|
|
|
wantDead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(time.Now().Unix())},
|
|
|
|
{Msg: m2, Score: float64(time.Now().Unix())},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
retry: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(t1.Unix())},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
2019-12-29 12:12:14 +08:00
|
|
|
dead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
|
|
|
want: 1,
|
2019-12-29 12:12:14 +08:00
|
|
|
wantRetry: []h.ZSetEntry{},
|
|
|
|
wantDead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(time.Now().Unix())},
|
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
retry: []h.ZSetEntry{},
|
|
|
|
dead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(t1.Unix())},
|
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
|
|
|
want: 0,
|
2019-12-29 12:12:14 +08:00
|
|
|
wantRetry: []h.ZSetEntry{},
|
|
|
|
wantDead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(t1.Unix())},
|
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2019-12-29 12:12:14 +08:00
|
|
|
h.FlushDB(t, r.client)
|
|
|
|
h.SeedRetryQueue(t, r.client, tc.retry)
|
|
|
|
h.SeedDeadQueue(t, r.client, tc.dead)
|
2019-12-27 22:22:33 +08:00
|
|
|
|
|
|
|
got, err := r.KillAllRetryTasks()
|
|
|
|
if got != tc.want || err != nil {
|
|
|
|
t.Errorf("(*RDB).KillAllRetryTasks() = %v, %v; want %v, nil",
|
|
|
|
got, err, tc.want)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
gotRetry := h.GetRetryEntries(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantRetry, gotRetry, h.SortZSetEntryOpt, timeCmpOpt); diff != "" {
|
2019-12-27 22:22:33 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want,+got)\n%s",
|
|
|
|
base.RetryQueue, diff)
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
gotDead := h.GetDeadEntries(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantDead, gotDead, h.SortZSetEntryOpt, timeCmpOpt); diff != "" {
|
2019-12-27 22:22:33 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want,+got)\n%s",
|
|
|
|
base.DeadQueue, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKillAllScheduledTasks(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-29 12:12:14 +08:00
|
|
|
m1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
m2 := h.NewTaskMessage("reindex", nil)
|
2019-12-27 22:22:33 +08:00
|
|
|
t1 := time.Now().Add(time.Minute)
|
|
|
|
t2 := time.Now().Add(time.Hour)
|
|
|
|
|
|
|
|
tests := []struct {
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled []h.ZSetEntry
|
|
|
|
dead []h.ZSetEntry
|
2019-12-27 22:22:33 +08:00
|
|
|
want int64
|
2019-12-29 12:12:14 +08:00
|
|
|
wantScheduled []h.ZSetEntry
|
|
|
|
wantDead []h.ZSetEntry
|
2019-12-27 22:22:33 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(t1.Unix())},
|
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
2019-12-29 12:12:14 +08:00
|
|
|
dead: []h.ZSetEntry{},
|
2019-12-27 22:22:33 +08:00
|
|
|
want: 2,
|
2019-12-29 12:12:14 +08:00
|
|
|
wantScheduled: []h.ZSetEntry{},
|
|
|
|
wantDead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(time.Now().Unix())},
|
|
|
|
{Msg: m2, Score: float64(time.Now().Unix())},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(t1.Unix())},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
2019-12-29 12:12:14 +08:00
|
|
|
dead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
|
|
|
want: 1,
|
2019-12-29 12:12:14 +08:00
|
|
|
wantScheduled: []h.ZSetEntry{},
|
|
|
|
wantDead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(time.Now().Unix())},
|
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled: []h.ZSetEntry{},
|
|
|
|
dead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(t1.Unix())},
|
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
|
|
|
want: 0,
|
2019-12-29 12:12:14 +08:00
|
|
|
wantScheduled: []h.ZSetEntry{},
|
|
|
|
wantDead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(t1.Unix())},
|
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2019-12-29 12:12:14 +08:00
|
|
|
h.FlushDB(t, r.client)
|
|
|
|
h.SeedScheduledQueue(t, r.client, tc.scheduled)
|
|
|
|
h.SeedDeadQueue(t, r.client, tc.dead)
|
2019-12-27 22:22:33 +08:00
|
|
|
|
|
|
|
got, err := r.KillAllScheduledTasks()
|
|
|
|
if got != tc.want || err != nil {
|
|
|
|
t.Errorf("(*RDB).KillAllScheduledTasks() = %v, %v; want %v, nil",
|
|
|
|
got, err, tc.want)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
gotScheduled := h.GetScheduledEntries(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantScheduled, gotScheduled, h.SortZSetEntryOpt, timeCmpOpt); diff != "" {
|
2019-12-27 22:22:33 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want,+got)\n%s",
|
|
|
|
base.ScheduledQueue, diff)
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
gotDead := h.GetDeadEntries(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantDead, gotDead, h.SortZSetEntryOpt, timeCmpOpt); diff != "" {
|
2019-12-26 23:17:26 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want,+got)\n%s",
|
|
|
|
base.DeadQueue, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-12 11:56:19 +08:00
|
|
|
func TestDeleteDeadTask(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-29 12:12:14 +08:00
|
|
|
m1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
m2 := h.NewTaskMessage("reindex", nil)
|
2019-12-12 11:56:19 +08:00
|
|
|
t1 := time.Now().Add(-5 * time.Minute)
|
|
|
|
t2 := time.Now().Add(-time.Hour)
|
|
|
|
|
|
|
|
tests := []struct {
|
2019-12-29 12:12:14 +08:00
|
|
|
dead []h.ZSetEntry
|
2019-12-12 11:56:19 +08:00
|
|
|
id xid.ID
|
|
|
|
score int64
|
|
|
|
want error
|
2019-12-22 23:15:45 +08:00
|
|
|
wantDead []*base.TaskMessage
|
2019-12-12 11:56:19 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
dead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(t1.Unix())},
|
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-12 11:56:19 +08:00
|
|
|
},
|
|
|
|
id: m1.ID,
|
|
|
|
score: t1.Unix(),
|
|
|
|
want: nil,
|
2019-12-22 23:15:45 +08:00
|
|
|
wantDead: []*base.TaskMessage{m2},
|
2019-12-12 11:56:19 +08:00
|
|
|
},
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
dead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(t1.Unix())},
|
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-12 11:56:19 +08:00
|
|
|
},
|
|
|
|
id: m1.ID,
|
|
|
|
score: t2.Unix(), // id and score mismatch
|
|
|
|
want: ErrTaskNotFound,
|
2019-12-22 23:15:45 +08:00
|
|
|
wantDead: []*base.TaskMessage{m1, m2},
|
2019-12-12 11:56:19 +08:00
|
|
|
},
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
dead: []h.ZSetEntry{},
|
2019-12-12 11:56:19 +08:00
|
|
|
id: m1.ID,
|
|
|
|
score: t1.Unix(),
|
|
|
|
want: ErrTaskNotFound,
|
2019-12-22 23:15:45 +08:00
|
|
|
wantDead: []*base.TaskMessage{},
|
2019-12-12 11:56:19 +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.SeedDeadQueue(t, r.client, tc.dead)
|
2019-12-12 11:56:19 +08:00
|
|
|
|
|
|
|
got := r.DeleteDeadTask(tc.id, tc.score)
|
|
|
|
if got != tc.want {
|
|
|
|
t.Errorf("r.DeleteDeadTask(%v, %v) = %v, want %v", tc.id, tc.score, got, tc.want)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
gotDead := h.GetDeadMessages(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantDead, gotDead, h.SortMsgOpt); diff != "" {
|
2019-12-22 23:15:45 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.DeadQueue, diff)
|
2019-12-12 11:56:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteRetryTask(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-29 12:12:14 +08:00
|
|
|
m1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
m2 := h.NewTaskMessage("reindex", nil)
|
2019-12-12 11:56:19 +08:00
|
|
|
t1 := time.Now().Add(5 * time.Minute)
|
|
|
|
t2 := time.Now().Add(time.Hour)
|
|
|
|
|
|
|
|
tests := []struct {
|
2019-12-29 12:12:14 +08:00
|
|
|
retry []h.ZSetEntry
|
2019-12-12 11:56:19 +08:00
|
|
|
id xid.ID
|
|
|
|
score int64
|
|
|
|
want error
|
2019-12-22 23:15:45 +08:00
|
|
|
wantRetry []*base.TaskMessage
|
2019-12-12 11:56:19 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
retry: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(t1.Unix())},
|
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-12 11:56:19 +08:00
|
|
|
},
|
|
|
|
id: m1.ID,
|
|
|
|
score: t1.Unix(),
|
|
|
|
want: nil,
|
2019-12-22 23:15:45 +08:00
|
|
|
wantRetry: []*base.TaskMessage{m2},
|
2019-12-12 11:56:19 +08:00
|
|
|
},
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
retry: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(t1.Unix())},
|
2019-12-12 11:56:19 +08:00
|
|
|
},
|
|
|
|
id: m2.ID,
|
|
|
|
score: t2.Unix(),
|
|
|
|
want: ErrTaskNotFound,
|
2019-12-22 23:15:45 +08:00
|
|
|
wantRetry: []*base.TaskMessage{m1},
|
2019-12-12 11:56:19 +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.SeedRetryQueue(t, r.client, tc.retry)
|
2019-12-12 11:56:19 +08:00
|
|
|
|
|
|
|
got := r.DeleteRetryTask(tc.id, tc.score)
|
|
|
|
if got != tc.want {
|
|
|
|
t.Errorf("r.DeleteRetryTask(%v, %v) = %v, want %v", tc.id, tc.score, got, tc.want)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
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-12-12 11:56:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteScheduledTask(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-29 12:12:14 +08:00
|
|
|
m1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
m2 := h.NewTaskMessage("reindex", nil)
|
2019-12-12 11:56:19 +08:00
|
|
|
t1 := time.Now().Add(5 * time.Minute)
|
|
|
|
t2 := time.Now().Add(time.Hour)
|
|
|
|
|
|
|
|
tests := []struct {
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled []h.ZSetEntry
|
2019-12-12 11:56:19 +08:00
|
|
|
id xid.ID
|
|
|
|
score int64
|
|
|
|
want error
|
2019-12-22 23:15:45 +08:00
|
|
|
wantScheduled []*base.TaskMessage
|
2019-12-12 11:56:19 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(t1.Unix())},
|
|
|
|
{Msg: m2, Score: float64(t2.Unix())},
|
2019-12-12 11:56:19 +08:00
|
|
|
},
|
|
|
|
id: m1.ID,
|
|
|
|
score: t1.Unix(),
|
|
|
|
want: nil,
|
2019-12-22 23:15:45 +08:00
|
|
|
wantScheduled: []*base.TaskMessage{m2},
|
2019-12-12 11:56:19 +08:00
|
|
|
},
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(t1.Unix())},
|
2019-12-12 11:56:19 +08:00
|
|
|
},
|
|
|
|
id: m2.ID,
|
|
|
|
score: t2.Unix(),
|
|
|
|
want: ErrTaskNotFound,
|
2019-12-22 23:15:45 +08:00
|
|
|
wantScheduled: []*base.TaskMessage{m1},
|
2019-12-12 11:56:19 +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)
|
2019-12-12 11:56:19 +08:00
|
|
|
|
|
|
|
got := r.DeleteScheduledTask(tc.id, tc.score)
|
|
|
|
if got != tc.want {
|
|
|
|
t.Errorf("r.DeleteScheduledTask(%v, %v) = %v, want %v", tc.id, tc.score, got, tc.want)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
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-12 11:56:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-12 22:38:01 +08:00
|
|
|
|
|
|
|
func TestDeleteAllDeadTasks(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-29 12:12:14 +08:00
|
|
|
m1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
m2 := h.NewTaskMessage("reindex", nil)
|
|
|
|
m3 := h.NewTaskMessage("gen_thumbnail", nil)
|
2019-12-12 22:38:01 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2019-12-29 12:12:14 +08:00
|
|
|
dead []h.ZSetEntry
|
2019-12-22 23:15:45 +08:00
|
|
|
wantDead []*base.TaskMessage
|
2019-12-12 22:38:01 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
dead: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(time.Now().Unix())},
|
|
|
|
{Msg: m2, Score: float64(time.Now().Unix())},
|
|
|
|
{Msg: m3, Score: float64(time.Now().Unix())},
|
2019-12-13 11:49:41 +08:00
|
|
|
},
|
2019-12-22 23:15:45 +08:00
|
|
|
wantDead: []*base.TaskMessage{},
|
2019-12-12 22:38:01 +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.SeedDeadQueue(t, r.client, tc.dead)
|
2019-12-12 22:38:01 +08:00
|
|
|
|
|
|
|
err := r.DeleteAllDeadTasks()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("r.DeleteAllDeaadTasks = %v, want nil", err)
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
gotDead := h.GetDeadMessages(t, r.client)
|
|
|
|
if diff := cmp.Diff(tc.wantDead, gotDead, h.SortMsgOpt); diff != "" {
|
2019-12-22 23:15:45 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.DeadQueue, diff)
|
2019-12-12 22:38:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteAllRetryTasks(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-29 12:12:14 +08:00
|
|
|
m1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
m2 := h.NewTaskMessage("reindex", nil)
|
|
|
|
m3 := h.NewTaskMessage("gen_thumbnail", nil)
|
2019-12-12 22:38:01 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2019-12-29 12:12:14 +08:00
|
|
|
retry []h.ZSetEntry
|
2019-12-22 23:15:45 +08:00
|
|
|
wantRetry []*base.TaskMessage
|
2019-12-12 22:38:01 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
retry: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(time.Now().Unix())},
|
|
|
|
{Msg: m2, Score: float64(time.Now().Unix())},
|
|
|
|
{Msg: m3, Score: float64(time.Now().Unix())},
|
2019-12-13 11:49:41 +08:00
|
|
|
},
|
2019-12-22 23:15:45 +08:00
|
|
|
wantRetry: []*base.TaskMessage{},
|
2019-12-12 22:38:01 +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.SeedRetryQueue(t, r.client, tc.retry)
|
2019-12-12 22:38:01 +08:00
|
|
|
|
|
|
|
err := r.DeleteAllRetryTasks()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("r.DeleteAllDeaadTasks = %v, want nil", err)
|
|
|
|
}
|
|
|
|
|
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-12-12 22:38:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteAllScheduledTasks(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-29 12:12:14 +08:00
|
|
|
m1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
m2 := h.NewTaskMessage("reindex", nil)
|
|
|
|
m3 := h.NewTaskMessage("gen_thumbnail", nil)
|
2019-12-12 22:38:01 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled []h.ZSetEntry
|
2019-12-22 23:15:45 +08:00
|
|
|
wantScheduled []*base.TaskMessage
|
2019-12-12 22:38:01 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-29 12:12:14 +08:00
|
|
|
scheduled: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{Msg: m1, Score: float64(time.Now().Add(time.Minute).Unix())},
|
|
|
|
{Msg: m2, Score: float64(time.Now().Add(time.Minute).Unix())},
|
|
|
|
{Msg: m3, Score: float64(time.Now().Add(time.Minute).Unix())},
|
2019-12-13 11:49:41 +08:00
|
|
|
},
|
2019-12-22 23:15:45 +08:00
|
|
|
wantScheduled: []*base.TaskMessage{},
|
2019-12-12 22:38:01 +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)
|
2019-12-12 22:38:01 +08:00
|
|
|
|
|
|
|
err := r.DeleteAllScheduledTasks()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("r.DeleteAllDeaadTasks = %v, want nil", err)
|
|
|
|
}
|
|
|
|
|
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-12 22:38:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-13 22:50:03 +08:00
|
|
|
|
|
|
|
func TestRemoveQueue(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
m1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
m2 := h.NewTaskMessage("reindex", nil)
|
|
|
|
m3 := h.NewTaskMessage("gen_thumbnail", nil)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
enqueued map[string][]*base.TaskMessage
|
|
|
|
qname string // queue to remove
|
2020-01-13 23:03:07 +08:00
|
|
|
force bool
|
2020-01-13 22:50:03 +08:00
|
|
|
wantEnqueued map[string][]*base.TaskMessage
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
enqueued: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1},
|
|
|
|
"critical": {m2, m3},
|
|
|
|
"low": {},
|
|
|
|
},
|
|
|
|
qname: "low",
|
2020-01-13 23:03:07 +08:00
|
|
|
force: false,
|
2020-01-13 22:50:03 +08:00
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1},
|
|
|
|
"critical": {m2, m3},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
enqueued: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1},
|
|
|
|
"critical": {m2, m3},
|
|
|
|
"low": {},
|
|
|
|
},
|
|
|
|
qname: "critical",
|
2020-01-13 23:03:07 +08:00
|
|
|
force: true, // allow removing non-empty queue
|
2020-01-13 22:50:03 +08:00
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1},
|
|
|
|
"low": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r.client)
|
|
|
|
for qname, msgs := range tc.enqueued {
|
|
|
|
h.SeedEnqueuedQueue(t, r.client, msgs, qname)
|
|
|
|
}
|
|
|
|
|
2020-01-13 23:03:07 +08:00
|
|
|
err := r.RemoveQueue(tc.qname, tc.force)
|
2020-01-13 22:50:03 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("(*RDB).RemoveQueue(%q) = %v, want nil", tc.qname, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
qkey := base.QueueKey(tc.qname)
|
|
|
|
if r.client.SIsMember(base.AllQueues, qkey).Val() {
|
|
|
|
t.Errorf("%q is a member of %q", qkey, base.AllQueues)
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.client.LLen(qkey).Val() != 0 {
|
|
|
|
t.Errorf("queue %q is not empty", qkey)
|
|
|
|
}
|
|
|
|
|
|
|
|
for qname, want := range tc.wantEnqueued {
|
|
|
|
gotEnqueued := h.GetEnqueuedMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotEnqueued, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q; (-want,+got):\n%s", base.QueueKey(qname), diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-13 23:03:07 +08:00
|
|
|
|
|
|
|
func TestRemoveQueueError(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
m1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
m2 := h.NewTaskMessage("reindex", nil)
|
|
|
|
m3 := h.NewTaskMessage("gen_thumbnail", nil)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
enqueued map[string][]*base.TaskMessage
|
|
|
|
qname string // queue to remove
|
|
|
|
force bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "removing non-existent queue",
|
|
|
|
enqueued: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1},
|
|
|
|
"critical": {m2, m3},
|
|
|
|
"low": {},
|
|
|
|
},
|
|
|
|
qname: "nonexistent",
|
|
|
|
force: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "removing non-empty queue",
|
|
|
|
enqueued: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1},
|
|
|
|
"critical": {m2, m3},
|
|
|
|
"low": {},
|
|
|
|
},
|
|
|
|
qname: "critical",
|
|
|
|
force: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r.client)
|
|
|
|
for qname, msgs := range tc.enqueued {
|
|
|
|
h.SeedEnqueuedQueue(t, r.client, msgs, qname)
|
|
|
|
}
|
|
|
|
|
|
|
|
got := r.RemoveQueue(tc.qname, tc.force)
|
|
|
|
if got == nil {
|
|
|
|
t.Errorf("%s;(*RDB).RemoveQueue(%q) = nil, want error", tc.desc, tc.qname)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that nothing changed
|
|
|
|
for qname, want := range tc.enqueued {
|
|
|
|
gotEnqueued := h.GetEnqueuedMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotEnqueued, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("%s;mismatch found in %q; (-want,+got):\n%s", tc.desc, base.QueueKey(qname), diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-02 14:22:48 +08:00
|
|
|
|
|
|
|
func TestListProcesses(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
|
|
|
|
ps1 := &base.ProcessInfo{
|
|
|
|
Concurrency: 10,
|
2020-02-13 14:23:25 +08:00
|
|
|
Queues: map[string]int{"default": 1},
|
2020-02-02 14:22:48 +08:00
|
|
|
Host: "do.droplet1",
|
|
|
|
PID: 1234,
|
|
|
|
State: "running",
|
|
|
|
Started: time.Now().Add(-time.Hour),
|
|
|
|
ActiveWorkerCount: 5,
|
|
|
|
}
|
|
|
|
|
|
|
|
ps2 := &base.ProcessInfo{
|
|
|
|
Concurrency: 20,
|
2020-02-13 14:23:25 +08:00
|
|
|
Queues: map[string]int{"email": 1},
|
2020-02-02 14:22:48 +08:00
|
|
|
Host: "do.droplet2",
|
|
|
|
PID: 9876,
|
|
|
|
State: "stopped",
|
|
|
|
Started: time.Now().Add(-2 * time.Hour),
|
|
|
|
ActiveWorkerCount: 20,
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
processes []*base.ProcessInfo
|
|
|
|
}{
|
|
|
|
{processes: []*base.ProcessInfo{}},
|
|
|
|
{processes: []*base.ProcessInfo{ps1}},
|
|
|
|
{processes: []*base.ProcessInfo{ps1, ps2}},
|
|
|
|
}
|
|
|
|
|
|
|
|
ignoreOpt := cmpopts.IgnoreUnexported(base.ProcessInfo{})
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r.client)
|
|
|
|
|
|
|
|
for _, ps := range tc.processes {
|
|
|
|
if err := r.WriteProcessInfo(ps, 5*time.Second); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
got, err := r.ListProcesses()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("r.ListProcesses returned an error: %v", err)
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(tc.processes, got, h.SortProcessInfoOpt, ignoreOpt); diff != "" {
|
|
|
|
t.Errorf("r.ListProcesses returned %v, want %v; (-want,+got)\n%s",
|
|
|
|
got, tc.processes, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|