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

94 lines
3.3 KiB
Go
Raw Normal View History

2020-01-03 10:13:16 +08:00
// Copyright 2020 Kentaro Hibino. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
2019-11-30 00:00:43 +08:00
package asynq
import (
2020-02-16 15:14:30 +08:00
"sync"
2019-11-30 00:00:43 +08:00
"testing"
"time"
"github.com/google/go-cmp/cmp"
h "github.com/hibiken/asynq/internal/asynqtest"
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
)
2019-12-29 05:33:24 +08:00
func TestScheduler(t *testing.T) {
2019-11-30 00:00:43 +08:00
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
2020-03-09 22:11:16 +08:00
s := newScheduler(testLogger, rdbClient, pollInterval, defaultQueueConfig)
t1 := h.NewTaskMessage("gen_thumbnail", nil)
t2 := h.NewTaskMessage("send_email", nil)
t3 := h.NewTaskMessage("reindex", nil)
t4 := h.NewTaskMessage("sync", nil)
now := time.Now()
2019-11-30 00:00:43 +08:00
tests := []struct {
initScheduled []h.ZSetEntry // scheduled queue initial state
initRetry []h.ZSetEntry // retry queue initial state
2019-12-22 23:15:45 +08:00
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: []h.ZSetEntry{
{Msg: t1, Score: float64(now.Add(time.Hour).Unix())},
{Msg: t2, Score: float64(now.Add(-2 * time.Second).Unix())},
2019-11-30 00:00:43 +08:00
},
initRetry: []h.ZSetEntry{
{Msg: t3, Score: float64(time.Now().Add(-500 * time.Millisecond).Unix())},
2019-11-30 00:00:43 +08:00
},
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: []h.ZSetEntry{
{Msg: t1, Score: float64(now.Unix())},
{Msg: t2, Score: float64(now.Add(-2 * time.Second).Unix())},
{Msg: t3, Score: float64(now.Add(-500 * time.Millisecond).Unix())},
2019-11-30 00:00:43 +08:00
},
initRetry: []h.ZSetEntry{},
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 {
h.FlushDB(t, r) // clean up db before each test case.
h.SeedScheduledQueue(t, r, tc.initScheduled) // initialize scheduled queue
h.SeedRetryQueue(t, r, tc.initRetry) // initialize retry queue
h.SeedEnqueuedQueue(t, r, tc.initQueue) // initialize default queue
2019-11-30 00:00:43 +08:00
2020-02-16 15:14:30 +08:00
var wg sync.WaitGroup
s.start(&wg)
2019-11-30 00:00:43 +08:00
time.Sleep(tc.wait)
2019-12-29 05:33:24 +08:00
s.terminate()
2019-11-30 00:00:43 +08:00
gotScheduled := h.GetScheduledMessages(t, r)
if diff := cmp.Diff(tc.wantScheduled, gotScheduled, h.SortMsgOpt); diff != "" {
2019-12-29 05:33:24 +08:00
t.Errorf("mismatch found in %q after running scheduler: (-want, +got)\n%s", base.ScheduledQueue, diff)
2019-11-30 00:00:43 +08:00
}
gotRetry := h.GetRetryMessages(t, r)
if diff := cmp.Diff(tc.wantRetry, gotRetry, h.SortMsgOpt); diff != "" {
2019-12-29 05:33:24 +08:00
t.Errorf("mismatch found in %q after running scheduler: (-want, +got)\n%s", base.RetryQueue, diff)
2019-11-30 00:00:43 +08:00
}
gotEnqueued := h.GetEnqueuedMessages(t, r)
if diff := cmp.Diff(tc.wantQueue, gotEnqueued, h.SortMsgOpt); diff != "" {
2019-12-29 05:33:24 +08:00
t.Errorf("mismatch found in %q after running scheduler: (-want, +got)\n%s", base.DefaultQueue, diff)
2019-11-30 00:00:43 +08:00
}
}
}