2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-11-14 11:31:18 +08:00
asynq/client_test.go

146 lines
3.7 KiB
Go
Raw Normal View History

2019-11-30 09:40:31 +08:00
package asynq
import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
2019-12-22 23:15:45 +08:00
"github.com/hibiken/asynq/internal/base"
"github.com/hibiken/asynq/internal/rdb"
2019-11-30 09:40:31 +08:00
)
func TestClient(t *testing.T) {
r := setup(t)
2019-12-04 13:01:26 +08:00
client := &Client{rdb.NewRDB(r)}
2019-11-30 09:40:31 +08:00
task := &Task{Type: "send_email", Payload: map[string]interface{}{"to": "customer@gmail.com", "from": "merchant@example.com"}}
2019-11-30 09:40:31 +08:00
tests := []struct {
desc string
task *Task
processAt time.Time
opts []Option
2019-12-22 23:15:45 +08:00
wantEnqueued []*base.TaskMessage
wantScheduled []sortedSetEntry
2019-11-30 09:40:31 +08:00
}{
{
desc: "Process task immediately",
task: task,
processAt: time.Now(),
opts: []Option{},
2019-12-22 23:15:45 +08:00
wantEnqueued: []*base.TaskMessage{
&base.TaskMessage{
Type: task.Type,
Payload: task.Payload,
Retry: defaultMaxRetry,
Queue: "default",
},
},
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,
processAt: time.Now().Add(2 * time.Hour),
opts: []Option{},
wantEnqueued: nil, // db is flushed in setup so list does not exist hence nil
wantScheduled: []sortedSetEntry{
{
2019-12-22 23:15:45 +08:00
msg: &base.TaskMessage{
Type: task.Type,
Payload: task.Payload,
Retry: defaultMaxRetry,
Queue: "default",
},
score: time.Now().Add(2 * time.Hour).Unix(),
},
},
},
{
desc: "Process task immediately with a custom retry count",
task: task,
processAt: time.Now(),
opts: []Option{
MaxRetry(3),
},
2019-12-22 23:15:45 +08:00
wantEnqueued: []*base.TaskMessage{
&base.TaskMessage{
Type: task.Type,
Payload: task.Payload,
Retry: 3,
Queue: "default",
},
},
wantScheduled: nil, // db is flushed in setup so zset does not exist hence nil
2019-11-30 09:40:31 +08:00
},
{
desc: "Negative retry count",
task: task,
processAt: time.Now(),
opts: []Option{
MaxRetry(-2),
},
2019-12-22 23:15:45 +08:00
wantEnqueued: []*base.TaskMessage{
&base.TaskMessage{
Type: task.Type,
Payload: task.Payload,
Retry: 0, // Retry count should be set to zero
Queue: "default",
},
},
wantScheduled: nil, // db is flushed in setup so zset does not exist hence nil
2019-11-30 09:40:31 +08:00
},
2019-12-22 02:02:03 +08:00
{
desc: "Conflicting options",
task: task,
processAt: time.Now(),
opts: []Option{
MaxRetry(2),
MaxRetry(10),
},
2019-12-22 23:15:45 +08:00
wantEnqueued: []*base.TaskMessage{
&base.TaskMessage{
2019-12-22 02:02:03 +08:00
Type: task.Type,
Payload: task.Payload,
Retry: 10, // Last option takes precedence
Queue: "default",
},
},
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 {
// clean up db before each test case.
2019-12-04 13:01:26 +08:00
if err := r.FlushDB().Err(); err != nil {
2019-11-30 09:40:31 +08:00
t.Fatal(err)
}
err := client.Process(tc.task, tc.processAt, tc.opts...)
2019-11-30 09:40:31 +08:00
if err != nil {
t.Error(err)
continue
}
2019-12-22 23:15:45 +08:00
gotEnqueuedRaw := r.LRange(base.DefaultQueue, 0, -1).Val()
gotEnqueued := mustUnmarshalSlice(t, gotEnqueuedRaw)
if diff := cmp.Diff(tc.wantEnqueued, gotEnqueued, ignoreIDOpt); diff != "" {
2019-12-22 23:15:45 +08:00
t.Errorf("%s;\nmismatch found in %q; (-want,+got)\n%s", tc.desc, base.DefaultQueue, diff)
}
2019-12-22 23:15:45 +08:00
gotScheduledRaw := r.ZRangeWithScores(base.ScheduledQueue, 0, -1).Val()
var gotScheduled []sortedSetEntry
for _, z := range gotScheduledRaw {
gotScheduled = append(gotScheduled, sortedSetEntry{
msg: mustUnmarshal(t, z.Member.(string)),
score: int64(z.Score),
})
2019-11-30 09:40:31 +08:00
}
cmpOpt := cmp.AllowUnexported(sortedSetEntry{})
if diff := cmp.Diff(tc.wantScheduled, gotScheduled, cmpOpt, 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
}
}
}