2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-11-14 11:31:18 +08:00
asynq/recoverer_test.go

281 lines
7.6 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"
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()
oneHourFromNow := now.Add(1 * time.Hour)
fiveMinutesFromNow := now.Add(5 * time.Minute)
fiveMinutesAgo := now.Add(-5 * time.Minute)
oneHourAgo := now.Add(-1 * time.Hour)
tests := []struct {
2020-09-06 03:43:15 +08:00
desc string
inProgress map[string][]*base.TaskMessage
deadlines map[string][]base.Z
retry map[string][]base.Z
archived map[string][]base.Z
2020-09-06 03:43:15 +08:00
wantActive map[string][]*base.TaskMessage
wantDeadlines 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",
2020-08-10 21:10:14 +08:00
inProgress: map[string][]*base.TaskMessage{
"default": {t1},
},
deadlines: map[string][]base.Z{
"default": {{Message: t1, Score: fiveMinutesAgo.Unix()}},
},
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": {},
},
wantDeadlines: map[string][]base.Z{
"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",
inProgress: map[string][]*base.TaskMessage{
"default": {t4},
"critical": {},
},
deadlines: map[string][]base.Z{
"default": {{Message: t4, Score: fiveMinutesAgo.Unix()}},
"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": {},
},
wantDeadlines: map[string][]base.Z{
"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",
2020-08-10 21:10:14 +08:00
inProgress: map[string][]*base.TaskMessage{
"default": {t1, t2},
"critical": {t3},
},
deadlines: map[string][]base.Z{
"default": {
{Message: t1, Score: oneHourAgo.Unix()},
{Message: t2, Score: fiveMinutesFromNow.Unix()},
},
"critical": {
{Message: t3, Score: oneHourFromNow.Unix()},
},
},
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},
},
wantDeadlines: map[string][]base.Z{
"default": {{Message: t2, Score: fiveMinutesFromNow.Unix()}},
"critical": {{Message: t3, Score: oneHourFromNow.Unix()}},
},
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",
2020-08-10 21:10:14 +08:00
inProgress: map[string][]*base.TaskMessage{
"default": {t1, t2},
"critical": {t3},
},
deadlines: map[string][]base.Z{
"default": {
{Message: t1, Score: oneHourAgo.Unix()},
{Message: t2, Score: oneHourFromNow.Unix()},
},
"critical": {
{Message: t3, Score: fiveMinutesAgo.Unix()},
},
},
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": {},
},
wantDeadlines: map[string][]base.Z{
"default": {{Message: t2, Score: oneHourFromNow.Unix()}},
},
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",
2020-08-10 21:10:14 +08:00
inProgress: map[string][]*base.TaskMessage{
"default": {},
"critical": {},
},
deadlines: map[string][]base.Z{
"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": {},
},
wantDeadlines: map[string][]base.Z{
"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)
2020-09-06 03:43:15 +08:00
h.SeedAllActiveQueues(t, r, tc.inProgress)
2020-08-10 21:10:14 +08:00
h.SeedAllDeadlines(t, r, tc.deadlines)
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
}
2020-08-10 21:10:14 +08:00
for qname, want := range tc.wantDeadlines {
gotDeadlines := h.GetDeadlinesEntries(t, r, qname)
if diff := cmp.Diff(want, gotDeadlines, h.SortZSetEntryOpt); diff != "" {
t.Errorf("%s; mismatch found in %q; (-want,+got)\n%s", tc.desc, base.DeadlinesKey(qname), diff)
}
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, "context deadline exceeded", 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, "context deadline exceeded", 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
}
}
}