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
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-01-08 13:22:19 +08:00
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2020-07-02 21:21:20 +08:00
|
|
|
"github.com/google/uuid"
|
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-05 12:30:37 +08:00
|
|
|
)
|
|
|
|
|
2020-08-11 20:35:06 +08:00
|
|
|
func TestAllQueues(t *testing.T) {
|
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-08-11 20:35:06 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
queues []string
|
|
|
|
}{
|
|
|
|
{queues: []string{"default"}},
|
|
|
|
{queues: []string{"custom1", "custom2"}},
|
|
|
|
{queues: []string{"default", "custom1", "custom2"}},
|
|
|
|
{queues: []string{}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r.client)
|
2020-08-13 21:54:32 +08:00
|
|
|
for _, qname := range tc.queues {
|
|
|
|
if err := r.client.SAdd(base.AllQueues, qname).Err(); err != nil {
|
|
|
|
t.Fatalf("could not initialize all queue set: %v", err)
|
|
|
|
}
|
2020-08-11 20:35:06 +08:00
|
|
|
}
|
|
|
|
got, err := r.AllQueues()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("AllQueues() returned an error: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2020-08-13 21:54:32 +08:00
|
|
|
if diff := cmp.Diff(tc.queues, got, h.SortStringSliceOpt); diff != "" {
|
2020-08-11 20:35:06 +08:00
|
|
|
t.Errorf("AllQueues() = %v, want %v; (-want, +got)\n%s", got, tc.queues, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 12:30:37 +08:00
|
|
|
func TestCurrentStats(t *testing.T) {
|
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-03-21 04:42:13 +08:00
|
|
|
m1 := h.NewTaskMessage("send_email", h.JSON(map[string]interface{}{"subject": "hello"}))
|
2019-12-29 12:12:14 +08:00
|
|
|
m2 := h.NewTaskMessage("reindex", nil)
|
2021-03-21 04:42:13 +08:00
|
|
|
m3 := h.NewTaskMessage("gen_thumbnail", h.JSON(map[string]interface{}{"src": "some/path/to/img"}))
|
2019-12-29 12:12:14 +08:00
|
|
|
m4 := h.NewTaskMessage("sync", nil)
|
2020-08-11 21:28:12 +08:00
|
|
|
m5 := h.NewTaskMessageWithQueue("important_notification", nil, "critical")
|
|
|
|
m6 := h.NewTaskMessageWithQueue("minor_notification", nil, "low")
|
2019-12-26 12:17:00 +08:00
|
|
|
now := time.Now()
|
|
|
|
|
2019-12-05 12:30:37 +08:00
|
|
|
tests := []struct {
|
2020-09-05 22:03:43 +08:00
|
|
|
pending map[string][]*base.TaskMessage
|
2020-08-11 21:28:12 +08:00
|
|
|
inProgress map[string][]*base.TaskMessage
|
|
|
|
scheduled map[string][]base.Z
|
|
|
|
retry map[string][]base.Z
|
2021-01-13 03:01:21 +08:00
|
|
|
archived map[string][]base.Z
|
2020-08-11 21:28:12 +08:00
|
|
|
processed map[string]int
|
|
|
|
failed map[string]int
|
2020-06-05 21:42:27 +08:00
|
|
|
paused []string
|
2020-08-11 21:28:12 +08:00
|
|
|
qname string
|
2019-12-05 12:30:37 +08:00
|
|
|
want *Stats
|
|
|
|
}{
|
|
|
|
{
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-08-11 21:28:12 +08:00
|
|
|
"default": {m1},
|
|
|
|
"critical": {m5},
|
|
|
|
"low": {m6},
|
|
|
|
},
|
|
|
|
inProgress: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m2},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
|
|
|
},
|
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m3, Score: now.Add(time.Hour).Unix()},
|
|
|
|
{Message: m4, Score: now.Unix()},
|
|
|
|
},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
|
|
|
},
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-11 21:28:12 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
|
|
|
},
|
|
|
|
processed: map[string]int{
|
|
|
|
"default": 120,
|
|
|
|
"critical": 100,
|
|
|
|
"low": 50,
|
|
|
|
},
|
|
|
|
failed: map[string]int{
|
|
|
|
"default": 2,
|
|
|
|
"critical": 0,
|
|
|
|
"low": 1,
|
|
|
|
},
|
|
|
|
paused: []string{},
|
|
|
|
qname: "default",
|
2019-12-05 12:30:37 +08:00
|
|
|
want: &Stats{
|
2020-09-06 03:43:15 +08:00
|
|
|
Queue: "default",
|
|
|
|
Paused: false,
|
|
|
|
Size: 4,
|
|
|
|
Pending: 1,
|
|
|
|
Active: 1,
|
|
|
|
Scheduled: 2,
|
|
|
|
Retry: 0,
|
2021-01-13 03:01:21 +08:00
|
|
|
Archived: 0,
|
2020-09-06 03:43:15 +08:00
|
|
|
Processed: 120,
|
|
|
|
Failed: 2,
|
|
|
|
Timestamp: now,
|
2019-12-05 12:30:37 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-08-11 21:28:12 +08:00
|
|
|
"default": {m1},
|
|
|
|
"critical": {m5},
|
|
|
|
"low": {m6},
|
|
|
|
},
|
|
|
|
inProgress: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m2},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
|
|
|
},
|
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m3, Score: now.Add(time.Hour).Unix()},
|
|
|
|
{Message: m4, Score: now.Unix()},
|
2020-06-05 21:42:27 +08:00
|
|
|
},
|
2020-08-11 21:28:12 +08:00
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
2020-06-05 21:42:27 +08:00
|
|
|
},
|
2020-08-11 21:28:12 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-11 21:28:12 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
|
|
|
},
|
|
|
|
processed: map[string]int{
|
|
|
|
"default": 120,
|
|
|
|
"critical": 100,
|
|
|
|
"low": 50,
|
|
|
|
},
|
|
|
|
failed: map[string]int{
|
|
|
|
"default": 2,
|
|
|
|
"critical": 0,
|
|
|
|
"low": 1,
|
|
|
|
},
|
|
|
|
paused: []string{"critical", "low"},
|
|
|
|
qname: "critical",
|
2020-06-05 21:42:27 +08:00
|
|
|
want: &Stats{
|
2020-09-06 03:43:15 +08:00
|
|
|
Queue: "critical",
|
|
|
|
Paused: true,
|
|
|
|
Size: 1,
|
|
|
|
Pending: 1,
|
|
|
|
Active: 0,
|
|
|
|
Scheduled: 0,
|
|
|
|
Retry: 0,
|
2021-01-13 03:01:21 +08:00
|
|
|
Archived: 0,
|
2020-09-06 03:43:15 +08:00
|
|
|
Processed: 100,
|
|
|
|
Failed: 0,
|
|
|
|
Timestamp: now,
|
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-06-05 21:42:27 +08:00
|
|
|
for _, qname := range tc.paused {
|
|
|
|
if err := r.Pause(qname); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2020-09-05 22:03:43 +08:00
|
|
|
h.SeedAllPendingQueues(t, r.client, tc.pending)
|
2020-09-06 03:43:15 +08:00
|
|
|
h.SeedAllActiveQueues(t, r.client, tc.inProgress)
|
2020-08-11 21:28:12 +08:00
|
|
|
h.SeedAllScheduledQueues(t, r.client, tc.scheduled)
|
|
|
|
h.SeedAllRetryQueues(t, r.client, tc.retry)
|
2021-01-13 03:01:21 +08:00
|
|
|
h.SeedAllArchivedQueues(t, r.client, tc.archived)
|
2020-08-11 21:28:12 +08:00
|
|
|
for qname, n := range tc.processed {
|
|
|
|
processedKey := base.ProcessedKey(qname, now)
|
|
|
|
r.client.Set(processedKey, n, 0)
|
2020-01-09 23:01:44 +08:00
|
|
|
}
|
2020-08-11 21:28:12 +08:00
|
|
|
for qname, n := range tc.failed {
|
2020-08-13 21:54:32 +08:00
|
|
|
failedKey := base.FailedKey(qname, now)
|
2020-08-11 21:28:12 +08:00
|
|
|
r.client.Set(failedKey, n, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
got, err := r.CurrentStats(tc.qname)
|
2019-12-05 12:30:37 +08:00
|
|
|
if err != nil {
|
2020-08-11 21:28:12 +08:00
|
|
|
t.Errorf("r.CurrentStats(%q) = %v, %v, want %v, nil", tc.qname, got, err, tc.want)
|
2019-12-05 12:30:37 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-01-27 10:36:45 +08:00
|
|
|
ignoreMemUsg := cmpopts.IgnoreFields(Stats{}, "MemoryUsage")
|
|
|
|
if diff := cmp.Diff(tc.want, got, timeCmpOpt, ignoreMemUsg); diff != "" {
|
2020-08-11 21:28:12 +08:00
|
|
|
t.Errorf("r.CurrentStats(%q) = %v, %v, want %v, nil; (-want, +got)\n%s", tc.qname, got, err, tc.want, diff)
|
2019-12-05 12:30:37 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2019-12-23 01:09:57 +08:00
|
|
|
}
|
|
|
|
|
2020-08-11 21:28:12 +08:00
|
|
|
func TestCurrentStatsWithNonExistentQueue(t *testing.T) {
|
2019-12-26 13:29:20 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2019-12-26 13:29:20 +08:00
|
|
|
|
2020-08-11 21:28:12 +08:00
|
|
|
qname := "non-existent"
|
|
|
|
got, err := r.CurrentStats(qname)
|
2020-08-13 21:54:32 +08:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("r.CurrentStats(%q) = %v, %v, want nil, %v", qname, got, err, &ErrQueueNotFound{qname})
|
2019-12-26 13:29:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 01:41:05 +08:00
|
|
|
func TestHistoricalStats(t *testing.T) {
|
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-01-05 01:41:05 +08:00
|
|
|
now := time.Now().UTC()
|
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-13 21:54:32 +08:00
|
|
|
qname string // queue of interest
|
|
|
|
n int // number of days
|
2020-01-05 01:41:05 +08:00
|
|
|
}{
|
2020-08-12 20:30:59 +08:00
|
|
|
{"default", 90},
|
|
|
|
{"custom", 7},
|
2020-08-21 21:00:49 +08:00
|
|
|
{"default", 1},
|
2020-01-05 01:41:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r.client)
|
|
|
|
|
2020-08-21 21:00:49 +08:00
|
|
|
r.client.SAdd(base.AllQueues, tc.qname)
|
2020-01-05 01:41:05 +08:00
|
|
|
// populate last n days data
|
|
|
|
for i := 0; i < tc.n; i++ {
|
|
|
|
ts := now.Add(-time.Duration(i) * 24 * time.Hour)
|
2020-08-12 20:30:59 +08:00
|
|
|
processedKey := base.ProcessedKey(tc.qname, ts)
|
2020-08-13 21:54:32 +08:00
|
|
|
failedKey := base.FailedKey(tc.qname, ts)
|
2020-01-05 01:41:05 +08:00
|
|
|
r.client.Set(processedKey, (i+1)*1000, 0)
|
|
|
|
r.client.Set(failedKey, (i+1)*10, 0)
|
|
|
|
}
|
|
|
|
|
2020-08-12 20:30:59 +08:00
|
|
|
got, err := r.HistoricalStats(tc.qname, tc.n)
|
2020-01-05 01:41:05 +08:00
|
|
|
if err != nil {
|
2020-08-12 20:30:59 +08:00
|
|
|
t.Errorf("RDB.HistoricalStats(%q, %d) returned error: %v", tc.qname, tc.n, err)
|
2020-01-05 01:41:05 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(got) != tc.n {
|
2020-08-12 20:30:59 +08:00
|
|
|
t.Errorf("RDB.HistorycalStats(%q, %d) returned %d daily stats, want %d", tc.qname, tc.n, len(got), tc.n)
|
2020-01-05 01:41:05 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < tc.n; i++ {
|
|
|
|
want := &DailyStats{
|
2020-08-12 20:30:59 +08:00
|
|
|
Queue: tc.qname,
|
2020-01-05 01:41:05 +08:00
|
|
|
Processed: (i + 1) * 1000,
|
|
|
|
Failed: (i + 1) * 10,
|
|
|
|
Time: now.Add(-time.Duration(i) * 24 * time.Hour),
|
|
|
|
}
|
2020-09-10 10:17:56 +08:00
|
|
|
// Allow 2 seconds difference in timestamp.
|
|
|
|
cmpOpt := cmpopts.EquateApproxTime(2 * time.Second)
|
2020-08-29 21:54:08 +08:00
|
|
|
if diff := cmp.Diff(want, got[i], cmpOpt); diff != "" {
|
2020-08-12 20:30:59 +08:00
|
|
|
t.Errorf("RDB.HistoricalStats for the last %d days; got %+v, want %+v; (-want,+got):\n%s", i, got[i], want, diff)
|
2020-01-05 01:41:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-23 01:09:57 +08:00
|
|
|
func TestRedisInfo(t *testing.T) {
|
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2019-12-23 01:09:57 +08:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-05 22:03:43 +08:00
|
|
|
func TestListPending(t *testing.T) {
|
2019-12-05 12:30:37 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2019-12-05 12:30:37 +08:00
|
|
|
|
2021-03-21 04:42:13 +08:00
|
|
|
m1 := h.NewTaskMessage("send_email", h.JSON(map[string]interface{}{"subject": "hello"}))
|
2019-12-29 12:12:14 +08:00
|
|
|
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-07-13 21:29:41 +08:00
|
|
|
|
2019-12-05 12:30:37 +08:00
|
|
|
tests := []struct {
|
2020-09-05 22:03:43 +08:00
|
|
|
pending map[string][]*base.TaskMessage
|
|
|
|
qname string
|
|
|
|
want []*base.TaskMessage
|
2019-12-05 12:30:37 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-01-10 22:56:51 +08:00
|
|
|
base.DefaultQueueName: {m1, m2},
|
|
|
|
},
|
2020-01-24 23:19:58 +08:00
|
|
|
qname: base.DefaultQueueName,
|
2020-07-13 21:29:41 +08:00
|
|
|
want: []*base.TaskMessage{m1, m2},
|
2020-01-10 22:56:51 +08:00
|
|
|
},
|
|
|
|
{
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-07-13 21:29:41 +08:00
|
|
|
base.DefaultQueueName: nil,
|
2020-01-10 22:56:51 +08:00
|
|
|
},
|
2020-01-24 23:19:58 +08:00
|
|
|
qname: base.DefaultQueueName,
|
2020-07-13 21:29:41 +08:00
|
|
|
want: []*base.TaskMessage(nil),
|
2019-12-05 12:30:37 +08:00
|
|
|
},
|
|
|
|
{
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-01-10 22:56:51 +08:00
|
|
|
base.DefaultQueueName: {m1, m2},
|
|
|
|
"critical": {m3},
|
|
|
|
"low": {m4},
|
|
|
|
},
|
2020-01-24 23:19:58 +08:00
|
|
|
qname: base.DefaultQueueName,
|
2020-07-13 21:29:41 +08:00
|
|
|
want: []*base.TaskMessage{m1, m2},
|
2020-01-11 13:32:15 +08:00
|
|
|
},
|
|
|
|
{
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-01-11 13:32:15 +08:00
|
|
|
base.DefaultQueueName: {m1, m2},
|
|
|
|
"critical": {m3},
|
|
|
|
"low": {m4},
|
|
|
|
},
|
2020-01-24 23:19:58 +08:00
|
|
|
qname: "critical",
|
2020-07-13 21:29:41 +08:00
|
|
|
want: []*base.TaskMessage{m3},
|
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-09-05 22:03:43 +08:00
|
|
|
h.SeedAllPendingQueues(t, r.client, tc.pending)
|
2019-12-13 11:49:41 +08:00
|
|
|
|
2020-09-05 22:03:43 +08:00
|
|
|
got, err := r.ListPending(tc.qname, Pagination{Size: 20, Page: 0})
|
|
|
|
op := fmt.Sprintf("r.ListPending(%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
|
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
if diff := cmp.Diff(tc.want, got); 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-07-13 21:29:41 +08:00
|
|
|
|
2020-09-05 22:03:43 +08:00
|
|
|
func TestListPendingPagination(t *testing.T) {
|
2020-01-24 23:19:58 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-01-24 23:19:58 +08:00
|
|
|
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
|
2020-09-05 22:03:43 +08:00
|
|
|
h.SeedPendingQueue(t, r.client, msgs, "default")
|
2020-01-24 23:19:58 +08:00
|
|
|
|
|
|
|
msgs = []*base.TaskMessage(nil) // empty list
|
|
|
|
for i := 0; i < 100; i++ {
|
2021-03-13 08:23:08 +08:00
|
|
|
msg := h.NewTaskMessageWithQueue(fmt.Sprintf("custom %d", i), nil, "custom")
|
2020-01-24 23:19:58 +08:00
|
|
|
msgs = append(msgs, msg)
|
|
|
|
}
|
|
|
|
// create 100 tasks in custom queue
|
2020-09-05 22:03:43 +08:00
|
|
|
h.SeedPendingQueue(t, r.client, msgs, "custom")
|
2020-01-24 23:19:58 +08:00
|
|
|
|
|
|
|
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 {
|
2020-09-05 22:03:43 +08:00
|
|
|
got, err := r.ListPending(tc.qname, Pagination{Size: tc.size, Page: tc.page})
|
|
|
|
op := fmt.Sprintf("r.ListPending(%q, Pagination{Size: %d, Page: %d})", tc.qname, tc.size, tc.page)
|
2020-01-24 23:19:58 +08:00
|
|
|
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
|
|
|
|
2020-09-06 03:43:15 +08:00
|
|
|
func TestListActive(t *testing.T) {
|
2019-12-05 12:30:37 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2019-12-05 12:30:37 +08:00
|
|
|
|
2020-07-13 21:29:41 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
2020-08-12 21:18:15 +08:00
|
|
|
m3 := h.NewTaskMessageWithQueue("task2", nil, "critical")
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task2", nil, "low")
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2019-12-05 12:30:37 +08:00
|
|
|
tests := []struct {
|
2020-08-12 21:18:15 +08:00
|
|
|
inProgress map[string][]*base.TaskMessage
|
|
|
|
qname string
|
|
|
|
want []*base.TaskMessage
|
2019-12-05 12:30:37 +08:00
|
|
|
}{
|
2020-08-12 21:18:15 +08:00
|
|
|
{
|
|
|
|
inProgress: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
|
|
|
"critical": {m3},
|
|
|
|
"low": {m4},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: []*base.TaskMessage{m1, m2},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inProgress: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2020-08-13 21:54:32 +08:00
|
|
|
want: []*base.TaskMessage(nil),
|
2020-08-12 21:18:15 +08:00
|
|
|
},
|
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-09-06 03:43:15 +08:00
|
|
|
h.SeedAllActiveQueues(t, r.client, tc.inProgress)
|
2019-12-13 11:49:41 +08:00
|
|
|
|
2020-09-06 03:43:15 +08:00
|
|
|
got, err := r.ListActive(tc.qname, Pagination{Size: 20, Page: 0})
|
|
|
|
op := fmt.Sprintf("r.ListActive(%q, Pagination{Size: 20, Page: 0})", tc.qname)
|
2019-12-05 12:30:37 +08:00
|
|
|
if err != nil {
|
2020-07-13 21:29:41 +08:00
|
|
|
t.Errorf("%s = %v, %v, want %v, nil", op, got, err, tc.inProgress)
|
2019-12-05 12:30:37 +08:00
|
|
|
continue
|
|
|
|
}
|
2020-08-12 21:18:15 +08:00
|
|
|
if diff := cmp.Diff(tc.want, got); diff != "" {
|
|
|
|
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-09-06 03:43:15 +08:00
|
|
|
func TestListActivePagination(t *testing.T) {
|
2020-01-24 23:19:58 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-01-24 23:19:58 +08:00
|
|
|
var msgs []*base.TaskMessage
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
msg := h.NewTaskMessage(fmt.Sprintf("task %d", i), nil)
|
|
|
|
msgs = append(msgs, msg)
|
|
|
|
}
|
2020-09-06 03:43:15 +08:00
|
|
|
h.SeedActiveQueue(t, r.client, msgs, "default")
|
2020-01-24 23:19:58 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
2020-08-12 21:18:15 +08:00
|
|
|
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
|
|
|
|
}{
|
2020-08-12 21:18:15 +08:00
|
|
|
{"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, "", ""},
|
2020-01-24 23:19:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2020-09-06 03:43:15 +08:00
|
|
|
got, err := r.ListActive(tc.qname, Pagination{Size: tc.size, Page: tc.page})
|
|
|
|
op := fmt.Sprintf("r.ListActive(%q, Pagination{Size: %d, Page: %d})", tc.qname, tc.size, tc.page)
|
2020-01-24 23:19:58 +08:00
|
|
|
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)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-07-13 21:29:41 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessage("task3", nil)
|
2020-08-12 21:18:15 +08:00
|
|
|
m4 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
2019-12-05 12:30:37 +08:00
|
|
|
p1 := time.Now().Add(30 * time.Minute)
|
|
|
|
p2 := time.Now().Add(24 * time.Hour)
|
2020-07-13 21:29:41 +08:00
|
|
|
p3 := time.Now().Add(5 * time.Minute)
|
2020-08-12 21:18:15 +08:00
|
|
|
p4 := time.Now().Add(2 * time.Minute)
|
2019-12-05 12:30:37 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-12 21:18:15 +08:00
|
|
|
scheduled map[string][]base.Z
|
|
|
|
qname string
|
2020-07-13 21:29:41 +08:00
|
|
|
want []base.Z
|
2019-12-05 12:30:37 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-12 21:18:15 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: p1.Unix()},
|
|
|
|
{Message: m2, Score: p2.Unix()},
|
|
|
|
{Message: m3, Score: p3.Unix()},
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
{Message: m4, Score: p4.Unix()},
|
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
2020-08-12 21:18:15 +08:00
|
|
|
qname: "default",
|
2020-07-13 21:29:41 +08:00
|
|
|
// should be sorted by score in ascending order
|
|
|
|
want: []base.Z{
|
|
|
|
{Message: m3, Score: p3.Unix()},
|
|
|
|
{Message: m1, Score: p1.Unix()},
|
|
|
|
{Message: m2, Score: p2.Unix()},
|
2019-12-05 12:30:37 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-12 21:18:15 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: p1.Unix()},
|
|
|
|
{Message: m2, Score: p2.Unix()},
|
|
|
|
{Message: m3, Score: p3.Unix()},
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
{Message: m4, Score: p4.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
|
|
|
want: []base.Z{
|
|
|
|
{Message: m4, Score: p4.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: []base.Z(nil),
|
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-08-12 21:18:15 +08:00
|
|
|
h.SeedAllScheduledQueues(t, r.client, tc.scheduled)
|
2019-12-05 12:30:37 +08:00
|
|
|
|
2020-08-12 21:18:15 +08:00
|
|
|
got, err := r.ListScheduled(tc.qname, Pagination{Size: 20, Page: 0})
|
|
|
|
op := fmt.Sprintf("r.ListScheduled(%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
|
|
|
|
}
|
2020-08-29 21:54:08 +08:00
|
|
|
if diff := cmp.Diff(tc.want, got, zScoreCmpOpt); 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)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-01-24 23:19:58 +08:00
|
|
|
// 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-08-12 21:18:15 +08:00
|
|
|
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
|
|
|
|
}{
|
2020-08-12 21:18:15 +08:00
|
|
|
{"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, "", ""},
|
2020-01-24 23:19:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2020-08-12 21:18:15 +08:00
|
|
|
got, err := r.ListScheduled(tc.qname, Pagination{Size: tc.size, Page: tc.page})
|
|
|
|
op := fmt.Sprintf("r.ListScheduled(%q, Pagination{Size: %d, Page: %d})", tc.qname, tc.size, tc.page)
|
2020-01-24 23:19:58 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-13 21:29:41 +08:00
|
|
|
first := got[0].Message
|
2020-01-24 23:19:58 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-07-13 21:29:41 +08:00
|
|
|
last := got[len(got)-1].Message
|
2020-01-24 23:19:58 +08:00
|
|
|
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)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2019-12-22 23:15:45 +08:00
|
|
|
m1 := &base.TaskMessage{
|
2020-07-02 21:21:20 +08:00
|
|
|
ID: uuid.New(),
|
2020-08-12 21:18:15 +08:00
|
|
|
Type: "task1",
|
2019-12-05 12:30:37 +08:00
|
|
|
Queue: "default",
|
2020-08-12 21:18:15 +08:00
|
|
|
Payload: nil,
|
|
|
|
ErrorMsg: "some error occurred",
|
2019-12-05 12:30:37 +08:00
|
|
|
Retry: 25,
|
|
|
|
Retried: 10,
|
|
|
|
}
|
2019-12-22 23:15:45 +08:00
|
|
|
m2 := &base.TaskMessage{
|
2020-07-02 21:21:20 +08:00
|
|
|
ID: uuid.New(),
|
2020-08-12 21:18:15 +08:00
|
|
|
Type: "task2",
|
2019-12-05 12:30:37 +08:00
|
|
|
Queue: "default",
|
|
|
|
Payload: nil,
|
2020-08-12 21:18:15 +08:00
|
|
|
ErrorMsg: "some error occurred",
|
2019-12-05 12:30:37 +08:00
|
|
|
Retry: 25,
|
|
|
|
Retried: 2,
|
|
|
|
}
|
2020-08-12 21:18:15 +08:00
|
|
|
m3 := &base.TaskMessage{
|
|
|
|
ID: uuid.New(),
|
|
|
|
Type: "task3",
|
|
|
|
Queue: "custom",
|
|
|
|
Payload: nil,
|
|
|
|
ErrorMsg: "some error occurred",
|
|
|
|
Retry: 25,
|
|
|
|
Retried: 3,
|
|
|
|
}
|
2019-12-05 12:30:37 +08:00
|
|
|
p1 := time.Now().Add(5 * time.Minute)
|
|
|
|
p2 := time.Now().Add(24 * time.Hour)
|
2020-08-12 21:18:15 +08:00
|
|
|
p3 := time.Now().Add(24 * time.Hour)
|
2019-12-05 12:30:37 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-12 21:18:15 +08:00
|
|
|
retry map[string][]base.Z
|
|
|
|
qname string
|
2020-07-13 21:29:41 +08:00
|
|
|
want []base.Z
|
2019-12-05 12:30:37 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-12 21:18:15 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: p1.Unix()},
|
|
|
|
{Message: m2, Score: p2.Unix()},
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
{Message: m3, Score: p3.Unix()},
|
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
2020-08-12 21:18:15 +08:00
|
|
|
qname: "default",
|
2020-07-13 21:29:41 +08:00
|
|
|
want: []base.Z{
|
|
|
|
{Message: m1, Score: p1.Unix()},
|
|
|
|
{Message: m2, Score: p2.Unix()},
|
2019-12-05 12:30:37 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-12 21:18:15 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: p1.Unix()},
|
|
|
|
{Message: m2, Score: p2.Unix()},
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
{Message: m3, Score: p3.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
|
|
|
want: []base.Z{
|
|
|
|
{Message: m3, Score: p3.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2020-07-13 21:29:41 +08:00
|
|
|
want: []base.Z(nil),
|
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-08-12 21:18:15 +08:00
|
|
|
h.SeedAllRetryQueues(t, r.client, tc.retry)
|
2019-12-05 12:30:37 +08:00
|
|
|
|
2020-08-13 21:54:32 +08:00
|
|
|
got, err := r.ListRetry(tc.qname, Pagination{Size: 20, Page: 0})
|
2020-08-12 21:18:15 +08:00
|
|
|
op := fmt.Sprintf("r.ListRetry(%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
|
|
|
|
}
|
2020-08-29 21:54:08 +08:00
|
|
|
if diff := cmp.Diff(tc.want, got, zScoreCmpOpt); diff != "" {
|
2020-07-13 21:29:41 +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)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-01-24 23:19:58 +08:00
|
|
|
// create 100 tasks with an increasing number of wait time.
|
2020-06-08 04:04:27 +08:00
|
|
|
now := time.Now()
|
2020-07-13 21:29:41 +08:00
|
|
|
var seed []base.Z
|
2020-01-24 23:19:58 +08:00
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
msg := h.NewTaskMessage(fmt.Sprintf("task %d", i), nil)
|
2020-06-08 04:04:27 +08:00
|
|
|
processAt := now.Add(time.Duration(i) * time.Second)
|
2020-07-13 21:29:41 +08:00
|
|
|
seed = append(seed, base.Z{Message: msg, Score: processAt.Unix()})
|
2020-01-24 23:19:58 +08:00
|
|
|
}
|
2020-08-12 21:18:15 +08:00
|
|
|
h.SeedRetryQueue(t, r.client, seed, "default")
|
2020-01-24 23:19:58 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
2020-08-12 21:18:15 +08:00
|
|
|
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
|
|
|
|
}{
|
2020-08-12 21:18:15 +08:00
|
|
|
{"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, "", ""},
|
2020-01-24 23:19:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2020-08-12 21:18:15 +08:00
|
|
|
got, err := r.ListRetry(tc.qname, Pagination{Size: tc.size, Page: tc.page})
|
|
|
|
op := fmt.Sprintf("r.ListRetry(%q, Pagination{Size: %d, Page: %d})",
|
|
|
|
tc.qname, tc.size, tc.page)
|
2020-01-24 23:19:58 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s; %s returned error %v", tc.desc, op, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(got) != tc.wantSize {
|
2020-07-13 21:29:41 +08:00
|
|
|
t.Errorf("%s; %s returned list of size %d, want %d",
|
|
|
|
tc.desc, op, len(got), tc.wantSize)
|
2020-01-24 23:19:58 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.wantSize == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-07-13 21:29:41 +08:00
|
|
|
first := got[0].Message
|
2020-01-24 23:19:58 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-07-13 21:29:41 +08:00
|
|
|
last := got[len(got)-1].Message
|
2020-01-24 23:19:58 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 08:23:08 +08:00
|
|
|
func TestListArchived(t *testing.T) {
|
2019-12-05 12:30:37 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2019-12-22 23:15:45 +08:00
|
|
|
m1 := &base.TaskMessage{
|
2020-07-02 21:21:20 +08:00
|
|
|
ID: uuid.New(),
|
2020-08-12 21:18:15 +08:00
|
|
|
Type: "task1",
|
2019-12-05 12:30:37 +08:00
|
|
|
Queue: "default",
|
2020-08-12 21:18:15 +08:00
|
|
|
Payload: nil,
|
|
|
|
ErrorMsg: "some error occurred",
|
2019-12-05 12:30:37 +08:00
|
|
|
}
|
2019-12-22 23:15:45 +08:00
|
|
|
m2 := &base.TaskMessage{
|
2020-07-02 21:21:20 +08:00
|
|
|
ID: uuid.New(),
|
2020-08-12 21:18:15 +08:00
|
|
|
Type: "task2",
|
2019-12-05 12:30:37 +08:00
|
|
|
Queue: "default",
|
|
|
|
Payload: nil,
|
2020-08-12 21:18:15 +08:00
|
|
|
ErrorMsg: "some error occurred",
|
|
|
|
}
|
|
|
|
m3 := &base.TaskMessage{
|
|
|
|
ID: uuid.New(),
|
|
|
|
Type: "task3",
|
|
|
|
Queue: "custom",
|
|
|
|
Payload: nil,
|
|
|
|
ErrorMsg: "some error occurred",
|
2019-12-05 12:30:37 +08:00
|
|
|
}
|
|
|
|
f1 := time.Now().Add(-5 * time.Minute)
|
|
|
|
f2 := time.Now().Add(-24 * time.Hour)
|
2020-08-12 21:18:15 +08:00
|
|
|
f3 := time.Now().Add(-4 * time.Hour)
|
2019-12-05 12:30:37 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2021-01-13 03:01:21 +08:00
|
|
|
archived map[string][]base.Z
|
|
|
|
qname string
|
|
|
|
want []base.Z
|
2019-12-05 12:30:37 +08:00
|
|
|
}{
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-12 21:18:15 +08:00
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: f1.Unix()},
|
|
|
|
{Message: m2, Score: f2.Unix()},
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
{Message: m3, Score: f3.Unix()},
|
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
2020-08-12 21:18:15 +08:00
|
|
|
qname: "default",
|
2020-07-13 21:29:41 +08:00
|
|
|
want: []base.Z{
|
|
|
|
{Message: m2, Score: f2.Unix()}, // FIXME: shouldn't be sorted in the other order?
|
|
|
|
{Message: m1, Score: f1.Unix()},
|
2019-12-05 12:30:37 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-12 21:18:15 +08:00
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: f1.Unix()},
|
|
|
|
{Message: m2, Score: f2.Unix()},
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
{Message: m3, Score: f3.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
|
|
|
want: []base.Z{
|
|
|
|
{Message: m3, Score: f3.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-12 21:18:15 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: []base.Z(nil),
|
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
|
2021-01-13 03:01:21 +08:00
|
|
|
h.SeedAllArchivedQueues(t, r.client, tc.archived)
|
2019-12-05 12:30:37 +08:00
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
got, err := r.ListArchived(tc.qname, Pagination{Size: 20, Page: 0})
|
2020-08-12 21:18:15 +08:00
|
|
|
op := fmt.Sprintf("r.ListDead(%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
|
|
|
|
}
|
2020-08-29 21:54:08 +08:00
|
|
|
if diff := cmp.Diff(tc.want, got, zScoreCmpOpt); diff != "" {
|
2020-07-13 21:29:41 +08:00
|
|
|
t.Errorf("%s = %v, %v, want %v, nil; (-want, +got)\n%s",
|
|
|
|
op, got, err, tc.want, diff)
|
2020-01-24 23:19:58 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 08:23:08 +08:00
|
|
|
func TestListArchivedPagination(t *testing.T) {
|
2020-01-24 23:19:58 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-07-13 21:29:41 +08:00
|
|
|
var entries []base.Z
|
2020-01-24 23:19:58 +08:00
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
msg := h.NewTaskMessage(fmt.Sprintf("task %d", i), nil)
|
2020-07-13 21:29:41 +08:00
|
|
|
entries = append(entries, base.Z{Message: msg, Score: int64(i)})
|
2020-01-24 23:19:58 +08:00
|
|
|
}
|
2021-01-13 03:01:21 +08:00
|
|
|
h.SeedArchivedQueue(t, r.client, entries, "default")
|
2020-01-24 23:19:58 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
2020-08-12 21:18:15 +08:00
|
|
|
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
|
|
|
|
}{
|
2020-08-12 21:18:15 +08:00
|
|
|
{"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, "", ""},
|
2020-01-24 23:19:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-13 03:01:21 +08:00
|
|
|
got, err := r.ListArchived(tc.qname, Pagination{Size: tc.size, Page: tc.page})
|
2020-07-13 21:29:41 +08:00
|
|
|
op := fmt.Sprintf("r.ListDead(Pagination{Size: %d, Page: %d})",
|
|
|
|
tc.size, tc.page)
|
2020-01-24 23:19:58 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s; %s returned error %v", tc.desc, op, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(got) != tc.wantSize {
|
2020-07-13 21:29:41 +08:00
|
|
|
t.Errorf("%s; %s returned list of size %d, want %d",
|
|
|
|
tc.desc, op, len(got), tc.wantSize)
|
2020-01-24 23:19:58 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.wantSize == 0 {
|
2019-12-05 12:30:37 +08:00
|
|
|
continue
|
|
|
|
}
|
2020-01-24 23:19:58 +08:00
|
|
|
|
2020-07-13 21:29:41 +08:00
|
|
|
first := got[0].Message
|
2020-01-24 23:19:58 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-07-13 21:29:41 +08:00
|
|
|
last := got[len(got)-1].Message
|
2020-01-24 23:19:58 +08:00
|
|
|
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-08-29 21:54:08 +08:00
|
|
|
var (
|
|
|
|
timeCmpOpt = cmpopts.EquateApproxTime(2 * time.Second) // allow for 2 seconds margin in time.Time
|
|
|
|
zScoreCmpOpt = h.EquateInt64Approx(2) // allow for 2 seconds margin in Z.Score
|
|
|
|
)
|
2019-12-05 12:30:37 +08:00
|
|
|
|
2021-03-13 08:23:08 +08:00
|
|
|
func TestRunArchivedTask(t *testing.T) {
|
2019-12-08 22:46:04 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2019-12-29 12:12:14 +08:00
|
|
|
t1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
t2 := h.NewTaskMessage("gen_thumbnail", nil)
|
2020-08-13 21:54:32 +08:00
|
|
|
t3 := h.NewTaskMessageWithQueue("send_notification", nil, "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 {
|
2021-01-13 03:01:21 +08:00
|
|
|
archived map[string][]base.Z
|
|
|
|
qname string
|
|
|
|
id uuid.UUID
|
2021-03-13 08:23:08 +08:00
|
|
|
want error // expected return value from calling RunArchivedTask
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived map[string][]*base.TaskMessage
|
|
|
|
wantPending map[string][]*base.TaskMessage
|
2019-12-08 22:46:04 +08:00
|
|
|
}{
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {
|
|
|
|
{Message: t1, Score: s1},
|
|
|
|
{Message: t2, Score: s2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
id: t2.ID,
|
|
|
|
want: nil,
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {t1},
|
2019-12-08 22:46:04 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {t2},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
2019-12-08 22:46:04 +08:00
|
|
|
},
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {
|
|
|
|
{Message: t1, Score: s1},
|
|
|
|
{Message: t2, Score: s2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2021-03-13 08:23:08 +08:00
|
|
|
id: uuid.New(),
|
2020-08-13 21:54:32 +08:00
|
|
|
want: ErrTaskNotFound,
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {t1, t2},
|
2019-12-08 22:46:04 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {
|
|
|
|
{Message: t1, Score: s1},
|
|
|
|
{Message: t2, Score: s2},
|
|
|
|
},
|
|
|
|
"critical": {
|
|
|
|
{Message: t3, Score: s1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "critical",
|
|
|
|
id: t3.ID,
|
|
|
|
want: nil,
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {t1, t2},
|
|
|
|
"critical": {},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {t3},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
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
|
2021-01-13 03:01:21 +08:00
|
|
|
h.SeedAllArchivedQueues(t, r.client, tc.archived)
|
2019-12-08 22:46:04 +08:00
|
|
|
|
2021-04-28 22:27:35 +08:00
|
|
|
got := r.RunTask(tc.qname, tc.id)
|
2019-12-08 22:46:04 +08:00
|
|
|
if got != tc.want {
|
2021-04-28 22:27:35 +08:00
|
|
|
t.Errorf("r.RunTask(%q, %s) = %v, want %v", tc.qname, tc.id, got, tc.want)
|
2019-12-08 22:46:04 +08:00
|
|
|
continue
|
|
|
|
}
|
2019-12-29 12:12:14 +08:00
|
|
|
|
2020-09-05 22:03:43 +08:00
|
|
|
for qname, want := range tc.wantPending {
|
|
|
|
gotPending := h.GetPendingMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
2021-03-13 08:23:08 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.PendingKey(qname), diff)
|
2020-01-08 23:16:21 +08:00
|
|
|
}
|
2019-12-08 22:46:04 +08:00
|
|
|
}
|
2019-12-29 12:12:14 +08:00
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
for qname, want := range tc.wantArchived {
|
|
|
|
gotDead := h.GetArchivedMessages(t, r.client, qname)
|
2020-08-13 21:54:32 +08:00
|
|
|
if diff := cmp.Diff(want, gotDead, h.SortMsgOpt); diff != "" {
|
2021-01-13 03:01:21 +08:00
|
|
|
t.Errorf("mismatch found in %q, (-want, +got)\n%s", base.ArchivedKey(qname), diff)
|
2020-08-13 21:54:32 +08:00
|
|
|
}
|
2019-12-08 22:46:04 +08:00
|
|
|
}
|
2019-12-05 12:30:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 04:35:52 +08:00
|
|
|
func TestRunRetryTask(t *testing.T) {
|
2019-12-08 22:46:04 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2019-12-08 22:46:04 +08:00
|
|
|
|
2019-12-29 12:12:14 +08:00
|
|
|
t1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
t2 := h.NewTaskMessage("gen_thumbnail", nil)
|
2020-08-13 21:54:32 +08:00
|
|
|
t3 := h.NewTaskMessageWithQueue("send_notification", nil, "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 {
|
2020-09-05 22:03:43 +08:00
|
|
|
retry map[string][]base.Z
|
|
|
|
qname string
|
|
|
|
id uuid.UUID
|
2020-09-06 04:35:52 +08:00
|
|
|
want error // expected return value from calling RunRetryTask
|
2020-09-05 22:03:43 +08:00
|
|
|
wantRetry map[string][]*base.TaskMessage
|
|
|
|
wantPending map[string][]*base.TaskMessage
|
2019-12-08 22:46:04 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-13 21:54:32 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: t1, Score: s1},
|
|
|
|
{Message: t2, Score: s2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
id: t2.ID,
|
|
|
|
want: nil,
|
|
|
|
wantRetry: map[string][]*base.TaskMessage{
|
|
|
|
"default": {t1},
|
2019-12-08 22:46:04 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {t2},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-13 21:54:32 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: t1, Score: s1},
|
|
|
|
{Message: t2, Score: s2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2021-03-13 08:23:08 +08:00
|
|
|
id: uuid.New(),
|
2020-08-13 21:54:32 +08:00
|
|
|
want: ErrTaskNotFound,
|
|
|
|
wantRetry: map[string][]*base.TaskMessage{
|
|
|
|
"default": {t1, t2},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
2019-12-08 22:46:04 +08:00
|
|
|
},
|
|
|
|
{
|
2020-08-13 21:54:32 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: t1, Score: s1},
|
|
|
|
{Message: t2, Score: s2},
|
|
|
|
},
|
|
|
|
"low": {
|
|
|
|
{Message: t3, Score: s2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "low",
|
|
|
|
id: t3.ID,
|
|
|
|
want: nil,
|
|
|
|
wantRetry: map[string][]*base.TaskMessage{
|
|
|
|
"default": {t1, t2},
|
|
|
|
"low": {},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {},
|
|
|
|
"low": {t3},
|
2019-12-08 22:46:04 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2020-08-13 21:54:32 +08:00
|
|
|
h.FlushDB(t, r.client) // clean up db before each test case
|
|
|
|
h.SeedAllRetryQueues(t, r.client, tc.retry) // initialize retry queue
|
2019-12-08 22:46:04 +08:00
|
|
|
|
2021-04-28 22:27:35 +08:00
|
|
|
got := r.RunTask(tc.qname, tc.id)
|
2019-12-08 22:46:04 +08:00
|
|
|
if got != tc.want {
|
2021-04-28 22:27:35 +08:00
|
|
|
t.Errorf("r.RunTask(%q, %s) = %v, want %v", tc.qname, tc.id, got, tc.want)
|
2019-12-08 22:46:04 +08:00
|
|
|
continue
|
|
|
|
}
|
2019-12-29 12:12:14 +08:00
|
|
|
|
2020-09-05 22:03:43 +08:00
|
|
|
for qname, want := range tc.wantPending {
|
|
|
|
gotPending := h.GetPendingMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
2021-03-13 08:23:08 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.PendingKey(qname), diff)
|
2020-01-08 23:16:21 +08:00
|
|
|
}
|
2019-12-08 22:46:04 +08:00
|
|
|
}
|
2019-12-29 12:12:14 +08:00
|
|
|
|
2020-08-13 21:54:32 +08:00
|
|
|
for qname, want := range tc.wantRetry {
|
|
|
|
gotRetry := h.GetRetryMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotRetry, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q, (-want, +got)\n%s", base.RetryKey(qname), diff)
|
|
|
|
}
|
2019-12-08 22:46:04 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-05 12:30:37 +08:00
|
|
|
}
|
|
|
|
|
2020-09-06 04:35:52 +08:00
|
|
|
func TestRunScheduledTask(t *testing.T) {
|
2019-12-08 22:46:04 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2019-12-29 12:12:14 +08:00
|
|
|
t1 := h.NewTaskMessage("send_email", nil)
|
|
|
|
t2 := h.NewTaskMessage("gen_thumbnail", nil)
|
2020-08-13 21:54:32 +08:00
|
|
|
t3 := h.NewTaskMessageWithQueue("send_notification", nil, "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 {
|
2020-08-13 21:54:32 +08:00
|
|
|
scheduled map[string][]base.Z
|
|
|
|
qname string
|
2020-07-02 21:21:20 +08:00
|
|
|
id uuid.UUID
|
2020-09-06 04:35:52 +08:00
|
|
|
want error // expected return value from calling RunScheduledTask
|
2020-08-13 21:54:32 +08:00
|
|
|
wantScheduled map[string][]*base.TaskMessage
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending map[string][]*base.TaskMessage
|
2019-12-08 22:46:04 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-13 21:54:32 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: t1, Score: s1},
|
|
|
|
{Message: t2, Score: s2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
id: t2.ID,
|
|
|
|
want: nil,
|
|
|
|
wantScheduled: map[string][]*base.TaskMessage{
|
|
|
|
"default": {t1},
|
2019-12-08 22:46:04 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {t2},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
2019-12-08 22:46:04 +08:00
|
|
|
},
|
|
|
|
{
|
2020-08-13 21:54:32 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: t1, Score: s1},
|
|
|
|
{Message: t2, Score: s2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2021-03-13 08:23:08 +08:00
|
|
|
id: uuid.New(),
|
2020-08-13 21:54:32 +08:00
|
|
|
want: ErrTaskNotFound,
|
|
|
|
wantScheduled: map[string][]*base.TaskMessage{
|
|
|
|
"default": {t1, t2},
|
2019-12-08 22:46:04 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-13 21:54:32 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: t1, Score: s1},
|
|
|
|
{Message: t2, Score: s2},
|
|
|
|
},
|
|
|
|
"notifications": {
|
|
|
|
{Message: t3, Score: s1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "notifications",
|
|
|
|
id: t3.ID,
|
|
|
|
want: nil,
|
|
|
|
wantScheduled: map[string][]*base.TaskMessage{
|
|
|
|
"default": {t1, t2},
|
|
|
|
"notifications": {},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {},
|
|
|
|
"notifications": {t3},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
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
|
2020-08-13 21:54:32 +08:00
|
|
|
h.SeedAllScheduledQueues(t, r.client, tc.scheduled)
|
2019-12-08 22:46:04 +08:00
|
|
|
|
2021-04-28 22:27:35 +08:00
|
|
|
got := r.RunTask(tc.qname, tc.id)
|
2019-12-08 22:46:04 +08:00
|
|
|
if got != tc.want {
|
2021-04-28 22:27:35 +08:00
|
|
|
t.Errorf("r.RunTask(%q, %s) = %v, want %v", tc.qname, tc.id, got, tc.want)
|
2019-12-08 22:46:04 +08:00
|
|
|
continue
|
|
|
|
}
|
2019-12-29 12:12:14 +08:00
|
|
|
|
2020-09-05 22:03:43 +08:00
|
|
|
for qname, want := range tc.wantPending {
|
|
|
|
gotPending := h.GetPendingMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
2021-03-13 08:23:08 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.PendingKey(qname), diff)
|
2020-01-08 23:16:21 +08:00
|
|
|
}
|
2019-12-08 22:46:04 +08:00
|
|
|
}
|
2019-12-29 12:12:14 +08:00
|
|
|
|
2020-08-13 21:54:32 +08:00
|
|
|
for qname, want := range tc.wantScheduled {
|
|
|
|
gotScheduled := h.GetScheduledMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotScheduled, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q, (-want, +got)\n%s", base.ScheduledKey(qname), 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
|
|
|
|
2020-09-06 04:35:52 +08:00
|
|
|
func TestRunAllScheduledTasks(t *testing.T) {
|
2019-12-11 12:28:31 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
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-08-13 21:54:32 +08:00
|
|
|
t4 := h.NewTaskMessageWithQueue("important_notification", nil, "custom")
|
|
|
|
t5 := h.NewTaskMessageWithQueue("minor_notification", nil, "custom")
|
2019-12-11 12:28:31 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-13 21:54:32 +08:00
|
|
|
desc string
|
|
|
|
scheduled map[string][]base.Z
|
|
|
|
qname string
|
|
|
|
want int64
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending map[string][]*base.TaskMessage
|
2020-08-13 21:54:32 +08:00
|
|
|
wantScheduled 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",
|
2020-08-13 21:54:32 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: t1, Score: time.Now().Add(time.Hour).Unix()},
|
|
|
|
{Message: t2, Score: time.Now().Add(time.Hour).Unix()},
|
|
|
|
{Message: t3, Score: time.Now().Add(time.Hour).Unix()},
|
|
|
|
},
|
2019-12-13 11:49:41 +08:00
|
|
|
},
|
2020-08-13 21:54:32 +08:00
|
|
|
qname: "default",
|
|
|
|
want: 3,
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {t1, t2, t3},
|
|
|
|
},
|
|
|
|
wantScheduled: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
2019-12-11 12:28:31 +08:00
|
|
|
},
|
|
|
|
{
|
2020-08-13 21:54:32 +08:00
|
|
|
desc: "with empty scheduled queue",
|
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 0,
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
wantScheduled: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "with custom queues",
|
2020-08-13 21:54:32 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: t1, Score: time.Now().Add(time.Hour).Unix()},
|
|
|
|
{Message: t2, Score: time.Now().Add(time.Hour).Unix()},
|
|
|
|
{Message: t3, Score: time.Now().Add(time.Hour).Unix()},
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
{Message: t4, Score: time.Now().Add(time.Hour).Unix()},
|
|
|
|
{Message: t5, Score: time.Now().Add(time.Hour).Unix()},
|
|
|
|
},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
2020-08-13 21:54:32 +08:00
|
|
|
qname: "custom",
|
|
|
|
want: 2,
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {t4, t5},
|
|
|
|
},
|
|
|
|
wantScheduled: map[string][]*base.TaskMessage{
|
|
|
|
"default": {t1, t2, t3},
|
|
|
|
"custom": {},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
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
|
2020-08-13 21:54:32 +08:00
|
|
|
h.SeedAllScheduledQueues(t, r.client, tc.scheduled)
|
2019-12-11 12:28:31 +08:00
|
|
|
|
2020-09-06 04:35:52 +08:00
|
|
|
got, err := r.RunAllScheduledTasks(tc.qname)
|
2019-12-11 12:28:31 +08:00
|
|
|
if err != nil {
|
2020-09-06 04:35:52 +08:00
|
|
|
t.Errorf("%s; r.RunAllScheduledTasks(%q) = %v, %v; want %v, nil",
|
2020-08-13 21:54:32 +08:00
|
|
|
tc.desc, tc.qname, got, err, tc.want)
|
2019-12-11 13:38:25 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if got != tc.want {
|
2020-09-06 04:35:52 +08:00
|
|
|
t.Errorf("%s; r.RunAllScheduledTasks(%q) = %v, %v; want %v, nil",
|
2020-08-13 21:54:32 +08:00
|
|
|
tc.desc, tc.qname, got, err, tc.want)
|
2019-12-11 12:28:31 +08:00
|
|
|
}
|
|
|
|
|
2020-09-05 22:03:43 +08:00
|
|
|
for qname, want := range tc.wantPending {
|
|
|
|
gotPending := h.GetPendingMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
2021-03-13 08:23:08 +08:00
|
|
|
t.Errorf("%s; mismatch found in %q; (-want, +got)\n%s", tc.desc, base.PendingKey(qname), diff)
|
2020-01-08 23:16:21 +08:00
|
|
|
}
|
2019-12-11 12:28:31 +08:00
|
|
|
}
|
2020-08-13 21:54:32 +08:00
|
|
|
for qname, want := range tc.wantScheduled {
|
|
|
|
gotScheduled := h.GetScheduledMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotScheduled, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("%s; mismatch found in %q; (-want, +got)\n%s", tc.desc, base.ScheduledKey(qname), diff)
|
|
|
|
}
|
|
|
|
}
|
2019-12-11 12:28:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 04:35:52 +08:00
|
|
|
func TestRunAllRetryTasks(t *testing.T) {
|
2019-12-11 12:28:31 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
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-08-13 21:54:32 +08:00
|
|
|
t4 := h.NewTaskMessageWithQueue("important_notification", nil, "custom")
|
|
|
|
t5 := h.NewTaskMessageWithQueue("minor_notification", nil, "custom")
|
2019-12-11 12:28:31 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-09-05 22:03:43 +08:00
|
|
|
desc string
|
|
|
|
retry map[string][]base.Z
|
|
|
|
qname string
|
|
|
|
want int64
|
|
|
|
wantPending map[string][]*base.TaskMessage
|
|
|
|
wantRetry 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",
|
2020-08-13 21:54:32 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: t1, Score: time.Now().Add(time.Hour).Unix()},
|
|
|
|
{Message: t2, Score: time.Now().Add(time.Hour).Unix()},
|
|
|
|
{Message: t3, Score: time.Now().Add(time.Hour).Unix()},
|
|
|
|
},
|
2019-12-13 11:49:41 +08:00
|
|
|
},
|
2020-08-13 21:54:32 +08:00
|
|
|
qname: "default",
|
|
|
|
want: 3,
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {t1, t2, t3},
|
|
|
|
},
|
|
|
|
wantRetry: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
2019-12-11 12:28:31 +08:00
|
|
|
},
|
|
|
|
{
|
2020-08-13 21:54:32 +08:00
|
|
|
desc: "with empty retry queue",
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2020-01-08 23:16:21 +08:00
|
|
|
want: 0,
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
wantRetry: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "with custom queues",
|
2020-08-13 21:54:32 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: t1, Score: time.Now().Add(time.Hour).Unix()},
|
|
|
|
{Message: t2, Score: time.Now().Add(time.Hour).Unix()},
|
|
|
|
{Message: t3, Score: time.Now().Add(time.Hour).Unix()},
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
{Message: t4, Score: time.Now().Add(time.Hour).Unix()},
|
|
|
|
{Message: t5, Score: time.Now().Add(time.Hour).Unix()},
|
|
|
|
},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
2020-08-13 21:54:32 +08:00
|
|
|
qname: "custom",
|
|
|
|
want: 2,
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {t4, t5},
|
|
|
|
},
|
|
|
|
wantRetry: map[string][]*base.TaskMessage{
|
|
|
|
"default": {t1, t2, t3},
|
|
|
|
"custom": {},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
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
|
2020-08-13 21:54:32 +08:00
|
|
|
h.SeedAllRetryQueues(t, r.client, tc.retry)
|
2019-12-11 12:28:31 +08:00
|
|
|
|
2020-09-06 04:35:52 +08:00
|
|
|
got, err := r.RunAllRetryTasks(tc.qname)
|
2019-12-11 12:28:31 +08:00
|
|
|
if err != nil {
|
2020-09-06 04:35:52 +08:00
|
|
|
t.Errorf("%s; r.RunAllRetryTasks(%q) = %v, %v; want %v, nil",
|
2020-08-13 21:54:32 +08:00
|
|
|
tc.desc, tc.qname, got, err, tc.want)
|
2019-12-11 13:38:25 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if got != tc.want {
|
2020-09-06 04:35:52 +08:00
|
|
|
t.Errorf("%s; r.RunAllRetryTasks(%q) = %v, %v; want %v, nil",
|
2020-08-13 21:54:32 +08:00
|
|
|
tc.desc, tc.qname, got, err, tc.want)
|
2019-12-11 12:28:31 +08:00
|
|
|
}
|
|
|
|
|
2020-09-05 22:03:43 +08:00
|
|
|
for qname, want := range tc.wantPending {
|
|
|
|
gotPending := h.GetPendingMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
2021-03-13 08:23:08 +08:00
|
|
|
t.Errorf("%s; mismatch found in %q; (-want, +got)\n%s", tc.desc, base.PendingKey(qname), diff)
|
2020-01-08 23:16:21 +08:00
|
|
|
}
|
2019-12-11 12:28:31 +08:00
|
|
|
}
|
2020-08-13 21:54:32 +08:00
|
|
|
for qname, want := range tc.wantRetry {
|
|
|
|
gotRetry := h.GetRetryMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotRetry, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("%s; mismatch found in %q; (-want, +got)\n%s", tc.desc, base.RetryKey(qname), diff)
|
|
|
|
}
|
|
|
|
}
|
2019-12-11 12:28:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 08:23:08 +08:00
|
|
|
func TestRunAllArchivedTasks(t *testing.T) {
|
2019-12-11 12:28:31 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
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-08-13 21:54:32 +08:00
|
|
|
t4 := h.NewTaskMessageWithQueue("important_notification", nil, "custom")
|
|
|
|
t5 := h.NewTaskMessageWithQueue("minor_notification", nil, "custom")
|
2019-12-11 12:28:31 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2021-01-13 03:01:21 +08:00
|
|
|
desc string
|
|
|
|
archived map[string][]base.Z
|
|
|
|
qname string
|
|
|
|
want int64
|
|
|
|
wantPending map[string][]*base.TaskMessage
|
|
|
|
wantArchived map[string][]*base.TaskMessage
|
2019-12-11 12:28:31 +08:00
|
|
|
}{
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
desc: "with tasks in archived queue",
|
|
|
|
archived: map[string][]base.Z{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {
|
|
|
|
{Message: t1, Score: time.Now().Add(-time.Minute).Unix()},
|
|
|
|
{Message: t2, Score: time.Now().Add(-time.Minute).Unix()},
|
|
|
|
{Message: t3, Score: time.Now().Add(-time.Minute).Unix()},
|
|
|
|
},
|
2019-12-13 11:49:41 +08:00
|
|
|
},
|
2020-08-13 21:54:32 +08:00
|
|
|
qname: "default",
|
|
|
|
want: 3,
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {t1, t2, t3},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
2019-12-11 12:28:31 +08:00
|
|
|
},
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
desc: "with empty archived queue",
|
|
|
|
archived: map[string][]base.Z{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 0,
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "with custom queues",
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {
|
|
|
|
{Message: t1, Score: time.Now().Add(-time.Minute).Unix()},
|
|
|
|
{Message: t2, Score: time.Now().Add(-time.Minute).Unix()},
|
|
|
|
{Message: t3, Score: time.Now().Add(-time.Minute).Unix()},
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
{Message: t4, Score: time.Now().Add(-time.Minute).Unix()},
|
|
|
|
{Message: t5, Score: time.Now().Add(-time.Minute).Unix()},
|
|
|
|
},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
2020-08-13 21:54:32 +08:00
|
|
|
qname: "custom",
|
|
|
|
want: 2,
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {t4, t5},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]*base.TaskMessage{
|
2020-08-13 21:54:32 +08:00
|
|
|
"default": {t1, t2, t3},
|
|
|
|
"custom": {},
|
2020-01-08 23:16:21 +08:00
|
|
|
},
|
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
|
2021-01-13 03:01:21 +08:00
|
|
|
h.SeedAllArchivedQueues(t, r.client, tc.archived)
|
2019-12-11 12:28:31 +08:00
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
got, err := r.RunAllArchivedTasks(tc.qname)
|
2019-12-11 12:28:31 +08:00
|
|
|
if err != nil {
|
2020-09-06 04:35:52 +08:00
|
|
|
t.Errorf("%s; r.RunAllDeadTasks(%q) = %v, %v; want %v, nil",
|
2020-08-13 21:54:32 +08:00
|
|
|
tc.desc, tc.qname, got, err, tc.want)
|
2019-12-11 13:38:25 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if got != tc.want {
|
2020-09-06 04:35:52 +08:00
|
|
|
t.Errorf("%s; r.RunAllDeadTasks(%q) = %v, %v; want %v, nil",
|
2020-08-13 21:54:32 +08:00
|
|
|
tc.desc, tc.qname, got, err, tc.want)
|
2019-12-11 12:28:31 +08:00
|
|
|
}
|
|
|
|
|
2020-09-05 22:03:43 +08:00
|
|
|
for qname, want := range tc.wantPending {
|
|
|
|
gotPending := h.GetPendingMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
2021-03-13 08:23:08 +08:00
|
|
|
t.Errorf("%s; mismatch found in %q; (-want, +got)\n%s", tc.desc, base.PendingKey(qname), diff)
|
2020-01-08 23:16:21 +08:00
|
|
|
}
|
2019-12-11 12:28:31 +08:00
|
|
|
}
|
2021-01-13 03:01:21 +08:00
|
|
|
for qname, want := range tc.wantArchived {
|
|
|
|
gotDead := h.GetArchivedMessages(t, r.client, qname)
|
2020-08-13 21:54:32 +08:00
|
|
|
if diff := cmp.Diff(want, gotDead, h.SortMsgOpt); diff != "" {
|
2021-01-13 03:01:21 +08:00
|
|
|
t.Errorf("%s; mismatch found in %q; (-want, +got)\n%s", tc.desc, base.ArchivedKey(qname), diff)
|
2020-08-13 21:54:32 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-11 12:28:31 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-12 11:56:19 +08:00
|
|
|
|
2021-03-13 08:23:08 +08:00
|
|
|
func TestArchiveRetryTask(t *testing.T) {
|
2019-12-26 23:17:26 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-08-15 21:38:33 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
|
|
|
t1 := time.Now().Add(1 * time.Minute)
|
|
|
|
t2 := time.Now().Add(1 * time.Hour)
|
|
|
|
t3 := time.Now().Add(2 * time.Hour)
|
|
|
|
t4 := time.Now().Add(3 * time.Hour)
|
2019-12-26 23:17:26 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2021-01-13 03:01:21 +08:00
|
|
|
retry map[string][]base.Z
|
|
|
|
archived map[string][]base.Z
|
|
|
|
qname string
|
|
|
|
id uuid.UUID
|
|
|
|
want error
|
|
|
|
wantRetry map[string][]base.Z
|
|
|
|
wantArchived map[string][]base.Z
|
2019-12-26 23:17:26 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-15 21:38:33 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2019-12-26 23:17:26 +08:00
|
|
|
id: m1.ID,
|
|
|
|
want: nil,
|
2020-08-15 21:38:33 +08:00
|
|
|
wantRetry: map[string][]base.Z{
|
|
|
|
"default": {{Message: m2, Score: t2.Unix()}},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {{Message: m1, Score: time.Now().Unix()}},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-15 21:38:33 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {{Message: m1, Score: t1.Unix()}},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {{Message: m2, Score: t2.Unix()}},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
2020-08-15 21:38:33 +08:00
|
|
|
qname: "default",
|
2021-03-13 08:23:08 +08:00
|
|
|
id: uuid.New(),
|
2019-12-26 23:17:26 +08:00
|
|
|
want: ErrTaskNotFound,
|
2020-08-15 21:38:33 +08:00
|
|
|
wantRetry: map[string][]base.Z{
|
|
|
|
"default": {{Message: m1, Score: t1.Unix()}},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {{Message: m2, Score: t2.Unix()}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
{Message: m3, Score: t3.Unix()},
|
|
|
|
{Message: m4, Score: t4.Unix()},
|
|
|
|
},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
|
|
|
id: m3.ID,
|
|
|
|
want: nil,
|
|
|
|
wantRetry: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
{Message: m4, Score: t4.Unix()},
|
|
|
|
},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {{Message: m3, Score: time.Now().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)
|
2020-08-15 21:38:33 +08:00
|
|
|
h.SeedAllRetryQueues(t, r.client, tc.retry)
|
2021-01-13 03:01:21 +08:00
|
|
|
h.SeedAllArchivedQueues(t, r.client, tc.archived)
|
2019-12-26 23:17:26 +08:00
|
|
|
|
2021-04-29 12:24:42 +08:00
|
|
|
got := r.ArchiveTask(tc.qname, tc.id)
|
2019-12-26 23:17:26 +08:00
|
|
|
if got != tc.want {
|
2021-04-29 12:24:42 +08:00
|
|
|
t.Errorf("(*RDB).ArchiveTask(%q, %v) = %v, want %v",
|
2021-03-13 08:23:08 +08:00
|
|
|
tc.qname, tc.id, got, tc.want)
|
2019-12-26 23:17:26 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-08-15 21:38:33 +08:00
|
|
|
for qname, want := range tc.wantRetry {
|
|
|
|
gotRetry := h.GetRetryEntries(t, r.client, qname)
|
2020-08-29 21:54:08 +08:00
|
|
|
if diff := cmp.Diff(want, gotRetry, h.SortZSetEntryOpt, zScoreCmpOpt); diff != "" {
|
2020-08-15 21:38:33 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want,+got)\n%s",
|
|
|
|
base.RetryKey(qname), diff)
|
|
|
|
}
|
2019-12-26 23:17:26 +08:00
|
|
|
}
|
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
for qname, want := range tc.wantArchived {
|
|
|
|
gotDead := h.GetArchivedEntries(t, r.client, qname)
|
2020-08-29 21:54:08 +08:00
|
|
|
if diff := cmp.Diff(want, gotDead, h.SortZSetEntryOpt, zScoreCmpOpt); diff != "" {
|
2020-08-15 21:38:33 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want,+got)\n%s",
|
2021-01-13 03:01:21 +08:00
|
|
|
base.ArchivedKey(qname), diff)
|
2020-08-15 21:38:33 +08:00
|
|
|
}
|
2019-12-26 23:17:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 08:23:08 +08:00
|
|
|
func TestArchiveScheduledTask(t *testing.T) {
|
2019-12-26 23:17:26 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-08-15 21:38:33 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
|
|
|
t1 := time.Now().Add(1 * time.Minute)
|
|
|
|
t2 := time.Now().Add(1 * time.Hour)
|
|
|
|
t3 := time.Now().Add(2 * time.Hour)
|
|
|
|
t4 := time.Now().Add(3 * time.Hour)
|
2019-12-26 23:17:26 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-15 21:38:33 +08:00
|
|
|
scheduled map[string][]base.Z
|
2021-01-13 03:01:21 +08:00
|
|
|
archived map[string][]base.Z
|
2020-08-15 21:38:33 +08:00
|
|
|
qname string
|
2020-07-02 21:21:20 +08:00
|
|
|
id uuid.UUID
|
2019-12-26 23:17:26 +08:00
|
|
|
want error
|
2020-08-15 21:38:33 +08:00
|
|
|
wantScheduled map[string][]base.Z
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived map[string][]base.Z
|
2019-12-26 23:17:26 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-15 21:38:33 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
2020-08-15 21:38:33 +08:00
|
|
|
qname: "default",
|
2019-12-26 23:17:26 +08:00
|
|
|
id: m1.ID,
|
|
|
|
want: nil,
|
2020-08-15 21:38:33 +08:00
|
|
|
wantScheduled: map[string][]base.Z{
|
|
|
|
"default": {{Message: m2, Score: t2.Unix()}},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {{Message: m1, Score: time.Now().Unix()}},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-15 21:38:33 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {{Message: m1, Score: t1.Unix()}},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {{Message: m2, Score: t2.Unix()}},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
2020-08-15 21:38:33 +08:00
|
|
|
qname: "default",
|
2019-12-26 23:17:26 +08:00
|
|
|
id: m2.ID,
|
|
|
|
want: ErrTaskNotFound,
|
2020-08-15 21:38:33 +08:00
|
|
|
wantScheduled: map[string][]base.Z{
|
|
|
|
"default": {{Message: m1, Score: t1.Unix()}},
|
2019-12-26 23:17:26 +08:00
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {{Message: m2, Score: t2.Unix()}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
{Message: m3, Score: t3.Unix()},
|
|
|
|
{Message: m4, Score: t4.Unix()},
|
|
|
|
},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
|
|
|
id: m3.ID,
|
|
|
|
want: nil,
|
|
|
|
wantScheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
{Message: m4, Score: t4.Unix()},
|
|
|
|
},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {{Message: m3, Score: time.Now().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)
|
2020-08-15 21:38:33 +08:00
|
|
|
h.SeedAllScheduledQueues(t, r.client, tc.scheduled)
|
2021-01-13 03:01:21 +08:00
|
|
|
h.SeedAllArchivedQueues(t, r.client, tc.archived)
|
2019-12-26 23:17:26 +08:00
|
|
|
|
2021-04-29 12:24:42 +08:00
|
|
|
got := r.ArchiveTask(tc.qname, tc.id)
|
2019-12-26 23:17:26 +08:00
|
|
|
if got != tc.want {
|
2021-04-29 12:24:42 +08:00
|
|
|
t.Errorf("(*RDB).ArchiveTask(%q, %v) = %v, want %v",
|
2021-03-13 08:23:08 +08:00
|
|
|
tc.qname, tc.id, got, tc.want)
|
2019-12-26 23:17:26 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-08-15 21:38:33 +08:00
|
|
|
for qname, want := range tc.wantScheduled {
|
|
|
|
gotScheduled := h.GetScheduledEntries(t, r.client, qname)
|
2020-08-29 21:54:08 +08:00
|
|
|
if diff := cmp.Diff(want, gotScheduled, h.SortZSetEntryOpt, zScoreCmpOpt); diff != "" {
|
2020-08-15 21:38:33 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want,+got)\n%s",
|
|
|
|
base.ScheduledKey(qname), diff)
|
|
|
|
}
|
2019-12-26 23:17:26 +08:00
|
|
|
}
|
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
for qname, want := range tc.wantArchived {
|
|
|
|
gotDead := h.GetArchivedEntries(t, r.client, qname)
|
2020-08-29 21:54:08 +08:00
|
|
|
if diff := cmp.Diff(want, gotDead, h.SortZSetEntryOpt, zScoreCmpOpt); diff != "" {
|
2020-08-15 21:38:33 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want,+got)\n%s",
|
2021-01-13 03:01:21 +08:00
|
|
|
base.ArchivedKey(qname), diff)
|
2020-08-15 21:38:33 +08:00
|
|
|
}
|
2019-12-27 22:22:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 08:23:08 +08:00
|
|
|
func TestArchivePendingTask(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
defer r.Close()
|
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
|
|
|
|
|
|
|
oneHourAgo := time.Now().Add(-1 * time.Hour)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
pending map[string][]*base.TaskMessage
|
|
|
|
archived map[string][]base.Z
|
|
|
|
qname string
|
|
|
|
id uuid.UUID
|
|
|
|
want error
|
|
|
|
wantPending map[string][]*base.TaskMessage
|
|
|
|
wantArchived map[string][]base.Z
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
|
|
|
},
|
|
|
|
archived: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
id: m1.ID,
|
|
|
|
want: nil,
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m2},
|
|
|
|
},
|
|
|
|
wantArchived: map[string][]base.Z{
|
|
|
|
"default": {{Message: m1, Score: time.Now().Unix()}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1},
|
|
|
|
},
|
|
|
|
archived: map[string][]base.Z{
|
|
|
|
"default": {{Message: m2, Score: oneHourAgo.Unix()}},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
id: m2.ID,
|
|
|
|
want: ErrTaskNotFound,
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1},
|
|
|
|
},
|
|
|
|
wantArchived: map[string][]base.Z{
|
|
|
|
"default": {{Message: m2, Score: oneHourAgo.Unix()}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {m3, m4},
|
|
|
|
},
|
|
|
|
archived: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
|
|
|
id: m3.ID,
|
|
|
|
want: nil,
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {m4},
|
|
|
|
},
|
|
|
|
wantArchived: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {{Message: m3, Score: time.Now().Unix()}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r.client)
|
|
|
|
h.SeedAllPendingQueues(t, r.client, tc.pending)
|
|
|
|
h.SeedAllArchivedQueues(t, r.client, tc.archived)
|
|
|
|
|
2021-04-29 12:24:42 +08:00
|
|
|
got := r.ArchiveTask(tc.qname, tc.id)
|
2021-03-13 08:23:08 +08:00
|
|
|
if got != tc.want {
|
2021-04-29 12:24:42 +08:00
|
|
|
t.Errorf("(*RDB).ArchiveTask(%q, %v) = %v, want %v",
|
2021-03-13 08:23:08 +08:00
|
|
|
tc.qname, tc.id, got, tc.want)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for qname, want := range tc.wantPending {
|
|
|
|
gotPending := h.GetPendingMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q; (-want,+got)\n%s",
|
|
|
|
base.PendingKey(qname), diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for qname, want := range tc.wantArchived {
|
|
|
|
gotDead := h.GetArchivedEntries(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotDead, h.SortZSetEntryOpt, zScoreCmpOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q; (-want,+got)\n%s",
|
|
|
|
base.ArchivedKey(qname), diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestArchiveAllPendingTasks(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
defer r.Close()
|
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
|
|
|
t1 := time.Now().Add(1 * time.Minute)
|
|
|
|
t2 := time.Now().Add(1 * time.Hour)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
pending map[string][]*base.TaskMessage
|
|
|
|
archived map[string][]base.Z
|
|
|
|
qname string
|
|
|
|
want int64
|
|
|
|
wantPending map[string][]*base.TaskMessage
|
|
|
|
wantArchived map[string][]base.Z
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
|
|
|
},
|
|
|
|
archived: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 2,
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
wantArchived: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: time.Now().Unix()},
|
|
|
|
{Message: m2, Score: time.Now().Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1},
|
|
|
|
},
|
|
|
|
archived: map[string][]base.Z{
|
|
|
|
"default": {{Message: m2, Score: t2.Unix()}},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 1,
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
wantArchived: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: time.Now().Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
archived: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 0,
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
wantArchived: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {m3, m4},
|
|
|
|
},
|
|
|
|
archived: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
|
|
|
want: 2,
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
wantArchived: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {
|
|
|
|
{Message: m3, Score: time.Now().Unix()},
|
|
|
|
{Message: m4, Score: time.Now().Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r.client)
|
|
|
|
h.SeedAllPendingQueues(t, r.client, tc.pending)
|
|
|
|
h.SeedAllArchivedQueues(t, r.client, tc.archived)
|
|
|
|
|
|
|
|
got, err := r.ArchiveAllPendingTasks(tc.qname)
|
|
|
|
if got != tc.want || err != nil {
|
|
|
|
t.Errorf("(*RDB).KillAllRetryTasks(%q) = %v, %v; want %v, nil",
|
|
|
|
tc.qname, got, err, tc.want)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for qname, want := range tc.wantPending {
|
|
|
|
gotPending := h.GetPendingMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q; (-want,+got)\n%s",
|
|
|
|
base.PendingKey(qname), diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for qname, want := range tc.wantArchived {
|
|
|
|
gotDead := h.GetArchivedEntries(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotDead, h.SortZSetEntryOpt, zScoreCmpOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q; (-want,+got)\n%s",
|
|
|
|
base.ArchivedKey(qname), diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestArchiveAllRetryTasks(t *testing.T) {
|
2019-12-27 22:22:33 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-08-15 21:38:33 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
|
|
|
t1 := time.Now().Add(1 * time.Minute)
|
|
|
|
t2 := time.Now().Add(1 * time.Hour)
|
|
|
|
t3 := time.Now().Add(2 * time.Hour)
|
|
|
|
t4 := time.Now().Add(3 * time.Hour)
|
2019-12-27 22:22:33 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2021-01-13 03:01:21 +08:00
|
|
|
retry map[string][]base.Z
|
|
|
|
archived map[string][]base.Z
|
|
|
|
qname string
|
|
|
|
want int64
|
|
|
|
wantRetry map[string][]base.Z
|
|
|
|
wantArchived map[string][]base.Z
|
2019-12-27 22:22:33 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-15 21:38:33 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
2020-08-15 21:38:33 +08:00
|
|
|
qname: "default",
|
|
|
|
want: 2,
|
|
|
|
wantRetry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: time.Now().Unix()},
|
|
|
|
{Message: m2, Score: time.Now().Unix()},
|
|
|
|
},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-15 21:38:33 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {{Message: m1, Score: t1.Unix()}},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {{Message: m2, Score: t2.Unix()}},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
2020-08-15 21:38:33 +08:00
|
|
|
qname: "default",
|
|
|
|
want: 1,
|
|
|
|
wantRetry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: time.Now().Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-15 21:38:33 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 0,
|
|
|
|
wantRetry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
{Message: m3, Score: t3.Unix()},
|
|
|
|
{Message: m4, Score: t4.Unix()},
|
|
|
|
},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
|
|
|
want: 2,
|
|
|
|
wantRetry: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
"custom": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {
|
|
|
|
{Message: m3, Score: time.Now().Unix()},
|
|
|
|
{Message: m4, Score: time.Now().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)
|
2020-08-15 21:38:33 +08:00
|
|
|
h.SeedAllRetryQueues(t, r.client, tc.retry)
|
2021-01-13 03:01:21 +08:00
|
|
|
h.SeedAllArchivedQueues(t, r.client, tc.archived)
|
2019-12-27 22:22:33 +08:00
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
got, err := r.ArchiveAllRetryTasks(tc.qname)
|
2019-12-27 22:22:33 +08:00
|
|
|
if got != tc.want || err != nil {
|
2020-08-15 21:38:33 +08:00
|
|
|
t.Errorf("(*RDB).KillAllRetryTasks(%q) = %v, %v; want %v, nil",
|
|
|
|
tc.qname, got, err, tc.want)
|
2019-12-27 22:22:33 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-08-15 21:38:33 +08:00
|
|
|
for qname, want := range tc.wantRetry {
|
|
|
|
gotRetry := h.GetRetryEntries(t, r.client, qname)
|
2020-08-29 21:54:08 +08:00
|
|
|
if diff := cmp.Diff(want, gotRetry, h.SortZSetEntryOpt, zScoreCmpOpt); diff != "" {
|
2020-08-15 21:38:33 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want,+got)\n%s",
|
|
|
|
base.RetryKey(qname), diff)
|
|
|
|
}
|
2019-12-27 22:22:33 +08:00
|
|
|
}
|
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
for qname, want := range tc.wantArchived {
|
|
|
|
gotDead := h.GetArchivedEntries(t, r.client, qname)
|
2020-08-29 21:54:08 +08:00
|
|
|
if diff := cmp.Diff(want, gotDead, h.SortZSetEntryOpt, zScoreCmpOpt); diff != "" {
|
2020-08-15 21:38:33 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want,+got)\n%s",
|
2021-01-13 03:01:21 +08:00
|
|
|
base.ArchivedKey(qname), diff)
|
2020-08-15 21:38:33 +08:00
|
|
|
}
|
2019-12-27 22:22:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 08:23:08 +08:00
|
|
|
func TestArchiveAllScheduledTasks(t *testing.T) {
|
2019-12-27 22:22:33 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-08-15 21:38:33 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
2019-12-27 22:22:33 +08:00
|
|
|
t1 := time.Now().Add(time.Minute)
|
|
|
|
t2 := time.Now().Add(time.Hour)
|
2020-08-15 21:38:33 +08:00
|
|
|
t3 := time.Now().Add(time.Hour)
|
|
|
|
t4 := time.Now().Add(time.Hour)
|
2019-12-27 22:22:33 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-15 21:38:33 +08:00
|
|
|
scheduled map[string][]base.Z
|
2021-01-13 03:01:21 +08:00
|
|
|
archived map[string][]base.Z
|
2020-08-15 21:38:33 +08:00
|
|
|
qname string
|
2019-12-27 22:22:33 +08:00
|
|
|
want int64
|
2020-08-15 21:38:33 +08:00
|
|
|
wantScheduled map[string][]base.Z
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived map[string][]base.Z
|
2019-12-27 22:22:33 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-15 21:38:33 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 2,
|
|
|
|
wantScheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: time.Now().Unix()},
|
|
|
|
{Message: m2, Score: time.Now().Unix()},
|
|
|
|
},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-15 21:38:33 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {{Message: m1, Score: t1.Unix()}},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {{Message: m2, Score: t2.Unix()}},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
2020-08-15 21:38:33 +08:00
|
|
|
qname: "default",
|
|
|
|
want: 1,
|
|
|
|
wantScheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: time.Now().Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-15 21:38:33 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
2019-12-27 22:22:33 +08:00
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 0,
|
|
|
|
wantScheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
{Message: m3, Score: t3.Unix()},
|
|
|
|
{Message: m4, Score: t4.Unix()},
|
|
|
|
},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
|
|
|
want: 2,
|
|
|
|
wantScheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
"custom": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-15 21:38:33 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {
|
|
|
|
{Message: m3, Score: time.Now().Unix()},
|
|
|
|
{Message: m4, Score: time.Now().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)
|
2020-08-15 21:38:33 +08:00
|
|
|
h.SeedAllScheduledQueues(t, r.client, tc.scheduled)
|
2021-01-13 03:01:21 +08:00
|
|
|
h.SeedAllArchivedQueues(t, r.client, tc.archived)
|
2019-12-27 22:22:33 +08:00
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
got, err := r.ArchiveAllScheduledTasks(tc.qname)
|
2019-12-27 22:22:33 +08:00
|
|
|
if got != tc.want || err != nil {
|
2020-08-15 21:38:33 +08:00
|
|
|
t.Errorf("(*RDB).KillAllScheduledTasks(%q) = %v, %v; want %v, nil",
|
|
|
|
tc.qname, got, err, tc.want)
|
2019-12-27 22:22:33 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-08-15 21:38:33 +08:00
|
|
|
for qname, want := range tc.wantScheduled {
|
|
|
|
gotScheduled := h.GetScheduledEntries(t, r.client, qname)
|
2020-08-29 21:54:08 +08:00
|
|
|
if diff := cmp.Diff(want, gotScheduled, h.SortZSetEntryOpt, zScoreCmpOpt); diff != "" {
|
2020-08-15 21:38:33 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want,+got)\n%s",
|
|
|
|
base.ScheduledKey(qname), diff)
|
|
|
|
}
|
2019-12-27 22:22:33 +08:00
|
|
|
}
|
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
for qname, want := range tc.wantArchived {
|
|
|
|
gotDead := h.GetArchivedEntries(t, r.client, qname)
|
2020-08-29 21:54:08 +08:00
|
|
|
if diff := cmp.Diff(want, gotDead, h.SortZSetEntryOpt, zScoreCmpOpt); diff != "" {
|
2020-08-15 21:38:33 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want,+got)\n%s",
|
2021-01-13 03:01:21 +08:00
|
|
|
base.ArchivedKey(qname), diff)
|
2020-08-15 21:38:33 +08:00
|
|
|
}
|
2019-12-26 23:17:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 08:23:08 +08:00
|
|
|
func TestDeleteArchivedTask(t *testing.T) {
|
2019-12-12 11:56:19 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-08-16 04:04:26 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
2019-12-12 11:56:19 +08:00
|
|
|
t1 := time.Now().Add(-5 * time.Minute)
|
|
|
|
t2 := time.Now().Add(-time.Hour)
|
2020-08-16 04:04:26 +08:00
|
|
|
t3 := time.Now().Add(-time.Hour)
|
2019-12-12 11:56:19 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2021-01-13 03:01:21 +08:00
|
|
|
archived map[string][]base.Z
|
|
|
|
qname string
|
|
|
|
id uuid.UUID
|
|
|
|
want error
|
|
|
|
wantArchived map[string][]*base.TaskMessage
|
2019-12-12 11:56:19 +08:00
|
|
|
}{
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-16 04:04:26 +08:00
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
id: m1.ID,
|
|
|
|
want: nil,
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]*base.TaskMessage{
|
2020-08-16 04:04:26 +08:00
|
|
|
"default": {m2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-16 04:04:26 +08:00
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
{Message: m3, Score: t3.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
|
|
|
id: m3.ID,
|
|
|
|
want: nil,
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]*base.TaskMessage{
|
2020-08-16 04:04:26 +08:00
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {},
|
2019-12-12 11:56:19 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-16 04:04:26 +08:00
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2021-03-13 08:23:08 +08:00
|
|
|
id: uuid.New(),
|
2020-08-16 04:04:26 +08:00
|
|
|
want: ErrTaskNotFound,
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]*base.TaskMessage{
|
2020-08-16 04:04:26 +08:00
|
|
|
"default": {m1, m2},
|
2019-12-12 11:56:19 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-16 04:04:26 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
id: m1.ID,
|
|
|
|
want: ErrTaskNotFound,
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]*base.TaskMessage{
|
2020-08-16 04:04:26 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
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
|
2021-01-13 03:01:21 +08:00
|
|
|
h.SeedAllArchivedQueues(t, r.client, tc.archived)
|
2019-12-12 11:56:19 +08:00
|
|
|
|
2021-03-13 08:23:08 +08:00
|
|
|
got := r.DeleteArchivedTask(tc.qname, tc.id)
|
2019-12-12 11:56:19 +08:00
|
|
|
if got != tc.want {
|
2021-03-13 08:23:08 +08:00
|
|
|
t.Errorf("r.DeleteArchivedTask(%q, %v) = %v, want %v", tc.qname, tc.id, got, tc.want)
|
2019-12-12 11:56:19 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
for qname, want := range tc.wantArchived {
|
|
|
|
gotDead := h.GetArchivedMessages(t, r.client, qname)
|
2020-08-16 04:04:26 +08:00
|
|
|
if diff := cmp.Diff(want, gotDead, h.SortMsgOpt); diff != "" {
|
2021-01-13 03:01:21 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.ArchivedKey(qname), diff)
|
2020-08-16 04:04:26 +08:00
|
|
|
}
|
2019-12-12 11:56:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteRetryTask(t *testing.T) {
|
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-08-16 04:04:26 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
2019-12-12 11:56:19 +08:00
|
|
|
t1 := time.Now().Add(5 * time.Minute)
|
|
|
|
t2 := time.Now().Add(time.Hour)
|
2020-08-16 04:04:26 +08:00
|
|
|
t3 := time.Now().Add(time.Hour)
|
2019-12-12 11:56:19 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-16 04:04:26 +08:00
|
|
|
retry map[string][]base.Z
|
|
|
|
qname string
|
2020-07-02 21:21:20 +08:00
|
|
|
id uuid.UUID
|
2019-12-12 11:56:19 +08:00
|
|
|
want error
|
2020-08-16 04:04:26 +08:00
|
|
|
wantRetry map[string][]*base.TaskMessage
|
2019-12-12 11:56:19 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-16 04:04:26 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
id: m1.ID,
|
|
|
|
want: nil,
|
|
|
|
wantRetry: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m2},
|
2019-12-12 11:56:19 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-16 04:04:26 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
{Message: m3, Score: t3.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
|
|
|
id: m3.ID,
|
|
|
|
want: nil,
|
|
|
|
wantRetry: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {{Message: m1, Score: t1.Unix()}},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2021-03-13 08:23:08 +08:00
|
|
|
id: uuid.New(),
|
2020-08-16 04:04:26 +08:00
|
|
|
want: ErrTaskNotFound,
|
|
|
|
wantRetry: map[string][]*base.TaskMessage{
|
|
|
|
"default": {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
|
2020-08-16 04:04:26 +08:00
|
|
|
h.SeedAllRetryQueues(t, r.client, tc.retry)
|
2019-12-12 11:56:19 +08:00
|
|
|
|
2021-03-13 08:23:08 +08:00
|
|
|
got := r.DeleteRetryTask(tc.qname, tc.id)
|
2019-12-12 11:56:19 +08:00
|
|
|
if got != tc.want {
|
2021-03-13 08:23:08 +08:00
|
|
|
t.Errorf("r.DeleteRetryTask(%q, %v) = %v, want %v", tc.qname, tc.id, got, tc.want)
|
2019-12-12 11:56:19 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-08-16 04:04:26 +08:00
|
|
|
for qname, want := range tc.wantRetry {
|
|
|
|
gotRetry := h.GetRetryMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotRetry, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.RetryKey(qname), diff)
|
|
|
|
}
|
2019-12-12 11:56:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteScheduledTask(t *testing.T) {
|
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-08-16 04:04:26 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
2019-12-12 11:56:19 +08:00
|
|
|
t1 := time.Now().Add(5 * time.Minute)
|
|
|
|
t2 := time.Now().Add(time.Hour)
|
2020-08-16 04:04:26 +08:00
|
|
|
t3 := time.Now().Add(time.Hour)
|
2019-12-12 11:56:19 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-16 04:04:26 +08:00
|
|
|
scheduled map[string][]base.Z
|
|
|
|
qname string
|
2020-07-02 21:21:20 +08:00
|
|
|
id uuid.UUID
|
2019-12-12 11:56:19 +08:00
|
|
|
want error
|
2020-08-16 04:04:26 +08:00
|
|
|
wantScheduled map[string][]*base.TaskMessage
|
2019-12-12 11:56:19 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-16 04:04:26 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
id: m1.ID,
|
|
|
|
want: nil,
|
|
|
|
wantScheduled: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: t1.Unix()},
|
|
|
|
{Message: m2, Score: t2.Unix()},
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
{Message: m3, Score: t3.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
|
|
|
id: m3.ID,
|
|
|
|
want: nil,
|
|
|
|
wantScheduled: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {},
|
2019-12-12 11:56:19 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-16 04:04:26 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {{Message: m1, Score: t1.Unix()}},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2021-03-13 08:23:08 +08:00
|
|
|
id: uuid.New(),
|
2020-08-16 04:04:26 +08:00
|
|
|
want: ErrTaskNotFound,
|
|
|
|
wantScheduled: map[string][]*base.TaskMessage{
|
|
|
|
"default": {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
|
2020-08-16 04:04:26 +08:00
|
|
|
h.SeedAllScheduledQueues(t, r.client, tc.scheduled)
|
2019-12-12 11:56:19 +08:00
|
|
|
|
2021-03-13 08:23:08 +08:00
|
|
|
got := r.DeleteScheduledTask(tc.qname, tc.id)
|
2019-12-12 11:56:19 +08:00
|
|
|
if got != tc.want {
|
2021-03-13 08:23:08 +08:00
|
|
|
t.Errorf("r.DeleteScheduledTask(%q, %v) = %v, want %v", tc.qname, tc.id, got, tc.want)
|
2019-12-12 11:56:19 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-08-16 04:04:26 +08:00
|
|
|
for qname, want := range tc.wantScheduled {
|
|
|
|
gotScheduled := h.GetScheduledMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotScheduled, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.ScheduledKey(qname), diff)
|
|
|
|
}
|
2019-12-12 11:56:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-12 22:38:01 +08:00
|
|
|
|
2021-03-13 08:23:08 +08:00
|
|
|
func TestDeletePendingTask(t *testing.T) {
|
2021-06-06 21:35:36 +08:00
|
|
|
r := setup(t)
|
|
|
|
defer r.Close()
|
2021-03-13 08:23:08 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
2021-06-06 21:35:36 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2021-03-13 08:23:08 +08:00
|
|
|
pending map[string][]*base.TaskMessage
|
|
|
|
qname string
|
|
|
|
id uuid.UUID
|
|
|
|
want error
|
|
|
|
wantPending map[string][]*base.TaskMessage
|
2021-06-06 21:35:36 +08:00
|
|
|
}{
|
|
|
|
{
|
2021-03-13 08:23:08 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
2021-06-06 21:35:36 +08:00
|
|
|
},
|
2021-03-13 08:23:08 +08:00
|
|
|
qname: "default",
|
|
|
|
id: m1.ID,
|
|
|
|
want: nil,
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {m3},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
|
|
|
id: m3.ID,
|
|
|
|
want: nil,
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
id: uuid.New(),
|
|
|
|
want: ErrTaskNotFound,
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
2021-06-06 21:35:36 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-03-13 08:23:08 +08:00
|
|
|
h.FlushDB(t, r.client)
|
|
|
|
h.SeedAllPendingQueues(t, r.client, tc.pending)
|
2021-06-06 21:35:36 +08:00
|
|
|
|
2021-03-13 08:23:08 +08:00
|
|
|
got := r.DeletePendingTask(tc.qname, tc.id)
|
|
|
|
if got != tc.want {
|
|
|
|
t.Errorf("r.DeletePendingTask(%q, %v) = %v, want %v", tc.qname, tc.id, got, tc.want)
|
2021-06-06 21:35:36 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-03-13 08:23:08 +08:00
|
|
|
for qname, want := range tc.wantPending {
|
|
|
|
gotPending := h.GetPendingMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.PendingKey(qname), diff)
|
2021-06-06 21:35:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-13 08:23:08 +08:00
|
|
|
|
2021-06-06 21:35:36 +08:00
|
|
|
func TestDeleteAllArchivedTasks(t *testing.T) {
|
2019-12-12 22:38:01 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-08-16 04:04:26 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
2019-12-12 22:38:01 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2021-01-13 03:01:21 +08:00
|
|
|
archived map[string][]base.Z
|
|
|
|
qname string
|
|
|
|
want int64
|
|
|
|
wantArchived map[string][]*base.TaskMessage
|
2019-12-12 22:38:01 +08:00
|
|
|
}{
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-16 04:04:26 +08:00
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: time.Now().Unix()},
|
|
|
|
{Message: m2, Score: time.Now().Unix()},
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
{Message: m3, Score: time.Now().Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 2,
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]*base.TaskMessage{
|
2020-08-16 04:04:26 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {m3},
|
2019-12-13 11:49:41 +08:00
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-16 04:04:26 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 0,
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]*base.TaskMessage{
|
2020-08-16 04:04:26 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
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
|
2021-01-13 03:01:21 +08:00
|
|
|
h.SeedAllArchivedQueues(t, r.client, tc.archived)
|
2019-12-12 22:38:01 +08:00
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
got, err := r.DeleteAllArchivedTasks(tc.qname)
|
2019-12-12 22:38:01 +08:00
|
|
|
if err != nil {
|
2020-08-16 04:04:26 +08:00
|
|
|
t.Errorf("r.DeleteAllDeadTasks(%q) returned error: %v", tc.qname, err)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
if got != tc.want {
|
2020-08-16 04:04:26 +08:00
|
|
|
t.Errorf("r.DeleteAllDeadTasks(%q) = %d, nil, want %d, nil", tc.qname, got, tc.want)
|
2019-12-12 22:38:01 +08:00
|
|
|
}
|
2021-01-13 03:01:21 +08:00
|
|
|
for qname, want := range tc.wantArchived {
|
|
|
|
gotDead := h.GetArchivedMessages(t, r.client, qname)
|
2020-08-16 04:04:26 +08:00
|
|
|
if diff := cmp.Diff(want, gotDead, h.SortMsgOpt); diff != "" {
|
2021-01-13 03:01:21 +08:00
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.ArchivedKey(qname), diff)
|
2020-08-16 04:04:26 +08:00
|
|
|
}
|
2019-12-12 22:38:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-06 21:35:36 +08:00
|
|
|
func TestDeleteAllArchivedTasksWithUniqueKey(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
defer r.Close()
|
|
|
|
m1 := &base.TaskMessage{
|
|
|
|
ID: uuid.New(),
|
|
|
|
Type: "task1",
|
|
|
|
Payload: nil,
|
|
|
|
Timeout: 1800,
|
|
|
|
Deadline: 0,
|
|
|
|
UniqueKey: "asynq:{default}:unique:task1:nil",
|
|
|
|
Queue: "default",
|
|
|
|
}
|
|
|
|
m2 := &base.TaskMessage{
|
|
|
|
ID: uuid.New(),
|
|
|
|
Type: "task2",
|
|
|
|
Payload: nil,
|
|
|
|
Timeout: 1800,
|
|
|
|
Deadline: 0,
|
|
|
|
UniqueKey: "asynq:{default}:unique:task2:nil",
|
|
|
|
Queue: "default",
|
|
|
|
}
|
|
|
|
m3 := h.NewTaskMessage("task3", nil)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
archived map[string][]base.Z
|
|
|
|
qname string
|
|
|
|
want int64
|
|
|
|
wantArchived map[string][]*base.TaskMessage
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
archived: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: time.Now().Unix()},
|
|
|
|
{Message: m2, Score: time.Now().Unix()},
|
|
|
|
{Message: m3, Score: time.Now().Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 3,
|
|
|
|
wantArchived: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r.client) // clean up db before each test case
|
|
|
|
h.SeedAllArchivedQueues(t, r.client, tc.archived)
|
|
|
|
var uniqueKeys []string // list of unique keys set in redis
|
|
|
|
for _, zs := range tc.archived {
|
|
|
|
for _, z := range zs {
|
|
|
|
if len(z.Message.UniqueKey) > 0 {
|
|
|
|
err := r.client.SetNX(z.Message.UniqueKey, z.Message.ID.String(), time.Minute).Err()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to set unique lock in redis: %v", err)
|
|
|
|
}
|
|
|
|
uniqueKeys = append(uniqueKeys, z.Message.UniqueKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
got, err := r.DeleteAllArchivedTasks(tc.qname)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("r.DeleteAllDeadTasks(%q) returned error: %v", tc.qname, err)
|
|
|
|
}
|
|
|
|
if got != tc.want {
|
|
|
|
t.Errorf("r.DeleteAllDeadTasks(%q) = %d, nil, want %d, nil", tc.qname, got, tc.want)
|
|
|
|
}
|
|
|
|
for qname, want := range tc.wantArchived {
|
|
|
|
gotArchived := h.GetArchivedMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotArchived, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.ArchivedKey(qname), diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, uniqueKey := range uniqueKeys {
|
|
|
|
if r.client.Exists(uniqueKey).Val() != 0 {
|
|
|
|
t.Errorf("Uniqueness lock %q still exists", uniqueKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-12 22:38:01 +08:00
|
|
|
func TestDeleteAllRetryTasks(t *testing.T) {
|
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-08-16 04:04:26 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
2019-12-12 22:38:01 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-16 04:04:26 +08:00
|
|
|
retry map[string][]base.Z
|
|
|
|
qname string
|
2020-07-13 21:29:41 +08:00
|
|
|
want int64
|
2020-08-16 04:04:26 +08:00
|
|
|
wantRetry map[string][]*base.TaskMessage
|
2019-12-12 22:38:01 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-16 04:04:26 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: time.Now().Unix()},
|
|
|
|
{Message: m2, Score: time.Now().Unix()},
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
{Message: m3, Score: time.Now().Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
|
|
|
want: 1,
|
|
|
|
wantRetry: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {},
|
2019-12-13 11:49:41 +08:00
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
{
|
2020-08-16 04:04:26 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 0,
|
|
|
|
wantRetry: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
},
|
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
|
2020-08-16 04:04:26 +08:00
|
|
|
h.SeedAllRetryQueues(t, r.client, tc.retry)
|
2019-12-12 22:38:01 +08:00
|
|
|
|
2020-08-16 04:04:26 +08:00
|
|
|
got, err := r.DeleteAllRetryTasks(tc.qname)
|
2019-12-12 22:38:01 +08:00
|
|
|
if err != nil {
|
2020-08-16 04:04:26 +08:00
|
|
|
t.Errorf("r.DeleteAllRetryTasks(%q) returned error: %v", tc.qname, err)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
if got != tc.want {
|
2020-08-16 04:04:26 +08:00
|
|
|
t.Errorf("r.DeleteAllRetryTasks(%q) = %d, nil, want %d, nil", tc.qname, got, tc.want)
|
2019-12-12 22:38:01 +08:00
|
|
|
}
|
2020-08-16 04:04:26 +08:00
|
|
|
for qname, want := range tc.wantRetry {
|
|
|
|
gotRetry := h.GetRetryMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotRetry, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.RetryKey(qname), diff)
|
|
|
|
}
|
2019-12-12 22:38:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteAllScheduledTasks(t *testing.T) {
|
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-08-16 04:04:26 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
2019-12-12 22:38:01 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-16 04:04:26 +08:00
|
|
|
scheduled map[string][]base.Z
|
|
|
|
qname string
|
2020-07-13 21:29:41 +08:00
|
|
|
want int64
|
2020-08-16 04:04:26 +08:00
|
|
|
wantScheduled map[string][]*base.TaskMessage
|
2019-12-12 22:38:01 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-16 04:04:26 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: time.Now().Add(time.Minute).Unix()},
|
|
|
|
{Message: m2, Score: time.Now().Add(time.Minute).Unix()},
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
{Message: m3, Score: time.Now().Add(time.Minute).Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 2,
|
|
|
|
wantScheduled: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
"custom": {m3},
|
2019-12-13 11:49:41 +08:00
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
{
|
2020-08-16 04:04:26 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
|
|
|
want: 0,
|
|
|
|
wantScheduled: map[string][]*base.TaskMessage{
|
|
|
|
"custom": {},
|
|
|
|
},
|
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
|
2020-08-16 04:04:26 +08:00
|
|
|
h.SeedAllScheduledQueues(t, r.client, tc.scheduled)
|
2019-12-12 22:38:01 +08:00
|
|
|
|
2020-08-16 04:04:26 +08:00
|
|
|
got, err := r.DeleteAllScheduledTasks(tc.qname)
|
2019-12-12 22:38:01 +08:00
|
|
|
if err != nil {
|
2020-08-16 04:04:26 +08:00
|
|
|
t.Errorf("r.DeleteAllScheduledTasks(%q) returned error: %v", tc.qname, err)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
if got != tc.want {
|
2020-08-16 04:04:26 +08:00
|
|
|
t.Errorf("r.DeleteAllScheduledTasks(%q) = %d, nil, want %d, nil", tc.qname, got, tc.want)
|
2019-12-12 22:38:01 +08:00
|
|
|
}
|
2020-08-16 04:04:26 +08:00
|
|
|
for qname, want := range tc.wantScheduled {
|
|
|
|
gotScheduled := h.GetScheduledMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotScheduled, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.ScheduledKey(qname), diff)
|
|
|
|
}
|
2019-12-12 22:38:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-13 22:50:03 +08:00
|
|
|
|
2021-03-13 08:23:08 +08:00
|
|
|
func TestDeleteAllPendingTasks(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
defer r.Close()
|
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
pending map[string][]*base.TaskMessage
|
|
|
|
qname string
|
|
|
|
want int64
|
|
|
|
wantPending map[string][]*base.TaskMessage
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {m3},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 2,
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
"custom": {m3},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
|
|
|
want: 0,
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r.client) // clean up db before each test case
|
|
|
|
h.SeedAllPendingQueues(t, r.client, tc.pending)
|
|
|
|
|
|
|
|
got, err := r.DeleteAllPendingTasks(tc.qname)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("r.DeleteAllPendingTasks(%q) returned error: %v", tc.qname, err)
|
|
|
|
}
|
|
|
|
if got != tc.want {
|
|
|
|
t.Errorf("r.DeleteAllPendingTasks(%q) = %d, nil, want %d, nil", tc.qname, got, tc.want)
|
|
|
|
}
|
|
|
|
for qname, want := range tc.wantPending {
|
|
|
|
gotPending := h.GetPendingMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q; (-want, +got)\n%s", base.PendingKey(qname), diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 22:50:03 +08:00
|
|
|
func TestRemoveQueue(t *testing.T) {
|
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-08-20 21:59:10 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
2020-01-13 22:50:03 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-09-05 22:03:43 +08:00
|
|
|
pending map[string][]*base.TaskMessage
|
2020-08-20 21:59:10 +08:00
|
|
|
inProgress map[string][]*base.TaskMessage
|
|
|
|
scheduled map[string][]base.Z
|
|
|
|
retry map[string][]base.Z
|
2021-01-13 03:01:21 +08:00
|
|
|
archived map[string][]base.Z
|
2020-08-20 21:59:10 +08:00
|
|
|
qname string // queue to remove
|
|
|
|
force bool
|
2020-01-13 22:50:03 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-08-20 21:59:10 +08:00
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {},
|
2020-01-13 22:50:03 +08:00
|
|
|
},
|
2020-08-20 21:59:10 +08:00
|
|
|
inProgress: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-20 21:59:10 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
2020-01-13 22:50:03 +08:00
|
|
|
},
|
2020-08-20 21:59:10 +08:00
|
|
|
qname: "custom",
|
|
|
|
force: false,
|
2020-01-13 22:50:03 +08:00
|
|
|
},
|
|
|
|
{
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-08-20 21:59:10 +08:00
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {m3},
|
2020-01-13 22:50:03 +08:00
|
|
|
},
|
2020-08-20 21:59:10 +08:00
|
|
|
inProgress: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {{Message: m4, Score: time.Now().Unix()}},
|
2020-01-13 22:50:03 +08:00
|
|
|
},
|
2020-08-20 21:59:10 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-20 21:59:10 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
|
|
|
force: true, // allow removing non-empty queue
|
2020-01-13 22:50:03 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r.client)
|
2020-09-05 22:03:43 +08:00
|
|
|
h.SeedAllPendingQueues(t, r.client, tc.pending)
|
2020-09-06 03:43:15 +08:00
|
|
|
h.SeedAllActiveQueues(t, r.client, tc.inProgress)
|
2020-08-20 21:59:10 +08:00
|
|
|
h.SeedAllScheduledQueues(t, r.client, tc.scheduled)
|
|
|
|
h.SeedAllRetryQueues(t, r.client, tc.retry)
|
2021-01-13 03:01:21 +08:00
|
|
|
h.SeedAllArchivedQueues(t, r.client, tc.archived)
|
2020-01-13 22:50:03 +08:00
|
|
|
|
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 {
|
2020-11-28 14:27:54 +08:00
|
|
|
t.Errorf("(*RDB).RemoveQueue(%q, %t) = %v, want nil",
|
|
|
|
tc.qname, tc.force, err)
|
2020-01-13 22:50:03 +08:00
|
|
|
continue
|
|
|
|
}
|
2020-08-20 21:59:10 +08:00
|
|
|
if r.client.SIsMember(base.AllQueues, tc.qname).Val() {
|
|
|
|
t.Errorf("%q is a member of %q", tc.qname, base.AllQueues)
|
2020-01-13 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
2020-08-20 21:59:10 +08:00
|
|
|
keys := []string{
|
2021-03-13 08:23:08 +08:00
|
|
|
base.PendingKey(tc.qname),
|
2020-09-06 03:43:15 +08:00
|
|
|
base.ActiveKey(tc.qname),
|
2020-08-20 21:59:10 +08:00
|
|
|
base.DeadlinesKey(tc.qname),
|
|
|
|
base.ScheduledKey(tc.qname),
|
|
|
|
base.RetryKey(tc.qname),
|
2021-01-13 03:01:21 +08:00
|
|
|
base.ArchivedKey(tc.qname),
|
2020-01-13 22:50:03 +08:00
|
|
|
}
|
2020-08-20 21:59:10 +08:00
|
|
|
for _, key := range keys {
|
|
|
|
if r.client.Exists(key).Val() != 0 {
|
|
|
|
t.Errorf("key %q still exists", key)
|
2020-01-13 22:50:03 +08:00
|
|
|
}
|
|
|
|
}
|
2021-03-13 08:23:08 +08:00
|
|
|
|
|
|
|
if n := len(r.client.Keys(base.TaskKeyPrefix(tc.qname) + "*").Val()); n != 0 {
|
|
|
|
t.Errorf("%d keys still exists for tasks", n)
|
|
|
|
}
|
2020-01-13 22:50:03 +08:00
|
|
|
}
|
|
|
|
}
|
2020-01-13 23:03:07 +08:00
|
|
|
|
|
|
|
func TestRemoveQueueError(t *testing.T) {
|
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-08-20 21:59:10 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
2020-01-13 23:03:07 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-20 21:59:10 +08:00
|
|
|
desc string
|
2020-09-05 22:03:43 +08:00
|
|
|
pending map[string][]*base.TaskMessage
|
2020-08-20 21:59:10 +08:00
|
|
|
inProgress map[string][]*base.TaskMessage
|
|
|
|
scheduled map[string][]base.Z
|
|
|
|
retry map[string][]base.Z
|
2021-01-13 03:01:21 +08:00
|
|
|
archived map[string][]base.Z
|
2020-08-20 21:59:10 +08:00
|
|
|
qname string // queue to remove
|
|
|
|
force bool
|
2020-01-13 23:03:07 +08:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "removing non-existent queue",
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-08-20 21:59:10 +08:00
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {m3},
|
|
|
|
},
|
|
|
|
inProgress: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-20 21:59:10 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
2020-01-13 23:03:07 +08:00
|
|
|
},
|
|
|
|
qname: "nonexistent",
|
|
|
|
force: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "removing non-empty queue",
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-08-20 21:59:10 +08:00
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {m3},
|
2020-01-13 23:03:07 +08:00
|
|
|
},
|
2020-08-20 21:59:10 +08:00
|
|
|
inProgress: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {{Message: m4, Score: time.Now().Unix()}},
|
|
|
|
},
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-20 21:59:10 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
2020-01-13 23:03:07 +08:00
|
|
|
force: false,
|
|
|
|
},
|
2020-08-20 21:59:10 +08:00
|
|
|
{
|
2020-09-06 03:43:15 +08:00
|
|
|
desc: "force removing queue with active tasks",
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-08-20 21:59:10 +08:00
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {m3},
|
|
|
|
},
|
|
|
|
inProgress: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
"custom": {m4},
|
|
|
|
},
|
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-20 21:59:10 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
2020-09-06 03:43:15 +08:00
|
|
|
// Even with force=true, it should error if there are active tasks.
|
2020-08-20 21:59:10 +08:00
|
|
|
force: true,
|
|
|
|
},
|
2020-01-13 23:03:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r.client)
|
2020-09-05 22:03:43 +08:00
|
|
|
h.SeedAllPendingQueues(t, r.client, tc.pending)
|
2020-09-06 03:43:15 +08:00
|
|
|
h.SeedAllActiveQueues(t, r.client, tc.inProgress)
|
2020-08-20 21:59:10 +08:00
|
|
|
h.SeedAllScheduledQueues(t, r.client, tc.scheduled)
|
|
|
|
h.SeedAllRetryQueues(t, r.client, tc.retry)
|
2021-01-13 03:01:21 +08:00
|
|
|
h.SeedAllArchivedQueues(t, r.client, tc.archived)
|
2020-01-13 23:03:07 +08:00
|
|
|
|
|
|
|
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
|
2020-09-05 22:03:43 +08:00
|
|
|
for qname, want := range tc.pending {
|
|
|
|
gotPending := h.GetPendingMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
2021-03-13 08:23:08 +08:00
|
|
|
t.Errorf("%s;mismatch found in %q; (-want,+got):\n%s", tc.desc, base.PendingKey(qname), diff)
|
2020-01-13 23:03:07 +08:00
|
|
|
}
|
|
|
|
}
|
2020-08-20 21:59:10 +08:00
|
|
|
for qname, want := range tc.inProgress {
|
2020-09-06 03:43:15 +08:00
|
|
|
gotActive := h.GetActiveMessages(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotActive, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("%s;mismatch found in %q; (-want,+got):\n%s", tc.desc, base.ActiveKey(qname), diff)
|
2020-08-20 21:59:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for qname, want := range tc.scheduled {
|
|
|
|
gotScheduled := h.GetScheduledEntries(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotScheduled, h.SortZSetEntryOpt); diff != "" {
|
|
|
|
t.Errorf("%s;mismatch found in %q; (-want,+got):\n%s", tc.desc, base.ScheduledKey(qname), diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for qname, want := range tc.retry {
|
|
|
|
gotRetry := h.GetRetryEntries(t, r.client, qname)
|
|
|
|
if diff := cmp.Diff(want, gotRetry, h.SortZSetEntryOpt); diff != "" {
|
|
|
|
t.Errorf("%s;mismatch found in %q; (-want,+got):\n%s", tc.desc, base.RetryKey(qname), diff)
|
|
|
|
}
|
|
|
|
}
|
2021-01-13 03:01:21 +08:00
|
|
|
for qname, want := range tc.archived {
|
|
|
|
gotDead := h.GetArchivedEntries(t, r.client, qname)
|
2020-08-20 21:59:10 +08:00
|
|
|
if diff := cmp.Diff(want, gotDead, h.SortZSetEntryOpt); diff != "" {
|
2021-01-13 03:01:21 +08:00
|
|
|
t.Errorf("%s;mismatch found in %q; (-want,+got):\n%s", tc.desc, base.ArchivedKey(qname), diff)
|
2020-08-20 21:59:10 +08:00
|
|
|
}
|
|
|
|
}
|
2020-01-13 23:03:07 +08:00
|
|
|
}
|
|
|
|
}
|
2020-02-02 14:22:48 +08:00
|
|
|
|
2020-04-13 08:09:58 +08:00
|
|
|
func TestListServers(t *testing.T) {
|
2020-02-02 14:22:48 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-02-02 14:22:48 +08:00
|
|
|
|
2020-02-21 23:18:22 +08:00
|
|
|
started1 := time.Now().Add(-time.Hour)
|
2020-04-13 07:42:11 +08:00
|
|
|
info1 := &base.ServerInfo{
|
2020-02-02 14:22:48 +08:00
|
|
|
Host: "do.droplet1",
|
|
|
|
PID: 1234,
|
2020-05-19 11:47:35 +08:00
|
|
|
ServerID: "server123",
|
|
|
|
Concurrency: 10,
|
|
|
|
Queues: map[string]int{"default": 1},
|
2021-03-23 21:20:54 +08:00
|
|
|
Status: "active",
|
2020-02-21 23:18:22 +08:00
|
|
|
Started: started1,
|
|
|
|
ActiveWorkerCount: 0,
|
2020-02-02 14:22:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-21 23:18:22 +08:00
|
|
|
started2 := time.Now().Add(-2 * time.Hour)
|
2020-04-13 07:42:11 +08:00
|
|
|
info2 := &base.ServerInfo{
|
2020-02-02 14:22:48 +08:00
|
|
|
Host: "do.droplet2",
|
|
|
|
PID: 9876,
|
2020-05-19 11:47:35 +08:00
|
|
|
ServerID: "server456",
|
|
|
|
Concurrency: 20,
|
|
|
|
Queues: map[string]int{"email": 1},
|
2020-02-18 22:57:39 +08:00
|
|
|
Status: "stopped",
|
2020-02-21 23:18:22 +08:00
|
|
|
Started: started2,
|
|
|
|
ActiveWorkerCount: 1,
|
2020-02-02 14:22:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
2020-05-19 11:47:35 +08:00
|
|
|
data []*base.ServerInfo
|
2020-02-02 14:22:48 +08:00
|
|
|
}{
|
2020-02-21 23:18:22 +08:00
|
|
|
{
|
2020-05-19 11:47:35 +08:00
|
|
|
data: []*base.ServerInfo{},
|
2020-02-21 23:18:22 +08:00
|
|
|
},
|
|
|
|
{
|
2020-05-19 11:47:35 +08:00
|
|
|
data: []*base.ServerInfo{info1},
|
2020-02-21 23:18:22 +08:00
|
|
|
},
|
|
|
|
{
|
2020-05-19 11:47:35 +08:00
|
|
|
data: []*base.ServerInfo{info1, info2},
|
2020-02-21 23:18:22 +08:00
|
|
|
},
|
2020-02-02 14:22:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r.client)
|
|
|
|
|
2020-05-19 11:47:35 +08:00
|
|
|
for _, info := range tc.data {
|
|
|
|
if err := r.WriteServerState(info, []*base.WorkerInfo{}, 5*time.Second); err != nil {
|
2020-02-02 14:22:48 +08:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 08:09:58 +08:00
|
|
|
got, err := r.ListServers()
|
2020-02-02 14:22:48 +08:00
|
|
|
if err != nil {
|
2020-04-13 08:09:58 +08:00
|
|
|
t.Errorf("r.ListServers returned an error: %v", err)
|
2020-02-02 14:22:48 +08:00
|
|
|
}
|
2020-05-19 11:47:35 +08:00
|
|
|
if diff := cmp.Diff(tc.data, got, h.SortServerInfoOpt); diff != "" {
|
2020-04-13 08:09:58 +08:00
|
|
|
t.Errorf("r.ListServers returned %v, want %v; (-want,+got)\n%s",
|
2020-05-19 11:47:35 +08:00
|
|
|
got, tc.data, diff)
|
2020-02-02 14:22:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-23 12:42:53 +08:00
|
|
|
|
|
|
|
func TestListWorkers(t *testing.T) {
|
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-02-23 12:42:53 +08:00
|
|
|
|
2020-05-19 11:47:35 +08:00
|
|
|
var (
|
2020-12-30 23:10:53 +08:00
|
|
|
host = "127.0.0.1"
|
|
|
|
pid = 4567
|
|
|
|
serverID = "server123"
|
2020-02-23 12:42:53 +08:00
|
|
|
|
2021-03-21 04:42:13 +08:00
|
|
|
m1 = h.NewTaskMessage("send_email", h.JSON(map[string]interface{}{"user_id": "abc123"}))
|
|
|
|
m2 = h.NewTaskMessage("gen_thumbnail", h.JSON(map[string]interface{}{"path": "some/path/to/image/file"}))
|
|
|
|
m3 = h.NewTaskMessage("reindex", h.JSON(map[string]interface{}{}))
|
2020-05-19 11:47:35 +08:00
|
|
|
)
|
2020-02-23 12:42:53 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-05-19 11:47:35 +08:00
|
|
|
data []*base.WorkerInfo
|
2020-02-23 12:42:53 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-05-19 11:47:35 +08:00
|
|
|
data: []*base.WorkerInfo{
|
2020-12-30 23:10:53 +08:00
|
|
|
{
|
|
|
|
Host: host,
|
|
|
|
PID: pid,
|
|
|
|
ServerID: serverID,
|
|
|
|
ID: m1.ID.String(),
|
|
|
|
Type: m1.Type,
|
|
|
|
Queue: m1.Queue,
|
|
|
|
Payload: m1.Payload,
|
|
|
|
Started: time.Now().Add(-1 * time.Second),
|
2021-01-28 07:55:43 +08:00
|
|
|
Deadline: time.Now().Add(30 * time.Second),
|
2020-12-30 23:10:53 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Host: host,
|
|
|
|
PID: pid,
|
|
|
|
ServerID: serverID,
|
|
|
|
ID: m2.ID.String(),
|
|
|
|
Type: m2.Type,
|
|
|
|
Queue: m2.Queue,
|
|
|
|
Payload: m2.Payload,
|
|
|
|
Started: time.Now().Add(-5 * time.Second),
|
2021-01-28 07:55:43 +08:00
|
|
|
Deadline: time.Now().Add(10 * time.Minute),
|
2020-12-30 23:10:53 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Host: host,
|
|
|
|
PID: pid,
|
|
|
|
ServerID: serverID,
|
|
|
|
ID: m3.ID.String(),
|
|
|
|
Type: m3.Type,
|
|
|
|
Queue: m3.Queue,
|
|
|
|
Payload: m3.Payload,
|
|
|
|
Started: time.Now().Add(-30 * time.Second),
|
2021-01-28 07:55:43 +08:00
|
|
|
Deadline: time.Now().Add(30 * time.Minute),
|
2020-12-30 23:10:53 +08:00
|
|
|
},
|
2020-02-23 12:42:53 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r.client)
|
|
|
|
|
2020-05-19 11:47:35 +08:00
|
|
|
err := r.WriteServerState(&base.ServerInfo{}, tc.data, time.Minute)
|
2020-02-23 12:42:53 +08:00
|
|
|
if err != nil {
|
2020-04-13 08:09:58 +08:00
|
|
|
t.Errorf("could not write server state to redis: %v", err)
|
2020-02-23 12:42:53 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
got, err := r.ListWorkers()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("(*RDB).ListWorkers() returned an error: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-05-19 11:47:35 +08:00
|
|
|
if diff := cmp.Diff(tc.data, got, h.SortWorkerInfoOpt); diff != "" {
|
|
|
|
t.Errorf("(*RDB).ListWorkers() = %v, want = %v; (-want,+got)\n%s", got, tc.data, diff)
|
2020-02-23 12:42:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-03 21:44:12 +08:00
|
|
|
|
2020-09-27 08:33:29 +08:00
|
|
|
func TestWriteListClearSchedulerEntries(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
now := time.Now().UTC()
|
|
|
|
schedulerID := "127.0.0.1:9876:abc123"
|
|
|
|
|
|
|
|
data := []*base.SchedulerEntry{
|
2021-01-21 07:03:34 +08:00
|
|
|
{
|
2020-09-27 08:33:29 +08:00
|
|
|
Spec: "* * * * *",
|
|
|
|
Type: "foo",
|
|
|
|
Payload: nil,
|
2020-10-12 01:06:28 +08:00
|
|
|
Opts: nil,
|
2020-09-27 08:33:29 +08:00
|
|
|
Next: now.Add(5 * time.Hour),
|
|
|
|
Prev: now.Add(-2 * time.Hour),
|
|
|
|
},
|
2021-01-21 07:03:34 +08:00
|
|
|
{
|
2020-09-27 08:33:29 +08:00
|
|
|
Spec: "@every 20m",
|
|
|
|
Type: "bar",
|
2021-03-21 04:42:13 +08:00
|
|
|
Payload: h.JSON(map[string]interface{}{"fiz": "baz"}),
|
2020-10-12 01:06:28 +08:00
|
|
|
Opts: nil,
|
2020-09-27 08:33:29 +08:00
|
|
|
Next: now.Add(1 * time.Minute),
|
|
|
|
Prev: now.Add(-19 * time.Minute),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.WriteSchedulerEntries(schedulerID, data, 30*time.Second); err != nil {
|
|
|
|
t.Fatalf("WriteSchedulerEnties failed: %v", err)
|
|
|
|
}
|
|
|
|
entries, err := r.ListSchedulerEntries()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ListSchedulerEntries failed: %v", err)
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(data, entries, h.SortSchedulerEntryOpt); diff != "" {
|
|
|
|
t.Errorf("ListSchedulerEntries() = %v, want %v; (-want,+got)\n%s", entries, data, diff)
|
|
|
|
}
|
|
|
|
if err := r.ClearSchedulerEntries(schedulerID); err != nil {
|
|
|
|
t.Fatalf("ClearSchedulerEntries failed: %v", err)
|
|
|
|
}
|
|
|
|
entries, err = r.ListSchedulerEntries()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ListSchedulerEntries() after clear failed: %v", err)
|
|
|
|
}
|
|
|
|
if len(entries) != 0 {
|
|
|
|
t.Errorf("found %d entries, want 0 after clearing", len(entries))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchedulerEnqueueEvents(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
|
|
|
|
var (
|
2020-12-27 06:35:25 +08:00
|
|
|
now = time.Now()
|
|
|
|
oneDayAgo = now.Add(-24 * time.Hour)
|
|
|
|
fiveHoursAgo = now.Add(-5 * time.Hour)
|
|
|
|
oneHourAgo = now.Add(-1 * time.Hour)
|
2020-09-27 08:33:29 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
entryID string
|
|
|
|
events []*base.SchedulerEnqueueEvent
|
2020-12-27 06:35:25 +08:00
|
|
|
want []*base.SchedulerEnqueueEvent
|
2020-09-27 08:33:29 +08:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
entryID: "entry123",
|
2021-01-13 03:01:21 +08:00
|
|
|
events: []*base.SchedulerEnqueueEvent{
|
|
|
|
{TaskID: "task123", EnqueuedAt: oneDayAgo},
|
|
|
|
{TaskID: "task789", EnqueuedAt: oneHourAgo},
|
2020-12-27 06:35:25 +08:00
|
|
|
{TaskID: "task456", EnqueuedAt: fiveHoursAgo},
|
|
|
|
},
|
|
|
|
// Recent events first
|
|
|
|
want: []*base.SchedulerEnqueueEvent{
|
|
|
|
{TaskID: "task789", EnqueuedAt: oneHourAgo},
|
|
|
|
{TaskID: "task456", EnqueuedAt: fiveHoursAgo},
|
|
|
|
{TaskID: "task123", EnqueuedAt: oneDayAgo},
|
2020-12-20 22:09:51 +08:00
|
|
|
},
|
2020-09-27 08:33:29 +08:00
|
|
|
},
|
|
|
|
{
|
2020-12-27 06:35:25 +08:00
|
|
|
entryID: "entry456",
|
|
|
|
events: nil,
|
|
|
|
want: nil,
|
2020-09-27 08:33:29 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
loop:
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r.client)
|
|
|
|
|
|
|
|
for _, e := range tc.events {
|
|
|
|
if err := r.RecordSchedulerEnqueueEvent(tc.entryID, e); err != nil {
|
|
|
|
t.Errorf("RecordSchedulerEnqueueEvent(%q, %v) failed: %v", tc.entryID, e, err)
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
}
|
2020-12-27 06:35:25 +08:00
|
|
|
got, err := r.ListSchedulerEnqueueEvents(tc.entryID, Pagination{Size: 20, Page: 0})
|
2020-09-27 08:33:29 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ListSchedulerEnqueueEvents(%q) failed: %v", tc.entryID, err)
|
|
|
|
continue
|
|
|
|
}
|
2020-12-27 06:35:25 +08:00
|
|
|
if diff := cmp.Diff(tc.want, got, timeCmpOpt); diff != "" {
|
2020-09-27 08:33:29 +08:00
|
|
|
t.Errorf("ListSchedulerEnqueueEvent(%q) = %v, want %v; (-want,+got)\n%s",
|
2020-12-27 06:35:25 +08:00
|
|
|
tc.entryID, got, tc.want, diff)
|
2020-09-27 08:33:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 14:32:37 +08:00
|
|
|
func TestRecordSchedulerEnqueueEventTrimsDataSet(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
var (
|
|
|
|
entryID = "entry123"
|
|
|
|
now = time.Now()
|
|
|
|
key = base.SchedulerHistoryKey(entryID)
|
|
|
|
)
|
|
|
|
|
|
|
|
// Record maximum number of events.
|
|
|
|
for i := 1; i <= maxEvents; i++ {
|
|
|
|
event := base.SchedulerEnqueueEvent{
|
|
|
|
TaskID: fmt.Sprintf("task%d", i),
|
|
|
|
EnqueuedAt: now.Add(-time.Duration(i) * time.Second),
|
|
|
|
}
|
|
|
|
if err := r.RecordSchedulerEnqueueEvent(entryID, &event); err != nil {
|
|
|
|
t.Fatalf("RecordSchedulerEnqueueEvent failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the set is full.
|
|
|
|
if n := r.client.ZCard(key).Val(); n != maxEvents {
|
|
|
|
t.Fatalf("unexpected number of events; got %d, want %d", n, maxEvents)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record one more event, should evict the oldest event.
|
|
|
|
event := base.SchedulerEnqueueEvent{
|
|
|
|
TaskID: "latest",
|
|
|
|
EnqueuedAt: now,
|
|
|
|
}
|
|
|
|
if err := r.RecordSchedulerEnqueueEvent(entryID, &event); err != nil {
|
|
|
|
t.Fatalf("RecordSchedulerEnqueueEvent failed: %v", err)
|
|
|
|
}
|
|
|
|
if n := r.client.ZCard(key).Val(); n != maxEvents {
|
|
|
|
t.Fatalf("unexpected number of events; got %d, want %d", n, maxEvents)
|
|
|
|
}
|
|
|
|
events, err := r.ListSchedulerEnqueueEvents(entryID, Pagination{Size: maxEvents})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ListSchedulerEnqueueEvents failed: %v", err)
|
|
|
|
}
|
|
|
|
if first := events[0]; first.TaskID != "latest" {
|
|
|
|
t.Errorf("unexpected first event; got %q, want %q", first.TaskID, "latest")
|
|
|
|
}
|
|
|
|
if last := events[maxEvents-1]; last.TaskID != fmt.Sprintf("task%d", maxEvents-1) {
|
|
|
|
t.Errorf("unexpected last event; got %q, want %q", last.TaskID, fmt.Sprintf("task%d", maxEvents-1))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 21:44:12 +08:00
|
|
|
func TestPause(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-12 12:36:49 +08:00
|
|
|
qname string // name of the queue to pause
|
2020-06-03 21:44:12 +08:00
|
|
|
}{
|
2020-08-12 12:36:49 +08:00
|
|
|
{qname: "default"},
|
|
|
|
{qname: "custom"},
|
2020-06-03 21:44:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r.client)
|
|
|
|
|
|
|
|
err := r.Pause(tc.qname)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Pause(%q) returned error: %v", tc.qname, err)
|
|
|
|
}
|
2020-08-13 21:54:32 +08:00
|
|
|
key := base.PausedKey(tc.qname)
|
2020-08-12 12:36:49 +08:00
|
|
|
if r.client.Exists(key).Val() == 0 {
|
|
|
|
t.Errorf("key %q does not exist", key)
|
2020-06-03 21:44:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPauseError(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-12 12:36:49 +08:00
|
|
|
desc string // test case description
|
|
|
|
paused []string // already paused queues
|
|
|
|
qname string // name of the queue to pause
|
2020-06-03 21:44:12 +08:00
|
|
|
}{
|
2020-08-12 12:36:49 +08:00
|
|
|
{"queue already paused", []string{"default", "custom"}, "default"},
|
2020-06-03 21:44:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r.client)
|
2020-08-12 12:36:49 +08:00
|
|
|
for _, qname := range tc.paused {
|
|
|
|
if err := r.Pause(qname); err != nil {
|
|
|
|
t.Fatalf("could not pause %q: %v", qname, err)
|
2020-06-03 21:44:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := r.Pause(tc.qname)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("%s; Pause(%q) returned nil: want error", tc.desc, tc.qname)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnpause(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-12 12:36:49 +08:00
|
|
|
paused []string // already paused queues
|
|
|
|
qname string // name of the queue to unpause
|
2020-06-03 21:44:12 +08:00
|
|
|
}{
|
2020-08-12 12:36:49 +08:00
|
|
|
{[]string{"default", "custom"}, "default"},
|
2020-06-03 21:44:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r.client)
|
2020-08-12 12:36:49 +08:00
|
|
|
for _, qname := range tc.paused {
|
|
|
|
if err := r.Pause(qname); err != nil {
|
|
|
|
t.Fatalf("could not pause %q: %v", qname, err)
|
2020-06-03 21:44:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := r.Unpause(tc.qname)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unpause(%q) returned error: %v", tc.qname, err)
|
|
|
|
}
|
2020-08-13 21:54:32 +08:00
|
|
|
key := base.PausedKey(tc.qname)
|
|
|
|
if r.client.Exists(key).Val() == 1 {
|
2020-08-12 12:36:49 +08:00
|
|
|
t.Errorf("key %q exists", key)
|
2020-06-03 21:44:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnpauseError(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-12 12:36:49 +08:00
|
|
|
desc string // test case description
|
|
|
|
paused []string // already paused queues
|
|
|
|
qname string // name of the queue to unpause
|
2020-06-03 21:44:12 +08:00
|
|
|
}{
|
2020-08-12 12:36:49 +08:00
|
|
|
{"queue is not paused", []string{"default"}, "custom"},
|
2020-06-03 21:44:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r.client)
|
2020-08-12 12:36:49 +08:00
|
|
|
for _, qname := range tc.paused {
|
|
|
|
if err := r.Pause(qname); err != nil {
|
|
|
|
t.Fatalf("could not pause %q: %v", qname, err)
|
2020-06-03 21:44:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := r.Unpause(tc.qname)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("%s; Unpause(%q) returned nil: want error", tc.desc, tc.qname)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|