mirror of
https://github.com/hibiken/asynq.git
synced 2025-09-19 05:17:30 +08:00
Add Scheduler
- Renamed previously called scheduler to forwarder to resolve name conflicts
This commit is contained in:
@@ -5,133 +5,109 @@
|
||||
package asynq
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
h "github.com/hibiken/asynq/internal/asynqtest"
|
||||
"github.com/hibiken/asynq/internal/asynqtest"
|
||||
"github.com/hibiken/asynq/internal/base"
|
||||
"github.com/hibiken/asynq/internal/rdb"
|
||||
)
|
||||
|
||||
func TestScheduler(t *testing.T) {
|
||||
r := setup(t)
|
||||
defer r.Close()
|
||||
rdbClient := rdb.NewRDB(r)
|
||||
const pollInterval = time.Second
|
||||
s := newScheduler(schedulerParams{
|
||||
logger: testLogger,
|
||||
broker: rdbClient,
|
||||
queues: []string{"default", "critical"},
|
||||
interval: pollInterval,
|
||||
})
|
||||
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")
|
||||
now := time.Now()
|
||||
|
||||
tests := []struct {
|
||||
initScheduled map[string][]base.Z // scheduled queue initial state
|
||||
initRetry map[string][]base.Z // retry queue initial state
|
||||
initPending map[string][]*base.TaskMessage // default queue initial state
|
||||
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
|
||||
wantPending map[string][]*base.TaskMessage // default queue final state
|
||||
cronspec string
|
||||
task *Task
|
||||
opts []Option
|
||||
wait time.Duration
|
||||
queue string
|
||||
want []*base.TaskMessage
|
||||
}{
|
||||
{
|
||||
initScheduled: map[string][]base.Z{
|
||||
"default": {{Message: t1, Score: now.Add(time.Hour).Unix()}},
|
||||
"critical": {{Message: t2, Score: now.Add(-2 * time.Second).Unix()}},
|
||||
},
|
||||
initRetry: map[string][]base.Z{
|
||||
"default": {{Message: t3, Score: time.Now().Add(-500 * time.Millisecond).Unix()}},
|
||||
"critical": {},
|
||||
},
|
||||
initPending: map[string][]*base.TaskMessage{
|
||||
"default": {},
|
||||
"critical": {t4},
|
||||
},
|
||||
wait: pollInterval * 2,
|
||||
wantScheduled: map[string][]*base.TaskMessage{
|
||||
"default": {t1},
|
||||
"critical": {},
|
||||
},
|
||||
wantRetry: map[string][]*base.TaskMessage{
|
||||
"default": {},
|
||||
"critical": {},
|
||||
},
|
||||
wantPending: map[string][]*base.TaskMessage{
|
||||
"default": {t3},
|
||||
"critical": {t2, t4},
|
||||
cronspec: "@every 3s",
|
||||
task: NewTask("task1", nil),
|
||||
opts: []Option{MaxRetry(10)},
|
||||
wait: 10 * time.Second,
|
||||
queue: "default",
|
||||
want: []*base.TaskMessage{
|
||||
{
|
||||
Type: "task1",
|
||||
Payload: nil,
|
||||
Retry: 10,
|
||||
Timeout: int64(defaultTimeout.Seconds()),
|
||||
Queue: "default",
|
||||
},
|
||||
{
|
||||
Type: "task1",
|
||||
Payload: nil,
|
||||
Retry: 10,
|
||||
Timeout: int64(defaultTimeout.Seconds()),
|
||||
Queue: "default",
|
||||
},
|
||||
{
|
||||
Type: "task1",
|
||||
Payload: nil,
|
||||
Retry: 10,
|
||||
Timeout: int64(defaultTimeout.Seconds()),
|
||||
Queue: "default",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
r := setup(t)
|
||||
|
||||
for _, tc := range tests {
|
||||
scheduler := NewScheduler(getRedisConnOpt(t), nil)
|
||||
if _, err := scheduler.Register(tc.cronspec, tc.task, tc.opts...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := scheduler.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
time.Sleep(tc.wait)
|
||||
if err := scheduler.Stop(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
got := asynqtest.GetPendingMessages(t, r, tc.queue)
|
||||
if diff := cmp.Diff(tc.want, got, asynqtest.IgnoreIDOpt); diff != "" {
|
||||
t.Errorf("mismatch found in queue %q: (-want,+got)\n%s", tc.queue, diff)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringifyOptions(t *testing.T) {
|
||||
now := time.Now()
|
||||
oneHourFromNow := now.Add(1 * time.Hour)
|
||||
twoHoursFromNow := now.Add(2 * time.Hour)
|
||||
tests := []struct {
|
||||
opts []Option
|
||||
want string
|
||||
}{
|
||||
{
|
||||
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": {},
|
||||
},
|
||||
initPending: map[string][]*base.TaskMessage{
|
||||
"default": {},
|
||||
"critical": {t4},
|
||||
},
|
||||
wait: pollInterval * 2,
|
||||
wantScheduled: map[string][]*base.TaskMessage{
|
||||
"default": {},
|
||||
"critical": {},
|
||||
},
|
||||
wantRetry: map[string][]*base.TaskMessage{
|
||||
"default": {},
|
||||
"critical": {},
|
||||
},
|
||||
wantPending: map[string][]*base.TaskMessage{
|
||||
"default": {t1, t3},
|
||||
"critical": {t2, t4},
|
||||
},
|
||||
opts: []Option{MaxRetry(10)},
|
||||
want: "MaxRetry(10)",
|
||||
},
|
||||
{
|
||||
opts: []Option{Queue("custom"), Timeout(1 * time.Minute)},
|
||||
want: `Queue("custom"), Timeout(1m0s)`,
|
||||
},
|
||||
{
|
||||
opts: []Option{ProcessAt(oneHourFromNow), Deadline(twoHoursFromNow)},
|
||||
want: fmt.Sprintf("ProcessAt(%v), Deadline(%v)", oneHourFromNow, twoHoursFromNow),
|
||||
},
|
||||
{
|
||||
opts: []Option{ProcessIn(30 * time.Minute), Unique(1 * time.Hour)},
|
||||
want: "ProcessIn(30m0s), Unique(1h0m0s)",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
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
|
||||
h.SeedAllPendingQueues(t, r, tc.initPending) // initialize default queue
|
||||
|
||||
var wg sync.WaitGroup
|
||||
s.start(&wg)
|
||||
time.Sleep(tc.wait)
|
||||
s.terminate()
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
for qname, want := range tc.wantPending {
|
||||
gotPending := h.GetPendingMessages(t, r, qname)
|
||||
if diff := cmp.Diff(want, gotPending, h.SortMsgOpt); diff != "" {
|
||||
t.Errorf("mismatch found in %q after running scheduler: (-want, +got)\n%s", base.QueueKey(qname), diff)
|
||||
}
|
||||
got := stringifyOptions(tc.opts)
|
||||
if got != tc.want {
|
||||
t.Errorf("got %v, want %v", got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user