2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-11-15 11:58:47 +08:00
asynq/recoverer_test.go

277 lines
7.5 KiB
Go
Raw Normal View History

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"
"github.com/hibiken/asynq/internal/base"
"github.com/hibiken/asynq/internal/rdb"
2022-03-19 22:16:55 +08:00
h "github.com/hibiken/asynq/internal/testutil"
2020-06-21 22:05:57 +08:00
)
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 {
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",
active: map[string][]*base.TaskMessage{
2020-08-10 21:10:14 +08:00
"default": {t1},
},
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": {},
},
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": {},
},
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
},
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",
active: map[string][]*base.TaskMessage{
2020-08-10 21:10:14 +08:00
"default": {t4},
"critical": {},
},
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": {},
},
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": {},
},
wantLease: map[string][]base.Z{
2020-08-10 21:10:14 +08:00
"default": {},
"critical": {},
},
wantRetry: map[string][]*base.TaskMessage{
"default": {},
"critical": {},
},
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",
active: map[string][]*base.TaskMessage{
2020-08-10 21:10:14 +08:00
"default": {t1, t2},
"critical": {t3},
},
lease: map[string][]base.Z{
2020-08-10 21:10:14 +08:00
"default": {
{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": {
{Message: t3, Score: now.Add(20 * time.Second).Unix()},
2020-08-10 21:10:14 +08:00
},
},
retry: map[string][]base.Z{
"default": {},
"critical": {},
},
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},
},
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
},
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",
active: map[string][]*base.TaskMessage{
2020-08-10 21:10:14 +08:00
"default": {t1, t2},
"critical": {t3},
},
lease: map[string][]base.Z{
2020-08-10 21:10:14 +08:00
"default": {
{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": {
{Message: t3, Score: now.Add(-1 * time.Minute).Unix()},
2020-08-10 21:10:14 +08:00
},
},
retry: map[string][]base.Z{
"default": {},
"cricial": {},
},
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": {},
},
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
},
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",
active: map[string][]*base.TaskMessage{
2020-08-10 21:10:14 +08:00
"default": {},
"critical": {},
},
lease: map[string][]base.Z{
2020-08-10 21:10:14 +08:00
"default": {},
"critical": {},
},
retry: map[string][]base.Z{
"default": {},
"critical": {},
},
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": {},
},
wantLease: map[string][]base.Z{
2020-08-10 21:10:14 +08:00
"default": {},
"critical": {},
},
wantRetry: map[string][]*base.TaskMessage{
"default": {},
"critical": {},
},
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)
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)
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 },
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)
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
}
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 {
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 {
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 != "" {
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
}
}
}