2020-07-13 21:29:41 +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.
|
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
package asynq
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
import (
|
2021-09-02 20:56:02 +08:00
|
|
|
"context"
|
2021-05-11 12:19:57 +08:00
|
|
|
"errors"
|
2020-07-13 21:29:41 +08:00
|
|
|
"fmt"
|
2020-12-01 23:37:09 +08:00
|
|
|
"sort"
|
2020-07-13 21:29:41 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2021-05-16 22:32:47 +08:00
|
|
|
"github.com/google/uuid"
|
2020-07-13 21:29:41 +08:00
|
|
|
h "github.com/hibiken/asynq/internal/asynqtest"
|
|
|
|
"github.com/hibiken/asynq/internal/base"
|
2020-12-01 23:37:09 +08:00
|
|
|
"github.com/hibiken/asynq/internal/rdb"
|
2021-12-11 22:27:44 +08:00
|
|
|
"github.com/hibiken/asynq/internal/timeutil"
|
2020-07-13 21:29:41 +08:00
|
|
|
)
|
|
|
|
|
2020-08-21 12:17:44 +08:00
|
|
|
func TestInspectorQueues(t *testing.T) {
|
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-08-21 12:17:44 +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)
|
|
|
|
for _, qname := range tc.queues {
|
2021-09-02 20:56:02 +08:00
|
|
|
if err := r.SAdd(context.Background(), base.AllQueues, qname).Err(); err != nil {
|
2020-08-21 12:17:44 +08:00
|
|
|
t.Fatalf("could not initialize all queue set: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
got, err := inspector.Queues()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Queues() returned an error: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(tc.queues, got, h.SortStringSliceOpt); diff != "" {
|
|
|
|
t.Errorf("Queues() = %v, want %v; (-want, +got)\n%s", got, tc.queues, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-28 14:27:54 +08:00
|
|
|
func TestInspectorDeleteQueue(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
defer r.Close()
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-11-28 14:27:54 +08:00
|
|
|
defer inspector.Close()
|
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
pending map[string][]*base.TaskMessage
|
|
|
|
active 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-11-28 14:27:54 +08:00
|
|
|
qname string // queue to remove
|
|
|
|
force bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
active: 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-11-28 14:27:54 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
|
|
|
force: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {m3},
|
|
|
|
},
|
|
|
|
active: 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-11-28 14:27:54 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
|
|
|
force: true, // allow removing non-empty queue
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllPendingQueues(t, r, tc.pending)
|
|
|
|
h.SeedAllActiveQueues(t, r, tc.active)
|
|
|
|
h.SeedAllScheduledQueues(t, r, tc.scheduled)
|
|
|
|
h.SeedAllRetryQueues(t, r, tc.retry)
|
2021-01-13 03:01:21 +08:00
|
|
|
h.SeedAllArchivedQueues(t, r, tc.archived)
|
2020-11-28 14:27:54 +08:00
|
|
|
|
|
|
|
err := inspector.DeleteQueue(tc.qname, tc.force)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("DeleteQueue(%q, %t) = %v, want nil",
|
|
|
|
tc.qname, tc.force, err)
|
|
|
|
continue
|
|
|
|
}
|
2021-09-02 20:56:02 +08:00
|
|
|
if r.SIsMember(context.Background(), base.AllQueues, tc.qname).Val() {
|
2020-11-28 14:27:54 +08:00
|
|
|
t.Errorf("%q is a member of %q", tc.qname, base.AllQueues)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInspectorDeleteQueueErrorQueueNotEmpty(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
defer r.Close()
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-11-28 14:27:54 +08:00
|
|
|
defer inspector.Close()
|
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
pending map[string][]*base.TaskMessage
|
|
|
|
active 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-11-28 14:27:54 +08:00
|
|
|
qname string // queue to remove
|
|
|
|
force bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
|
|
|
},
|
|
|
|
active: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m3, m4},
|
|
|
|
},
|
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-11-28 14:27:54 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
force: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllPendingQueues(t, r, tc.pending)
|
|
|
|
h.SeedAllActiveQueues(t, r, tc.active)
|
|
|
|
h.SeedAllScheduledQueues(t, r, tc.scheduled)
|
|
|
|
h.SeedAllRetryQueues(t, r, tc.retry)
|
2021-01-13 03:01:21 +08:00
|
|
|
h.SeedAllArchivedQueues(t, r, tc.archived)
|
2020-11-28 14:27:54 +08:00
|
|
|
|
|
|
|
err := inspector.DeleteQueue(tc.qname, tc.force)
|
2021-05-11 12:19:57 +08:00
|
|
|
if !errors.Is(err, ErrQueueNotEmpty) {
|
2020-11-28 14:27:54 +08:00
|
|
|
t.Errorf("DeleteQueue(%v, %t) did not return ErrQueueNotEmpty",
|
|
|
|
tc.qname, tc.force)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInspectorDeleteQueueErrorQueueNotFound(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
defer r.Close()
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-11-28 14:27:54 +08:00
|
|
|
defer inspector.Close()
|
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
pending map[string][]*base.TaskMessage
|
|
|
|
active 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-11-28 14:27:54 +08:00
|
|
|
qname string // queue to remove
|
|
|
|
force bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
|
|
|
},
|
|
|
|
active: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m3, m4},
|
|
|
|
},
|
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-11-28 14:27:54 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "nonexistent",
|
|
|
|
force: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllPendingQueues(t, r, tc.pending)
|
|
|
|
h.SeedAllActiveQueues(t, r, tc.active)
|
|
|
|
h.SeedAllScheduledQueues(t, r, tc.scheduled)
|
|
|
|
h.SeedAllRetryQueues(t, r, tc.retry)
|
2021-01-13 03:01:21 +08:00
|
|
|
h.SeedAllArchivedQueues(t, r, tc.archived)
|
2020-11-28 14:27:54 +08:00
|
|
|
|
|
|
|
err := inspector.DeleteQueue(tc.qname, tc.force)
|
2021-05-11 12:19:57 +08:00
|
|
|
if !errors.Is(err, ErrQueueNotFound) {
|
2020-11-28 14:27:54 +08:00
|
|
|
t.Errorf("DeleteQueue(%v, %t) did not return ErrQueueNotFound",
|
|
|
|
tc.qname, tc.force)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-23 03:13:19 +08:00
|
|
|
func TestInspectorGetQueueInfo(t *testing.T) {
|
2020-07-13 21:29:41 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessage("task3", nil)
|
|
|
|
m4 := h.NewTaskMessage("task4", nil)
|
|
|
|
m5 := h.NewTaskMessageWithQueue("task5", nil, "critical")
|
2020-07-13 21:29:41 +08:00
|
|
|
m6 := h.NewTaskMessageWithQueue("task6", nil, "low")
|
|
|
|
now := time.Now()
|
|
|
|
timeCmpOpt := cmpopts.EquateApproxTime(time.Second)
|
2021-05-23 03:13:19 +08:00
|
|
|
ignoreMemUsg := cmpopts.IgnoreFields(QueueInfo{}, "MemoryUsage")
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2021-12-11 22:27:44 +08:00
|
|
|
inspector.rdb.SetClock(timeutil.NewSimulatedClock(now))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2021-12-11 22:27:44 +08:00
|
|
|
pending map[string][]*base.TaskMessage
|
|
|
|
active map[string][]*base.TaskMessage
|
|
|
|
scheduled map[string][]base.Z
|
|
|
|
retry map[string][]base.Z
|
|
|
|
archived map[string][]base.Z
|
|
|
|
completed map[string][]base.Z
|
|
|
|
processed map[string]int
|
|
|
|
failed map[string]int
|
2021-12-17 08:53:02 +08:00
|
|
|
processedTotal map[string]int
|
|
|
|
failedTotal map[string]int
|
2021-12-11 22:27:44 +08:00
|
|
|
oldestPendingMessageEnqueueTime map[string]time.Time
|
|
|
|
qname string
|
|
|
|
want *QueueInfo
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {m1},
|
|
|
|
"critical": {m5},
|
|
|
|
"low": {m6},
|
|
|
|
},
|
2020-11-28 14:27:54 +08:00
|
|
|
active: map[string][]*base.TaskMessage{
|
2020-08-17 05:51:56 +08:00
|
|
|
"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": {},
|
|
|
|
},
|
2020-08-18 21:01:38 +08:00
|
|
|
retry: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
|
|
|
},
|
2021-11-06 07:52:54 +08:00
|
|
|
completed: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
|
|
|
},
|
2020-08-17 05:51:56 +08:00
|
|
|
processed: map[string]int{
|
|
|
|
"default": 120,
|
|
|
|
"critical": 100,
|
|
|
|
"low": 42,
|
|
|
|
},
|
|
|
|
failed: map[string]int{
|
|
|
|
"default": 2,
|
|
|
|
"critical": 0,
|
|
|
|
"low": 5,
|
|
|
|
},
|
2021-12-17 08:53:02 +08:00
|
|
|
processedTotal: map[string]int{
|
|
|
|
"default": 11111,
|
|
|
|
"critical": 22222,
|
|
|
|
"low": 33333,
|
|
|
|
},
|
|
|
|
failedTotal: map[string]int{
|
|
|
|
"default": 111,
|
|
|
|
"critical": 222,
|
|
|
|
"low": 333,
|
|
|
|
},
|
2021-12-11 22:27:44 +08:00
|
|
|
oldestPendingMessageEnqueueTime: map[string]time.Time{
|
|
|
|
"default": now.Add(-15 * time.Second),
|
|
|
|
"critical": now.Add(-200 * time.Millisecond),
|
|
|
|
"low": now.Add(-30 * time.Second),
|
|
|
|
},
|
2020-08-17 05:51:56 +08:00
|
|
|
qname: "default",
|
2021-05-23 03:13:19 +08:00
|
|
|
want: &QueueInfo{
|
2021-12-17 08:53:02 +08:00
|
|
|
Queue: "default",
|
|
|
|
Latency: 15 * time.Second,
|
|
|
|
Size: 4,
|
|
|
|
Pending: 1,
|
|
|
|
Active: 1,
|
|
|
|
Scheduled: 2,
|
|
|
|
Retry: 0,
|
|
|
|
Archived: 0,
|
|
|
|
Completed: 0,
|
|
|
|
Processed: 120,
|
|
|
|
Failed: 2,
|
|
|
|
ProcessedTotal: 11111,
|
|
|
|
FailedTotal: 111,
|
|
|
|
Paused: false,
|
|
|
|
Timestamp: now,
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllPendingQueues(t, r, tc.pending)
|
|
|
|
h.SeedAllActiveQueues(t, r, tc.active)
|
|
|
|
h.SeedAllScheduledQueues(t, r, tc.scheduled)
|
|
|
|
h.SeedAllRetryQueues(t, r, tc.retry)
|
|
|
|
h.SeedAllArchivedQueues(t, r, tc.archived)
|
2021-11-06 07:52:54 +08:00
|
|
|
h.SeedAllCompletedQueues(t, r, tc.completed)
|
2021-12-11 22:27:44 +08:00
|
|
|
ctx := context.Background()
|
2020-08-17 05:51:56 +08:00
|
|
|
for qname, n := range tc.processed {
|
2021-12-17 08:53:02 +08:00
|
|
|
r.Set(ctx, base.ProcessedKey(qname, now), n, 0)
|
2020-08-17 05:51:56 +08:00
|
|
|
}
|
|
|
|
for qname, n := range tc.failed {
|
2021-12-17 08:53:02 +08:00
|
|
|
r.Set(ctx, base.FailedKey(qname, now), n, 0)
|
|
|
|
}
|
|
|
|
for qname, n := range tc.processedTotal {
|
|
|
|
r.Set(ctx, base.ProcessedTotalKey(qname), n, 0)
|
|
|
|
}
|
|
|
|
for qname, n := range tc.failedTotal {
|
|
|
|
r.Set(ctx, base.FailedTotalKey(qname), n, 0)
|
2021-12-11 22:27:44 +08:00
|
|
|
}
|
|
|
|
for qname, enqueueTime := range tc.oldestPendingMessageEnqueueTime {
|
|
|
|
if enqueueTime.IsZero() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
oldestPendingMessageID := r.LRange(ctx, base.PendingKey(qname), -1, -1).Val()[0] // get the right most msg in the list
|
|
|
|
r.HSet(ctx, base.TaskKey(qname, oldestPendingMessageID), "pending_since", enqueueTime.UnixNano())
|
2020-08-17 05:51:56 +08:00
|
|
|
}
|
|
|
|
|
2021-05-23 03:13:19 +08:00
|
|
|
got, err := inspector.GetQueueInfo(tc.qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
2021-05-23 03:13:19 +08:00
|
|
|
t.Errorf("r.GetQueueInfo(%q) = %v, %v, want %v, nil",
|
2020-08-17 05:51:56 +08:00
|
|
|
tc.qname, got, err, tc.want)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-01-27 10:36:45 +08:00
|
|
|
if diff := cmp.Diff(tc.want, got, timeCmpOpt, ignoreMemUsg); diff != "" {
|
2021-05-23 03:13:19 +08:00
|
|
|
t.Errorf("r.GetQueueInfo(%q) = %v, %v, want %v, nil; (-want, +got)\n%s",
|
2020-08-17 05:51:56 +08:00
|
|
|
tc.qname, got, err, tc.want, diff)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInspectorHistory(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
|
|
|
now := time.Now().UTC()
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-17 05:51:56 +08:00
|
|
|
qname string // queue of interest
|
|
|
|
n int // number of days
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
2020-08-17 05:51:56 +08:00
|
|
|
{"default", 90},
|
|
|
|
{"custom", 7},
|
2020-08-21 21:00:49 +08:00
|
|
|
{"default", 1},
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-09-02 20:56:02 +08:00
|
|
|
r.SAdd(context.Background(), base.AllQueues, tc.qname)
|
2020-07-13 21:29:41 +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-17 05:51:56 +08:00
|
|
|
processedKey := base.ProcessedKey(tc.qname, ts)
|
|
|
|
failedKey := base.FailedKey(tc.qname, ts)
|
2021-09-02 20:56:02 +08:00
|
|
|
r.Set(context.Background(), processedKey, (i+1)*1000, 0)
|
|
|
|
r.Set(context.Background(), failedKey, (i+1)*10, 0)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
|
2020-08-17 05:51:56 +08:00
|
|
|
got, err := inspector.History(tc.qname, tc.n)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
2020-08-17 05:51:56 +08:00
|
|
|
t.Errorf("Inspector.History(%q, %d) returned error: %v", tc.qname, tc.n, err)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(got) != tc.n {
|
2020-08-17 05:51:56 +08:00
|
|
|
t.Errorf("Inspector.History(%q, %d) returned %d daily stats, want %d",
|
|
|
|
tc.qname, tc.n, len(got), tc.n)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for i := 0; i < tc.n; i++ {
|
|
|
|
want := &DailyStats{
|
2020-08-17 05:51:56 +08:00
|
|
|
Queue: tc.qname,
|
2020-07-13 21:29:41 +08:00
|
|
|
Processed: (i + 1) * 1000,
|
|
|
|
Failed: (i + 1) * 10,
|
|
|
|
Date: now.Add(-time.Duration(i) * 24 * time.Hour),
|
|
|
|
}
|
2020-09-10 10:17:56 +08:00
|
|
|
// Allow 2 seconds difference in timestamp.
|
|
|
|
timeCmpOpt := cmpopts.EquateApproxTime(2 * time.Second)
|
2020-07-13 21:29:41 +08:00
|
|
|
if diff := cmp.Diff(want, got[i], timeCmpOpt); diff != "" {
|
|
|
|
t.Errorf("Inspector.History %d days ago data; got %+v, want %+v; (-want,+got):\n%s",
|
|
|
|
i, got[i], want, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 10:13:52 +08:00
|
|
|
func createPendingTask(msg *base.TaskMessage) *TaskInfo {
|
2021-11-06 07:52:54 +08:00
|
|
|
return newTaskInfo(msg, base.TaskStatePending, time.Now(), nil)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
|
2021-05-23 09:30:44 +08:00
|
|
|
func TestInspectorGetTaskInfo(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
m1 := h.NewTaskMessageWithQueue("task1", nil, "default")
|
|
|
|
m2 := h.NewTaskMessageWithQueue("task2", nil, "default")
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
|
|
|
m5 := h.NewTaskMessageWithQueue("task5", nil, "custom")
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
fiveMinsFromNow := now.Add(5 * time.Minute)
|
|
|
|
oneHourFromNow := now.Add(1 * time.Hour)
|
|
|
|
twoHoursAgo := now.Add(-2 * time.Hour)
|
|
|
|
|
|
|
|
fixtures := struct {
|
|
|
|
active map[string][]*base.TaskMessage
|
|
|
|
pending map[string][]*base.TaskMessage
|
|
|
|
scheduled map[string][]base.Z
|
|
|
|
retry map[string][]base.Z
|
|
|
|
archived map[string][]base.Z
|
|
|
|
}{
|
|
|
|
active: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
"custom": {m5},
|
|
|
|
},
|
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {{Message: m2, Score: fiveMinsFromNow.Unix()}},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {{Message: m3, Score: oneHourFromNow.Unix()}},
|
|
|
|
},
|
|
|
|
archived: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {{Message: m4, Score: twoHoursAgo.Unix()}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
h.SeedAllActiveQueues(t, r, fixtures.active)
|
|
|
|
h.SeedAllPendingQueues(t, r, fixtures.pending)
|
|
|
|
h.SeedAllScheduledQueues(t, r, fixtures.scheduled)
|
|
|
|
h.SeedAllRetryQueues(t, r, fixtures.retry)
|
|
|
|
h.SeedAllArchivedQueues(t, r, fixtures.archived)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
qname string
|
|
|
|
id string
|
|
|
|
want *TaskInfo
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
qname: "default",
|
2021-09-10 21:29:37 +08:00
|
|
|
id: m1.ID,
|
2021-06-14 22:31:39 +08:00
|
|
|
want: newTaskInfo(
|
|
|
|
m1,
|
|
|
|
base.TaskStateActive,
|
|
|
|
time.Time{}, // zero value for n/a
|
2021-11-06 07:52:54 +08:00
|
|
|
nil,
|
2021-06-14 22:31:39 +08:00
|
|
|
),
|
2021-05-23 09:30:44 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
qname: "default",
|
2021-09-10 21:29:37 +08:00
|
|
|
id: m2.ID,
|
2021-06-14 22:31:39 +08:00
|
|
|
want: newTaskInfo(
|
|
|
|
m2,
|
|
|
|
base.TaskStateScheduled,
|
|
|
|
fiveMinsFromNow,
|
2021-11-06 07:52:54 +08:00
|
|
|
nil,
|
2021-06-14 22:31:39 +08:00
|
|
|
),
|
2021-05-23 09:30:44 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
qname: "custom",
|
2021-09-10 21:29:37 +08:00
|
|
|
id: m3.ID,
|
2021-06-14 22:31:39 +08:00
|
|
|
want: newTaskInfo(
|
|
|
|
m3,
|
|
|
|
base.TaskStateRetry,
|
|
|
|
oneHourFromNow,
|
2021-11-06 07:52:54 +08:00
|
|
|
nil,
|
2021-06-14 22:31:39 +08:00
|
|
|
),
|
2021-05-23 09:30:44 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
qname: "custom",
|
2021-09-10 21:29:37 +08:00
|
|
|
id: m4.ID,
|
2021-06-14 22:31:39 +08:00
|
|
|
want: newTaskInfo(
|
|
|
|
m4,
|
|
|
|
base.TaskStateArchived,
|
|
|
|
time.Time{}, // zero value for n/a
|
2021-11-06 07:52:54 +08:00
|
|
|
nil,
|
2021-06-14 22:31:39 +08:00
|
|
|
),
|
2021-05-23 09:30:44 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
qname: "custom",
|
2021-09-10 21:29:37 +08:00
|
|
|
id: m5.ID,
|
2021-06-14 22:31:39 +08:00
|
|
|
want: newTaskInfo(
|
|
|
|
m5,
|
|
|
|
base.TaskStatePending,
|
|
|
|
now,
|
2021-11-06 07:52:54 +08:00
|
|
|
nil,
|
2021-06-14 22:31:39 +08:00
|
|
|
),
|
2021-05-23 09:30:44 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
|
|
|
for _, tc := range tests {
|
|
|
|
got, err := inspector.GetTaskInfo(tc.qname, tc.id)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("GetTaskInfo(%q, %q) returned error: %v", tc.qname, tc.id, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cmpOpts := []cmp.Option{
|
|
|
|
cmp.AllowUnexported(TaskInfo{}),
|
|
|
|
cmpopts.EquateApproxTime(2 * time.Second),
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(tc.want, got, cmpOpts...); diff != "" {
|
|
|
|
t.Errorf("GetTaskInfo(%q, %q) = %v, want %v; (-want, +got)\n%s", tc.qname, tc.id, got, tc.want, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInspectorGetTaskInfoError(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
m1 := h.NewTaskMessageWithQueue("task1", nil, "default")
|
|
|
|
m2 := h.NewTaskMessageWithQueue("task2", nil, "default")
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
|
|
|
m5 := h.NewTaskMessageWithQueue("task5", nil, "custom")
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
fiveMinsFromNow := now.Add(5 * time.Minute)
|
|
|
|
oneHourFromNow := now.Add(1 * time.Hour)
|
|
|
|
twoHoursAgo := now.Add(-2 * time.Hour)
|
|
|
|
|
|
|
|
fixtures := struct {
|
|
|
|
active map[string][]*base.TaskMessage
|
|
|
|
pending map[string][]*base.TaskMessage
|
|
|
|
scheduled map[string][]base.Z
|
|
|
|
retry map[string][]base.Z
|
|
|
|
archived map[string][]base.Z
|
|
|
|
}{
|
|
|
|
active: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
"custom": {m5},
|
|
|
|
},
|
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {{Message: m2, Score: fiveMinsFromNow.Unix()}},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {{Message: m3, Score: oneHourFromNow.Unix()}},
|
|
|
|
},
|
|
|
|
archived: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {{Message: m4, Score: twoHoursAgo.Unix()}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
h.SeedAllActiveQueues(t, r, fixtures.active)
|
|
|
|
h.SeedAllPendingQueues(t, r, fixtures.pending)
|
|
|
|
h.SeedAllScheduledQueues(t, r, fixtures.scheduled)
|
|
|
|
h.SeedAllRetryQueues(t, r, fixtures.retry)
|
|
|
|
h.SeedAllArchivedQueues(t, r, fixtures.archived)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
qname string
|
|
|
|
id string
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
qname: "nonexistent",
|
2021-09-10 21:29:37 +08:00
|
|
|
id: m1.ID,
|
2021-05-23 09:30:44 +08:00
|
|
|
wantErr: ErrQueueNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
qname: "default",
|
|
|
|
id: uuid.NewString(),
|
|
|
|
wantErr: ErrTaskNotFound,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
info, err := inspector.GetTaskInfo(tc.qname, tc.id)
|
|
|
|
if info != nil {
|
|
|
|
t.Errorf("GetTaskInfo(%q, %q) returned info: %v", tc.qname, tc.id, info)
|
|
|
|
}
|
|
|
|
if !errors.Is(err, tc.wantErr) {
|
|
|
|
t.Errorf("GetTaskInfo(%q, %q) returned unexpected error: %v, want %v", tc.qname, tc.id, err, tc.wantErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 22:03:43 +08:00
|
|
|
func TestInspectorListPendingTasks(t *testing.T) {
|
2020-07-13 21:29:41 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
2021-03-13 08:23:08 +08:00
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "critical")
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "low")
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-09-05 22:03:43 +08:00
|
|
|
desc string
|
|
|
|
pending map[string][]*base.TaskMessage
|
|
|
|
qname string
|
2021-05-19 10:13:52 +08:00
|
|
|
want []*TaskInfo
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "with default queue",
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-07-13 21:29:41 +08:00
|
|
|
"default": {m1, m2},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2021-05-19 10:13:52 +08:00
|
|
|
want: []*TaskInfo{
|
2020-09-05 22:03:43 +08:00
|
|
|
createPendingTask(m1),
|
|
|
|
createPendingTask(m2),
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "with named queue",
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-07-13 21:29:41 +08:00
|
|
|
"default": {m1, m2},
|
|
|
|
"critical": {m3},
|
|
|
|
"low": {m4},
|
|
|
|
},
|
|
|
|
qname: "critical",
|
2021-05-19 10:13:52 +08:00
|
|
|
want: []*TaskInfo{
|
2020-09-05 22:03:43 +08:00
|
|
|
createPendingTask(m3),
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "with empty queue",
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-07-13 21:29:41 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2021-05-19 10:13:52 +08:00
|
|
|
want: []*TaskInfo(nil),
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
2020-09-05 22:03:43 +08:00
|
|
|
for q, msgs := range tc.pending {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.SeedPendingQueue(t, r, msgs, q)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
|
2020-09-05 22:03:43 +08:00
|
|
|
got, err := inspector.ListPendingTasks(tc.qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
2020-09-05 22:03:43 +08:00
|
|
|
t.Errorf("%s; ListPendingTasks(%q) returned error: %v",
|
2020-07-13 21:29:41 +08:00
|
|
|
tc.desc, tc.qname, err)
|
|
|
|
continue
|
|
|
|
}
|
2021-05-19 10:13:52 +08:00
|
|
|
cmpOpts := []cmp.Option{
|
|
|
|
cmpopts.EquateApproxTime(2 * time.Second),
|
|
|
|
cmp.AllowUnexported(TaskInfo{}),
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(tc.want, got, cmpOpts...); diff != "" {
|
2020-09-05 22:03:43 +08:00
|
|
|
t.Errorf("%s; ListPendingTasks(%q) = %v, want %v; (-want,+got)\n%s",
|
2020-07-13 21:29:41 +08:00
|
|
|
tc.desc, tc.qname, got, tc.want, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 03:43:15 +08:00
|
|
|
func TestInspectorListActiveTasks(t *testing.T) {
|
2020-07-13 21:29:41 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
2021-03-13 08:23:08 +08:00
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-11-28 14:27:54 +08:00
|
|
|
desc string
|
|
|
|
active map[string][]*base.TaskMessage
|
|
|
|
qname string
|
2021-05-19 10:13:52 +08:00
|
|
|
want []*TaskInfo
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-09-06 03:43:15 +08:00
|
|
|
desc: "with a few active tasks",
|
2020-11-28 14:27:54 +08:00
|
|
|
active: map[string][]*base.TaskMessage{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {m3, m4},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2021-05-19 10:13:52 +08:00
|
|
|
want: []*TaskInfo{
|
2021-11-06 07:52:54 +08:00
|
|
|
newTaskInfo(m1, base.TaskStateActive, time.Time{}, nil),
|
|
|
|
newTaskInfo(m2, base.TaskStateActive, time.Time{}, nil),
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllActiveQueues(t, r, tc.active)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2020-09-06 03:43:15 +08:00
|
|
|
got, err := inspector.ListActiveTasks(tc.qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
2020-09-06 03:43:15 +08:00
|
|
|
t.Errorf("%s; ListActiveTasks(%q) returned error: %v", tc.qname, tc.desc, err)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-05-19 10:13:52 +08:00
|
|
|
if diff := cmp.Diff(tc.want, got, cmp.AllowUnexported(TaskInfo{})); diff != "" {
|
2020-09-06 03:43:15 +08:00
|
|
|
t.Errorf("%s; ListActiveTask(%q) = %v, want %v; (-want,+got)\n%s",
|
2020-08-17 05:51:56 +08:00
|
|
|
tc.desc, tc.qname, got, tc.want, diff)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 10:13:52 +08:00
|
|
|
func createScheduledTask(z base.Z) *TaskInfo {
|
2021-06-14 22:31:39 +08:00
|
|
|
return newTaskInfo(
|
|
|
|
z.Message,
|
|
|
|
base.TaskStateScheduled,
|
|
|
|
time.Unix(z.Score, 0),
|
2021-11-06 07:52:54 +08:00
|
|
|
nil,
|
2021-06-14 22:31:39 +08:00
|
|
|
)
|
2020-08-17 05:51:56 +08:00
|
|
|
}
|
|
|
|
|
2020-07-13 21:29:41 +08:00
|
|
|
func TestInspectorListScheduledTasks(t *testing.T) {
|
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessage("task3", nil)
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
2020-07-13 21:29:41 +08:00
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(2 * time.Minute).Unix()}
|
2020-08-17 05:51:56 +08:00
|
|
|
z4 := base.Z{Message: m4, Score: now.Add(2 * time.Minute).Unix()}
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
2020-08-17 05:51:56 +08:00
|
|
|
scheduled map[string][]base.Z
|
|
|
|
qname string
|
2021-05-19 10:13:52 +08:00
|
|
|
want []*TaskInfo
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
desc: "with a few scheduled tasks",
|
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {z1, z2, z3},
|
|
|
|
"custom": {z4},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2020-09-05 22:11:55 +08:00
|
|
|
// Should be sorted by NextProcessAt.
|
2021-05-19 10:13:52 +08:00
|
|
|
want: []*TaskInfo{
|
2020-07-13 21:29:41 +08:00
|
|
|
createScheduledTask(z3),
|
|
|
|
createScheduledTask(z1),
|
|
|
|
createScheduledTask(z2),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
desc: "with empty scheduled queue",
|
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2021-05-19 10:13:52 +08:00
|
|
|
want: []*TaskInfo(nil),
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllScheduledQueues(t, r, tc.scheduled)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2020-08-18 21:01:38 +08:00
|
|
|
got, err := inspector.ListScheduledTasks(tc.qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
2020-08-17 05:51:56 +08:00
|
|
|
t.Errorf("%s; ListScheduledTasks(%q) returned error: %v", tc.desc, tc.qname, err)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-05-19 10:13:52 +08:00
|
|
|
if diff := cmp.Diff(tc.want, got, cmp.AllowUnexported(TaskInfo{})); diff != "" {
|
2020-08-17 05:51:56 +08:00
|
|
|
t.Errorf("%s; ListScheduledTask(%q) = %v, want %v; (-want,+got)\n%s",
|
|
|
|
tc.desc, tc.qname, got, tc.want, diff)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 10:13:52 +08:00
|
|
|
func createRetryTask(z base.Z) *TaskInfo {
|
2021-06-14 22:31:39 +08:00
|
|
|
return newTaskInfo(
|
|
|
|
z.Message,
|
|
|
|
base.TaskStateRetry,
|
|
|
|
time.Unix(z.Score, 0),
|
2021-11-06 07:52:54 +08:00
|
|
|
nil,
|
2021-06-14 22:31:39 +08:00
|
|
|
)
|
2020-08-17 05:51:56 +08:00
|
|
|
}
|
|
|
|
|
2020-07-13 21:29:41 +08:00
|
|
|
func TestInspectorListRetryTasks(t *testing.T) {
|
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessage("task3", nil)
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
2020-07-13 21:29:41 +08:00
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(2 * time.Minute).Unix()}
|
2020-08-17 05:51:56 +08:00
|
|
|
z4 := base.Z{Message: m4, Score: now.Add(2 * time.Minute).Unix()}
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
2020-08-17 05:51:56 +08:00
|
|
|
retry map[string][]base.Z
|
|
|
|
qname string
|
2021-05-19 10:13:52 +08:00
|
|
|
want []*TaskInfo
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
desc: "with a few retry tasks",
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {z1, z2, z3},
|
|
|
|
"custom": {z4},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2020-09-05 22:11:55 +08:00
|
|
|
// Should be sorted by NextProcessAt.
|
2021-05-19 10:13:52 +08:00
|
|
|
want: []*TaskInfo{
|
2020-07-13 21:29:41 +08:00
|
|
|
createRetryTask(z3),
|
|
|
|
createRetryTask(z1),
|
|
|
|
createRetryTask(z2),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
desc: "with empty retry queue",
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2021-05-19 10:13:52 +08:00
|
|
|
want: []*TaskInfo(nil),
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
2020-08-17 05:51:56 +08:00
|
|
|
// TODO(hibiken): ErrQueueNotFound when queue doesn't exist
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllRetryQueues(t, r, tc.retry)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2020-08-18 21:01:38 +08:00
|
|
|
got, err := inspector.ListRetryTasks(tc.qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
2020-08-17 05:51:56 +08:00
|
|
|
t.Errorf("%s; ListRetryTasks(%q) returned error: %v", tc.desc, tc.qname, err)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-05-19 10:13:52 +08:00
|
|
|
if diff := cmp.Diff(tc.want, got, cmp.AllowUnexported(TaskInfo{})); diff != "" {
|
2020-08-17 05:51:56 +08:00
|
|
|
t.Errorf("%s; ListRetryTask(%q) = %v, want %v; (-want,+got)\n%s",
|
|
|
|
tc.desc, tc.qname, got, tc.want, diff)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 12:06:53 +08:00
|
|
|
func createArchivedTask(z base.Z) *TaskInfo {
|
2021-06-14 22:31:39 +08:00
|
|
|
return newTaskInfo(
|
|
|
|
z.Message,
|
|
|
|
base.TaskStateArchived,
|
|
|
|
time.Time{}, // zero value for n/a
|
2021-11-06 07:52:54 +08:00
|
|
|
nil,
|
2021-06-14 22:31:39 +08:00
|
|
|
)
|
2020-08-17 05:51:56 +08:00
|
|
|
}
|
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
func TestInspectorListArchivedTasks(t *testing.T) {
|
2020-07-13 21:29:41 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessage("task3", nil)
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
2020-07-13 21:29:41 +08:00
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(-5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(-15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(-2 * time.Minute).Unix()}
|
2020-08-17 05:51:56 +08:00
|
|
|
z4 := base.Z{Message: m4, Score: now.Add(-2 * time.Minute).Unix()}
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2021-01-13 03:01:21 +08:00
|
|
|
desc string
|
|
|
|
archived map[string][]base.Z
|
|
|
|
qname string
|
2021-05-19 12:06:53 +08:00
|
|
|
want []*TaskInfo
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
desc: "with a few archived tasks",
|
|
|
|
archived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {z1, z2, z3},
|
|
|
|
"custom": {z4},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2020-07-13 21:29:41 +08:00
|
|
|
// Should be sorted by LastFailedAt.
|
2021-05-19 12:06:53 +08:00
|
|
|
want: []*TaskInfo{
|
2021-01-13 03:01:21 +08:00
|
|
|
createArchivedTask(z2),
|
|
|
|
createArchivedTask(z1),
|
|
|
|
createArchivedTask(z3),
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
desc: "with empty archived queue",
|
|
|
|
archived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2021-05-19 12:06:53 +08:00
|
|
|
want: []*TaskInfo(nil),
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllArchivedQueues(t, r, tc.archived)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
got, err := inspector.ListArchivedTasks(tc.qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
2021-01-13 03:01:21 +08:00
|
|
|
t.Errorf("%s; ListArchivedTasks(%q) returned error: %v", tc.desc, tc.qname, err)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-05-19 12:06:53 +08:00
|
|
|
if diff := cmp.Diff(tc.want, got, cmp.AllowUnexported(TaskInfo{})); diff != "" {
|
2021-01-13 03:01:21 +08:00
|
|
|
t.Errorf("%s; ListArchivedTask(%q) = %v, want %v; (-want,+got)\n%s",
|
2020-08-17 05:51:56 +08:00
|
|
|
tc.desc, tc.qname, got, tc.want, diff)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 07:52:54 +08:00
|
|
|
func newCompletedTaskMessage(typename, qname string, retention time.Duration, completedAt time.Time) *base.TaskMessage {
|
|
|
|
msg := h.NewTaskMessageWithQueue(typename, nil, qname)
|
|
|
|
msg.Retention = int64(retention.Seconds())
|
|
|
|
msg.CompletedAt = completedAt.Unix()
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
func createCompletedTask(z base.Z) *TaskInfo {
|
|
|
|
return newTaskInfo(
|
|
|
|
z.Message,
|
|
|
|
base.TaskStateCompleted,
|
|
|
|
time.Time{}, // zero value for n/a
|
|
|
|
nil, // TODO: Test with result data
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInspectorListCompletedTasks(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
defer r.Close()
|
|
|
|
now := time.Now()
|
|
|
|
m1 := newCompletedTaskMessage("task1", "default", 1*time.Hour, now.Add(-3*time.Minute)) // Expires in 57 mins
|
|
|
|
m2 := newCompletedTaskMessage("task2", "default", 30*time.Minute, now.Add(-10*time.Minute)) // Expires in 20 mins
|
|
|
|
m3 := newCompletedTaskMessage("task3", "default", 2*time.Hour, now.Add(-30*time.Minute)) // Expires in 90 mins
|
|
|
|
m4 := newCompletedTaskMessage("task4", "custom", 15*time.Minute, now.Add(-2*time.Minute)) // Expires in 13 mins
|
|
|
|
z1 := base.Z{Message: m1, Score: m1.CompletedAt + m1.Retention}
|
|
|
|
z2 := base.Z{Message: m2, Score: m2.CompletedAt + m2.Retention}
|
|
|
|
z3 := base.Z{Message: m3, Score: m3.CompletedAt + m3.Retention}
|
|
|
|
z4 := base.Z{Message: m4, Score: m4.CompletedAt + m4.Retention}
|
|
|
|
|
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
completed map[string][]base.Z
|
|
|
|
qname string
|
|
|
|
want []*TaskInfo
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "with a few completed tasks",
|
|
|
|
completed: map[string][]base.Z{
|
|
|
|
"default": {z1, z2, z3},
|
|
|
|
"custom": {z4},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
// Should be sorted by expiration time (CompletedAt + Retention).
|
|
|
|
want: []*TaskInfo{
|
|
|
|
createCompletedTask(z2),
|
|
|
|
createCompletedTask(z1),
|
|
|
|
createCompletedTask(z3),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "with empty completed queue",
|
|
|
|
completed: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: []*TaskInfo(nil),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllCompletedQueues(t, r, tc.completed)
|
|
|
|
|
|
|
|
got, err := inspector.ListCompletedTasks(tc.qname)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s; ListCompletedTasks(%q) returned error: %v", tc.desc, tc.qname, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(tc.want, got, cmp.AllowUnexported(TaskInfo{})); diff != "" {
|
|
|
|
t.Errorf("%s; ListCompletedTasks(%q) = %v, want %v; (-want,+got)\n%s",
|
|
|
|
tc.desc, tc.qname, got, tc.want, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-13 21:29:41 +08:00
|
|
|
func TestInspectorListPagination(t *testing.T) {
|
|
|
|
// Create 100 tasks.
|
|
|
|
var msgs []*base.TaskMessage
|
|
|
|
for i := 0; i <= 99; i++ {
|
|
|
|
msgs = append(msgs,
|
2021-01-29 00:59:13 +08:00
|
|
|
h.NewTaskMessage(fmt.Sprintf("task%d", i), nil))
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
h.SeedPendingQueue(t, r, msgs, base.DefaultQueueName)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
page int
|
|
|
|
pageSize int
|
2021-05-19 10:13:52 +08:00
|
|
|
want []*TaskInfo
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
page: 1,
|
|
|
|
pageSize: 5,
|
2021-05-19 10:13:52 +08:00
|
|
|
want: []*TaskInfo{
|
2020-09-05 22:03:43 +08:00
|
|
|
createPendingTask(msgs[0]),
|
|
|
|
createPendingTask(msgs[1]),
|
|
|
|
createPendingTask(msgs[2]),
|
|
|
|
createPendingTask(msgs[3]),
|
|
|
|
createPendingTask(msgs[4]),
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
page: 3,
|
|
|
|
pageSize: 10,
|
2021-05-19 10:13:52 +08:00
|
|
|
want: []*TaskInfo{
|
2020-09-05 22:03:43 +08:00
|
|
|
createPendingTask(msgs[20]),
|
|
|
|
createPendingTask(msgs[21]),
|
|
|
|
createPendingTask(msgs[22]),
|
|
|
|
createPendingTask(msgs[23]),
|
|
|
|
createPendingTask(msgs[24]),
|
|
|
|
createPendingTask(msgs[25]),
|
|
|
|
createPendingTask(msgs[26]),
|
|
|
|
createPendingTask(msgs[27]),
|
|
|
|
createPendingTask(msgs[28]),
|
|
|
|
createPendingTask(msgs[29]),
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2020-09-05 22:03:43 +08:00
|
|
|
got, err := inspector.ListPendingTasks("default", Page(tc.page), PageSize(tc.pageSize))
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
2020-09-05 22:03:43 +08:00
|
|
|
t.Errorf("ListPendingTask('default') returned error: %v", err)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-05-19 10:13:52 +08:00
|
|
|
cmpOpts := []cmp.Option{
|
|
|
|
cmpopts.EquateApproxTime(2 * time.Second),
|
|
|
|
cmp.AllowUnexported(TaskInfo{}),
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(tc.want, got, cmpOpts...); diff != "" {
|
2020-09-05 22:03:43 +08:00
|
|
|
t.Errorf("ListPendingTask('default') = %v, want %v; (-want,+got)\n%s",
|
2020-07-13 21:29:41 +08:00
|
|
|
got, tc.want, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 20:59:30 +08:00
|
|
|
func TestInspectorListTasksQueueNotFoundError(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
qname string
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
qname: "nonexistent",
|
|
|
|
wantErr: ErrQueueNotFound,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r)
|
|
|
|
|
|
|
|
if _, err := inspector.ListActiveTasks(tc.qname); !errors.Is(err, tc.wantErr) {
|
|
|
|
t.Errorf("ListActiveTasks(%q) returned error %v, want %v", tc.qname, err, tc.wantErr)
|
|
|
|
}
|
|
|
|
if _, err := inspector.ListPendingTasks(tc.qname); !errors.Is(err, tc.wantErr) {
|
|
|
|
t.Errorf("ListPendingTasks(%q) returned error %v, want %v", tc.qname, err, tc.wantErr)
|
|
|
|
}
|
|
|
|
if _, err := inspector.ListScheduledTasks(tc.qname); !errors.Is(err, tc.wantErr) {
|
|
|
|
t.Errorf("ListScheduledTasks(%q) returned error %v, want %v", tc.qname, err, tc.wantErr)
|
|
|
|
}
|
|
|
|
if _, err := inspector.ListRetryTasks(tc.qname); !errors.Is(err, tc.wantErr) {
|
|
|
|
t.Errorf("ListRetryTasks(%q) returned error %v, want %v", tc.qname, err, tc.wantErr)
|
|
|
|
}
|
|
|
|
if _, err := inspector.ListArchivedTasks(tc.qname); !errors.Is(err, tc.wantErr) {
|
|
|
|
t.Errorf("ListArchivedTasks(%q) returned error %v, want %v", tc.qname, err, tc.wantErr)
|
|
|
|
}
|
2021-11-06 07:52:54 +08:00
|
|
|
if _, err := inspector.ListCompletedTasks(tc.qname); !errors.Is(err, tc.wantErr) {
|
|
|
|
t.Errorf("ListCompletedTasks(%q) returned error %v, want %v", tc.qname, err, tc.wantErr)
|
|
|
|
}
|
2021-05-19 20:59:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 07:03:34 +08:00
|
|
|
func TestInspectorDeleteAllPendingTasks(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessage("task3", nil)
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
2021-01-21 07:03:34 +08:00
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2021-01-21 07:03:34 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
pending map[string][]*base.TaskMessage
|
|
|
|
qname string
|
|
|
|
want int
|
|
|
|
wantPending map[string][]*base.TaskMessage
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2, m3},
|
|
|
|
"custom": {m4},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 3,
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
"custom": {m4},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2, m3},
|
|
|
|
"custom": {m4},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
|
|
|
want: 1,
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2, m3},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllPendingQueues(t, r, tc.pending)
|
2021-01-21 07:03:34 +08:00
|
|
|
|
|
|
|
got, err := inspector.DeleteAllPendingTasks(tc.qname)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("DeleteAllPendingTasks(%q) returned error: %v", tc.qname, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if got != tc.want {
|
|
|
|
t.Errorf("DeleteAllPendingTasks(%q) = %d, want %d", tc.qname, got, tc.want)
|
|
|
|
}
|
|
|
|
|
|
|
|
for qname, want := range tc.wantPending {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotPending := h.GetPendingMessages(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
2021-01-21 07:03:34 +08:00
|
|
|
t.Errorf("unexpected pending tasks in queue %q: (-want, +got)\n%s", qname, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-13 21:29:41 +08:00
|
|
|
func TestInspectorDeleteAllScheduledTasks(t *testing.T) {
|
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessage("task3", nil)
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
2020-07-13 21:29:41 +08:00
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(2 * time.Minute).Unix()}
|
2020-08-17 05:51:56 +08:00
|
|
|
z4 := base.Z{Message: m4, Score: now.Add(2 * time.Minute).Unix()}
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-18 21:46:19 +08:00
|
|
|
scheduled map[string][]base.Z
|
|
|
|
qname string
|
|
|
|
want int
|
|
|
|
wantScheduled map[string][]base.Z
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {z1, z2, z3},
|
|
|
|
"custom": {z4},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 3,
|
2020-08-18 21:46:19 +08:00
|
|
|
wantScheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {z4},
|
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 0,
|
2020-08-18 21:46:19 +08:00
|
|
|
wantScheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllScheduledQueues(t, r, tc.scheduled)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2020-08-17 05:51:56 +08:00
|
|
|
got, err := inspector.DeleteAllScheduledTasks(tc.qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
2020-08-17 05:51:56 +08:00
|
|
|
t.Errorf("DeleteAllScheduledTasks(%q) returned error: %v", tc.qname, err)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if got != tc.want {
|
2020-08-17 05:51:56 +08:00
|
|
|
t.Errorf("DeleteAllScheduledTasks(%q) = %d, want %d", tc.qname, got, tc.want)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2020-08-18 21:46:19 +08:00
|
|
|
for qname, want := range tc.wantScheduled {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotScheduled := h.GetScheduledEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotScheduled, h.SortZSetEntryOpt); diff != "" {
|
2020-08-18 21:46:19 +08:00
|
|
|
t.Errorf("unexpected scheduled tasks in queue %q: (-want, +got)\n%s", qname, diff)
|
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInspectorDeleteAllRetryTasks(t *testing.T) {
|
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessage("task3", nil)
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
2020-07-13 21:29:41 +08:00
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(2 * time.Minute).Unix()}
|
2020-08-17 05:51:56 +08:00
|
|
|
z4 := base.Z{Message: m4, Score: now.Add(2 * time.Minute).Unix()}
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-18 21:46:19 +08:00
|
|
|
retry map[string][]base.Z
|
|
|
|
qname string
|
|
|
|
want int
|
|
|
|
wantRetry map[string][]base.Z
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {z1, z2, z3},
|
|
|
|
"custom": {z4},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2020-07-13 21:29:41 +08:00
|
|
|
want: 3,
|
2020-08-18 21:46:19 +08:00
|
|
|
wantRetry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {z4},
|
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2020-07-13 21:29:41 +08:00
|
|
|
want: 0,
|
2020-08-18 21:46:19 +08:00
|
|
|
wantRetry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllRetryQueues(t, r, tc.retry)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2020-08-17 05:51:56 +08:00
|
|
|
got, err := inspector.DeleteAllRetryTasks(tc.qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
2020-08-17 05:51:56 +08:00
|
|
|
t.Errorf("DeleteAllRetryTasks(%q) returned error: %v", tc.qname, err)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if got != tc.want {
|
2020-08-17 05:51:56 +08:00
|
|
|
t.Errorf("DeleteAllRetryTasks(%q) = %d, want %d", tc.qname, got, tc.want)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2020-08-18 21:46:19 +08:00
|
|
|
for qname, want := range tc.wantRetry {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotRetry := h.GetRetryEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotRetry, h.SortZSetEntryOpt); diff != "" {
|
2020-08-18 21:46:19 +08:00
|
|
|
t.Errorf("unexpected retry tasks in queue %q: (-want, +got)\n%s", qname, diff)
|
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
func TestInspectorDeleteAllArchivedTasks(t *testing.T) {
|
2020-07-13 21:29:41 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessage("task3", nil)
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
2020-07-13 21:29:41 +08:00
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(2 * time.Minute).Unix()}
|
2020-08-17 05:51:56 +08:00
|
|
|
z4 := base.Z{Message: m4, Score: now.Add(2 * time.Minute).Unix()}
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2021-01-13 03:01:21 +08:00
|
|
|
archived map[string][]base.Z
|
|
|
|
qname string
|
|
|
|
want int
|
|
|
|
wantArchived map[string][]base.Z
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {z1, z2, z3},
|
|
|
|
"custom": {z4},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 3,
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-18 21:46:19 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {z4},
|
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 0,
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-18 21:46:19 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllArchivedQueues(t, r, tc.archived)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
got, err := inspector.DeleteAllArchivedTasks(tc.qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
2021-01-13 03:01:21 +08:00
|
|
|
t.Errorf("DeleteAllArchivedTasks(%q) returned error: %v", tc.qname, err)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if got != tc.want {
|
2021-01-13 03:01:21 +08:00
|
|
|
t.Errorf("DeleteAllArchivedTasks(%q) = %d, want %d", tc.qname, got, tc.want)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2021-01-13 03:01:21 +08:00
|
|
|
for qname, want := range tc.wantArchived {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotArchived := h.GetArchivedEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotArchived, h.SortZSetEntryOpt); diff != "" {
|
2021-01-13 03:01:21 +08:00
|
|
|
t.Errorf("unexpected archived tasks in queue %q: (-want, +got)\n%s", qname, diff)
|
2020-08-18 21:46:19 +08:00
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 07:52:54 +08:00
|
|
|
func TestInspectorDeleteAllCompletedTasks(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
defer r.Close()
|
|
|
|
now := time.Now()
|
|
|
|
m1 := newCompletedTaskMessage("task1", "default", 30*time.Minute, now.Add(-2*time.Minute))
|
|
|
|
m2 := newCompletedTaskMessage("task2", "default", 30*time.Minute, now.Add(-5*time.Minute))
|
|
|
|
m3 := newCompletedTaskMessage("task3", "default", 30*time.Minute, now.Add(-10*time.Minute))
|
|
|
|
m4 := newCompletedTaskMessage("task4", "custom", 30*time.Minute, now.Add(-3*time.Minute))
|
|
|
|
z1 := base.Z{Message: m1, Score: m1.CompletedAt + m1.Retention}
|
|
|
|
z2 := base.Z{Message: m2, Score: m2.CompletedAt + m2.Retention}
|
|
|
|
z3 := base.Z{Message: m3, Score: m3.CompletedAt + m3.Retention}
|
|
|
|
z4 := base.Z{Message: m4, Score: m4.CompletedAt + m4.Retention}
|
|
|
|
|
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
completed map[string][]base.Z
|
|
|
|
qname string
|
|
|
|
want int
|
|
|
|
wantCompleted map[string][]base.Z
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
completed: map[string][]base.Z{
|
|
|
|
"default": {z1, z2, z3},
|
|
|
|
"custom": {z4},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 3,
|
|
|
|
wantCompleted: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {z4},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
completed: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 0,
|
|
|
|
wantCompleted: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllCompletedQueues(t, r, tc.completed)
|
|
|
|
|
|
|
|
got, err := inspector.DeleteAllCompletedTasks(tc.qname)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("DeleteAllCompletedTasks(%q) returned error: %v", tc.qname, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if got != tc.want {
|
|
|
|
t.Errorf("DeleteAllCompletedTasks(%q) = %d, want %d", tc.qname, got, tc.want)
|
|
|
|
}
|
|
|
|
for qname, want := range tc.wantCompleted {
|
|
|
|
gotCompleted := h.GetCompletedEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotCompleted, h.SortZSetEntryOpt); diff != "" {
|
|
|
|
t.Errorf("unexpected completed tasks in queue %q: (-want, +got)\n%s", qname, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 07:03:34 +08:00
|
|
|
func TestInspectorArchiveAllPendingTasks(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessage("task3", nil)
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
2021-01-21 07:03:34 +08:00
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(15 * time.Minute).Unix()}
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2022-01-06 01:07:42 +08:00
|
|
|
inspector.rdb.SetClock(timeutil.NewSimulatedClock(now))
|
2021-01-21 07:03:34 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
pending map[string][]*base.TaskMessage
|
|
|
|
archived map[string][]base.Z
|
|
|
|
qname string
|
|
|
|
want int
|
|
|
|
wantPending map[string][]*base.TaskMessage
|
|
|
|
wantArchived map[string][]base.Z
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2, m3},
|
|
|
|
"custom": {m4},
|
|
|
|
},
|
|
|
|
archived: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 3,
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
"custom": {m4},
|
|
|
|
},
|
|
|
|
wantArchived: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
base.Z{Message: m1, Score: now.Unix()},
|
|
|
|
base.Z{Message: m2, Score: now.Unix()},
|
|
|
|
base.Z{Message: m3, Score: now.Unix()},
|
|
|
|
},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
archived: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 0,
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
wantArchived: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
2021-03-13 08:23:08 +08:00
|
|
|
"default": {m3},
|
2021-01-21 07:03:34 +08:00
|
|
|
},
|
|
|
|
archived: map[string][]base.Z{
|
|
|
|
"default": {z1, z2},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2021-03-13 08:23:08 +08:00
|
|
|
want: 1,
|
2021-01-21 07:03:34 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
wantArchived: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
z1,
|
|
|
|
z2,
|
|
|
|
base.Z{Message: m3, Score: now.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllPendingQueues(t, r, tc.pending)
|
|
|
|
h.SeedAllArchivedQueues(t, r, tc.archived)
|
2021-01-21 07:03:34 +08:00
|
|
|
|
|
|
|
got, err := inspector.ArchiveAllPendingTasks(tc.qname)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ArchiveAllPendingTasks(%q) returned error: %v", tc.qname, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if got != tc.want {
|
|
|
|
t.Errorf("ArchiveAllPendingTasks(%q) = %d, want %d", tc.qname, got, tc.want)
|
|
|
|
}
|
|
|
|
for qname, want := range tc.wantPending {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotPending := h.GetPendingMessages(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
2021-01-21 07:03:34 +08:00
|
|
|
t.Errorf("unexpected pending tasks in queue %q: (-want, +got)\n%s", qname, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for qname, want := range tc.wantArchived {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotArchived := h.GetArchivedEntries(t, r, qname)
|
2022-01-06 01:07:42 +08:00
|
|
|
if diff := cmp.Diff(want, gotArchived, h.SortZSetEntryOpt); diff != "" {
|
2021-01-21 07:03:34 +08:00
|
|
|
t.Errorf("unexpected archived tasks in queue %q: (-want, +got)\n%s", qname, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInspectorArchiveAllScheduledTasks(t *testing.T) {
|
2020-07-13 21:29:41 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessage("task3", nil)
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
2020-07-13 21:29:41 +08:00
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(2 * time.Minute).Unix()}
|
2020-08-17 05:51:56 +08:00
|
|
|
z4 := base.Z{Message: m4, Score: now.Add(2 * time.Minute).Unix()}
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2022-01-06 01:07:42 +08:00
|
|
|
inspector.rdb.SetClock(timeutil.NewSimulatedClock(now))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-17 05:51:56 +08:00
|
|
|
scheduled map[string][]base.Z
|
2021-01-13 03:01:21 +08:00
|
|
|
archived map[string][]base.Z
|
2020-08-17 05:51:56 +08:00
|
|
|
qname string
|
|
|
|
want int
|
|
|
|
wantScheduled map[string][]base.Z
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived map[string][]base.Z
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {z1, z2, z3},
|
|
|
|
"custom": {z4},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
want: 3,
|
|
|
|
wantScheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {z4},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {
|
|
|
|
base.Z{Message: m1, Score: now.Unix()},
|
|
|
|
base.Z{Message: m2, Score: now.Unix()},
|
|
|
|
base.Z{Message: m3, Score: now.Unix()},
|
|
|
|
},
|
|
|
|
"custom": {},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-18 21:01:38 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {z1, z2},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-18 21:01:38 +08:00
|
|
|
"default": {z3},
|
|
|
|
},
|
|
|
|
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-18 21:01:38 +08:00
|
|
|
"default": {
|
|
|
|
z3,
|
|
|
|
base.Z{Message: m1, Score: now.Unix()},
|
|
|
|
base.Z{Message: m2, Score: now.Unix()},
|
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-18 21:01:38 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-18 21:01:38 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
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-18 21:01:38 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
{
|
2020-08-18 21:01:38 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-18 21:01:38 +08:00
|
|
|
"default": {z1, z2},
|
|
|
|
},
|
|
|
|
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-18 21:01:38 +08:00
|
|
|
"default": {z1, z2},
|
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllScheduledQueues(t, r, tc.scheduled)
|
|
|
|
h.SeedAllArchivedQueues(t, r, tc.archived)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
got, err := inspector.ArchiveAllScheduledTasks(tc.qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
2021-01-21 07:03:34 +08:00
|
|
|
t.Errorf("ArchiveAllScheduledTasks(%q) returned error: %v", tc.qname, err)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if got != tc.want {
|
2021-01-21 07:03:34 +08:00
|
|
|
t.Errorf("ArchiveAllScheduledTasks(%q) = %d, want %d", tc.qname, got, tc.want)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2020-08-17 05:51:56 +08:00
|
|
|
for qname, want := range tc.wantScheduled {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotScheduled := h.GetScheduledEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotScheduled, h.SortZSetEntryOpt); diff != "" {
|
2020-08-18 21:46:19 +08:00
|
|
|
t.Errorf("unexpected scheduled tasks in queue %q: (-want, +got)\n%s", qname, diff)
|
2020-08-17 05:51:56 +08:00
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2021-01-13 03:01:21 +08:00
|
|
|
for qname, want := range tc.wantArchived {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotArchived := h.GetArchivedEntries(t, r, qname)
|
2022-01-06 01:07:42 +08:00
|
|
|
if diff := cmp.Diff(want, gotArchived, h.SortZSetEntryOpt); diff != "" {
|
2021-01-13 03:01:21 +08:00
|
|
|
t.Errorf("unexpected archived tasks in queue %q: (-want, +got)\n%s", qname, diff)
|
2020-08-17 05:51:56 +08:00
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 07:03:34 +08:00
|
|
|
func TestInspectorArchiveAllRetryTasks(t *testing.T) {
|
2020-07-13 21:29:41 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessage("task3", nil)
|
|
|
|
m4 := h.NewTaskMessageWithQueue("task4", nil, "custom")
|
2020-07-13 21:29:41 +08:00
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(2 * time.Minute).Unix()}
|
2020-08-17 05:51:56 +08:00
|
|
|
z4 := base.Z{Message: m4, Score: now.Add(2 * time.Minute).Unix()}
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2022-01-06 01:07:42 +08:00
|
|
|
inspector.rdb.SetClock(timeutil.NewSimulatedClock(now))
|
2020-07-13 21:29:41 +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 int
|
|
|
|
wantRetry map[string][]base.Z
|
|
|
|
wantArchived map[string][]base.Z
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {z1, z2, z3},
|
|
|
|
"custom": {z4},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2020-07-13 21:29:41 +08:00
|
|
|
want: 3,
|
2020-08-17 05:51:56 +08:00
|
|
|
wantRetry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {z4},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {
|
|
|
|
base.Z{Message: m1, Score: now.Unix()},
|
|
|
|
base.Z{Message: m2, Score: now.Unix()},
|
|
|
|
base.Z{Message: m3, Score: now.Unix()},
|
|
|
|
},
|
|
|
|
"custom": {},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {z1, z2},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {z3},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2020-07-13 21:29:41 +08:00
|
|
|
want: 2,
|
2020-08-17 05:51:56 +08:00
|
|
|
wantRetry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {
|
|
|
|
z3,
|
|
|
|
base.Z{Message: m1, Score: now.Unix()},
|
|
|
|
base.Z{Message: m2, Score: now.Unix()},
|
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {z1, z2},
|
|
|
|
},
|
2020-08-29 21:54:08 +08:00
|
|
|
qname: "default",
|
|
|
|
want: 0,
|
2020-08-17 05:51:56 +08:00
|
|
|
wantRetry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {z1, z2},
|
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllRetryQueues(t, r, tc.retry)
|
|
|
|
h.SeedAllArchivedQueues(t, r, tc.archived)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
got, err := inspector.ArchiveAllRetryTasks(tc.qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
2021-01-21 07:03:34 +08:00
|
|
|
t.Errorf("ArchiveAllRetryTasks(%q) returned error: %v", tc.qname, err)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if got != tc.want {
|
2021-01-21 07:03:34 +08:00
|
|
|
t.Errorf("ArchiveAllRetryTasks(%q) = %d, want %d", tc.qname, got, tc.want)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2020-08-17 05:51:56 +08:00
|
|
|
for qname, want := range tc.wantRetry {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotRetry := h.GetRetryEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotRetry, h.SortZSetEntryOpt); diff != "" {
|
2020-08-18 21:46:19 +08:00
|
|
|
t.Errorf("unexpected retry tasks in queue %q: (-want, +got)\n%s", qname, diff)
|
2020-08-17 05:51:56 +08:00
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2021-01-13 03:01:21 +08:00
|
|
|
for qname, want := range tc.wantArchived {
|
2021-01-29 00:59:13 +08:00
|
|
|
wantArchived := h.GetArchivedEntries(t, r, qname)
|
2022-01-06 01:07:42 +08:00
|
|
|
if diff := cmp.Diff(want, wantArchived, h.SortZSetEntryOpt); diff != "" {
|
2021-01-13 03:01:21 +08:00
|
|
|
t.Errorf("unexpected archived tasks in queue %q: (-want, +got)\n%s", qname, diff)
|
2020-08-17 05:51:56 +08:00
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 04:35:52 +08:00
|
|
|
func TestInspectorRunAllScheduledTasks(t *testing.T) {
|
2020-07-13 21:29:41 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessageWithQueue("task2", nil, "critical")
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "low")
|
|
|
|
m4 := h.NewTaskMessage("task4", nil)
|
2020-07-13 21:29:41 +08:00
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(2 * time.Minute).Unix()}
|
2020-08-17 05:51:56 +08:00
|
|
|
z4 := base.Z{Message: m4, Score: now.Add(2 * time.Minute).Unix()}
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-17 05:51:56 +08:00
|
|
|
scheduled map[string][]base.Z
|
2020-09-05 22:03:43 +08:00
|
|
|
pending map[string][]*base.TaskMessage
|
2020-08-17 05:51:56 +08:00
|
|
|
qname string
|
|
|
|
want int
|
|
|
|
wantScheduled map[string][]base.Z
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending map[string][]*base.TaskMessage
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {z1, z4},
|
|
|
|
"critical": {z2},
|
|
|
|
"low": {z3},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-07-13 21:29:41 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
|
|
|
},
|
2020-08-17 05:51:56 +08:00
|
|
|
qname: "default",
|
|
|
|
want: 2,
|
|
|
|
wantScheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"critical": {z2},
|
|
|
|
"low": {z3},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {m1, m4},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {z1},
|
|
|
|
"critical": {z2},
|
|
|
|
"low": {z3},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-07-13 21:29:41 +08:00
|
|
|
"default": {m4},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
|
|
|
},
|
2020-08-17 05:51:56 +08:00
|
|
|
qname: "default",
|
|
|
|
want: 1,
|
|
|
|
wantScheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"critical": {z2},
|
|
|
|
"low": {z3},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-07-13 21:29:41 +08:00
|
|
|
"default": {m4, m1},
|
2020-08-17 05:51:56 +08:00
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-07-13 21:29:41 +08:00
|
|
|
"default": {m1, m4},
|
|
|
|
},
|
2020-08-17 05:51:56 +08:00
|
|
|
qname: "default",
|
|
|
|
want: 0,
|
|
|
|
wantScheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-07-13 21:29:41 +08:00
|
|
|
"default": {m1, m4},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllScheduledQueues(t, r, tc.scheduled)
|
|
|
|
h.SeedAllPendingQueues(t, r, tc.pending)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2020-09-06 04:35:52 +08:00
|
|
|
got, err := inspector.RunAllScheduledTasks(tc.qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
2020-09-06 04:35:52 +08:00
|
|
|
t.Errorf("RunAllScheduledTasks(%q) returned error: %v", tc.qname, err)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if got != tc.want {
|
2020-09-06 04:35:52 +08:00
|
|
|
t.Errorf("RunAllScheduledTasks(%q) = %d, want %d", tc.qname, got, tc.want)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2020-08-17 05:51:56 +08:00
|
|
|
for qname, want := range tc.wantScheduled {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotScheduled := h.GetScheduledEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotScheduled, h.SortZSetEntryOpt); diff != "" {
|
2020-08-18 21:46:19 +08:00
|
|
|
t.Errorf("unexpected scheduled tasks in queue %q: (-want, +got)\n%s", qname, diff)
|
2020-08-17 05:51:56 +08:00
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2020-09-05 22:03:43 +08:00
|
|
|
for qname, want := range tc.wantPending {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotPending := h.GetPendingMessages(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
2020-09-05 22:03:43 +08:00
|
|
|
t.Errorf("unexpected pending tasks in queue %q: (-want, +got)\n%s", qname, diff)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 04:35:52 +08:00
|
|
|
func TestInspectorRunAllRetryTasks(t *testing.T) {
|
2020-07-13 21:29:41 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessageWithQueue("task2", nil, "critical")
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "low")
|
|
|
|
m4 := h.NewTaskMessage("task2", nil)
|
2020-07-13 21:29:41 +08:00
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(2 * time.Minute).Unix()}
|
2020-08-17 05:51:56 +08:00
|
|
|
z4 := base.Z{Message: m4, Score: now.Add(2 * time.Minute).Unix()}
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-09-05 22:03:43 +08:00
|
|
|
retry map[string][]base.Z
|
|
|
|
pending map[string][]*base.TaskMessage
|
|
|
|
qname string
|
|
|
|
want int
|
|
|
|
wantRetry map[string][]base.Z
|
|
|
|
wantPending map[string][]*base.TaskMessage
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {z1, z4},
|
|
|
|
"critical": {z2},
|
|
|
|
"low": {z3},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-07-13 21:29:41 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
|
|
|
},
|
2020-08-17 05:51:56 +08:00
|
|
|
qname: "default",
|
|
|
|
want: 2,
|
|
|
|
wantRetry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"critical": {z2},
|
|
|
|
"low": {z3},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {m1, m4},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {z1},
|
|
|
|
"critical": {z2},
|
|
|
|
"low": {z3},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-07-13 21:29:41 +08:00
|
|
|
"default": {m4},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
|
|
|
},
|
2020-08-17 05:51:56 +08:00
|
|
|
qname: "default",
|
|
|
|
want: 1,
|
|
|
|
wantRetry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"critical": {z2},
|
|
|
|
"low": {z3},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-07-13 21:29:41 +08:00
|
|
|
"default": {m4, m1},
|
2020-08-19 20:45:02 +08:00
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-07-13 21:29:41 +08:00
|
|
|
"default": {m1, m4},
|
|
|
|
},
|
2020-08-17 05:51:56 +08:00
|
|
|
qname: "default",
|
|
|
|
want: 0,
|
|
|
|
wantRetry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-07-13 21:29:41 +08:00
|
|
|
"default": {m1, m4},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllRetryQueues(t, r, tc.retry)
|
|
|
|
h.SeedAllPendingQueues(t, r, tc.pending)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2020-09-06 04:35:52 +08:00
|
|
|
got, err := inspector.RunAllRetryTasks(tc.qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
2020-09-06 04:35:52 +08:00
|
|
|
t.Errorf("RunAllRetryTasks(%q) returned error: %v", tc.qname, err)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if got != tc.want {
|
2020-09-06 04:35:52 +08:00
|
|
|
t.Errorf("RunAllRetryTasks(%q) = %d, want %d", tc.qname, got, tc.want)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2020-08-17 05:51:56 +08:00
|
|
|
for qname, want := range tc.wantRetry {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotRetry := h.GetRetryEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotRetry, h.SortZSetEntryOpt); diff != "" {
|
2020-08-18 21:46:19 +08:00
|
|
|
t.Errorf("unexpected retry tasks in queue %q: (-want, +got)\n%s", qname, diff)
|
2020-08-17 05:51:56 +08:00
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2020-09-05 22:03:43 +08:00
|
|
|
for qname, want := range tc.wantPending {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotPending := h.GetPendingMessages(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
2020-09-05 22:03:43 +08:00
|
|
|
t.Errorf("unexpected pending tasks in queue %q: (-want, +got)\n%s", qname, diff)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-17 05:51:56 +08:00
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
func TestInspectorRunAllArchivedTasks(t *testing.T) {
|
2020-07-13 21:29:41 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessageWithQueue("task2", nil, "critical")
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "low")
|
|
|
|
m4 := h.NewTaskMessage("task2", nil)
|
2020-07-13 21:29:41 +08:00
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(-5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(-15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(-2 * time.Minute).Unix()}
|
2020-08-17 05:51:56 +08:00
|
|
|
z4 := base.Z{Message: m4, Score: now.Add(-2 * time.Minute).Unix()}
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2021-01-13 03:01:21 +08:00
|
|
|
archived map[string][]base.Z
|
|
|
|
pending map[string][]*base.TaskMessage
|
|
|
|
qname string
|
|
|
|
want int
|
|
|
|
wantArchived map[string][]base.Z
|
|
|
|
wantPending map[string][]*base.TaskMessage
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {z1, z4},
|
|
|
|
"critical": {z2},
|
|
|
|
"low": {z3},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-07-13 21:29:41 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
|
|
|
},
|
2020-08-17 05:51:56 +08:00
|
|
|
qname: "default",
|
|
|
|
want: 2,
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {z2},
|
|
|
|
"low": {z3},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {m1, m4},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {z1},
|
|
|
|
"critical": {z2},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-07-13 21:29:41 +08:00
|
|
|
"default": {m4},
|
|
|
|
"critical": {},
|
|
|
|
},
|
2020-08-17 05:51:56 +08:00
|
|
|
qname: "default",
|
|
|
|
want: 1,
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {z2},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-07-13 21:29:41 +08:00
|
|
|
"default": {m4, m1},
|
2020-08-17 05:51:56 +08:00
|
|
|
"critical": {},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-07-13 21:29:41 +08:00
|
|
|
"default": {m1, m4},
|
|
|
|
},
|
2020-08-17 05:51:56 +08:00
|
|
|
qname: "default",
|
|
|
|
want: 0,
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-07-13 21:29:41 +08:00
|
|
|
"default": {m1, m4},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllArchivedQueues(t, r, tc.archived)
|
|
|
|
h.SeedAllPendingQueues(t, r, tc.pending)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
got, err := inspector.RunAllArchivedTasks(tc.qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
2021-01-13 03:01:21 +08:00
|
|
|
t.Errorf("RunAllArchivedTasks(%q) returned error: %v", tc.qname, err)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if got != tc.want {
|
2021-01-13 03:01:21 +08:00
|
|
|
t.Errorf("RunAllArchivedTasks(%q) = %d, want %d", tc.qname, got, tc.want)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2021-01-13 03:01:21 +08:00
|
|
|
for qname, want := range tc.wantArchived {
|
2021-01-29 00:59:13 +08:00
|
|
|
wantArchived := h.GetArchivedEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, wantArchived, h.SortZSetEntryOpt); diff != "" {
|
2021-01-13 03:01:21 +08:00
|
|
|
t.Errorf("unexpected archived tasks in queue %q: (-want, +got)\n%s", qname, diff)
|
2020-08-17 05:51:56 +08:00
|
|
|
}
|
|
|
|
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2020-09-05 22:03:43 +08:00
|
|
|
for qname, want := range tc.wantPending {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotPending := h.GetPendingMessages(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
2020-09-05 22:03:43 +08:00
|
|
|
t.Errorf("unexpected pending tasks in queue %q: (-want, +got)\n%s", qname, diff)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 07:37:30 +08:00
|
|
|
func TestInspectorDeleteTaskDeletesPendingTask(t *testing.T) {
|
2021-01-21 07:03:34 +08:00
|
|
|
r := setup(t)
|
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2021-01-21 07:03:34 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
pending map[string][]*base.TaskMessage
|
|
|
|
qname string
|
2021-05-18 07:37:30 +08:00
|
|
|
id string
|
2021-01-21 07:03:34 +08:00
|
|
|
wantPending map[string][]*base.TaskMessage
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {m3},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2021-06-14 22:31:39 +08:00
|
|
|
id: createPendingTask(m2).ID,
|
2021-01-21 07:03:34 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1},
|
|
|
|
"custom": {m3},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {m3},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
2021-06-14 22:31:39 +08:00
|
|
|
id: createPendingTask(m3).ID,
|
2021-01-21 07:03:34 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1, m2},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllPendingQueues(t, r, tc.pending)
|
2021-01-21 07:03:34 +08:00
|
|
|
|
2021-05-18 07:37:30 +08:00
|
|
|
if err := inspector.DeleteTask(tc.qname, tc.id); err != nil {
|
|
|
|
t.Errorf("DeleteTask(%q, %q) returned error: %v", tc.qname, tc.id, err)
|
2021-01-21 07:03:34 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for qname, want := range tc.wantPending {
|
2021-01-29 00:59:13 +08:00
|
|
|
got := h.GetPendingMessages(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, got, h.SortMsgOpt); diff != "" {
|
2021-01-21 07:03:34 +08:00
|
|
|
t.Errorf("unspected pending tasks in queue %q: (-want,+got):\n%s",
|
|
|
|
qname, diff)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 07:37:30 +08:00
|
|
|
func TestInspectorDeleteTaskDeletesScheduledTask(t *testing.T) {
|
2020-07-13 21:29:41 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
2020-07-13 21:29:41 +08:00
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(2 * time.Minute).Unix()}
|
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-17 05:51:56 +08:00
|
|
|
scheduled map[string][]base.Z
|
|
|
|
qname string
|
2021-05-18 07:37:30 +08:00
|
|
|
id string
|
2020-08-17 05:51:56 +08:00
|
|
|
wantScheduled map[string][]base.Z
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {z1, z2},
|
|
|
|
"custom": {z3},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2021-06-14 22:31:39 +08:00
|
|
|
id: createScheduledTask(z2).ID,
|
2020-08-17 05:51:56 +08:00
|
|
|
wantScheduled: map[string][]base.Z{
|
|
|
|
"default": {z1},
|
|
|
|
"custom": {z3},
|
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllScheduledQueues(t, r, tc.scheduled)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-18 07:37:30 +08:00
|
|
|
if err := inspector.DeleteTask(tc.qname, tc.id); err != nil {
|
|
|
|
t.Errorf("DeleteTask(%q, %q) returned error: %v", tc.qname, tc.id, err)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2020-08-17 05:51:56 +08:00
|
|
|
for qname, want := range tc.wantScheduled {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotScheduled := h.GetScheduledEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotScheduled, h.SortZSetEntryOpt); diff != "" {
|
2020-08-18 21:46:19 +08:00
|
|
|
t.Errorf("unexpected scheduled tasks in queue %q: (-want, +got)\n%s", qname, diff)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2020-08-17 05:51:56 +08:00
|
|
|
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 07:37:30 +08:00
|
|
|
func TestInspectorDeleteTaskDeletesRetryTask(t *testing.T) {
|
2020-07-13 21:29:41 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
2020-07-13 21:29:41 +08:00
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(2 * time.Minute).Unix()}
|
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-17 05:51:56 +08:00
|
|
|
retry map[string][]base.Z
|
|
|
|
qname string
|
2021-05-18 07:37:30 +08:00
|
|
|
id string
|
2020-08-17 05:51:56 +08:00
|
|
|
wantRetry map[string][]base.Z
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {z1, z2},
|
|
|
|
"custom": {z3},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2021-06-14 22:31:39 +08:00
|
|
|
id: createRetryTask(z2).ID,
|
2020-08-17 05:51:56 +08:00
|
|
|
wantRetry: map[string][]base.Z{
|
|
|
|
"default": {z1},
|
|
|
|
"custom": {z3},
|
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllRetryQueues(t, r, tc.retry)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-18 07:37:30 +08:00
|
|
|
if err := inspector.DeleteTask(tc.qname, tc.id); err != nil {
|
|
|
|
t.Errorf("DeleteTask(%q, %q) returned error: %v", tc.qname, tc.id, err)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
2020-08-17 05:51:56 +08:00
|
|
|
for qname, want := range tc.wantRetry {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotRetry := h.GetRetryEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotRetry, h.SortZSetEntryOpt); diff != "" {
|
2020-08-18 21:46:19 +08:00
|
|
|
t.Errorf("unexpected retry tasks in queue %q: (-want, +got)\n%s", qname, diff)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 07:37:30 +08:00
|
|
|
func TestInspectorDeleteTaskDeletesArchivedTask(t *testing.T) {
|
2020-07-13 21:29:41 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
2020-07-13 21:29:41 +08:00
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(-5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(-15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(-2 * time.Minute).Unix()}
|
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2021-01-13 03:01:21 +08:00
|
|
|
archived map[string][]base.Z
|
|
|
|
qname string
|
2021-05-18 07:37:30 +08:00
|
|
|
id string
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived map[string][]base.Z
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {z1, z2},
|
|
|
|
"custom": {z3},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2021-06-14 22:31:39 +08:00
|
|
|
id: createArchivedTask(z2).ID,
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {z1},
|
|
|
|
"custom": {z3},
|
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllArchivedQueues(t, r, tc.archived)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-18 07:37:30 +08:00
|
|
|
if err := inspector.DeleteTask(tc.qname, tc.id); err != nil {
|
|
|
|
t.Errorf("DeleteTask(%q, %q) returned error: %v", tc.qname, tc.id, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for qname, want := range tc.wantArchived {
|
|
|
|
wantArchived := h.GetArchivedEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, wantArchived, h.SortZSetEntryOpt); diff != "" {
|
|
|
|
t.Errorf("unexpected archived tasks in queue %q: (-want, +got)\n%s", qname, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInspectorDeleteTaskError(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")
|
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(-5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(-15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(-2 * time.Minute).Unix()}
|
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2021-05-18 07:37:30 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
archived map[string][]base.Z
|
|
|
|
qname string
|
|
|
|
id string
|
|
|
|
wantErr error
|
|
|
|
wantArchived map[string][]base.Z
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
archived: map[string][]base.Z{
|
|
|
|
"default": {z1, z2},
|
|
|
|
"custom": {z3},
|
|
|
|
},
|
|
|
|
qname: "nonexistent",
|
2021-06-14 22:31:39 +08:00
|
|
|
id: createArchivedTask(z2).ID,
|
2021-05-18 07:37:30 +08:00
|
|
|
wantErr: ErrQueueNotFound,
|
|
|
|
wantArchived: map[string][]base.Z{
|
|
|
|
"default": {z1, z2},
|
|
|
|
"custom": {z3},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
archived: map[string][]base.Z{
|
|
|
|
"default": {z1, z2},
|
|
|
|
"custom": {z3},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
id: uuid.NewString(),
|
|
|
|
wantErr: ErrTaskNotFound,
|
|
|
|
wantArchived: map[string][]base.Z{
|
|
|
|
"default": {z1, z2},
|
|
|
|
"custom": {z3},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllArchivedQueues(t, r, tc.archived)
|
|
|
|
|
|
|
|
if err := inspector.DeleteTask(tc.qname, tc.id); !errors.Is(err, tc.wantErr) {
|
|
|
|
t.Errorf("DeleteTask(%q, %q) = %v, want %v", tc.qname, tc.id, err, tc.wantErr)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-01-13 03:01:21 +08:00
|
|
|
for qname, want := range tc.wantArchived {
|
2021-01-29 00:59:13 +08:00
|
|
|
wantArchived := h.GetArchivedEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, wantArchived, h.SortZSetEntryOpt); diff != "" {
|
2021-01-13 03:01:21 +08:00
|
|
|
t.Errorf("unexpected archived tasks in queue %q: (-want, +got)\n%s", qname, diff)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 22:32:47 +08:00
|
|
|
func TestInspectorRunTaskRunsScheduledTask(t *testing.T) {
|
2020-07-13 21:29:41 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessage("task2", nil)
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
2020-07-13 21:29:41 +08:00
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(2 * time.Minute).Unix()}
|
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-17 05:51:56 +08:00
|
|
|
scheduled map[string][]base.Z
|
2020-09-05 22:03:43 +08:00
|
|
|
pending map[string][]*base.TaskMessage
|
2020-08-17 05:51:56 +08:00
|
|
|
qname string
|
2021-05-16 22:32:47 +08:00
|
|
|
id string
|
2020-08-17 05:51:56 +08:00
|
|
|
wantScheduled map[string][]base.Z
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending map[string][]*base.TaskMessage
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {z1, z2},
|
|
|
|
"custom": {z3},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2021-06-14 22:31:39 +08:00
|
|
|
id: createScheduledTask(z2).ID,
|
2020-08-17 05:51:56 +08:00
|
|
|
wantScheduled: map[string][]base.Z{
|
|
|
|
"default": {z1},
|
|
|
|
"custom": {z3},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {m2},
|
|
|
|
"custom": {},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllScheduledQueues(t, r, tc.scheduled)
|
|
|
|
h.SeedAllPendingQueues(t, r, tc.pending)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-16 22:32:47 +08:00
|
|
|
if err := inspector.RunTask(tc.qname, tc.id); err != nil {
|
|
|
|
t.Errorf("RunTask(%q, %q) returned error: %v", tc.qname, tc.id, err)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
2020-08-17 05:51:56 +08:00
|
|
|
for qname, want := range tc.wantScheduled {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotScheduled := h.GetScheduledEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotScheduled, h.SortZSetEntryOpt); diff != "" {
|
2020-08-17 05:51:56 +08:00
|
|
|
t.Errorf("unexpected scheduled tasks in queue %q: (-want, +got)\n%s",
|
2020-08-18 21:46:19 +08:00
|
|
|
qname, diff)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2020-08-17 05:51:56 +08:00
|
|
|
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2020-09-05 22:03:43 +08:00
|
|
|
for qname, want := range tc.wantPending {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotPending := h.GetPendingMessages(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
2020-09-05 22:03:43 +08:00
|
|
|
t.Errorf("unexpected pending tasks in queue %q: (-want, +got)\n%s",
|
2020-08-18 21:46:19 +08:00
|
|
|
qname, diff)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 22:32:47 +08:00
|
|
|
func TestInspectorRunTaskRunsRetryTask(t *testing.T) {
|
2020-07-13 21:29:41 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessageWithQueue("task2", nil, "custom")
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
2020-07-13 21:29:41 +08:00
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(2 * time.Minute).Unix()}
|
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-09-05 22:03:43 +08:00
|
|
|
retry map[string][]base.Z
|
|
|
|
pending map[string][]*base.TaskMessage
|
|
|
|
qname string
|
2021-05-16 22:32:47 +08:00
|
|
|
id string
|
2020-09-05 22:03:43 +08:00
|
|
|
wantRetry map[string][]base.Z
|
|
|
|
wantPending map[string][]*base.TaskMessage
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {z1},
|
|
|
|
"custom": {z2, z3},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
2021-06-14 22:31:39 +08:00
|
|
|
id: createRetryTask(z2).ID,
|
2020-08-17 05:51:56 +08:00
|
|
|
wantRetry: map[string][]base.Z{
|
|
|
|
"default": {z1},
|
|
|
|
"custom": {z3},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {m2},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllRetryQueues(t, r, tc.retry)
|
|
|
|
h.SeedAllPendingQueues(t, r, tc.pending)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-16 22:32:47 +08:00
|
|
|
if err := inspector.RunTask(tc.qname, tc.id); err != nil {
|
|
|
|
t.Errorf("RunTaskBy(%q, %q) returned error: %v", tc.qname, tc.id, err)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
2020-08-17 05:51:56 +08:00
|
|
|
for qname, want := range tc.wantRetry {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotRetry := h.GetRetryEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotRetry, h.SortZSetEntryOpt); diff != "" {
|
2020-08-17 05:51:56 +08:00
|
|
|
t.Errorf("unexpected retry tasks in queue %q: (-want, +got)\n%s",
|
2020-08-18 21:46:19 +08:00
|
|
|
qname, diff)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
2020-09-05 22:03:43 +08:00
|
|
|
for qname, want := range tc.wantPending {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotPending := h.GetPendingMessages(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
2020-09-05 22:03:43 +08:00
|
|
|
t.Errorf("unexpected pending tasks in queue %q: (-want, +got)\n%s",
|
2020-08-18 21:46:19 +08:00
|
|
|
qname, diff)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 22:32:47 +08:00
|
|
|
func TestInspectorRunTaskRunsArchivedTask(t *testing.T) {
|
2020-07-13 21:29:41 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessageWithQueue("task2", nil, "critical")
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "low")
|
2020-07-13 21:29:41 +08:00
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(-5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(-15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(-2 * time.Minute).Unix()}
|
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2021-01-13 03:01:21 +08:00
|
|
|
archived map[string][]base.Z
|
|
|
|
pending map[string][]*base.TaskMessage
|
|
|
|
qname string
|
2021-05-16 22:32:47 +08:00
|
|
|
id string
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived map[string][]base.Z
|
|
|
|
wantPending map[string][]*base.TaskMessage
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {z1},
|
|
|
|
"critical": {z2},
|
|
|
|
"low": {z3},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
pending: map[string][]*base.TaskMessage{
|
2020-07-13 21:29:41 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
|
|
|
},
|
2020-08-17 05:51:56 +08:00
|
|
|
qname: "critical",
|
2021-06-14 22:31:39 +08:00
|
|
|
id: createArchivedTask(z2).ID,
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {z1},
|
|
|
|
"critical": {},
|
|
|
|
"low": {z3},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-07-13 21:29:41 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {m2},
|
|
|
|
"low": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllArchivedQueues(t, r, tc.archived)
|
|
|
|
h.SeedAllPendingQueues(t, r, tc.pending)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-16 22:32:47 +08:00
|
|
|
if err := inspector.RunTask(tc.qname, tc.id); err != nil {
|
|
|
|
t.Errorf("RunTask(%q, %q) returned error: %v", tc.qname, tc.id, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for qname, want := range tc.wantArchived {
|
|
|
|
wantArchived := h.GetArchivedEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, wantArchived, h.SortZSetEntryOpt); diff != "" {
|
|
|
|
t.Errorf("unexpected archived tasks in queue %q: (-want, +got)\n%s",
|
|
|
|
qname, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for qname, want := range tc.wantPending {
|
|
|
|
gotPending := h.GetPendingMessages(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("unexpected pending tasks in queue %q: (-want, +got)\n%s",
|
|
|
|
qname, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInspectorRunTaskError(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
defer r.Close()
|
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessageWithQueue("task2", nil, "critical")
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "low")
|
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(-5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(-15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(-2 * time.Minute).Unix()}
|
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2021-05-16 22:32:47 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
archived map[string][]base.Z
|
|
|
|
pending map[string][]*base.TaskMessage
|
|
|
|
qname string
|
|
|
|
id string
|
|
|
|
wantErr error
|
|
|
|
wantArchived map[string][]base.Z
|
|
|
|
wantPending map[string][]*base.TaskMessage
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
archived: map[string][]base.Z{
|
|
|
|
"default": {z1},
|
|
|
|
"critical": {z2},
|
|
|
|
"low": {z3},
|
|
|
|
},
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
|
|
|
},
|
|
|
|
qname: "nonexistent",
|
2021-06-14 22:31:39 +08:00
|
|
|
id: createArchivedTask(z2).ID,
|
2021-05-16 22:32:47 +08:00
|
|
|
wantErr: ErrQueueNotFound,
|
|
|
|
wantArchived: map[string][]base.Z{
|
|
|
|
"default": {z1},
|
|
|
|
"critical": {z2},
|
|
|
|
"low": {z3},
|
|
|
|
},
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
archived: map[string][]base.Z{
|
|
|
|
"default": {z1},
|
|
|
|
"critical": {z2},
|
|
|
|
"low": {z3},
|
|
|
|
},
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
|
|
|
id: uuid.NewString(),
|
|
|
|
wantErr: ErrTaskNotFound,
|
|
|
|
wantArchived: map[string][]base.Z{
|
|
|
|
"default": {z1},
|
|
|
|
"critical": {z2},
|
|
|
|
"low": {z3},
|
|
|
|
},
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
"low": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllArchivedQueues(t, r, tc.archived)
|
|
|
|
h.SeedAllPendingQueues(t, r, tc.pending)
|
|
|
|
|
|
|
|
if err := inspector.RunTask(tc.qname, tc.id); !errors.Is(err, tc.wantErr) {
|
|
|
|
t.Errorf("RunTask(%q, %q) = %v, want %v", tc.qname, tc.id, err, tc.wantErr)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-01-13 03:01:21 +08:00
|
|
|
for qname, want := range tc.wantArchived {
|
2021-01-29 00:59:13 +08:00
|
|
|
wantArchived := h.GetArchivedEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, wantArchived, h.SortZSetEntryOpt); diff != "" {
|
2021-01-13 03:01:21 +08:00
|
|
|
t.Errorf("unexpected archived tasks in queue %q: (-want, +got)\n%s",
|
2020-08-18 21:46:19 +08:00
|
|
|
qname, diff)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
2020-09-05 22:03:43 +08:00
|
|
|
for qname, want := range tc.wantPending {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotPending := h.GetPendingMessages(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
2020-09-05 22:03:43 +08:00
|
|
|
t.Errorf("unexpected pending tasks in queue %q: (-want, +got)\n%s",
|
2020-08-18 21:46:19 +08:00
|
|
|
qname, diff)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 07:27:16 +08:00
|
|
|
func TestInspectorArchiveTaskArchivesPendingTask(t *testing.T) {
|
2021-01-21 07:03:34 +08:00
|
|
|
r := setup(t)
|
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessageWithQueue("task2", nil, "custom")
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
2021-01-21 07:03:34 +08:00
|
|
|
now := time.Now()
|
2022-01-06 01:07:42 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
|
|
|
inspector.rdb.SetClock(timeutil.NewSimulatedClock(now))
|
2021-01-21 07:03:34 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
pending map[string][]*base.TaskMessage
|
|
|
|
archived map[string][]base.Z
|
|
|
|
qname string
|
2021-05-19 07:27:16 +08:00
|
|
|
id string
|
2021-01-21 07:03:34 +08:00
|
|
|
wantPending map[string][]*base.TaskMessage
|
|
|
|
wantArchived map[string][]base.Z
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1},
|
|
|
|
"custom": {m2, m3},
|
|
|
|
},
|
|
|
|
archived: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "default",
|
2021-06-14 22:31:39 +08:00
|
|
|
id: createPendingTask(m1).ID,
|
2021-01-21 07:03:34 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
"custom": {m2, m3},
|
|
|
|
},
|
|
|
|
wantArchived: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: m1, Score: now.Unix()},
|
|
|
|
},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1},
|
|
|
|
"custom": {m2, m3},
|
|
|
|
},
|
|
|
|
archived: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
2021-06-14 22:31:39 +08:00
|
|
|
id: createPendingTask(m2).ID,
|
2021-01-21 07:03:34 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {m1},
|
|
|
|
"custom": {m3},
|
|
|
|
},
|
|
|
|
wantArchived: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {
|
|
|
|
{Message: m2, Score: now.Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllPendingQueues(t, r, tc.pending)
|
|
|
|
h.SeedAllArchivedQueues(t, r, tc.archived)
|
2021-01-21 07:03:34 +08:00
|
|
|
|
2021-05-19 07:27:16 +08:00
|
|
|
if err := inspector.ArchiveTask(tc.qname, tc.id); err != nil {
|
|
|
|
t.Errorf("ArchiveTask(%q, %q) returned error: %v", tc.qname, tc.id, err)
|
2021-01-21 07:03:34 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for qname, want := range tc.wantPending {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotPending := h.GetPendingMessages(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
2021-01-21 07:03:34 +08:00
|
|
|
t.Errorf("unexpected pending tasks in queue %q: (-want,+got)\n%s",
|
|
|
|
qname, diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
for qname, want := range tc.wantArchived {
|
2021-01-29 00:59:13 +08:00
|
|
|
wantArchived := h.GetArchivedEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, wantArchived, h.SortZSetEntryOpt); diff != "" {
|
2021-01-21 07:03:34 +08:00
|
|
|
t.Errorf("unexpected archived tasks in queue %q: (-want,+got)\n%s",
|
|
|
|
qname, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 07:27:16 +08:00
|
|
|
func TestInspectorArchiveTaskArchivesScheduledTask(t *testing.T) {
|
2020-07-13 21:29:41 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessageWithQueue("task2", nil, "custom")
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
2020-07-13 21:29:41 +08:00
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(2 * time.Minute).Unix()}
|
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2022-01-06 01:07:42 +08:00
|
|
|
inspector.rdb.SetClock(timeutil.NewSimulatedClock(now))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-17 05:51:56 +08:00
|
|
|
scheduled map[string][]base.Z
|
2021-01-13 03:01:21 +08:00
|
|
|
archived map[string][]base.Z
|
2020-08-17 05:51:56 +08:00
|
|
|
qname string
|
2021-05-19 07:27:16 +08:00
|
|
|
id string
|
2020-08-17 05:51:56 +08:00
|
|
|
want string
|
|
|
|
wantScheduled map[string][]base.Z
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived map[string][]base.Z
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
scheduled: map[string][]base.Z{
|
|
|
|
"default": {z1},
|
|
|
|
"custom": {z2, z3},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
2021-06-14 22:31:39 +08:00
|
|
|
id: createScheduledTask(z2).ID,
|
2020-08-18 21:01:38 +08:00
|
|
|
wantScheduled: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {z1},
|
|
|
|
"custom": {z3},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {},
|
2020-12-20 22:09:51 +08:00
|
|
|
"custom": {
|
|
|
|
{
|
|
|
|
Message: m2,
|
|
|
|
Score: now.Unix(),
|
|
|
|
},
|
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllScheduledQueues(t, r, tc.scheduled)
|
|
|
|
h.SeedAllArchivedQueues(t, r, tc.archived)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-19 07:27:16 +08:00
|
|
|
if err := inspector.ArchiveTask(tc.qname, tc.id); err != nil {
|
|
|
|
t.Errorf("ArchiveTask(%q, %q) returned error: %v", tc.qname, tc.id, err)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
2020-08-17 05:51:56 +08:00
|
|
|
for qname, want := range tc.wantScheduled {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotScheduled := h.GetScheduledEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotScheduled, h.SortZSetEntryOpt); diff != "" {
|
2020-08-17 05:51:56 +08:00
|
|
|
t.Errorf("unexpected scheduled tasks in queue %q: (-want, +got)\n%s",
|
2020-08-18 21:46:19 +08:00
|
|
|
qname, diff)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2020-08-17 05:51:56 +08:00
|
|
|
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2021-01-13 03:01:21 +08:00
|
|
|
for qname, want := range tc.wantArchived {
|
2021-01-29 00:59:13 +08:00
|
|
|
wantArchived := h.GetArchivedEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, wantArchived, h.SortZSetEntryOpt); diff != "" {
|
2021-01-13 03:01:21 +08:00
|
|
|
t.Errorf("unexpected archived tasks in queue %q: (-want, +got)\n%s",
|
2020-08-18 21:46:19 +08:00
|
|
|
qname, diff)
|
2020-08-17 05:51:56 +08:00
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 07:27:16 +08:00
|
|
|
func TestInspectorArchiveTaskArchivesRetryTask(t *testing.T) {
|
2020-07-13 21:29:41 +08:00
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2021-01-29 00:59:13 +08:00
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessageWithQueue("task2", nil, "custom")
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
2020-07-13 21:29:41 +08:00
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(2 * time.Minute).Unix()}
|
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2022-01-06 01:07:42 +08:00
|
|
|
inspector.rdb.SetClock(timeutil.NewSimulatedClock(now))
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2021-01-13 03:01:21 +08:00
|
|
|
retry map[string][]base.Z
|
|
|
|
archived map[string][]base.Z
|
|
|
|
qname string
|
2021-05-19 07:27:16 +08:00
|
|
|
id string
|
2021-01-13 03:01:21 +08:00
|
|
|
wantRetry map[string][]base.Z
|
|
|
|
wantArchived map[string][]base.Z
|
2020-07-13 21:29:41 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-17 05:51:56 +08:00
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {z1},
|
|
|
|
"custom": {z2, z3},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
2021-06-14 22:31:39 +08:00
|
|
|
id: createRetryTask(z2).ID,
|
2020-08-17 05:51:56 +08:00
|
|
|
wantRetry: map[string][]base.Z{
|
|
|
|
"default": {z1},
|
|
|
|
"custom": {z3},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]base.Z{
|
2020-08-17 05:51:56 +08:00
|
|
|
"default": {},
|
2020-12-20 22:09:51 +08:00
|
|
|
"custom": {
|
|
|
|
{
|
|
|
|
Message: m2,
|
|
|
|
Score: now.Unix(),
|
|
|
|
},
|
|
|
|
},
|
2020-07-13 21:29:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllRetryQueues(t, r, tc.retry)
|
|
|
|
h.SeedAllArchivedQueues(t, r, tc.archived)
|
2020-07-13 21:29:41 +08:00
|
|
|
|
2021-05-19 07:27:16 +08:00
|
|
|
if err := inspector.ArchiveTask(tc.qname, tc.id); err != nil {
|
|
|
|
t.Errorf("ArchiveTask(%q, %q) returned error: %v", tc.qname, tc.id, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for qname, want := range tc.wantRetry {
|
|
|
|
gotRetry := h.GetRetryEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotRetry, h.SortZSetEntryOpt); diff != "" {
|
|
|
|
t.Errorf("unexpected retry tasks in queue %q: (-want, +got)\n%s",
|
|
|
|
qname, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for qname, want := range tc.wantArchived {
|
|
|
|
wantArchived := h.GetArchivedEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, wantArchived, h.SortZSetEntryOpt); diff != "" {
|
|
|
|
t.Errorf("unexpected archived tasks in queue %q: (-want, +got)\n%s",
|
|
|
|
qname, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInspectorArchiveTaskError(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
defer r.Close()
|
|
|
|
m1 := h.NewTaskMessage("task1", nil)
|
|
|
|
m2 := h.NewTaskMessageWithQueue("task2", nil, "custom")
|
|
|
|
m3 := h.NewTaskMessageWithQueue("task3", nil, "custom")
|
|
|
|
now := time.Now()
|
|
|
|
z1 := base.Z{Message: m1, Score: now.Add(5 * time.Minute).Unix()}
|
|
|
|
z2 := base.Z{Message: m2, Score: now.Add(15 * time.Minute).Unix()}
|
|
|
|
z3 := base.Z{Message: m3, Score: now.Add(2 * time.Minute).Unix()}
|
|
|
|
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2022-01-06 01:07:42 +08:00
|
|
|
inspector.rdb.SetClock(timeutil.NewSimulatedClock(now))
|
2021-05-19 07:27:16 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
retry map[string][]base.Z
|
|
|
|
archived map[string][]base.Z
|
|
|
|
qname string
|
|
|
|
id string
|
|
|
|
wantErr error
|
|
|
|
wantRetry map[string][]base.Z
|
|
|
|
wantArchived map[string][]base.Z
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {z1},
|
|
|
|
"custom": {z2, z3},
|
|
|
|
},
|
|
|
|
archived: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "nonexistent",
|
2021-06-14 22:31:39 +08:00
|
|
|
id: createRetryTask(z2).ID,
|
2021-05-19 07:27:16 +08:00
|
|
|
wantErr: ErrQueueNotFound,
|
|
|
|
wantRetry: map[string][]base.Z{
|
|
|
|
"default": {z1},
|
|
|
|
"custom": {z2, z3},
|
|
|
|
},
|
|
|
|
wantArchived: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {z1},
|
|
|
|
"custom": {z2, z3},
|
|
|
|
},
|
|
|
|
archived: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
qname: "custom",
|
|
|
|
id: uuid.NewString(),
|
|
|
|
wantErr: ErrTaskNotFound,
|
|
|
|
wantRetry: map[string][]base.Z{
|
|
|
|
"default": {z1},
|
|
|
|
"custom": {z2, z3},
|
|
|
|
},
|
|
|
|
wantArchived: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"custom": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r)
|
|
|
|
h.SeedAllRetryQueues(t, r, tc.retry)
|
|
|
|
h.SeedAllArchivedQueues(t, r, tc.archived)
|
|
|
|
|
|
|
|
if err := inspector.ArchiveTask(tc.qname, tc.id); !errors.Is(err, tc.wantErr) {
|
|
|
|
t.Errorf("ArchiveTask(%q, %q) = %v, want %v", tc.qname, tc.id, err, tc.wantErr)
|
2020-07-13 21:29:41 +08:00
|
|
|
continue
|
|
|
|
}
|
2020-08-17 05:51:56 +08:00
|
|
|
for qname, want := range tc.wantRetry {
|
2021-01-29 00:59:13 +08:00
|
|
|
gotRetry := h.GetRetryEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotRetry, h.SortZSetEntryOpt); diff != "" {
|
2020-08-17 05:51:56 +08:00
|
|
|
t.Errorf("unexpected retry tasks in queue %q: (-want, +got)\n%s",
|
2020-08-18 21:46:19 +08:00
|
|
|
qname, diff)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
2021-01-13 03:01:21 +08:00
|
|
|
for qname, want := range tc.wantArchived {
|
2021-01-29 00:59:13 +08:00
|
|
|
wantArchived := h.GetArchivedEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, wantArchived, h.SortZSetEntryOpt); diff != "" {
|
2021-01-13 03:01:21 +08:00
|
|
|
t.Errorf("unexpected archived tasks in queue %q: (-want, +got)\n%s",
|
2020-08-18 21:46:19 +08:00
|
|
|
qname, diff)
|
2020-08-17 05:51:56 +08:00
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-01 23:37:09 +08:00
|
|
|
|
|
|
|
var sortSchedulerEntry = cmp.Transformer("SortSchedulerEntry", func(in []*SchedulerEntry) []*SchedulerEntry {
|
|
|
|
out := append([]*SchedulerEntry(nil), in...)
|
|
|
|
sort.Slice(out, func(i, j int) bool {
|
|
|
|
return out[i].Spec < out[j].Spec
|
|
|
|
})
|
|
|
|
return out
|
|
|
|
})
|
|
|
|
|
|
|
|
func TestInspectorSchedulerEntries(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
rdbClient := rdb.NewRDB(r)
|
2021-05-19 09:45:15 +08:00
|
|
|
inspector := NewInspector(getRedisConnOpt(t))
|
2020-12-01 23:37:09 +08:00
|
|
|
|
|
|
|
now := time.Now().UTC()
|
|
|
|
schedulerID := "127.0.0.1:9876:abc123"
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
data []*base.SchedulerEntry // data to seed redis
|
|
|
|
want []*SchedulerEntry
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
data: []*base.SchedulerEntry{
|
2021-01-29 00:59:13 +08:00
|
|
|
{
|
2020-12-01 23:37:09 +08:00
|
|
|
Spec: "* * * * *",
|
|
|
|
Type: "foo",
|
|
|
|
Payload: nil,
|
|
|
|
Opts: nil,
|
|
|
|
Next: now.Add(5 * time.Hour),
|
|
|
|
Prev: now.Add(-2 * time.Hour),
|
|
|
|
},
|
2021-01-29 00:59:13 +08:00
|
|
|
{
|
2020-12-01 23:37:09 +08:00
|
|
|
Spec: "@every 20m",
|
|
|
|
Type: "bar",
|
2021-03-21 04:42:13 +08:00
|
|
|
Payload: h.JSON(map[string]interface{}{"fiz": "baz"}),
|
2020-12-01 23:37:09 +08:00
|
|
|
Opts: []string{`Queue("bar")`, `MaxRetry(20)`},
|
|
|
|
Next: now.Add(1 * time.Minute),
|
|
|
|
Prev: now.Add(-19 * time.Minute),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*SchedulerEntry{
|
2021-01-29 00:59:13 +08:00
|
|
|
{
|
2020-12-01 23:37:09 +08:00
|
|
|
Spec: "* * * * *",
|
2021-05-19 09:45:15 +08:00
|
|
|
Task: NewTask("foo", nil),
|
2020-12-01 23:37:09 +08:00
|
|
|
Opts: nil,
|
|
|
|
Next: now.Add(5 * time.Hour),
|
|
|
|
Prev: now.Add(-2 * time.Hour),
|
|
|
|
},
|
2021-01-29 00:59:13 +08:00
|
|
|
{
|
2020-12-01 23:37:09 +08:00
|
|
|
Spec: "@every 20m",
|
2021-05-19 09:45:15 +08:00
|
|
|
Task: NewTask("bar", h.JSON(map[string]interface{}{"fiz": "baz"})),
|
|
|
|
Opts: []Option{Queue("bar"), MaxRetry(20)},
|
2020-12-01 23:37:09 +08:00
|
|
|
Next: now.Add(1 * time.Minute),
|
|
|
|
Prev: now.Add(-19 * time.Minute),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2021-01-29 00:59:13 +08:00
|
|
|
h.FlushDB(t, r)
|
2020-12-01 23:37:09 +08:00
|
|
|
err := rdbClient.WriteSchedulerEntries(schedulerID, tc.data, time.Minute)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not write data: %v", err)
|
|
|
|
}
|
|
|
|
got, err := inspector.SchedulerEntries()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("SchedulerEntries() returned error: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2021-05-19 09:45:15 +08:00
|
|
|
ignoreOpt := cmpopts.IgnoreUnexported(Task{})
|
2020-12-01 23:37:09 +08:00
|
|
|
if diff := cmp.Diff(tc.want, got, sortSchedulerEntry, ignoreOpt); diff != "" {
|
|
|
|
t.Errorf("SchedulerEntries() = %v, want %v; (-want,+got)\n%s",
|
|
|
|
got, tc.want, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 00:59:13 +08:00
|
|
|
|
|
|
|
func TestParseOption(t *testing.T) {
|
|
|
|
oneHourFromNow := time.Now().Add(1 * time.Hour)
|
|
|
|
tests := []struct {
|
|
|
|
s string
|
2021-05-19 09:45:15 +08:00
|
|
|
wantType OptionType
|
2021-01-29 00:59:13 +08:00
|
|
|
wantVal interface{}
|
|
|
|
}{
|
2021-05-19 09:45:15 +08:00
|
|
|
{`MaxRetry(10)`, MaxRetryOpt, 10},
|
|
|
|
{`Queue("email")`, QueueOpt, "email"},
|
|
|
|
{`Timeout(3m)`, TimeoutOpt, 3 * time.Minute},
|
|
|
|
{Deadline(oneHourFromNow).String(), DeadlineOpt, oneHourFromNow},
|
|
|
|
{`Unique(1h)`, UniqueOpt, 1 * time.Hour},
|
|
|
|
{ProcessAt(oneHourFromNow).String(), ProcessAtOpt, oneHourFromNow},
|
|
|
|
{`ProcessIn(10m)`, ProcessInOpt, 10 * time.Minute},
|
2021-11-06 07:52:54 +08:00
|
|
|
{`Retention(24h)`, RetentionOpt, 24 * time.Hour},
|
2021-01-29 00:59:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.s, func(t *testing.T) {
|
|
|
|
got, err := parseOption(tc.s)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("returned error: %v", err)
|
|
|
|
}
|
|
|
|
if got == nil {
|
|
|
|
t.Fatal("returned nil")
|
|
|
|
}
|
|
|
|
if got.Type() != tc.wantType {
|
|
|
|
t.Fatalf("got type %v, want type %v ", got.Type(), tc.wantType)
|
|
|
|
}
|
|
|
|
switch tc.wantType {
|
2021-05-19 09:45:15 +08:00
|
|
|
case QueueOpt:
|
2021-01-29 00:59:13 +08:00
|
|
|
gotVal, ok := got.Value().(string)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("returned Option with non-string value")
|
|
|
|
}
|
|
|
|
if gotVal != tc.wantVal.(string) {
|
|
|
|
t.Fatalf("got value %v, want %v", gotVal, tc.wantVal)
|
|
|
|
}
|
2021-05-19 09:45:15 +08:00
|
|
|
case MaxRetryOpt:
|
2021-01-29 00:59:13 +08:00
|
|
|
gotVal, ok := got.Value().(int)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("returned Option with non-int value")
|
|
|
|
}
|
|
|
|
if gotVal != tc.wantVal.(int) {
|
|
|
|
t.Fatalf("got value %v, want %v", gotVal, tc.wantVal)
|
|
|
|
}
|
2021-11-06 07:52:54 +08:00
|
|
|
case TimeoutOpt, UniqueOpt, ProcessInOpt, RetentionOpt:
|
2021-01-29 00:59:13 +08:00
|
|
|
gotVal, ok := got.Value().(time.Duration)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("returned Option with non duration value")
|
|
|
|
}
|
|
|
|
if gotVal != tc.wantVal.(time.Duration) {
|
|
|
|
t.Fatalf("got value %v, want %v", gotVal, tc.wantVal)
|
|
|
|
}
|
2021-05-19 09:45:15 +08:00
|
|
|
case DeadlineOpt, ProcessAtOpt:
|
2021-01-29 00:59:13 +08:00
|
|
|
gotVal, ok := got.Value().(time.Time)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("returned Option with non time value")
|
|
|
|
}
|
|
|
|
if cmp.Equal(gotVal, tc.wantVal.(time.Time)) {
|
|
|
|
t.Fatalf("got value %v, want %v", gotVal, tc.wantVal)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatalf("returned Option with unexpected type: %v", got.Type())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-01-31 02:43:57 +08:00
|
|
|
}
|