2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-09-20 11:05:58 +08:00
asynq/scheduler_test.go

98 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
s := newScheduler(schedulerParams{
logger: testLogger,
broker: rdbClient,
interval: pollInterval,
})
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 {
2020-07-13 21:29:41 +08:00
initScheduled []base.Z // scheduled queue initial state
initRetry []base.Z // 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
}{
{
2020-07-13 21:29:41 +08:00
initScheduled: []base.Z{
{Message: t1, Score: now.Add(time.Hour).Unix()},
{Message: t2, Score: now.Add(-2 * time.Second).Unix()},
2019-11-30 00:00:43 +08:00
},
2020-07-13 21:29:41 +08:00
initRetry: []base.Z{
{Message: t3, Score: 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
},
{
2020-07-13 21:29:41 +08:00
initScheduled: []base.Z{
{Message: t1, Score: now.Unix()},
{Message: t2, Score: now.Add(-2 * time.Second).Unix()},
{Message: t3, Score: now.Add(-500 * time.Millisecond).Unix()},
2019-11-30 00:00:43 +08:00
},
2020-07-13 21:29:41 +08:00
initRetry: []base.Z{},
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
}
}
}