2020-05-28 21:28:14 +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.
|
|
|
|
|
|
|
|
package asynq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-07-02 21:21:20 +08:00
|
|
|
"github.com/google/uuid"
|
2020-05-28 21:28:14 +08:00
|
|
|
"github.com/hibiken/asynq/internal/base"
|
|
|
|
)
|
|
|
|
|
2020-06-19 20:34:36 +08:00
|
|
|
func TestCreateContextWithFutureDeadline(t *testing.T) {
|
2020-05-28 21:28:14 +08:00
|
|
|
tests := []struct {
|
2020-06-19 20:34:36 +08:00
|
|
|
deadline time.Time
|
2020-05-28 21:28:14 +08:00
|
|
|
}{
|
2020-06-19 20:34:36 +08:00
|
|
|
{time.Now().Add(time.Hour)},
|
2020-05-28 21:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
msg := &base.TaskMessage{
|
2020-06-19 20:34:36 +08:00
|
|
|
Type: "something",
|
2020-07-02 21:21:20 +08:00
|
|
|
ID: uuid.New(),
|
2020-06-19 20:34:36 +08:00
|
|
|
Payload: nil,
|
2020-05-28 21:28:14 +08:00
|
|
|
}
|
|
|
|
|
2020-06-19 20:34:36 +08:00
|
|
|
ctx, cancel := createContext(msg, tc.deadline)
|
2020-05-28 21:28:14 +08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case x := <-ctx.Done():
|
2020-06-19 20:34:36 +08:00
|
|
|
t.Errorf("<-ctx.Done() == %v, want nothing (it should block)", x)
|
2020-05-28 21:28:14 +08:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
got, ok := ctx.Deadline()
|
|
|
|
if !ok {
|
2020-06-19 20:34:36 +08:00
|
|
|
t.Errorf("ctx.Deadline() returned false, want deadline to be set")
|
2020-05-28 21:28:14 +08:00
|
|
|
}
|
2020-06-19 20:34:36 +08:00
|
|
|
if !cmp.Equal(tc.deadline, got) {
|
|
|
|
t.Errorf("ctx.Deadline() returned %v, want %v", got, tc.deadline)
|
2020-05-28 21:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
default:
|
|
|
|
t.Errorf("ctx.Done() blocked, want it to be non-blocking")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 20:34:36 +08:00
|
|
|
func TestCreateContextWithPastDeadline(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
deadline time.Time
|
|
|
|
}{
|
|
|
|
{time.Now().Add(-2 * time.Hour)},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
msg := &base.TaskMessage{
|
|
|
|
Type: "something",
|
2020-07-02 21:21:20 +08:00
|
|
|
ID: uuid.New(),
|
2020-06-19 20:34:36 +08:00
|
|
|
Payload: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := createContext(msg, tc.deadline)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
default:
|
|
|
|
t.Errorf("ctx.Done() blocked, want it to be non-blocking")
|
|
|
|
}
|
|
|
|
|
|
|
|
got, ok := ctx.Deadline()
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("ctx.Deadline() returned false, want deadline to be set")
|
|
|
|
}
|
|
|
|
if !cmp.Equal(tc.deadline, got) {
|
|
|
|
t.Errorf("ctx.Deadline() returned %v, want %v", got, tc.deadline)
|
2020-06-17 12:12:50 +08:00
|
|
|
}
|
2020-05-28 21:28:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetTaskMetadataFromContext(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
msg *base.TaskMessage
|
|
|
|
}{
|
2020-09-08 01:54:42 +08:00
|
|
|
{"with zero retried message", &base.TaskMessage{Type: "something", ID: uuid.New(), Retry: 25, Retried: 0, Timeout: 1800, Queue: "default"}},
|
|
|
|
{"with non-zero retried message", &base.TaskMessage{Type: "something", ID: uuid.New(), Retry: 10, Retried: 5, Timeout: 1800, Queue: "default"}},
|
|
|
|
{"with custom queue name", &base.TaskMessage{Type: "something", ID: uuid.New(), Retry: 25, Retried: 0, Timeout: 1800, Queue: "custom"}},
|
2020-05-28 21:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2020-06-19 20:34:36 +08:00
|
|
|
ctx, cancel := createContext(tc.msg, time.Now().Add(30*time.Minute))
|
|
|
|
defer cancel()
|
2020-05-28 21:28:14 +08:00
|
|
|
|
|
|
|
id, ok := GetTaskID(ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("%s: GetTaskID(ctx) returned ok == false", tc.desc)
|
|
|
|
}
|
|
|
|
if ok && id != tc.msg.ID.String() {
|
|
|
|
t.Errorf("%s: GetTaskID(ctx) returned id == %q, want %q", tc.desc, id, tc.msg.ID.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
retried, ok := GetRetryCount(ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("%s: GetRetryCount(ctx) returned ok == false", tc.desc)
|
|
|
|
}
|
|
|
|
if ok && retried != tc.msg.Retried {
|
|
|
|
t.Errorf("%s: GetRetryCount(ctx) returned n == %d want %d", tc.desc, retried, tc.msg.Retried)
|
|
|
|
}
|
|
|
|
|
|
|
|
maxRetry, ok := GetMaxRetry(ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("%s: GetMaxRetry(ctx) returned ok == false", tc.desc)
|
|
|
|
}
|
|
|
|
if ok && maxRetry != tc.msg.Retry {
|
|
|
|
t.Errorf("%s: GetMaxRetry(ctx) returned n == %d want %d", tc.desc, maxRetry, tc.msg.Retry)
|
|
|
|
}
|
2020-09-08 01:54:42 +08:00
|
|
|
|
|
|
|
qname, ok := GetQueueName(ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("%s: GetQueueName(ctx) returned ok == false", tc.desc)
|
|
|
|
}
|
|
|
|
if ok && qname != tc.msg.Queue {
|
|
|
|
t.Errorf("%s: GetQueueName(ctx) returned qname == %q, want %q", tc.desc, qname, tc.msg.Queue)
|
|
|
|
}
|
2020-05-28 21:28:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetTaskMetadataFromContextError(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
ctx context.Context
|
|
|
|
}{
|
|
|
|
{"with background context", context.Background()},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
if _, ok := GetTaskID(tc.ctx); ok {
|
|
|
|
t.Errorf("%s: GetTaskID(ctx) returned ok == true", tc.desc)
|
|
|
|
}
|
|
|
|
if _, ok := GetRetryCount(tc.ctx); ok {
|
|
|
|
t.Errorf("%s: GetRetryCount(ctx) returned ok == true", tc.desc)
|
|
|
|
}
|
|
|
|
if _, ok := GetMaxRetry(tc.ctx); ok {
|
|
|
|
t.Errorf("%s: GetMaxRetry(ctx) returned ok == true", tc.desc)
|
|
|
|
}
|
2020-09-08 01:54:42 +08:00
|
|
|
if _, ok := GetQueueName(tc.ctx); ok {
|
|
|
|
t.Errorf("%s: GetQueueName(ctx) returned ok == true", tc.desc)
|
|
|
|
}
|
2020-05-28 21:28:14 +08:00
|
|
|
}
|
|
|
|
}
|