2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-27 00:02:19 +08:00
asynq/poller_test.go

118 lines
3.5 KiB
Go
Raw Normal View History

2019-11-30 00:00:43 +08:00
package asynq
import (
"testing"
"time"
"github.com/go-redis/redis/v7"
2019-11-30 00:00:43 +08:00
"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-30 00:00:43 +08:00
)
func TestPoller(t *testing.T) {
type scheduledTask struct {
2019-12-22 23:15:45 +08:00
msg *base.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-22 23:15:45 +08:00
initScheduled []scheduledTask // scheduled queue initial state
initRetry []scheduledTask // retry queue initial state
initQueue []*base.TaskMessage // default queue initial state
wait time.Duration // wait duration before checking for final state
wantScheduled []*base.TaskMessage // schedule queue final state
wantRetry []*base.TaskMessage // retry queue final state
wantQueue []*base.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-22 23:15:45 +08:00
initQueue: []*base.TaskMessage{t4},
2019-11-30 00:00:43 +08:00
wait: pollInterval * 2,
2019-12-22 23:15:45 +08:00
wantScheduled: []*base.TaskMessage{t1},
wantRetry: []*base.TaskMessage{},
wantQueue: []*base.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-22 23:15:45 +08:00
initQueue: []*base.TaskMessage{t4},
2019-11-30 00:00:43 +08:00
wait: pollInterval * 2,
2019-12-22 23:15:45 +08:00
wantScheduled: []*base.TaskMessage{},
wantRetry: []*base.TaskMessage{},
wantQueue: []*base.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 {
2019-12-22 23:15:45 +08:00
err := r.ZAdd(base.RetryQueue, &redis.Z{
Member: mustMarshal(t, st.msg),
Score: float64(st.processAt.Unix()),
}).Err()
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()
2019-12-22 23:15:45 +08:00
gotScheduledRaw := r.ZRange(base.ScheduledQueue, 0, -1).Val()
2019-11-30 00:00:43 +08:00
gotScheduled := mustUnmarshalSlice(t, gotScheduledRaw)
if diff := cmp.Diff(tc.wantScheduled, gotScheduled, sortMsgOpt); diff != "" {
2019-12-22 23:15:45 +08:00
t.Errorf("mismatch found in %q after running poller: (-want, +got)\n%s", base.ScheduledQueue, diff)
2019-11-30 00:00:43 +08:00
}
2019-12-22 23:15:45 +08:00
gotRetryRaw := r.ZRange(base.RetryQueue, 0, -1).Val()
2019-11-30 00:00:43 +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 poller: (-want, +got)\n%s", base.RetryQueue, diff)
2019-11-30 00:00:43 +08:00
}
2019-12-22 23:15:45 +08:00
gotQueueRaw := r.LRange(base.DefaultQueue, 0, -1).Val()
2019-11-30 00:00:43 +08:00
gotQueue := mustUnmarshalSlice(t, gotQueueRaw)
if diff := cmp.Diff(tc.wantQueue, gotQueue, sortMsgOpt); diff != "" {
2019-12-22 23:15:45 +08:00
t.Errorf("mismatch found in %q after running poller: (-want, +got)\n%s", base.DefaultQueue, diff)
2019-11-30 00:00:43 +08:00
}
}
}