2019-11-28 06:03:04 +08:00
|
|
|
package asynq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-11-30 04:48:54 +08:00
|
|
|
"sync"
|
2019-11-28 06:03:04 +08:00
|
|
|
"testing"
|
2019-11-30 04:48:54 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2019-12-22 23:15:45 +08:00
|
|
|
"github.com/hibiken/asynq/internal/base"
|
2019-12-04 13:01:26 +08:00
|
|
|
"github.com/hibiken/asynq/internal/rdb"
|
2019-11-28 06:03:04 +08:00
|
|
|
)
|
|
|
|
|
2019-11-30 04:48:54 +08:00
|
|
|
func TestProcessorSuccess(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-04 13:01:26 +08:00
|
|
|
rdbClient := rdb.NewRDB(r)
|
2019-11-30 04:48:54 +08:00
|
|
|
|
|
|
|
m1 := randomTask("send_email", "default", nil)
|
|
|
|
m2 := randomTask("gen_thumbnail", "default", nil)
|
|
|
|
m3 := randomTask("reindex", "default", nil)
|
|
|
|
m4 := randomTask("sync", "default", nil)
|
|
|
|
|
|
|
|
t1 := &Task{Type: m1.Type, Payload: m1.Payload}
|
|
|
|
t2 := &Task{Type: m2.Type, Payload: m2.Payload}
|
|
|
|
t3 := &Task{Type: m3.Type, Payload: m3.Payload}
|
|
|
|
t4 := &Task{Type: m4.Type, Payload: m4.Payload}
|
|
|
|
|
|
|
|
tests := []struct {
|
2019-12-22 23:15:45 +08:00
|
|
|
initQueue []*base.TaskMessage // initial default queue state
|
|
|
|
incoming []*base.TaskMessage // tasks to be enqueued during run
|
|
|
|
wait time.Duration // wait duration between starting and stopping processor for this test case
|
|
|
|
wantProcessed []*Task // tasks to be processed at the end
|
2019-11-30 04:48:54 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-22 23:15:45 +08:00
|
|
|
initQueue: []*base.TaskMessage{m1},
|
|
|
|
incoming: []*base.TaskMessage{m2, m3, m4},
|
2019-11-30 04:48:54 +08:00
|
|
|
wait: time.Second,
|
|
|
|
wantProcessed: []*Task{t1, t2, t3, t4},
|
|
|
|
},
|
|
|
|
{
|
2019-12-22 23:15:45 +08:00
|
|
|
initQueue: []*base.TaskMessage{},
|
|
|
|
incoming: []*base.TaskMessage{m1},
|
2019-11-30 04:48:54 +08:00
|
|
|
wait: time.Second,
|
|
|
|
wantProcessed: []*Task{t1},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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-30 04:48:54 +08:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// instantiate a new processor
|
|
|
|
var mu sync.Mutex
|
|
|
|
var processed []*Task
|
2019-12-03 12:42:21 +08:00
|
|
|
var h HandlerFunc
|
|
|
|
h = func(task *Task) error {
|
2019-11-30 04:48:54 +08:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
processed = append(processed, task)
|
|
|
|
return nil
|
|
|
|
}
|
2019-12-04 13:01:26 +08:00
|
|
|
p := newProcessor(rdbClient, 10, h)
|
2019-11-30 04:48:54 +08:00
|
|
|
p.dequeueTimeout = time.Second // short time out for test purpose
|
|
|
|
// initialize default queue.
|
|
|
|
for _, msg := range tc.initQueue {
|
2019-12-04 13:01:26 +08:00
|
|
|
err := rdbClient.Enqueue(msg)
|
2019-11-30 04:48:54 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p.start()
|
|
|
|
|
|
|
|
for _, msg := range tc.incoming {
|
2019-12-04 13:01:26 +08:00
|
|
|
err := rdbClient.Enqueue(msg)
|
2019-11-30 04:48:54 +08:00
|
|
|
if err != nil {
|
|
|
|
p.terminate()
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(tc.wait)
|
|
|
|
p.terminate()
|
|
|
|
|
|
|
|
if diff := cmp.Diff(tc.wantProcessed, processed, sortTaskOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in processed tasks; (-want, +got)\n%s", diff)
|
|
|
|
}
|
|
|
|
|
2019-12-22 23:15:45 +08:00
|
|
|
if l := r.LLen(base.InProgressQueue).Val(); l != 0 {
|
|
|
|
t.Errorf("%q has %d tasks, want 0", base.InProgressQueue, l)
|
2019-11-30 04:48:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessorRetry(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-04 13:01:26 +08:00
|
|
|
rdbClient := rdb.NewRDB(r)
|
2019-11-30 04:48:54 +08:00
|
|
|
|
|
|
|
m1 := randomTask("send_email", "default", nil)
|
|
|
|
m1.Retried = m1.Retry // m1 has reached its max retry count
|
|
|
|
m2 := randomTask("gen_thumbnail", "default", nil)
|
|
|
|
m3 := randomTask("reindex", "default", nil)
|
|
|
|
m4 := randomTask("sync", "default", nil)
|
|
|
|
|
|
|
|
errMsg := "something went wrong"
|
|
|
|
// r* is m* after retry
|
|
|
|
r1 := *m1
|
|
|
|
r1.ErrorMsg = errMsg
|
|
|
|
r2 := *m2
|
|
|
|
r2.ErrorMsg = errMsg
|
|
|
|
r2.Retried = m2.Retried + 1
|
|
|
|
r3 := *m3
|
|
|
|
r3.ErrorMsg = errMsg
|
|
|
|
r3.Retried = m3.Retried + 1
|
|
|
|
r4 := *m4
|
|
|
|
r4.ErrorMsg = errMsg
|
|
|
|
r4.Retried = m4.Retried + 1
|
|
|
|
|
|
|
|
tests := []struct {
|
2019-12-22 23:15:45 +08:00
|
|
|
initQueue []*base.TaskMessage // initial default queue state
|
|
|
|
incoming []*base.TaskMessage // tasks to be enqueued during run
|
|
|
|
wait time.Duration // wait duration between starting and stopping processor for this test case
|
|
|
|
wantRetry []*base.TaskMessage // tasks in retry queue at the end
|
|
|
|
wantDead []*base.TaskMessage // tasks in dead queue at the end
|
2019-11-30 04:48:54 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-22 23:15:45 +08:00
|
|
|
initQueue: []*base.TaskMessage{m1, m2},
|
|
|
|
incoming: []*base.TaskMessage{m3, m4},
|
2019-11-30 04:48:54 +08:00
|
|
|
wait: time.Second,
|
2019-12-22 23:15:45 +08:00
|
|
|
wantRetry: []*base.TaskMessage{&r2, &r3, &r4},
|
|
|
|
wantDead: []*base.TaskMessage{&r1},
|
2019-11-30 04:48:54 +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-30 04:48:54 +08:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// instantiate a new processor
|
2019-12-03 12:42:21 +08:00
|
|
|
var h HandlerFunc
|
|
|
|
h = func(task *Task) error {
|
2019-11-30 04:48:54 +08:00
|
|
|
return fmt.Errorf(errMsg)
|
|
|
|
}
|
2019-12-04 13:01:26 +08:00
|
|
|
p := newProcessor(rdbClient, 10, h)
|
2019-11-30 04:48:54 +08:00
|
|
|
p.dequeueTimeout = time.Second // short time out for test purpose
|
|
|
|
// initialize default queue.
|
|
|
|
for _, msg := range tc.initQueue {
|
2019-12-04 13:01:26 +08:00
|
|
|
err := rdbClient.Enqueue(msg)
|
2019-11-30 04:48:54 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p.start()
|
|
|
|
for _, msg := range tc.incoming {
|
2019-12-04 13:01:26 +08:00
|
|
|
err := rdbClient.Enqueue(msg)
|
2019-11-30 04:48:54 +08:00
|
|
|
if err != nil {
|
|
|
|
p.terminate()
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(tc.wait)
|
|
|
|
p.terminate()
|
|
|
|
|
2019-12-22 23:15:45 +08:00
|
|
|
gotRetryRaw := r.ZRange(base.RetryQueue, 0, -1).Val()
|
2019-11-30 04:48:54 +08:00
|
|
|
gotRetry := mustUnmarshalSlice(t, gotRetryRaw)
|
|
|
|
if diff := cmp.Diff(tc.wantRetry, gotRetry, sortMsgOpt); diff != "" {
|
2019-12-22 23:15:45 +08:00
|
|
|
t.Errorf("mismatch found in %q after running processor; (-want, +got)\n%s", base.RetryQueue, diff)
|
2019-11-30 04:48:54 +08:00
|
|
|
}
|
|
|
|
|
2019-12-22 23:15:45 +08:00
|
|
|
gotDeadRaw := r.ZRange(base.DeadQueue, 0, -1).Val()
|
2019-11-30 04:48:54 +08:00
|
|
|
gotDead := mustUnmarshalSlice(t, gotDeadRaw)
|
|
|
|
if diff := cmp.Diff(tc.wantDead, gotDead, sortMsgOpt); diff != "" {
|
2019-12-22 23:15:45 +08:00
|
|
|
t.Errorf("mismatch found in %q after running processor; (-want, +got)\n%s", base.DeadQueue, diff)
|
2019-11-30 04:48:54 +08:00
|
|
|
}
|
|
|
|
|
2019-12-22 23:15:45 +08:00
|
|
|
if l := r.LLen(base.InProgressQueue).Val(); l != 0 {
|
|
|
|
t.Errorf("%q has %d tasks, want 0", base.InProgressQueue, l)
|
2019-11-30 04:48:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 06:03:04 +08:00
|
|
|
func TestPerform(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
2019-12-03 12:42:21 +08:00
|
|
|
handler HandlerFunc
|
2019-11-28 06:03:04 +08:00
|
|
|
task *Task
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "handler returns nil",
|
|
|
|
handler: func(t *Task) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
task: &Task{Type: "gen_thumbnail", Payload: map[string]interface{}{"src": "some/img/path"}},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "handler returns error",
|
|
|
|
handler: func(t *Task) error {
|
|
|
|
return fmt.Errorf("something went wrong")
|
|
|
|
},
|
|
|
|
task: &Task{Type: "gen_thumbnail", Payload: map[string]interface{}{"src": "some/img/path"}},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "handler panics",
|
|
|
|
handler: func(t *Task) error {
|
|
|
|
panic("something went terribly wrong")
|
|
|
|
},
|
|
|
|
task: &Task{Type: "gen_thumbnail", Payload: map[string]interface{}{"src": "some/img/path"}},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
got := perform(tc.handler, tc.task)
|
|
|
|
if !tc.wantErr && got != nil {
|
|
|
|
t.Errorf("%s: perform() = %v, want nil", tc.desc, got)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if tc.wantErr && got == nil {
|
|
|
|
t.Errorf("%s: perform() = nil, want non-nil error", tc.desc)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|