2020-06-21 22:05:57 +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.
|
|
|
|
|
|
|
|
package asynq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
h "github.com/hibiken/asynq/internal/asynqtest"
|
|
|
|
"github.com/hibiken/asynq/internal/base"
|
|
|
|
"github.com/hibiken/asynq/internal/rdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRecoverer(t *testing.T) {
|
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-06-21 22:05:57 +08:00
|
|
|
rdbClient := rdb.NewRDB(r)
|
|
|
|
|
2020-08-10 21:10:14 +08:00
|
|
|
t1 := h.NewTaskMessageWithQueue("task1", nil, "default")
|
|
|
|
t2 := h.NewTaskMessageWithQueue("task2", nil, "default")
|
2020-06-21 22:05:57 +08:00
|
|
|
t3 := h.NewTaskMessageWithQueue("task3", nil, "critical")
|
2020-08-10 21:10:14 +08:00
|
|
|
t4 := h.NewTaskMessageWithQueue("task4", nil, "default")
|
2020-06-21 22:05:57 +08:00
|
|
|
t4.Retried = t4.Retry // t4 has reached its max retry count
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
tests := []struct {
|
2022-02-11 22:18:27 +08:00
|
|
|
desc string
|
|
|
|
active map[string][]*base.TaskMessage
|
|
|
|
lease map[string][]base.Z
|
|
|
|
retry map[string][]base.Z
|
|
|
|
archived map[string][]base.Z
|
|
|
|
wantActive map[string][]*base.TaskMessage
|
|
|
|
wantLease map[string][]base.Z
|
|
|
|
wantRetry map[string][]*base.TaskMessage
|
|
|
|
wantArchived map[string][]*base.TaskMessage
|
2020-06-21 22:05:57 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-09-06 03:43:15 +08:00
|
|
|
desc: "with one active task",
|
2022-02-11 22:18:27 +08:00
|
|
|
active: map[string][]*base.TaskMessage{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {t1},
|
|
|
|
},
|
2022-02-11 22:18:27 +08:00
|
|
|
lease: map[string][]base.Z{
|
|
|
|
"default": {{Message: t1, Score: now.Add(-1 * time.Minute).Unix()}},
|
2020-08-10 21:10:14 +08:00
|
|
|
},
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
2020-09-06 03:43:15 +08:00
|
|
|
wantActive: map[string][]*base.TaskMessage{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
2022-02-11 22:18:27 +08:00
|
|
|
wantLease: map[string][]base.Z{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
wantRetry: map[string][]*base.TaskMessage{
|
2021-06-03 21:58:07 +08:00
|
|
|
"default": {t1},
|
2020-08-10 21:10:14 +08:00
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]*base.TaskMessage{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {},
|
2020-06-21 22:05:57 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-10 21:10:14 +08:00
|
|
|
desc: "with a task with max-retry reached",
|
2022-02-11 22:18:27 +08:00
|
|
|
active: map[string][]*base.TaskMessage{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {t4},
|
|
|
|
"critical": {},
|
|
|
|
},
|
2022-02-11 22:18:27 +08:00
|
|
|
lease: map[string][]base.Z{
|
|
|
|
"default": {{Message: t4, Score: now.Add(-40 * time.Second).Unix()}},
|
2020-08-10 21:10:14 +08:00
|
|
|
"critical": {},
|
|
|
|
},
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
},
|
2020-09-06 03:43:15 +08:00
|
|
|
wantActive: map[string][]*base.TaskMessage{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
},
|
2022-02-11 22:18:27 +08:00
|
|
|
wantLease: map[string][]base.Z{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
},
|
|
|
|
wantRetry: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]*base.TaskMessage{
|
2021-06-03 21:58:07 +08:00
|
|
|
"default": {t4},
|
2020-08-10 21:10:14 +08:00
|
|
|
"critical": {},
|
|
|
|
},
|
2020-06-21 22:05:57 +08:00
|
|
|
},
|
|
|
|
{
|
2020-09-06 03:43:15 +08:00
|
|
|
desc: "with multiple active tasks, and one expired",
|
2022-02-11 22:18:27 +08:00
|
|
|
active: map[string][]*base.TaskMessage{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {t1, t2},
|
|
|
|
"critical": {t3},
|
|
|
|
},
|
2022-02-11 22:18:27 +08:00
|
|
|
lease: map[string][]base.Z{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {
|
2022-02-11 22:18:27 +08:00
|
|
|
{Message: t1, Score: now.Add(-2 * time.Minute).Unix()},
|
|
|
|
{Message: t2, Score: now.Add(20 * time.Second).Unix()},
|
2020-08-10 21:10:14 +08:00
|
|
|
},
|
|
|
|
"critical": {
|
2022-02-11 22:18:27 +08:00
|
|
|
{Message: t3, Score: now.Add(20 * time.Second).Unix()},
|
2020-08-10 21:10:14 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
},
|
2020-09-06 03:43:15 +08:00
|
|
|
wantActive: map[string][]*base.TaskMessage{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {t2},
|
|
|
|
"critical": {t3},
|
|
|
|
},
|
2022-02-11 22:18:27 +08:00
|
|
|
wantLease: map[string][]base.Z{
|
|
|
|
"default": {{Message: t2, Score: now.Add(20 * time.Second).Unix()}},
|
|
|
|
"critical": {{Message: t3, Score: now.Add(20 * time.Second).Unix()}},
|
2020-08-10 21:10:14 +08:00
|
|
|
},
|
|
|
|
wantRetry: map[string][]*base.TaskMessage{
|
2021-06-03 21:58:07 +08:00
|
|
|
"default": {t1},
|
2020-08-10 21:10:14 +08:00
|
|
|
"critical": {},
|
2020-06-21 22:05:57 +08:00
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]*base.TaskMessage{
|
2020-08-18 21:01:38 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
},
|
2020-06-21 22:05:57 +08:00
|
|
|
},
|
|
|
|
{
|
2020-09-06 03:43:15 +08:00
|
|
|
desc: "with multiple expired active tasks",
|
2022-02-11 22:18:27 +08:00
|
|
|
active: map[string][]*base.TaskMessage{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {t1, t2},
|
|
|
|
"critical": {t3},
|
|
|
|
},
|
2022-02-11 22:18:27 +08:00
|
|
|
lease: map[string][]base.Z{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {
|
2022-02-11 22:18:27 +08:00
|
|
|
{Message: t1, Score: now.Add(-1 * time.Minute).Unix()},
|
|
|
|
{Message: t2, Score: now.Add(10 * time.Second).Unix()},
|
2020-08-10 21:10:14 +08:00
|
|
|
},
|
|
|
|
"critical": {
|
2022-02-11 22:18:27 +08:00
|
|
|
{Message: t3, Score: now.Add(-1 * time.Minute).Unix()},
|
2020-08-10 21:10:14 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"cricial": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {},
|
|
|
|
"cricial": {},
|
|
|
|
},
|
2020-09-06 03:43:15 +08:00
|
|
|
wantActive: map[string][]*base.TaskMessage{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {t2},
|
|
|
|
"critical": {},
|
|
|
|
},
|
2022-02-11 22:18:27 +08:00
|
|
|
wantLease: map[string][]base.Z{
|
|
|
|
"default": {{Message: t2, Score: now.Add(10 * time.Second).Unix()}},
|
2020-08-10 21:10:14 +08:00
|
|
|
},
|
|
|
|
wantRetry: map[string][]*base.TaskMessage{
|
2021-06-03 21:58:07 +08:00
|
|
|
"default": {t1},
|
|
|
|
"critical": {t3},
|
2020-08-10 21:10:14 +08:00
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]*base.TaskMessage{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
2020-06-21 22:05:57 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-09-06 03:43:15 +08:00
|
|
|
desc: "with empty active queue",
|
2022-02-11 22:18:27 +08:00
|
|
|
active: map[string][]*base.TaskMessage{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
},
|
2022-02-11 22:18:27 +08:00
|
|
|
lease: map[string][]base.Z{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
},
|
|
|
|
retry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
archived: map[string][]base.Z{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
},
|
2020-09-06 03:43:15 +08:00
|
|
|
wantActive: map[string][]*base.TaskMessage{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
},
|
2022-02-11 22:18:27 +08:00
|
|
|
wantLease: map[string][]base.Z{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
},
|
|
|
|
wantRetry: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
},
|
2021-01-13 03:01:21 +08:00
|
|
|
wantArchived: map[string][]*base.TaskMessage{
|
2020-08-10 21:10:14 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
},
|
2020-06-21 22:05:57 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r)
|
2022-02-11 22:18:27 +08:00
|
|
|
h.SeedAllActiveQueues(t, r, tc.active)
|
|
|
|
h.SeedAllLease(t, r, tc.lease)
|
2020-08-10 21:10:14 +08:00
|
|
|
h.SeedAllRetryQueues(t, r, tc.retry)
|
2021-01-13 03:01:21 +08:00
|
|
|
h.SeedAllArchivedQueues(t, r, tc.archived)
|
2020-06-21 22:05:57 +08:00
|
|
|
|
|
|
|
recoverer := newRecoverer(recovererParams{
|
|
|
|
logger: testLogger,
|
|
|
|
broker: rdbClient,
|
2020-08-10 21:10:14 +08:00
|
|
|
queues: []string{"default", "critical"},
|
2020-06-21 22:05:57 +08:00
|
|
|
interval: 1 * time.Second,
|
|
|
|
retryDelayFunc: func(n int, err error, task *Task) time.Duration { return 30 * time.Second },
|
2021-09-01 21:00:54 +08:00
|
|
|
isFailureFunc: defaultIsFailureFunc,
|
2020-06-21 22:05:57 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
recoverer.start(&wg)
|
2021-06-03 21:58:07 +08:00
|
|
|
runTime := time.Now() // time when recoverer is running
|
2020-06-21 22:05:57 +08:00
|
|
|
time.Sleep(2 * time.Second)
|
2021-03-23 21:20:54 +08:00
|
|
|
recoverer.shutdown()
|
2020-06-21 22:05:57 +08:00
|
|
|
|
2020-09-06 03:43:15 +08:00
|
|
|
for qname, want := range tc.wantActive {
|
|
|
|
gotActive := h.GetActiveMessages(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotActive, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("%s; mismatch found in %q; (-want,+got)\n%s", tc.desc, base.ActiveKey(qname), diff)
|
2020-08-10 21:10:14 +08:00
|
|
|
}
|
2020-06-21 22:05:57 +08:00
|
|
|
}
|
2022-02-11 22:18:27 +08:00
|
|
|
for qname, want := range tc.wantLease {
|
|
|
|
gotLease := h.GetLeaseEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotLease, h.SortZSetEntryOpt); diff != "" {
|
|
|
|
t.Errorf("%s; mismatch found in %q; (-want,+got)\n%s", tc.desc, base.LeaseKey(qname), diff)
|
2020-08-10 21:10:14 +08:00
|
|
|
}
|
2020-06-21 22:05:57 +08:00
|
|
|
}
|
2021-06-03 21:58:07 +08:00
|
|
|
cmpOpt := h.EquateInt64Approx(2) // allow up to two-second difference in `LastFailedAt`
|
|
|
|
for qname, msgs := range tc.wantRetry {
|
2020-08-10 21:10:14 +08:00
|
|
|
gotRetry := h.GetRetryMessages(t, r, qname)
|
2021-06-03 21:58:07 +08:00
|
|
|
var wantRetry []*base.TaskMessage // Note: construct message here since `LastFailedAt` is relative to each test run
|
|
|
|
for _, msg := range msgs {
|
2022-02-11 22:18:27 +08:00
|
|
|
wantRetry = append(wantRetry, h.TaskMessageAfterRetry(*msg, ErrLeaseExpired.Error(), runTime))
|
2021-06-03 21:58:07 +08:00
|
|
|
}
|
|
|
|
if diff := cmp.Diff(wantRetry, gotRetry, h.SortMsgOpt, cmpOpt); diff != "" {
|
2020-08-10 21:10:14 +08:00
|
|
|
t.Errorf("%s; mismatch found in %q: (-want, +got)\n%s", tc.desc, base.RetryKey(qname), diff)
|
|
|
|
}
|
2020-06-21 22:05:57 +08:00
|
|
|
}
|
2021-06-03 21:58:07 +08:00
|
|
|
for qname, msgs := range tc.wantArchived {
|
|
|
|
gotArchived := h.GetArchivedMessages(t, r, qname)
|
|
|
|
var wantArchived []*base.TaskMessage
|
|
|
|
for _, msg := range msgs {
|
2022-02-11 22:18:27 +08:00
|
|
|
wantArchived = append(wantArchived, h.TaskMessageWithError(*msg, ErrLeaseExpired.Error(), runTime))
|
2021-06-03 21:58:07 +08:00
|
|
|
}
|
|
|
|
if diff := cmp.Diff(wantArchived, gotArchived, h.SortMsgOpt, cmpOpt); diff != "" {
|
2021-01-13 03:01:21 +08:00
|
|
|
t.Errorf("%s; mismatch found in %q: (-want, +got)\n%s", tc.desc, base.ArchivedKey(qname), diff)
|
2020-08-10 21:10:14 +08:00
|
|
|
}
|
2020-06-21 22:05:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|