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 (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2019-12-30 01:41:00 +08:00
|
|
|
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-01-14 23:26:41 +08:00
|
|
|
s := newScheduler(rdbClient, pollInterval, defaultQueueConfig)
|
2019-12-30 01:41:00 +08:00
|
|
|
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 {
|
2019-12-30 01:41:00 +08:00
|
|
|
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
|
|
|
}{
|
|
|
|
{
|
2019-12-30 01:41:00 +08:00
|
|
|
initScheduled: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{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
|
|
|
},
|
2019-12-30 01:41:00 +08:00
|
|
|
initRetry: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{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
|
|
|
},
|
|
|
|
{
|
2019-12-30 01:41:00 +08:00
|
|
|
initScheduled: []h.ZSetEntry{
|
2020-01-12 02:02:13 +08:00
|
|
|
{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
|
|
|
},
|
2019-12-30 01:41:00 +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 {
|
2019-12-30 01:41:00 +08:00
|
|
|
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
|
2020-01-07 22:28:34 +08:00
|
|
|
h.SeedEnqueuedQueue(t, r, tc.initQueue) // initialize default queue
|
2019-11-30 00:00:43 +08:00
|
|
|
|
2019-12-29 05:33:24 +08:00
|
|
|
s.start()
|
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
|
|
|
|
2019-12-30 01:41:00 +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
|
|
|
}
|
|
|
|
|
2019-12-30 01:41:00 +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
|
|
|
}
|
|
|
|
|
2019-12-30 01:41:00 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|