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 (
|
2021-09-02 20:56:02 +08:00
|
|
|
"context"
|
2020-03-18 21:49:39 +08:00
|
|
|
"errors"
|
2019-11-30 09:40:31 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
2019-12-21 23:42:32 +08:00
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-03-18 21:49:39 +08:00
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2019-12-22 23:15:45 +08:00
|
|
|
"github.com/hibiken/asynq/internal/base"
|
2022-03-19 22:16:55 +08:00
|
|
|
h "github.com/hibiken/asynq/internal/testutil"
|
2023-09-19 16:16:51 +08:00
|
|
|
"github.com/redis/go-redis/v9"
|
2019-11-30 09:40:31 +08:00
|
|
|
)
|
|
|
|
|
2020-09-05 21:29:08 +08:00
|
|
|
func TestClientEnqueueWithProcessAtOption(t *testing.T) {
|
2019-11-30 09:40:31 +08:00
|
|
|
r := setup(t)
|
2020-08-29 21:54:08 +08:00
|
|
|
client := NewClient(getRedisConnOpt(t))
|
2020-09-08 21:51:01 +08:00
|
|
|
defer client.Close()
|
2019-11-30 09:40:31 +08:00
|
|
|
|
2021-03-21 04:42:13 +08:00
|
|
|
task := NewTask("send_email", h.JSON(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)
|
|
|
|
)
|
|
|
|
|
2019-11-30 09:40:31 +08:00
|
|
|
tests := []struct {
|
2019-12-21 23:42:32 +08:00
|
|
|
desc string
|
|
|
|
task *Task
|
2020-09-05 21:29:08 +08:00
|
|
|
processAt time.Time // value for ProcessAt option
|
|
|
|
opts []Option // other options
|
2021-05-15 21:43:18 +08:00
|
|
|
wantInfo *TaskInfo
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending map[string][]*base.TaskMessage
|
2020-08-18 21:01:38 +08:00
|
|
|
wantScheduled map[string][]base.Z
|
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{},
|
2021-05-15 21:43:18 +08:00
|
|
|
wantInfo: &TaskInfo{
|
2021-06-14 22:31:39 +08:00
|
|
|
Queue: "default",
|
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
|
|
|
State: TaskStatePending,
|
|
|
|
MaxRetry: defaultMaxRetry,
|
|
|
|
Retried: 0,
|
|
|
|
LastErr: "",
|
|
|
|
LastFailedAt: time.Time{},
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
Deadline: time.Time{},
|
|
|
|
NextProcessAt: now,
|
2020-07-03 20:49:52 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-03-14 05:18:18 +08:00
|
|
|
"default": {
|
|
|
|
{
|
2021-03-21 04:42:13 +08:00
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
2020-03-08 12:24:03 +08:00
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
Queue: "default",
|
2020-06-22 23:33:58 +08:00
|
|
|
Timeout: int64(defaultTimeout.Seconds()),
|
|
|
|
Deadline: noDeadline.Unix(),
|
2020-01-06 22:53:40 +08:00
|
|
|
},
|
2019-12-21 23:42:32 +08:00
|
|
|
},
|
|
|
|
},
|
2020-08-18 21:01:38 +08:00
|
|
|
wantScheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2019-12-21 23:42:32 +08:00
|
|
|
},
|
|
|
|
{
|
2020-07-03 20:49:52 +08:00
|
|
|
desc: "Schedule task to be processed in the future",
|
|
|
|
task: task,
|
|
|
|
processAt: oneHourLater,
|
|
|
|
opts: []Option{},
|
2021-05-15 21:43:18 +08:00
|
|
|
wantInfo: &TaskInfo{
|
2021-06-14 22:31:39 +08:00
|
|
|
Queue: "default",
|
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
|
|
|
State: TaskStateScheduled,
|
|
|
|
MaxRetry: defaultMaxRetry,
|
|
|
|
Retried: 0,
|
|
|
|
LastErr: "",
|
|
|
|
LastFailedAt: time.Time{},
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
Deadline: time.Time{},
|
|
|
|
NextProcessAt: oneHourLater,
|
2020-07-03 20:49:52 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-18 21:01:38 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
wantScheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{
|
|
|
|
Message: &base.TaskMessage{
|
2021-03-21 04:42:13 +08:00
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
2020-08-18 21:01:38 +08:00
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
Queue: "default",
|
|
|
|
Timeout: int64(defaultTimeout.Seconds()),
|
|
|
|
Deadline: noDeadline.Unix(),
|
|
|
|
},
|
|
|
|
Score: 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.
|
|
|
|
|
2020-09-05 21:29:08 +08:00
|
|
|
opts := append(tc.opts, ProcessAt(tc.processAt))
|
2021-05-15 21:43:18 +08:00
|
|
|
gotInfo, err := client.Enqueue(tc.task, opts...)
|
2020-02-24 07:40:04 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
2020-09-05 21:29:08 +08:00
|
|
|
cmpOptions := []cmp.Option{
|
2021-06-14 22:31:39 +08:00
|
|
|
cmpopts.IgnoreFields(TaskInfo{}, "ID"),
|
2020-09-05 21:29:08 +08:00
|
|
|
cmpopts.EquateApproxTime(500 * time.Millisecond),
|
|
|
|
}
|
2021-05-15 21:43:18 +08:00
|
|
|
if diff := cmp.Diff(tc.wantInfo, gotInfo, cmpOptions...); diff != "" {
|
2020-09-27 08:33:29 +08:00
|
|
|
t.Errorf("%s;\nEnqueue(task, ProcessAt(%v)) returned %v, want %v; (-want,+got)\n%s",
|
2021-05-15 21:43:18 +08:00
|
|
|
tc.desc, tc.processAt, gotInfo, tc.wantInfo, diff)
|
2020-07-03 20:49:52 +08:00
|
|
|
}
|
2020-02-24 07:40:04 +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.IgnoreIDOpt, cmpopts.EquateEmpty()); diff != "" {
|
2021-03-13 08:23:08 +08:00
|
|
|
t.Errorf("%s;\nmismatch found in %q; (-want,+got)\n%s", tc.desc, base.PendingKey(qname), diff)
|
2020-02-24 07:40:04 +08:00
|
|
|
}
|
|
|
|
}
|
2020-08-18 21:01:38 +08:00
|
|
|
for qname, want := range tc.wantScheduled {
|
|
|
|
gotScheduled := h.GetScheduledEntries(t, r, qname)
|
2020-08-19 20:32:05 +08:00
|
|
|
if diff := cmp.Diff(want, gotScheduled, h.IgnoreIDOpt, cmpopts.EquateEmpty()); diff != "" {
|
2020-08-18 21:01:38 +08:00
|
|
|
t.Errorf("%s;\nmismatch found in %q; (-want,+got)\n%s", tc.desc, base.ScheduledKey(qname), diff)
|
|
|
|
}
|
2020-02-24 07:40:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 16:16:51 +08:00
|
|
|
func testClientEnqueue(t *testing.T, client *Client, r redis.UniversalClient) {
|
2021-03-21 04:42:13 +08:00
|
|
|
task := NewTask("send_email", h.JSON(map[string]interface{}{"to": "customer@gmail.com", "from": "merchant@example.com"}))
|
2020-09-05 21:29:08 +08:00
|
|
|
now := time.Now()
|
2020-02-24 07:40:04 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-09-05 22:03:43 +08:00
|
|
|
desc string
|
|
|
|
task *Task
|
|
|
|
opts []Option
|
2021-05-15 21:43:18 +08:00
|
|
|
wantInfo *TaskInfo
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending map[string][]*base.TaskMessage
|
2020-02-24 07:40:04 +08:00
|
|
|
}{
|
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),
|
|
|
|
},
|
2021-05-15 21:43:18 +08:00
|
|
|
wantInfo: &TaskInfo{
|
2021-06-14 22:31:39 +08:00
|
|
|
Queue: "default",
|
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
|
|
|
State: TaskStatePending,
|
|
|
|
MaxRetry: 3,
|
|
|
|
Retried: 0,
|
|
|
|
LastErr: "",
|
|
|
|
LastFailedAt: time.Time{},
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
Deadline: time.Time{},
|
|
|
|
NextProcessAt: now,
|
2020-07-03 20:49:52 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-03-14 05:18:18 +08:00
|
|
|
"default": {
|
|
|
|
{
|
2021-03-21 04:42:13 +08:00
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
2020-03-08 12:24:03 +08:00
|
|
|
Retry: 3,
|
|
|
|
Queue: "default",
|
2020-06-22 23:33:58 +08:00
|
|
|
Timeout: int64(defaultTimeout.Seconds()),
|
|
|
|
Deadline: noDeadline.Unix(),
|
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),
|
|
|
|
},
|
2021-05-15 21:43:18 +08:00
|
|
|
wantInfo: &TaskInfo{
|
2021-06-14 22:31:39 +08:00
|
|
|
Queue: "default",
|
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
|
|
|
State: TaskStatePending,
|
|
|
|
MaxRetry: 0, // Retry count should be set to zero
|
|
|
|
Retried: 0,
|
|
|
|
LastErr: "",
|
|
|
|
LastFailedAt: time.Time{},
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
Deadline: time.Time{},
|
|
|
|
NextProcessAt: now,
|
2020-07-03 20:49:52 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-03-14 05:18:18 +08:00
|
|
|
"default": {
|
|
|
|
{
|
2021-03-21 04:42:13 +08:00
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
2020-03-08 12:24:03 +08:00
|
|
|
Retry: 0, // Retry count should be set to zero
|
|
|
|
Queue: "default",
|
2020-06-22 23:33:58 +08:00
|
|
|
Timeout: int64(defaultTimeout.Seconds()),
|
|
|
|
Deadline: noDeadline.Unix(),
|
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),
|
|
|
|
},
|
2021-05-15 21:43:18 +08:00
|
|
|
wantInfo: &TaskInfo{
|
2021-06-14 22:31:39 +08:00
|
|
|
Queue: "default",
|
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
|
|
|
State: TaskStatePending,
|
|
|
|
MaxRetry: 10, // Last option takes precedence
|
|
|
|
Retried: 0,
|
|
|
|
LastErr: "",
|
|
|
|
LastFailedAt: time.Time{},
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
Deadline: time.Time{},
|
|
|
|
NextProcessAt: now,
|
2020-07-03 20:49:52 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-03-14 05:18:18 +08:00
|
|
|
"default": {
|
|
|
|
{
|
2021-03-21 04:42:13 +08:00
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
2020-03-08 12:24:03 +08:00
|
|
|
Retry: 10, // Last option takes precedence
|
|
|
|
Queue: "default",
|
2020-06-22 23:33:58 +08:00
|
|
|
Timeout: int64(defaultTimeout.Seconds()),
|
|
|
|
Deadline: noDeadline.Unix(),
|
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"),
|
|
|
|
},
|
2021-05-15 21:43:18 +08:00
|
|
|
wantInfo: &TaskInfo{
|
2021-06-14 22:31:39 +08:00
|
|
|
Queue: "custom",
|
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
|
|
|
State: TaskStatePending,
|
|
|
|
MaxRetry: defaultMaxRetry,
|
|
|
|
Retried: 0,
|
|
|
|
LastErr: "",
|
|
|
|
LastFailedAt: time.Time{},
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
Deadline: time.Time{},
|
|
|
|
NextProcessAt: now,
|
2020-07-03 20:49:52 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-03-14 05:18:18 +08:00
|
|
|
"custom": {
|
|
|
|
{
|
2021-03-21 04:42:13 +08:00
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
2020-03-08 12:24:03 +08:00
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
Queue: "custom",
|
2020-06-22 23:33:58 +08:00
|
|
|
Timeout: int64(defaultTimeout.Seconds()),
|
|
|
|
Deadline: noDeadline.Unix(),
|
2020-01-06 22:53:40 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-07-15 21:12:13 +08:00
|
|
|
desc: "Queue option should be case sensitive",
|
2020-02-24 07:40:04 +08:00
|
|
|
task: task,
|
2020-01-06 22:53:40 +08:00
|
|
|
opts: []Option{
|
2021-07-15 21:12:13 +08:00
|
|
|
Queue("MyQueue"),
|
2020-01-06 22:53:40 +08:00
|
|
|
},
|
2021-05-15 21:43:18 +08:00
|
|
|
wantInfo: &TaskInfo{
|
2021-07-15 21:12:13 +08:00
|
|
|
Queue: "MyQueue",
|
2021-06-14 22:31:39 +08:00
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
|
|
|
State: TaskStatePending,
|
|
|
|
MaxRetry: defaultMaxRetry,
|
|
|
|
Retried: 0,
|
|
|
|
LastErr: "",
|
|
|
|
LastFailedAt: time.Time{},
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
Deadline: time.Time{},
|
|
|
|
NextProcessAt: now,
|
2020-07-03 20:49:52 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2021-07-15 21:12:13 +08:00
|
|
|
"MyQueue": {
|
2020-03-14 05:18:18 +08:00
|
|
|
{
|
2021-03-21 04:42:13 +08:00
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
2020-03-08 12:24:03 +08:00
|
|
|
Retry: defaultMaxRetry,
|
2021-07-15 21:12:13 +08:00
|
|
|
Queue: "MyQueue",
|
2020-06-22 23:33:58 +08:00
|
|
|
Timeout: int64(defaultTimeout.Seconds()),
|
|
|
|
Deadline: noDeadline.Unix(),
|
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),
|
|
|
|
},
|
2021-05-15 21:43:18 +08:00
|
|
|
wantInfo: &TaskInfo{
|
2021-06-14 22:31:39 +08:00
|
|
|
Queue: "default",
|
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
|
|
|
State: TaskStatePending,
|
|
|
|
MaxRetry: defaultMaxRetry,
|
|
|
|
Retried: 0,
|
|
|
|
LastErr: "",
|
|
|
|
LastFailedAt: time.Time{},
|
|
|
|
Timeout: 20 * time.Second,
|
|
|
|
Deadline: time.Time{},
|
|
|
|
NextProcessAt: now,
|
2020-07-03 20:49:52 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-03-14 05:18:18 +08:00
|
|
|
"default": {
|
|
|
|
{
|
2021-03-21 04:42:13 +08:00
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
2020-03-08 12:24:03 +08:00
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
Queue: "default",
|
2020-06-17 12:12:50 +08:00
|
|
|
Timeout: 20,
|
2020-06-22 23:33:58 +08:00
|
|
|
Deadline: noDeadline.Unix(),
|
2020-03-08 12:24:03 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "With deadline option",
|
|
|
|
task: task,
|
|
|
|
opts: []Option{
|
|
|
|
Deadline(time.Date(2020, time.June, 24, 0, 0, 0, 0, time.UTC)),
|
|
|
|
},
|
2021-05-15 21:43:18 +08:00
|
|
|
wantInfo: &TaskInfo{
|
2021-06-14 22:31:39 +08:00
|
|
|
Queue: "default",
|
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
|
|
|
State: TaskStatePending,
|
|
|
|
MaxRetry: defaultMaxRetry,
|
|
|
|
Retried: 0,
|
|
|
|
LastErr: "",
|
|
|
|
LastFailedAt: time.Time{},
|
|
|
|
Timeout: noTimeout,
|
|
|
|
Deadline: time.Date(2020, time.June, 24, 0, 0, 0, 0, time.UTC),
|
|
|
|
NextProcessAt: now,
|
2020-07-03 20:49:52 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-03-14 05:18:18 +08:00
|
|
|
"default": {
|
|
|
|
{
|
2021-03-21 04:42:13 +08:00
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
2020-03-08 12:24:03 +08:00
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
Queue: "default",
|
2020-06-22 23:33:58 +08:00
|
|
|
Timeout: int64(noTimeout.Seconds()),
|
|
|
|
Deadline: time.Date(2020, time.June, 24, 0, 0, 0, 0, time.UTC).Unix(),
|
2020-06-17 12:12:50 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "With both deadline and timeout options",
|
|
|
|
task: task,
|
|
|
|
opts: []Option{
|
|
|
|
Timeout(20 * time.Second),
|
|
|
|
Deadline(time.Date(2020, time.June, 24, 0, 0, 0, 0, time.UTC)),
|
|
|
|
},
|
2021-05-15 21:43:18 +08:00
|
|
|
wantInfo: &TaskInfo{
|
2021-06-14 22:31:39 +08:00
|
|
|
Queue: "default",
|
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
|
|
|
State: TaskStatePending,
|
|
|
|
MaxRetry: defaultMaxRetry,
|
|
|
|
Retried: 0,
|
|
|
|
LastErr: "",
|
|
|
|
LastFailedAt: time.Time{},
|
|
|
|
Timeout: 20 * time.Second,
|
|
|
|
Deadline: time.Date(2020, time.June, 24, 0, 0, 0, 0, time.UTC),
|
|
|
|
NextProcessAt: now,
|
2020-07-03 20:49:52 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-06-17 12:12:50 +08:00
|
|
|
"default": {
|
|
|
|
{
|
2021-03-21 04:42:13 +08:00
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
2020-06-17 12:12:50 +08:00
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
Queue: "default",
|
|
|
|
Timeout: 20,
|
2020-06-22 23:33:58 +08:00
|
|
|
Deadline: time.Date(2020, time.June, 24, 0, 0, 0, 0, time.UTC).Unix(),
|
2020-01-06 22:53:40 +08:00
|
|
|
},
|
2019-12-22 02:02:03 +08:00
|
|
|
},
|
|
|
|
},
|
2020-02-24 07:40:04 +08:00
|
|
|
},
|
2021-11-06 07:52:54 +08:00
|
|
|
{
|
|
|
|
desc: "With Retention option",
|
|
|
|
task: task,
|
|
|
|
opts: []Option{
|
|
|
|
Retention(24 * time.Hour),
|
|
|
|
},
|
|
|
|
wantInfo: &TaskInfo{
|
|
|
|
Queue: "default",
|
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
|
|
|
State: TaskStatePending,
|
|
|
|
MaxRetry: defaultMaxRetry,
|
|
|
|
Retried: 0,
|
|
|
|
LastErr: "",
|
|
|
|
LastFailedAt: time.Time{},
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
Deadline: time.Time{},
|
|
|
|
NextProcessAt: now,
|
|
|
|
Retention: 24 * time.Hour,
|
|
|
|
},
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {
|
|
|
|
{
|
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
Queue: "default",
|
|
|
|
Timeout: int64(defaultTimeout.Seconds()),
|
|
|
|
Deadline: noDeadline.Unix(),
|
|
|
|
Retention: int64((24 * time.Hour).Seconds()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-02-24 07:40:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r) // clean up db before each test case.
|
|
|
|
|
2021-05-15 21:43:18 +08:00
|
|
|
gotInfo, err := client.Enqueue(tc.task, tc.opts...)
|
2020-02-24 07:40:04 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
2020-09-05 21:29:08 +08:00
|
|
|
cmpOptions := []cmp.Option{
|
2021-06-14 22:31:39 +08:00
|
|
|
cmpopts.IgnoreFields(TaskInfo{}, "ID"),
|
2020-09-05 21:29:08 +08:00
|
|
|
cmpopts.EquateApproxTime(500 * time.Millisecond),
|
|
|
|
}
|
2021-05-15 21:43:18 +08:00
|
|
|
if diff := cmp.Diff(tc.wantInfo, gotInfo, cmpOptions...); diff != "" {
|
2020-07-03 20:49:52 +08:00
|
|
|
t.Errorf("%s;\nEnqueue(task) returned %v, want %v; (-want,+got)\n%s",
|
2021-05-15 21:43:18 +08:00
|
|
|
tc.desc, gotInfo, tc.wantInfo, diff)
|
2020-07-03 20:49:52 +08:00
|
|
|
}
|
2020-02-24 07:40:04 +08:00
|
|
|
|
2020-09-05 22:03:43 +08:00
|
|
|
for qname, want := range tc.wantPending {
|
|
|
|
got := h.GetPendingMessages(t, r, qname)
|
2020-02-24 07:40:04 +08:00
|
|
|
if diff := cmp.Diff(want, got, h.IgnoreIDOpt); diff != "" {
|
2021-03-13 08:23:08 +08:00
|
|
|
t.Errorf("%s;\nmismatch found in %q; (-want,+got)\n%s", tc.desc, base.PendingKey(qname), diff)
|
2020-02-24 07:40:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 16:16:51 +08:00
|
|
|
func TestClientEnqueue(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
client := NewClient(getRedisConnOpt(t))
|
|
|
|
defer client.Close()
|
|
|
|
testClientEnqueue(t, client, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientFromRedisClientEnqueue(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
redisClient := getRedisConnOpt(t).MakeRedisClient().(redis.UniversalClient)
|
|
|
|
client := NewClientFromRedisClient(redisClient)
|
|
|
|
testClientEnqueue(t, client, r)
|
|
|
|
err := client.Close()
|
|
|
|
if err == nil {
|
|
|
|
t.Error("client.Close() should have failed because of a shared client but it didn't")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-05 22:41:44 +08:00
|
|
|
func TestClientEnqueueWithGroupOption(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
client := NewClient(getRedisConnOpt(t))
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
task := NewTask("mytask", []byte("foo"))
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
task *Task
|
|
|
|
opts []Option
|
|
|
|
wantInfo *TaskInfo
|
|
|
|
wantPending map[string][]*base.TaskMessage
|
|
|
|
wantGroups map[string]map[string][]base.Z // map queue name to a set of groups
|
|
|
|
wantScheduled map[string][]base.Z
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "With only Group option",
|
|
|
|
task: task,
|
|
|
|
opts: []Option{
|
|
|
|
Group("mygroup"),
|
|
|
|
},
|
|
|
|
wantInfo: &TaskInfo{
|
|
|
|
Queue: "default",
|
2022-03-17 11:22:07 +08:00
|
|
|
Group: "mygroup",
|
2022-03-05 22:41:44 +08:00
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
|
|
|
State: TaskStateAggregating,
|
|
|
|
MaxRetry: defaultMaxRetry,
|
|
|
|
Retried: 0,
|
|
|
|
LastErr: "",
|
|
|
|
LastFailedAt: time.Time{},
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
Deadline: time.Time{},
|
|
|
|
NextProcessAt: time.Time{},
|
|
|
|
},
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {}, // should not be pending
|
|
|
|
},
|
|
|
|
wantGroups: map[string]map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
"mygroup": {
|
|
|
|
{
|
|
|
|
Message: &base.TaskMessage{
|
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
Queue: "default",
|
|
|
|
Timeout: int64(defaultTimeout.Seconds()),
|
|
|
|
Deadline: noDeadline.Unix(),
|
2022-03-06 21:53:59 +08:00
|
|
|
GroupKey: "mygroup",
|
2022-03-05 22:41:44 +08:00
|
|
|
},
|
|
|
|
Score: now.Unix(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantScheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-10-19 14:45:18 +08:00
|
|
|
desc: "With Group and ProcessAt options",
|
2022-03-05 22:41:44 +08:00
|
|
|
task: task,
|
|
|
|
opts: []Option{
|
|
|
|
Group("mygroup"),
|
2024-10-19 14:45:18 +08:00
|
|
|
ProcessAt(now.Add(30 * time.Minute)),
|
2022-03-05 22:41:44 +08:00
|
|
|
},
|
|
|
|
wantInfo: &TaskInfo{
|
|
|
|
Queue: "default",
|
2022-03-17 11:22:07 +08:00
|
|
|
Group: "mygroup",
|
2022-03-05 22:41:44 +08:00
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
|
|
|
State: TaskStateScheduled,
|
|
|
|
MaxRetry: defaultMaxRetry,
|
|
|
|
Retried: 0,
|
|
|
|
LastErr: "",
|
|
|
|
LastFailedAt: time.Time{},
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
Deadline: time.Time{},
|
|
|
|
NextProcessAt: now.Add(30 * time.Minute),
|
|
|
|
},
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {}, // should not be pending
|
|
|
|
},
|
|
|
|
wantGroups: map[string]map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
"mygroup": {}, // should not be added to the group yet
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantScheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{
|
|
|
|
Message: &base.TaskMessage{
|
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
Queue: "default",
|
|
|
|
Timeout: int64(defaultTimeout.Seconds()),
|
|
|
|
Deadline: noDeadline.Unix(),
|
2022-03-06 21:53:59 +08:00
|
|
|
GroupKey: "mygroup",
|
2022-03-05 22:41:44 +08:00
|
|
|
},
|
|
|
|
Score: now.Add(30 * time.Minute).Unix(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r) // clean up db before each test case.
|
|
|
|
|
|
|
|
gotInfo, err := client.Enqueue(tc.task, tc.opts...)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cmpOptions := []cmp.Option{
|
|
|
|
cmpopts.IgnoreFields(TaskInfo{}, "ID"),
|
|
|
|
cmpopts.EquateApproxTime(500 * time.Millisecond),
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(tc.wantInfo, gotInfo, cmpOptions...); diff != "" {
|
|
|
|
t.Errorf("%s;\nEnqueue(task) returned %v, want %v; (-want,+got)\n%s",
|
|
|
|
tc.desc, gotInfo, tc.wantInfo, diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
for qname, want := range tc.wantPending {
|
|
|
|
got := h.GetPendingMessages(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, got, h.IgnoreIDOpt, cmpopts.EquateEmpty()); diff != "" {
|
|
|
|
t.Errorf("%s;\nmismatch found in %q; (-want,+got)\n%s", tc.desc, base.PendingKey(qname), diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for qname, groups := range tc.wantGroups {
|
|
|
|
for groupKey, want := range groups {
|
|
|
|
got := h.GetGroupEntries(t, r, qname, groupKey)
|
|
|
|
if diff := cmp.Diff(want, got, h.IgnoreIDOpt, cmpopts.EquateEmpty()); diff != "" {
|
|
|
|
t.Errorf("%s;\nmismatch found in %q; (-want,+got)\n%s", tc.desc, base.GroupKey(qname, groupKey), diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for qname, want := range tc.wantScheduled {
|
|
|
|
gotScheduled := h.GetScheduledEntries(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, gotScheduled, h.IgnoreIDOpt, cmpopts.EquateEmpty()); diff != "" {
|
|
|
|
t.Errorf("%s;\nmismatch found in %q; (-want,+got)\n%s", tc.desc, base.ScheduledKey(qname), diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 12:11:04 +08:00
|
|
|
func TestClientEnqueueWithTaskIDOption(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
client := NewClient(getRedisConnOpt(t))
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
task := NewTask("send_email", nil)
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
task *Task
|
|
|
|
opts []Option
|
|
|
|
wantInfo *TaskInfo
|
|
|
|
wantPending map[string][]*base.TaskMessage
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "With a valid TaskID option",
|
|
|
|
task: task,
|
|
|
|
opts: []Option{
|
|
|
|
TaskID("custom_id"),
|
|
|
|
},
|
|
|
|
wantInfo: &TaskInfo{
|
|
|
|
ID: "custom_id",
|
|
|
|
Queue: "default",
|
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
|
|
|
State: TaskStatePending,
|
|
|
|
MaxRetry: defaultMaxRetry,
|
|
|
|
Retried: 0,
|
|
|
|
LastErr: "",
|
|
|
|
LastFailedAt: time.Time{},
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
Deadline: time.Time{},
|
|
|
|
NextProcessAt: now,
|
|
|
|
},
|
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
|
|
|
"default": {
|
|
|
|
{
|
|
|
|
ID: "custom_id",
|
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
Queue: "default",
|
|
|
|
Timeout: int64(defaultTimeout.Seconds()),
|
|
|
|
Deadline: noDeadline.Unix(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r) // clean up db before each test case.
|
|
|
|
|
|
|
|
gotInfo, err := client.Enqueue(tc.task, tc.opts...)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("got non-nil error %v, want nil", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
cmpOptions := []cmp.Option{
|
|
|
|
cmpopts.EquateApproxTime(500 * time.Millisecond),
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(tc.wantInfo, gotInfo, cmpOptions...); diff != "" {
|
|
|
|
t.Errorf("%s;\nEnqueue(task) returned %v, want %v; (-want,+got)\n%s",
|
|
|
|
tc.desc, gotInfo, tc.wantInfo, diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
for qname, want := range tc.wantPending {
|
|
|
|
got := h.GetPendingMessages(t, r, qname)
|
|
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
|
|
t.Errorf("%s;\nmismatch found in %q; (-want,+got)\n%s", tc.desc, base.PendingKey(qname), diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientEnqueueWithConflictingTaskID(t *testing.T) {
|
|
|
|
setup(t)
|
|
|
|
client := NewClient(getRedisConnOpt(t))
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
const taskID = "custom_id"
|
|
|
|
task := NewTask("foo", nil)
|
|
|
|
|
|
|
|
if _, err := client.Enqueue(task, TaskID(taskID)); err != nil {
|
|
|
|
t.Fatalf("First task: Enqueue failed: %v", err)
|
|
|
|
}
|
|
|
|
_, err := client.Enqueue(task, TaskID(taskID))
|
|
|
|
if !errors.Is(err, ErrTaskIDConflict) {
|
|
|
|
t.Errorf("Second task: Enqueue returned %v, want %v", err, ErrTaskIDConflict)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 21:29:08 +08:00
|
|
|
func TestClientEnqueueWithProcessInOption(t *testing.T) {
|
2020-02-24 07:40:04 +08:00
|
|
|
r := setup(t)
|
2020-08-29 21:54:08 +08:00
|
|
|
client := NewClient(getRedisConnOpt(t))
|
2020-09-08 21:51:01 +08:00
|
|
|
defer client.Close()
|
2020-02-24 07:40:04 +08:00
|
|
|
|
2021-03-21 04:42:13 +08:00
|
|
|
task := NewTask("send_email", h.JSON(map[string]interface{}{"to": "customer@gmail.com", "from": "merchant@example.com"}))
|
2020-09-05 21:29:08 +08:00
|
|
|
now := time.Now()
|
2020-02-24 07:40:04 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
task *Task
|
2020-09-05 21:29:08 +08:00
|
|
|
delay time.Duration // value for ProcessIn option
|
|
|
|
opts []Option // other options
|
2021-05-15 21:43:18 +08:00
|
|
|
wantInfo *TaskInfo
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending map[string][]*base.TaskMessage
|
2020-08-18 21:01:38 +08:00
|
|
|
wantScheduled map[string][]base.Z
|
2020-02-24 07:40:04 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-09-05 22:03:43 +08:00
|
|
|
desc: "schedule a task to be processed in one hour",
|
2020-07-03 20:49:52 +08:00
|
|
|
task: task,
|
2020-09-05 21:29:08 +08:00
|
|
|
delay: 1 * time.Hour,
|
2020-07-03 20:49:52 +08:00
|
|
|
opts: []Option{},
|
2021-05-15 21:43:18 +08:00
|
|
|
wantInfo: &TaskInfo{
|
2021-06-14 22:31:39 +08:00
|
|
|
Queue: "default",
|
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
|
|
|
State: TaskStateScheduled,
|
|
|
|
MaxRetry: defaultMaxRetry,
|
|
|
|
Retried: 0,
|
|
|
|
LastErr: "",
|
|
|
|
LastFailedAt: time.Time{},
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
Deadline: time.Time{},
|
|
|
|
NextProcessAt: time.Now().Add(1 * time.Hour),
|
2020-07-03 20:49:52 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-08-18 21:01:38 +08:00
|
|
|
"default": {},
|
|
|
|
},
|
|
|
|
wantScheduled: map[string][]base.Z{
|
|
|
|
"default": {
|
|
|
|
{
|
|
|
|
Message: &base.TaskMessage{
|
2021-03-21 04:42:13 +08:00
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
2020-08-18 21:01:38 +08:00
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
Queue: "default",
|
|
|
|
Timeout: int64(defaultTimeout.Seconds()),
|
|
|
|
Deadline: noDeadline.Unix(),
|
|
|
|
},
|
|
|
|
Score: time.Now().Add(time.Hour).Unix(),
|
2020-02-24 07:40:04 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Zero delay",
|
|
|
|
task: task,
|
|
|
|
delay: 0,
|
|
|
|
opts: []Option{},
|
2021-05-15 21:43:18 +08:00
|
|
|
wantInfo: &TaskInfo{
|
2021-06-14 22:31:39 +08:00
|
|
|
Queue: "default",
|
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
|
|
|
State: TaskStatePending,
|
|
|
|
MaxRetry: defaultMaxRetry,
|
|
|
|
Retried: 0,
|
|
|
|
LastErr: "",
|
|
|
|
LastFailedAt: time.Time{},
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
Deadline: time.Time{},
|
|
|
|
NextProcessAt: now,
|
2020-07-03 20:49:52 +08:00
|
|
|
},
|
2020-09-05 22:03:43 +08:00
|
|
|
wantPending: map[string][]*base.TaskMessage{
|
2020-03-14 05:18:18 +08:00
|
|
|
"default": {
|
|
|
|
{
|
2021-03-21 04:42:13 +08:00
|
|
|
Type: task.Type(),
|
|
|
|
Payload: task.Payload(),
|
2020-03-08 12:24:03 +08:00
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
Queue: "default",
|
2020-06-22 23:33:58 +08:00
|
|
|
Timeout: int64(defaultTimeout.Seconds()),
|
|
|
|
Deadline: noDeadline.Unix(),
|
2020-02-24 07:40:04 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-08-18 21:01:38 +08:00
|
|
|
wantScheduled: map[string][]base.Z{
|
|
|
|
"default": {},
|
|
|
|
},
|
2019-12-22 02:02:03 +08:00
|
|
|
},
|
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-09-05 21:29:08 +08:00
|
|
|
opts := append(tc.opts, ProcessIn(tc.delay))
|
2021-05-15 21:43:18 +08:00
|
|
|
gotInfo, err := client.Enqueue(tc.task, opts...)
|
2019-11-30 09:40:31 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
2020-09-05 21:29:08 +08:00
|
|
|
cmpOptions := []cmp.Option{
|
2021-06-14 22:31:39 +08:00
|
|
|
cmpopts.IgnoreFields(TaskInfo{}, "ID"),
|
2020-09-05 21:29:08 +08:00
|
|
|
cmpopts.EquateApproxTime(500 * time.Millisecond),
|
|
|
|
}
|
2021-05-15 21:43:18 +08:00
|
|
|
if diff := cmp.Diff(tc.wantInfo, gotInfo, cmpOptions...); diff != "" {
|
2020-09-27 08:33:29 +08:00
|
|
|
t.Errorf("%s;\nEnqueue(task, ProcessIn(%v)) returned %v, want %v; (-want,+got)\n%s",
|
2021-05-15 21:43:18 +08:00
|
|
|
tc.desc, tc.delay, gotInfo, tc.wantInfo, diff)
|
2020-07-03 20:49:52 +08:00
|
|
|
}
|
2019-11-30 09:40:31 +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.IgnoreIDOpt, cmpopts.EquateEmpty()); diff != "" {
|
2021-03-13 08:23:08 +08:00
|
|
|
t.Errorf("%s;\nmismatch found in %q; (-want,+got)\n%s", tc.desc, base.PendingKey(qname), diff)
|
2020-01-06 22:53:40 +08:00
|
|
|
}
|
2019-12-21 23:42:32 +08:00
|
|
|
}
|
2020-08-18 21:01:38 +08:00
|
|
|
for qname, want := range tc.wantScheduled {
|
|
|
|
gotScheduled := h.GetScheduledEntries(t, r, qname)
|
2020-08-19 20:32:05 +08:00
|
|
|
if diff := cmp.Diff(want, gotScheduled, h.IgnoreIDOpt, cmpopts.EquateEmpty()); diff != "" {
|
2020-08-18 21:01:38 +08:00
|
|
|
t.Errorf("%s;\nmismatch found in %q; (-want,+got)\n%s", tc.desc, base.ScheduledKey(qname), diff)
|
|
|
|
}
|
2019-11-30 09:40:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 21:49:39 +08:00
|
|
|
|
2020-08-31 20:39:45 +08:00
|
|
|
func TestClientEnqueueError(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
client := NewClient(getRedisConnOpt(t))
|
2020-09-08 21:51:01 +08:00
|
|
|
defer client.Close()
|
2020-08-31 20:39:45 +08:00
|
|
|
|
2021-03-21 04:42:13 +08:00
|
|
|
task := NewTask("send_email", h.JSON(map[string]interface{}{"to": "customer@gmail.com", "from": "merchant@example.com"}))
|
2020-08-31 20:39:45 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
task *Task
|
|
|
|
opts []Option
|
|
|
|
}{
|
2022-03-11 07:59:58 +08:00
|
|
|
{
|
|
|
|
desc: "With nil task",
|
|
|
|
task: nil,
|
|
|
|
opts: []Option{},
|
|
|
|
},
|
2020-08-31 20:39:45 +08:00
|
|
|
{
|
|
|
|
desc: "With empty queue name",
|
|
|
|
task: task,
|
|
|
|
opts: []Option{
|
|
|
|
Queue(""),
|
|
|
|
},
|
|
|
|
},
|
2021-08-01 22:24:45 +08:00
|
|
|
{
|
|
|
|
desc: "With empty task typename",
|
|
|
|
task: NewTask("", h.JSON(map[string]interface{}{})),
|
|
|
|
opts: []Option{},
|
|
|
|
},
|
2021-08-03 06:11:49 +08:00
|
|
|
{
|
|
|
|
desc: "With blank task typename",
|
|
|
|
task: NewTask(" ", h.JSON(map[string]interface{}{})),
|
|
|
|
opts: []Option{},
|
|
|
|
},
|
2021-09-12 12:11:04 +08:00
|
|
|
{
|
|
|
|
desc: "With empty task ID",
|
|
|
|
task: NewTask("foo", nil),
|
|
|
|
opts: []Option{TaskID("")},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "With blank task ID",
|
|
|
|
task: NewTask("foo", nil),
|
|
|
|
opts: []Option{TaskID(" ")},
|
|
|
|
},
|
2021-11-10 07:53:48 +08:00
|
|
|
{
|
|
|
|
desc: "With unique option less than 1s",
|
|
|
|
task: NewTask("foo", nil),
|
|
|
|
opts: []Option{Unique(300 * time.Millisecond)},
|
|
|
|
},
|
2020-08-31 20:39:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r)
|
|
|
|
|
|
|
|
_, err := client.Enqueue(tc.task, tc.opts...)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("%s; client.Enqueue(task, opts...) did not return non-nil error", tc.desc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 20:56:17 +08:00
|
|
|
func TestClientWithDefaultOptions(t *testing.T) {
|
2020-04-26 22:48:38 +08:00
|
|
|
r := setup(t)
|
|
|
|
|
2020-09-05 21:29:08 +08:00
|
|
|
now := time.Now()
|
|
|
|
|
2020-04-26 22:48:38 +08:00
|
|
|
tests := []struct {
|
|
|
|
desc string
|
2021-09-10 20:56:17 +08:00
|
|
|
defaultOpts []Option // options set at task initialization time
|
2020-04-26 22:48:38 +08:00
|
|
|
opts []Option // options used at enqueue time.
|
2021-09-10 20:56:17 +08:00
|
|
|
tasktype string
|
|
|
|
payload []byte
|
2021-05-15 21:43:18 +08:00
|
|
|
wantInfo *TaskInfo
|
2020-04-26 22:48:38 +08:00
|
|
|
queue string // queue that the message should go into.
|
|
|
|
want *base.TaskMessage
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "With queue routing option",
|
|
|
|
defaultOpts: []Option{Queue("feed")},
|
|
|
|
opts: []Option{},
|
2021-09-10 20:56:17 +08:00
|
|
|
tasktype: "feed:import",
|
|
|
|
payload: nil,
|
2021-05-15 21:43:18 +08:00
|
|
|
wantInfo: &TaskInfo{
|
2021-06-14 22:31:39 +08:00
|
|
|
Queue: "feed",
|
|
|
|
Type: "feed:import",
|
|
|
|
Payload: nil,
|
|
|
|
State: TaskStatePending,
|
|
|
|
MaxRetry: defaultMaxRetry,
|
|
|
|
Retried: 0,
|
|
|
|
LastErr: "",
|
|
|
|
LastFailedAt: time.Time{},
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
Deadline: time.Time{},
|
|
|
|
NextProcessAt: now,
|
2020-07-03 20:49:52 +08:00
|
|
|
},
|
|
|
|
queue: "feed",
|
2020-04-26 22:48:38 +08:00
|
|
|
want: &base.TaskMessage{
|
|
|
|
Type: "feed:import",
|
|
|
|
Payload: nil,
|
|
|
|
Retry: defaultMaxRetry,
|
|
|
|
Queue: "feed",
|
2020-06-22 23:33:58 +08:00
|
|
|
Timeout: int64(defaultTimeout.Seconds()),
|
|
|
|
Deadline: noDeadline.Unix(),
|
2020-04-26 22:48:38 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "With multiple options",
|
|
|
|
defaultOpts: []Option{Queue("feed"), MaxRetry(5)},
|
|
|
|
opts: []Option{},
|
2021-09-10 20:56:17 +08:00
|
|
|
tasktype: "feed:import",
|
|
|
|
payload: nil,
|
2021-05-15 21:43:18 +08:00
|
|
|
wantInfo: &TaskInfo{
|
2021-06-14 22:31:39 +08:00
|
|
|
Queue: "feed",
|
|
|
|
Type: "feed:import",
|
|
|
|
Payload: nil,
|
|
|
|
State: TaskStatePending,
|
|
|
|
MaxRetry: 5,
|
|
|
|
Retried: 0,
|
|
|
|
LastErr: "",
|
|
|
|
LastFailedAt: time.Time{},
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
Deadline: time.Time{},
|
|
|
|
NextProcessAt: now,
|
2020-07-03 20:49:52 +08:00
|
|
|
},
|
|
|
|
queue: "feed",
|
2020-04-26 22:48:38 +08:00
|
|
|
want: &base.TaskMessage{
|
|
|
|
Type: "feed:import",
|
|
|
|
Payload: nil,
|
|
|
|
Retry: 5,
|
|
|
|
Queue: "feed",
|
2020-06-22 23:33:58 +08:00
|
|
|
Timeout: int64(defaultTimeout.Seconds()),
|
|
|
|
Deadline: noDeadline.Unix(),
|
2020-04-26 22:48:38 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "With overriding options at enqueue time",
|
|
|
|
defaultOpts: []Option{Queue("feed"), MaxRetry(5)},
|
|
|
|
opts: []Option{Queue("critical")},
|
2021-09-10 20:56:17 +08:00
|
|
|
tasktype: "feed:import",
|
|
|
|
payload: nil,
|
2021-05-15 21:43:18 +08:00
|
|
|
wantInfo: &TaskInfo{
|
2021-06-14 22:31:39 +08:00
|
|
|
Queue: "critical",
|
|
|
|
Type: "feed:import",
|
|
|
|
Payload: nil,
|
|
|
|
State: TaskStatePending,
|
|
|
|
MaxRetry: 5,
|
|
|
|
LastErr: "",
|
|
|
|
LastFailedAt: time.Time{},
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
Deadline: time.Time{},
|
|
|
|
NextProcessAt: now,
|
2020-07-03 20:49:52 +08:00
|
|
|
},
|
|
|
|
queue: "critical",
|
2020-04-26 22:48:38 +08:00
|
|
|
want: &base.TaskMessage{
|
|
|
|
Type: "feed:import",
|
|
|
|
Payload: nil,
|
|
|
|
Retry: 5,
|
|
|
|
Queue: "critical",
|
2020-06-22 23:33:58 +08:00
|
|
|
Timeout: int64(defaultTimeout.Seconds()),
|
|
|
|
Deadline: noDeadline.Unix(),
|
2020-04-26 22:48:38 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r)
|
2020-08-29 21:54:08 +08:00
|
|
|
c := NewClient(getRedisConnOpt(t))
|
2020-09-08 21:51:01 +08:00
|
|
|
defer c.Close()
|
2021-09-10 20:56:17 +08:00
|
|
|
task := NewTask(tc.tasktype, tc.payload, tc.defaultOpts...)
|
|
|
|
gotInfo, err := c.Enqueue(task, tc.opts...)
|
2020-04-26 22:48:38 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-09-05 21:29:08 +08:00
|
|
|
cmpOptions := []cmp.Option{
|
2021-06-14 22:31:39 +08:00
|
|
|
cmpopts.IgnoreFields(TaskInfo{}, "ID"),
|
2020-09-05 21:29:08 +08:00
|
|
|
cmpopts.EquateApproxTime(500 * time.Millisecond),
|
|
|
|
}
|
2021-05-15 21:43:18 +08:00
|
|
|
if diff := cmp.Diff(tc.wantInfo, gotInfo, cmpOptions...); diff != "" {
|
2020-07-03 20:49:52 +08:00
|
|
|
t.Errorf("%s;\nEnqueue(task, opts...) returned %v, want %v; (-want,+got)\n%s",
|
2021-05-15 21:43:18 +08:00
|
|
|
tc.desc, gotInfo, tc.wantInfo, diff)
|
2020-07-03 20:49:52 +08:00
|
|
|
}
|
2020-09-05 22:03:43 +08:00
|
|
|
pending := h.GetPendingMessages(t, r, tc.queue)
|
|
|
|
if len(pending) != 1 {
|
2020-04-26 22:48:38 +08:00
|
|
|
t.Errorf("%s;\nexpected queue %q to have one message; got %d messages in the queue.",
|
2020-09-05 22:03:43 +08:00
|
|
|
tc.desc, tc.queue, len(pending))
|
2020-04-26 22:48:38 +08:00
|
|
|
continue
|
|
|
|
}
|
2020-09-05 22:03:43 +08:00
|
|
|
got := pending[0]
|
2020-04-26 22:48:38 +08:00
|
|
|
if diff := cmp.Diff(tc.want, got, h.IgnoreIDOpt); diff != "" {
|
2020-09-05 22:03:43 +08:00
|
|
|
t.Errorf("%s;\nmismatch found in pending task message; (-want,+got)\n%s",
|
2020-04-26 22:48:38 +08:00
|
|
|
tc.desc, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-29 21:54:08 +08:00
|
|
|
func TestClientEnqueueUnique(t *testing.T) {
|
2020-03-18 21:49:39 +08:00
|
|
|
r := setup(t)
|
2020-08-29 21:54:08 +08:00
|
|
|
c := NewClient(getRedisConnOpt(t))
|
2020-09-08 21:51:01 +08:00
|
|
|
defer c.Close()
|
2020-03-18 21:49:39 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
task *Task
|
|
|
|
ttl time.Duration
|
|
|
|
}{
|
|
|
|
{
|
2021-03-21 04:42:13 +08:00
|
|
|
NewTask("email", h.JSON(map[string]interface{}{"user_id": 123})),
|
2020-03-18 21:49:39 +08:00
|
|
|
time.Hour,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r) // clean up db before each test case.
|
|
|
|
|
|
|
|
// Enqueue the task first. It should succeed.
|
2020-07-03 20:49:52 +08:00
|
|
|
_, err := c.Enqueue(tc.task, Unique(tc.ttl))
|
2020-03-18 21:49:39 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-09-02 20:56:02 +08:00
|
|
|
gotTTL := r.TTL(context.Background(), base.UniqueKey(base.DefaultQueueName, tc.task.Type(), tc.task.Payload())).Val()
|
2020-03-18 21:49:39 +08:00
|
|
|
if !cmp.Equal(tc.ttl.Seconds(), gotTTL.Seconds(), cmpopts.EquateApprox(0, 1)) {
|
|
|
|
t.Errorf("TTL = %v, want %v", gotTTL, tc.ttl)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enqueue the task again. It should fail.
|
2020-07-03 20:49:52 +08:00
|
|
|
_, err = c.Enqueue(tc.task, Unique(tc.ttl))
|
2020-03-18 21:49:39 +08:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Enqueueing %+v did not return an error", tc.task)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !errors.Is(err, ErrDuplicateTask) {
|
|
|
|
t.Errorf("Enqueueing %+v returned an error that is not ErrDuplicateTask", tc.task)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 21:29:08 +08:00
|
|
|
func TestClientEnqueueUniqueWithProcessInOption(t *testing.T) {
|
2020-03-18 21:49:39 +08:00
|
|
|
r := setup(t)
|
2020-08-29 21:54:08 +08:00
|
|
|
c := NewClient(getRedisConnOpt(t))
|
2020-09-08 21:51:01 +08:00
|
|
|
defer c.Close()
|
2020-03-18 21:49:39 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
task *Task
|
|
|
|
d time.Duration
|
|
|
|
ttl time.Duration
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
NewTask("reindex", nil),
|
|
|
|
time.Hour,
|
|
|
|
10 * time.Minute,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r) // clean up db before each test case.
|
|
|
|
|
|
|
|
// Enqueue the task first. It should succeed.
|
2020-09-05 21:29:08 +08:00
|
|
|
_, err := c.Enqueue(tc.task, ProcessIn(tc.d), Unique(tc.ttl))
|
2020-03-18 21:49:39 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-09-02 20:56:02 +08:00
|
|
|
gotTTL := r.TTL(context.Background(), base.UniqueKey(base.DefaultQueueName, tc.task.Type(), tc.task.Payload())).Val()
|
2020-03-18 21:49:39 +08:00
|
|
|
wantTTL := time.Duration(tc.ttl.Seconds()+tc.d.Seconds()) * time.Second
|
|
|
|
if !cmp.Equal(wantTTL.Seconds(), gotTTL.Seconds(), cmpopts.EquateApprox(0, 1)) {
|
|
|
|
t.Errorf("TTL = %v, want %v", gotTTL, wantTTL)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enqueue the task again. It should fail.
|
2020-09-05 21:29:08 +08:00
|
|
|
_, err = c.Enqueue(tc.task, ProcessIn(tc.d), Unique(tc.ttl))
|
2020-03-18 21:49:39 +08:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Enqueueing %+v did not return an error", tc.task)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !errors.Is(err, ErrDuplicateTask) {
|
|
|
|
t.Errorf("Enqueueing %+v returned an error that is not ErrDuplicateTask", tc.task)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 21:29:08 +08:00
|
|
|
func TestClientEnqueueUniqueWithProcessAtOption(t *testing.T) {
|
2020-03-18 21:49:39 +08:00
|
|
|
r := setup(t)
|
2020-08-29 21:54:08 +08:00
|
|
|
c := NewClient(getRedisConnOpt(t))
|
2020-09-08 21:51:01 +08:00
|
|
|
defer c.Close()
|
2020-03-18 21:49:39 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
task *Task
|
|
|
|
at time.Time
|
|
|
|
ttl time.Duration
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
NewTask("reindex", nil),
|
|
|
|
time.Now().Add(time.Hour),
|
|
|
|
10 * time.Minute,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r) // clean up db before each test case.
|
|
|
|
|
|
|
|
// Enqueue the task first. It should succeed.
|
2020-09-05 21:29:08 +08:00
|
|
|
_, err := c.Enqueue(tc.task, ProcessAt(tc.at), Unique(tc.ttl))
|
2020-03-18 21:49:39 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-09-02 20:56:02 +08:00
|
|
|
gotTTL := r.TTL(context.Background(), base.UniqueKey(base.DefaultQueueName, tc.task.Type(), tc.task.Payload())).Val()
|
2020-03-18 21:49:39 +08:00
|
|
|
wantTTL := tc.at.Add(tc.ttl).Sub(time.Now())
|
|
|
|
if !cmp.Equal(wantTTL.Seconds(), gotTTL.Seconds(), cmpopts.EquateApprox(0, 1)) {
|
|
|
|
t.Errorf("TTL = %v, want %v", gotTTL, wantTTL)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enqueue the task again. It should fail.
|
2020-09-05 21:29:08 +08:00
|
|
|
_, err = c.Enqueue(tc.task, ProcessAt(tc.at), Unique(tc.ttl))
|
2020-03-18 21:49:39 +08:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Enqueueing %+v did not return an error", tc.task)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !errors.Is(err, ErrDuplicateTask) {
|
|
|
|
t.Errorf("Enqueueing %+v returned an error that is not ErrDuplicateTask", tc.task)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|