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"
|
2020-09-27 08:33:29 +08:00
|
|
|
"github.com/hibiken/asynq/internal/asynqtest"
|
2019-12-22 23:15:45 +08:00
|
|
|
"github.com/hibiken/asynq/internal/base"
|
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
|
|
|
tests := []struct {
|
2020-09-27 08:33:29 +08:00
|
|
|
cronspec string
|
|
|
|
task *Task
|
|
|
|
opts []Option
|
|
|
|
wait time.Duration
|
|
|
|
queue string
|
|
|
|
want []*base.TaskMessage
|
2019-11-30 00:00:43 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-09-27 08:33:29 +08:00
|
|
|
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",
|
2020-08-09 22:13:42 +08:00
|
|
|
},
|
2020-09-27 08:33:29 +08:00
|
|
|
{
|
|
|
|
Type: "task1",
|
|
|
|
Payload: nil,
|
|
|
|
Retry: 10,
|
|
|
|
Timeout: int64(defaultTimeout.Seconds()),
|
|
|
|
Queue: "default",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "task1",
|
|
|
|
Payload: nil,
|
|
|
|
Retry: 10,
|
|
|
|
Timeout: int64(defaultTimeout.Seconds()),
|
|
|
|
Queue: "default",
|
2020-08-09 22:13:42 +08:00
|
|
|
},
|
2019-11-30 00:00:43 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-09-27 08:33:29 +08:00
|
|
|
r := setup(t)
|
|
|
|
|
2019-11-30 00:00:43 +08:00
|
|
|
for _, tc := range tests {
|
2020-09-27 08:33:29 +08:00
|
|
|
scheduler := NewScheduler(getRedisConnOpt(t), nil)
|
|
|
|
if _, err := scheduler.Register(tc.cronspec, tc.task, tc.opts...); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-30 00:00:43 +08:00
|
|
|
|
2020-09-27 08:33:29 +08:00
|
|
|
if err := scheduler.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-30 00:00:43 +08:00
|
|
|
time.Sleep(tc.wait)
|
2020-09-27 08:33:29 +08:00
|
|
|
if err := scheduler.Stop(); err != nil {
|
|
|
|
t.Fatal(err)
|
2019-11-30 00:00:43 +08:00
|
|
|
}
|
|
|
|
|
2020-09-27 08:33:29 +08:00
|
|
|
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)
|
2019-11-30 00:00:43 +08:00
|
|
|
}
|
2020-09-27 08:33:29 +08:00
|
|
|
}
|
|
|
|
}
|