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"
|
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)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
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-05-18 03:33:55 +08:00
|
|
|
s := newScheduler(schedulerParams{
|
|
|
|
logger: testLogger,
|
|
|
|
broker: rdbClient,
|
2020-08-09 22:13:42 +08:00
|
|
|
queues: []string{"default", "critical"},
|
2020-05-18 03:33:55 +08:00
|
|
|
interval: pollInterval,
|
|
|
|
})
|
2020-08-09 22:13:42 +08:00
|
|
|
t1 := h.NewTaskMessageWithQueue("gen_thumbnail", nil, "default")
|
|
|
|
t2 := h.NewTaskMessageWithQueue("send_email", nil, "critical")
|
|
|
|
t3 := h.NewTaskMessageWithQueue("reindex", nil, "default")
|
|
|
|
t4 := h.NewTaskMessageWithQueue("sync", nil, "critical")
|
2019-12-30 01:41:00 +08:00
|
|
|
now := time.Now()
|
2019-11-30 00:00:43 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-08-09 22:13:42 +08:00
|
|
|
initScheduled map[string][]base.Z // scheduled queue initial state
|
|
|
|
initRetry map[string][]base.Z // retry queue initial state
|
2020-09-05 22:03:43 +08:00
|
|
|
initPending map[string][]*base.TaskMessage // default queue initial state
|
2020-08-09 22:13:42 +08:00
|
|
|
wait time.Duration // wait duration before checking for final state
|
|
|
|
wantScheduled map[string][]*base.TaskMessage // schedule queue final state
|
|
|
|
wantRetry map[string][]*base.TaskMessage // retry queue final state
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending map[string][]*base.TaskMessage // default queue final state
|
2019-11-30 00:00:43 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-08-09 22:13:42 +08:00
|
|
|
initScheduled: map[string][]base.Z{
|
|
|
|
"default": {{Message: t1, Score: now.Add(time.Hour).Unix()}},
|
|
|
|
"critical": {{Message: t2, Score: now.Add(-2 * time.Second).Unix()}},
|
2019-11-30 00:00:43 +08:00
|
|
|
},
|
2020-08-09 22:13:42 +08:00
|
|
|
initRetry: map[string][]base.Z{
|
|
|
|
"default": {{Message: t3, Score: time.Now().Add(-500 * time.Millisecond).Unix()}},
|
|
|
|
"critical": {},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
initPending: map[string][]*base.TaskMessage{
|
2020-08-09 22:13:42 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {t4},
|
|
|
|
},
|
|
|
|
wait: pollInterval * 2,
|
|
|
|
wantScheduled: map[string][]*base.TaskMessage{
|
|
|
|
"default": {t1},
|
|
|
|
"critical": {},
|
|
|
|
},
|
|
|
|
wantRetry: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-09 22:13:42 +08:00
|
|
|
"default": {t3},
|
|
|
|
"critical": {t2, t4},
|
2019-11-30 00:00:43 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-08-09 22:13:42 +08:00
|
|
|
initScheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{Message: t1, Score: now.Unix()},
|
|
|
|
{Message: t3, Score: now.Add(-500 * time.Millisecond).Unix()},
|
|
|
|
},
|
|
|
|
"critical": {
|
|
|
|
{Message: t2, Score: now.Add(-2 * time.Second).Unix()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
initRetry: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
initPending: map[string][]*base.TaskMessage{
|
2020-08-09 22:13:42 +08:00
|
|
|
"default": {},
|
|
|
|
"critical": {t4},
|
|
|
|
},
|
|
|
|
wait: pollInterval * 2,
|
|
|
|
wantScheduled: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
},
|
|
|
|
wantRetry: map[string][]*base.TaskMessage{
|
|
|
|
"default": {},
|
|
|
|
"critical": {},
|
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-09 22:13:42 +08:00
|
|
|
"default": {t1, t3},
|
|
|
|
"critical": {t2, t4},
|
2019-11-30 00:00:43 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2020-08-09 22:13:42 +08:00
|
|
|
h.FlushDB(t, r) // clean up db before each test case.
|
|
|
|
h.SeedAllScheduledQueues(t, r, tc.initScheduled) // initialize scheduled queue
|
|
|
|
h.SeedAllRetryQueues(t, r, tc.initRetry) // initialize retry queue
|
2020-09-05 22:03:43 +08:00
|
|
|
h.SeedAllPendingQueues(t, r, tc.initPending) // 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
|
|
|
|
2020-08-09 22:13:42 +08:00
|
|
|
for qname, want := range tc.wantScheduled {
|
|
|
|
gotScheduled := h.GetScheduledMessages(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotScheduled, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q after running scheduler: (-want, +got)\n%s", base.ScheduledKey(qname), diff)
|
|
|
|
}
|
2019-11-30 00:00:43 +08:00
|
|
|
}
|
|
|
|
|
2020-08-09 22:13:42 +08:00
|
|
|
for qname, want := range tc.wantRetry {
|
|
|
|
gotRetry := h.GetRetryMessages(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotRetry, h.SortMsgOpt); diff != "" {
|
|
|
|
t.Errorf("mismatch found in %q after running scheduler: (-want, +got)\n%s", base.RetryKey(qname), diff)
|
|
|
|
}
|
2019-11-30 00:00:43 +08:00
|
|
|
}
|
|
|
|
|
2020-09-05 22:03:43 +08:00
|
|
|
for qname, want := range tc.wantPending {
|
|
|
|
gotPending := h.GetPendingMessages(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
2020-08-18 21:01:38 +08:00
|
|
|
t.Errorf("mismatch found in %q after running scheduler: (-want, +got)\n%s", base.QueueKey(qname), diff)
|
2020-08-09 22:13:42 +08:00
|
|
|
}
|
2019-11-30 00:00:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|