2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-11-10 11:31:58 +08:00
asynq/retry_test.go

72 lines
2.1 KiB
Go
Raw Normal View History

2019-11-29 03:22:42 +08:00
package asynq
import (
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
2019-12-04 13:01:26 +08:00
"github.com/hibiken/asynq/internal/rdb"
2019-11-29 03:22:42 +08:00
)
func TestRetry(t *testing.T) {
r := setup(t)
2019-12-04 13:01:26 +08:00
rdbClient := rdb.NewRDB(r)
2019-11-29 03:22:42 +08:00
errMsg := "email server not responding"
// t1 is a task with max-retry count reached.
2019-12-04 13:01:26 +08:00
t1 := &rdb.TaskMessage{Type: "send_email", Retry: 10, Retried: 10, Queue: "default", ID: uuid.New()}
2019-11-29 03:22:42 +08:00
// t2 is t1 with updated error message.
t2 := *t1
t2.ErrorMsg = errMsg
// t3 is a task which hasn't reached max-retry count.
2019-12-04 13:01:26 +08:00
t3 := &rdb.TaskMessage{Type: "send_email", Retry: 10, Retried: 5, Queue: "default", ID: uuid.New()}
2019-11-29 03:22:42 +08:00
// t4 is t3 after retry.
t4 := *t3
t4.Retried++
t4.ErrorMsg = errMsg
tests := []struct {
2019-12-04 13:01:26 +08:00
desc string // test case description
msg *rdb.TaskMessage // task to retry
err error // error that caused retry
wantDead []*rdb.TaskMessage // state "dead" queue should be in
wantRetry []*rdb.TaskMessage // state "retry" queue should be in
2019-11-29 03:22:42 +08:00
}{
{
desc: "With retry exhausted task",
msg: t1,
err: fmt.Errorf(errMsg),
2019-12-04 13:01:26 +08:00
wantDead: []*rdb.TaskMessage{&t2},
wantRetry: []*rdb.TaskMessage{},
2019-11-29 03:22:42 +08:00
},
{
desc: "With retry-able task",
msg: t3,
err: fmt.Errorf(errMsg),
2019-12-04 13:01:26 +08:00
wantDead: []*rdb.TaskMessage{},
wantRetry: []*rdb.TaskMessage{&t4},
2019-11-29 03:22:42 +08:00
},
}
for _, tc := range tests {
// clean up db before each test case.
2019-12-04 13:01:26 +08:00
if err := r.FlushDB().Err(); err != nil {
2019-11-29 03:22:42 +08:00
t.Fatal(err)
}
2019-12-04 13:01:26 +08:00
retryTask(rdbClient, tc.msg, tc.err)
2019-11-29 03:22:42 +08:00
2019-12-04 13:01:26 +08:00
deadQueue := r.ZRange(rdb.Dead, 0, -1).Val()
2019-11-29 03:22:42 +08:00
gotDead := mustUnmarshalSlice(t, deadQueue)
if diff := cmp.Diff(tc.wantDead, gotDead, sortMsgOpt); diff != "" {
2019-12-04 13:01:26 +08:00
t.Errorf("%s;\nmismatch found in %q after retryTask(); (-want, +got)\n%s", tc.desc, rdb.Dead, diff)
2019-11-29 03:22:42 +08:00
}
2019-12-04 13:01:26 +08:00
retryQueue := r.ZRange(rdb.Retry, 0, -1).Val()
2019-11-29 03:22:42 +08:00
gotRetry := mustUnmarshalSlice(t, retryQueue)
if diff := cmp.Diff(tc.wantRetry, gotRetry, sortMsgOpt); diff != "" {
2019-12-04 13:01:26 +08:00
t.Errorf("%s;\nmismatch found in %q after retryTask(); (-want, +got)\n%s", tc.desc, rdb.Dead, diff)
2019-11-29 03:22:42 +08:00
}
}
}