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 09:40:31 +08:00
|
|
|
package asynq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2019-12-21 23:42:32 +08:00
|
|
|
|
|
|
|
"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-11-30 09:40:31 +08:00
|
|
|
)
|
|
|
|
|
2020-02-24 07:40:04 +08:00
|
|
|
func TestClientEnqueueAt(t *testing.T) {
|
2019-11-30 09:40:31 +08:00
|
|
|
r := setup(t)
|
2020-02-23 12:42:53 +08:00
|
|
|
client := NewClient(RedisClientOpt{
|
2020-02-24 07:40:04 +08:00
|
|
|
Addr: redisAddr,
|
|
|
|
DB: redisDB,
|
2020-01-17 13:01:27 +08:00
|
|
|
})
|
2019-11-30 09:40:31 +08:00
|
|
|
|
2020-01-05 05:13:46 +08:00
|
|
|
task := NewTask("send_email", map[string]interface{}{"to": "customer@gmail.com", "from": "merchant@example.com"})
|
2019-12-21 23:42:32 +08:00
|
|
|
|
2020-02-24 07:40:04 +08:00
|
|
|
var (
|
|
|
|
now = time.Now()
|
|
|
|
oneHourLater = now.Add(time.Hour)
|
2020-03-08 12:24:03 +08:00
|
|
|
|
|
|
|
noTimeout = time.Duration(0).String()
|
|
|
|
noDeadline = time.Time{}.Format(time.RFC3339)
|
2020-02-24 07:40:04 +08:00
|
|
|
)
|
|
|
|
|
2019-11-30 09:40:31 +08:00
|
|
|
tests := []struct {
|
2019-12-21 23:42:32 +08:00
|
|
|
desc string
|
|
|
|
task *Task
|
|
|
|
processAt time.Time
|
|
|
|
opts []Option
|
2020-01-06 22:53:40 +08:00
|
|
|
wantEnqueued map[string][]*base.TaskMessage
|
2019-12-30 01:41:00 +08:00
|
|
|
wantScheduled []h.ZSetEntry
|
2019-11-30 09:40:31 +08:00
|
|
|
}{
|
|
|
|
{
|
2019-12-21 23:42:32 +08:00
|
|
|
desc: "Process task immediately",
|
|
|
|
task: task,
|
2020-02-24 07:40:04 +08:00
|
|
|
processAt: now,
|
2019-12-21 23:42:32 +08:00
|
|
|
opts: []Option{},
|
2020-01-06 22:53:40 +08:00
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
"default": []*base.TaskMessage{
|
|
|
|
&base.TaskMessage{
|
2020-03-08 12:24:03 +08:00
|
|
|
Type: task.Type,
|
|
|
|
Payload: task.Payload.data,
|
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
Queue: "default",
|
|
|
|
Timeout: noTimeout,
|
|
|
|
Deadline: noDeadline,
|
2020-01-06 22:53:40 +08:00
|
|
|
},
|
2019-12-21 23:42:32 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantScheduled: nil, // db is flushed in setup so zset does not exist hence nil
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Schedule task to be processed in the future",
|
|
|
|
task: task,
|
2020-02-24 07:40:04 +08:00
|
|
|
processAt: oneHourLater,
|
2019-12-21 23:42:32 +08:00
|
|
|
opts: []Option{},
|
|
|
|
wantEnqueued: nil, // db is flushed in setup so list does not exist hence nil
|
2019-12-30 01:41:00 +08:00
|
|
|
wantScheduled: []h.ZSetEntry{
|
2019-12-21 23:42:32 +08:00
|
|
|
{
|
2019-12-30 01:41:00 +08:00
|
|
|
Msg: &base.TaskMessage{
|
2020-03-08 12:24:03 +08:00
|
|
|
Type: task.Type,
|
|
|
|
Payload: task.Payload.data,
|
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
Queue: "default",
|
|
|
|
Timeout: noTimeout,
|
|
|
|
Deadline: noDeadline,
|
2019-12-21 23:42:32 +08:00
|
|
|
},
|
2020-02-24 07:40:04 +08:00
|
|
|
Score: float64(oneHourLater.Unix()),
|
2019-12-21 23:42:32 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-02-24 07:40:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r) // clean up db before each test case.
|
|
|
|
|
|
|
|
err := client.EnqueueAt(tc.processAt, tc.task, tc.opts...)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for qname, want := range tc.wantEnqueued {
|
|
|
|
gotEnqueued := h.GetEnqueuedMessages(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotEnqueued, h.IgnoreIDOpt); diff != "" {
|
|
|
|
t.Errorf("%s;\nmismatch found in %q; (-want,+got)\n%s", tc.desc, base.QueueKey(qname), diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gotScheduled := h.GetScheduledEntries(t, r)
|
|
|
|
if diff := cmp.Diff(tc.wantScheduled, gotScheduled, h.IgnoreIDOpt); diff != "" {
|
|
|
|
t.Errorf("%s;\nmismatch found in %q; (-want,+got)\n%s", tc.desc, base.ScheduledQueue, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientEnqueue(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
client := NewClient(RedisClientOpt{
|
|
|
|
Addr: redisAddr,
|
|
|
|
DB: redisDB,
|
|
|
|
})
|
|
|
|
|
|
|
|
task := NewTask("send_email", map[string]interface{}{"to": "customer@gmail.com", "from": "merchant@example.com"})
|
|
|
|
|
2020-03-08 12:24:03 +08:00
|
|
|
var (
|
|
|
|
noTimeout = time.Duration(0).String()
|
|
|
|
noDeadline = time.Time{}.Format(time.RFC3339)
|
|
|
|
)
|
|
|
|
|
2020-02-24 07:40:04 +08:00
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
task *Task
|
|
|
|
opts []Option
|
|
|
|
wantEnqueued map[string][]*base.TaskMessage
|
|
|
|
}{
|
2019-12-21 23:42:32 +08:00
|
|
|
{
|
2020-02-24 07:40:04 +08:00
|
|
|
desc: "Process task immediately with a custom retry count",
|
|
|
|
task: task,
|
2019-12-21 23:42:32 +08:00
|
|
|
opts: []Option{
|
|
|
|
MaxRetry(3),
|
|
|
|
},
|
2020-01-06 22:53:40 +08:00
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
"default": []*base.TaskMessage{
|
|
|
|
&base.TaskMessage{
|
2020-03-08 12:24:03 +08:00
|
|
|
Type: task.Type,
|
|
|
|
Payload: task.Payload.data,
|
|
|
|
Retry: 3,
|
|
|
|
Queue: "default",
|
|
|
|
Timeout: noTimeout,
|
|
|
|
Deadline: noDeadline,
|
2020-01-06 22:53:40 +08:00
|
|
|
},
|
2019-12-21 23:42:32 +08:00
|
|
|
},
|
|
|
|
},
|
2019-11-30 09:40:31 +08:00
|
|
|
},
|
|
|
|
{
|
2020-02-24 07:40:04 +08:00
|
|
|
desc: "Negative retry count",
|
|
|
|
task: task,
|
2019-12-21 23:42:32 +08:00
|
|
|
opts: []Option{
|
|
|
|
MaxRetry(-2),
|
|
|
|
},
|
2020-01-06 22:53:40 +08:00
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
"default": []*base.TaskMessage{
|
|
|
|
&base.TaskMessage{
|
2020-03-08 12:24:03 +08:00
|
|
|
Type: task.Type,
|
|
|
|
Payload: task.Payload.data,
|
|
|
|
Retry: 0, // Retry count should be set to zero
|
|
|
|
Queue: "default",
|
|
|
|
Timeout: noTimeout,
|
|
|
|
Deadline: noDeadline,
|
2020-01-06 22:53:40 +08:00
|
|
|
},
|
2019-12-21 23:42:32 +08:00
|
|
|
},
|
|
|
|
},
|
2019-11-30 09:40:31 +08:00
|
|
|
},
|
2019-12-22 02:02:03 +08:00
|
|
|
{
|
2020-02-24 07:40:04 +08:00
|
|
|
desc: "Conflicting options",
|
|
|
|
task: task,
|
2019-12-22 02:02:03 +08:00
|
|
|
opts: []Option{
|
|
|
|
MaxRetry(2),
|
|
|
|
MaxRetry(10),
|
|
|
|
},
|
2020-01-06 22:53:40 +08:00
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
"default": []*base.TaskMessage{
|
|
|
|
&base.TaskMessage{
|
2020-03-08 12:24:03 +08:00
|
|
|
Type: task.Type,
|
|
|
|
Payload: task.Payload.data,
|
|
|
|
Retry: 10, // Last option takes precedence
|
|
|
|
Queue: "default",
|
|
|
|
Timeout: noTimeout,
|
|
|
|
Deadline: noDeadline,
|
2020-01-06 22:53:40 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-02-24 07:40:04 +08:00
|
|
|
desc: "With queue option",
|
|
|
|
task: task,
|
2020-01-06 22:53:40 +08:00
|
|
|
opts: []Option{
|
|
|
|
Queue("custom"),
|
|
|
|
},
|
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
"custom": []*base.TaskMessage{
|
|
|
|
&base.TaskMessage{
|
2020-03-08 12:24:03 +08:00
|
|
|
Type: task.Type,
|
|
|
|
Payload: task.Payload.data,
|
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
Queue: "custom",
|
|
|
|
Timeout: noTimeout,
|
|
|
|
Deadline: noDeadline,
|
2020-01-06 22:53:40 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-02-24 07:40:04 +08:00
|
|
|
desc: "Queue option should be case-insensitive",
|
|
|
|
task: task,
|
2020-01-06 22:53:40 +08:00
|
|
|
opts: []Option{
|
|
|
|
Queue("HIGH"),
|
|
|
|
},
|
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
"high": []*base.TaskMessage{
|
|
|
|
&base.TaskMessage{
|
2020-03-08 12:24:03 +08:00
|
|
|
Type: task.Type,
|
|
|
|
Payload: task.Payload.data,
|
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
Queue: "high",
|
|
|
|
Timeout: noTimeout,
|
|
|
|
Deadline: noDeadline,
|
2020-02-12 13:53:59 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-03-08 12:24:03 +08:00
|
|
|
desc: "With timeout option",
|
2020-02-24 07:40:04 +08:00
|
|
|
task: task,
|
2020-02-12 13:53:59 +08:00
|
|
|
opts: []Option{
|
|
|
|
Timeout(20 * time.Second),
|
|
|
|
},
|
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
"default": []*base.TaskMessage{
|
|
|
|
&base.TaskMessage{
|
2020-03-08 12:24:03 +08:00
|
|
|
Type: task.Type,
|
|
|
|
Payload: task.Payload.data,
|
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
Queue: "default",
|
|
|
|
Timeout: (20 * time.Second).String(),
|
|
|
|
Deadline: noDeadline,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "With deadline option",
|
|
|
|
task: task,
|
|
|
|
opts: []Option{
|
|
|
|
Deadline(time.Date(2020, time.June, 24, 0, 0, 0, 0, time.UTC)),
|
|
|
|
},
|
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
"default": []*base.TaskMessage{
|
|
|
|
&base.TaskMessage{
|
|
|
|
Type: task.Type,
|
|
|
|
Payload: task.Payload.data,
|
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
Queue: "default",
|
|
|
|
Timeout: noTimeout,
|
|
|
|
Deadline: time.Date(2020, time.June, 24, 0, 0, 0, 0, time.UTC).Format(time.RFC3339),
|
2020-01-06 22:53:40 +08:00
|
|
|
},
|
2019-12-22 02:02:03 +08:00
|
|
|
},
|
|
|
|
},
|
2020-02-24 07:40:04 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r) // clean up db before each test case.
|
|
|
|
|
|
|
|
err := client.Enqueue(tc.task, tc.opts...)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for qname, want := range tc.wantEnqueued {
|
|
|
|
got := h.GetEnqueuedMessages(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, got, h.IgnoreIDOpt); diff != "" {
|
|
|
|
t.Errorf("%s;\nmismatch found in %q; (-want,+got)\n%s", tc.desc, base.QueueKey(qname), diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientEnqueueIn(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
client := NewClient(RedisClientOpt{
|
|
|
|
Addr: redisAddr,
|
|
|
|
DB: redisDB,
|
|
|
|
})
|
|
|
|
|
|
|
|
task := NewTask("send_email", map[string]interface{}{"to": "customer@gmail.com", "from": "merchant@example.com"})
|
|
|
|
|
2020-03-08 12:24:03 +08:00
|
|
|
var (
|
|
|
|
noTimeout = time.Duration(0).String()
|
|
|
|
noDeadline = time.Time{}.Format(time.RFC3339)
|
|
|
|
)
|
|
|
|
|
2020-02-24 07:40:04 +08:00
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
task *Task
|
|
|
|
delay time.Duration
|
|
|
|
opts []Option
|
|
|
|
wantEnqueued map[string][]*base.TaskMessage
|
|
|
|
wantScheduled []h.ZSetEntry
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "schedule a task to be enqueued in one hour",
|
|
|
|
task: task,
|
|
|
|
delay: time.Hour,
|
|
|
|
opts: []Option{},
|
|
|
|
wantEnqueued: nil, // db is flushed in setup so list does not exist hence nil
|
|
|
|
wantScheduled: []h.ZSetEntry{
|
|
|
|
{
|
|
|
|
Msg: &base.TaskMessage{
|
2020-03-08 12:24:03 +08:00
|
|
|
Type: task.Type,
|
|
|
|
Payload: task.Payload.data,
|
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
Queue: "default",
|
|
|
|
Timeout: noTimeout,
|
|
|
|
Deadline: noDeadline,
|
2020-02-24 07:40:04 +08:00
|
|
|
},
|
|
|
|
Score: float64(time.Now().Add(time.Hour).Unix()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Zero delay",
|
|
|
|
task: task,
|
|
|
|
delay: 0,
|
|
|
|
opts: []Option{},
|
|
|
|
wantEnqueued: map[string][]*base.TaskMessage{
|
|
|
|
"default": []*base.TaskMessage{
|
|
|
|
&base.TaskMessage{
|
2020-03-08 12:24:03 +08:00
|
|
|
Type: task.Type,
|
|
|
|
Payload: task.Payload.data,
|
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
Queue: "default",
|
|
|
|
Timeout: noTimeout,
|
|
|
|
Deadline: noDeadline,
|
2020-02-24 07:40:04 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-12-22 02:02:03 +08:00
|
|
|
wantScheduled: nil, // db is flushed in setup so zset does not exist hence nil
|
|
|
|
},
|
2019-11-30 09:40:31 +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.
|
2019-11-30 09:40:31 +08:00
|
|
|
|
2020-02-24 07:40:04 +08:00
|
|
|
err := client.EnqueueIn(tc.delay, tc.task, tc.opts...)
|
2019-11-30 09:40:31 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-01-06 22:53:40 +08:00
|
|
|
for qname, want := range tc.wantEnqueued {
|
|
|
|
gotEnqueued := h.GetEnqueuedMessages(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotEnqueued, h.IgnoreIDOpt); diff != "" {
|
|
|
|
t.Errorf("%s;\nmismatch found in %q; (-want,+got)\n%s", tc.desc, base.QueueKey(qname), diff)
|
|
|
|
}
|
2019-12-21 23:42:32 +08:00
|
|
|
}
|
|
|
|
|
2019-12-30 01:41:00 +08:00
|
|
|
gotScheduled := h.GetScheduledEntries(t, r)
|
|
|
|
if diff := cmp.Diff(tc.wantScheduled, gotScheduled, h.IgnoreIDOpt); diff != "" {
|
2019-12-22 23:15:45 +08:00
|
|
|
t.Errorf("%s;\nmismatch found in %q; (-want,+got)\n%s", tc.desc, base.ScheduledQueue, diff)
|
2019-11-30 09:40:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|