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

113 lines
3.3 KiB
Go
Raw Normal View History

2019-11-30 00:00:43 +08:00
package asynq
import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
2019-12-04 13:01:26 +08:00
"github.com/hibiken/asynq/internal/rdb"
2019-11-30 00:00:43 +08:00
)
func TestPoller(t *testing.T) {
type scheduledTask struct {
2019-12-04 13:01:26 +08:00
msg *rdb.TaskMessage
2019-11-30 00:00:43 +08:00
processAt time.Time
}
r := setup(t)
2019-12-04 13:01:26 +08:00
rdbClient := rdb.NewRDB(r)
2019-11-30 00:00:43 +08:00
const pollInterval = time.Second
2019-12-04 23:14:37 +08:00
p := newPoller(rdbClient, pollInterval)
2019-11-30 00:00:43 +08:00
t1 := randomTask("gen_thumbnail", "default", nil)
t2 := randomTask("send_email", "default", nil)
t3 := randomTask("reindex", "default", nil)
t4 := randomTask("sync", "default", nil)
tests := []struct {
2019-12-04 13:01:26 +08:00
initScheduled []scheduledTask // scheduled queue initial state
initRetry []scheduledTask // retry queue initial state
initQueue []*rdb.TaskMessage // default queue initial state
wait time.Duration // wait duration before checking for final state
wantScheduled []*rdb.TaskMessage // schedule queue final state
wantRetry []*rdb.TaskMessage // retry queue final state
wantQueue []*rdb.TaskMessage // default queue final state
2019-11-30 00:00:43 +08:00
}{
{
initScheduled: []scheduledTask{
{t1, time.Now().Add(time.Hour)},
{t2, time.Now().Add(-2 * time.Second)},
},
initRetry: []scheduledTask{
{t3, time.Now().Add(-500 * time.Millisecond)},
},
2019-12-04 13:01:26 +08:00
initQueue: []*rdb.TaskMessage{t4},
2019-11-30 00:00:43 +08:00
wait: pollInterval * 2,
2019-12-04 13:01:26 +08:00
wantScheduled: []*rdb.TaskMessage{t1},
wantRetry: []*rdb.TaskMessage{},
wantQueue: []*rdb.TaskMessage{t2, t3, t4},
2019-11-30 00:00:43 +08:00
},
{
initScheduled: []scheduledTask{
{t1, time.Now()},
{t2, time.Now().Add(-2 * time.Second)},
{t3, time.Now().Add(-500 * time.Millisecond)},
},
initRetry: []scheduledTask{},
2019-12-04 13:01:26 +08:00
initQueue: []*rdb.TaskMessage{t4},
2019-11-30 00:00:43 +08:00
wait: pollInterval * 2,
2019-12-04 13:01:26 +08:00
wantScheduled: []*rdb.TaskMessage{},
wantRetry: []*rdb.TaskMessage{},
wantQueue: []*rdb.TaskMessage{t1, t2, t3, t4},
2019-11-30 00:00:43 +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 00:00:43 +08:00
t.Fatal(err)
}
// initialize scheduled queue
for _, st := range tc.initScheduled {
err := rdbClient.Schedule(st.msg, st.processAt)
2019-11-30 00:00:43 +08:00
if err != nil {
t.Fatal(err)
}
}
// initialize retry queue
for _, st := range tc.initRetry {
err := rdbClient.RetryLater(st.msg, st.processAt)
2019-11-30 00:00:43 +08:00
if err != nil {
t.Fatal(err)
}
}
// initialize default queue
for _, msg := range tc.initQueue {
2019-12-04 13:01:26 +08:00
err := rdbClient.Enqueue(msg)
2019-11-30 00:00:43 +08:00
if err != nil {
t.Fatal(err)
}
}
p.start()
time.Sleep(tc.wait)
p.terminate()
gotScheduledRaw := r.ZRange(scheduledQ, 0, -1).Val()
2019-11-30 00:00:43 +08:00
gotScheduled := mustUnmarshalSlice(t, gotScheduledRaw)
if diff := cmp.Diff(tc.wantScheduled, gotScheduled, sortMsgOpt); diff != "" {
t.Errorf("mismatch found in %q after running poller: (-want, +got)\n%s", scheduledQ, diff)
2019-11-30 00:00:43 +08:00
}
gotRetryRaw := r.ZRange(retryQ, 0, -1).Val()
2019-11-30 00:00:43 +08:00
gotRetry := mustUnmarshalSlice(t, gotRetryRaw)
if diff := cmp.Diff(tc.wantRetry, gotRetry, sortMsgOpt); diff != "" {
t.Errorf("mismatch found in %q after running poller: (-want, +got)\n%s", retryQ, diff)
2019-11-30 00:00:43 +08:00
}
gotQueueRaw := r.LRange(defaultQ, 0, -1).Val()
2019-11-30 00:00:43 +08:00
gotQueue := mustUnmarshalSlice(t, gotQueueRaw)
if diff := cmp.Diff(tc.wantQueue, gotQueue, sortMsgOpt); diff != "" {
t.Errorf("mismatch found in %q after running poller: (-want, +got)\n%s", defaultQ, diff)
2019-11-30 00:00:43 +08:00
}
}
}