2019-11-29 03:22:42 +08:00
|
|
|
package asynq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2019-12-11 23:38:24 +08:00
|
|
|
"github.com/rs/xid"
|
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-11 23:38:24 +08:00
|
|
|
t1 := &rdb.TaskMessage{Type: "send_email", Retry: 10, Retried: 10, Queue: "default", ID: xid.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-11 23:38:24 +08:00
|
|
|
t3 := &rdb.TaskMessage{Type: "send_email", Retry: 10, Retried: 5, Queue: "default", ID: xid.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-05 09:23:11 +08:00
|
|
|
deadQueue := r.ZRange(deadQ, 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-05 09:23:11 +08:00
|
|
|
t.Errorf("%s;\nmismatch found in %q after retryTask(); (-want, +got)\n%s", tc.desc, deadQ, diff)
|
2019-11-29 03:22:42 +08:00
|
|
|
}
|
|
|
|
|
2019-12-05 09:23:11 +08:00
|
|
|
retryQueue := r.ZRange(retryQ, 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-05 09:23:11 +08:00
|
|
|
t.Errorf("%s;\nmismatch found in %q after retryTask(); (-want, +got)\n%s", tc.desc, deadQ, diff)
|
2019-11-29 03:22:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|