2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-26 07:42:17 +08:00

Rename randomTask test helper to newTaskMessage

This commit is contained in:
Ken Hibino 2019-12-12 19:45:03 -08:00
parent d2eeaf6ab1
commit 9410a65338
2 changed files with 60 additions and 61 deletions

View File

@ -50,10 +50,10 @@ func (a timeApproximator) compare(x, y time.Time) bool {
func TestCurrentStats(t *testing.T) { func TestCurrentStats(t *testing.T) {
r := setup(t) r := setup(t)
m1 := randomTask("send_email", "default", map[string]interface{}{"subject": "hello"}) m1 := newTaskMessage("send_email", map[string]interface{}{"subject": "hello"})
m2 := randomTask("reindex", "default", nil) m2 := newTaskMessage("reindex", nil)
m3 := randomTask("gen_thumbnail", "default", map[string]interface{}{"src": "some/path/to/img"}) m3 := newTaskMessage("gen_thumbnail", map[string]interface{}{"src": "some/path/to/img"})
m4 := randomTask("sync", "default", nil) m4 := newTaskMessage("sync", nil)
tests := []struct { tests := []struct {
enqueued []*TaskMessage enqueued []*TaskMessage
@ -144,8 +144,8 @@ func TestCurrentStats(t *testing.T) {
func TestListEnqueued(t *testing.T) { func TestListEnqueued(t *testing.T) {
r := setup(t) r := setup(t)
m1 := randomTask("send_email", "default", map[string]interface{}{"subject": "hello"}) m1 := newTaskMessage("send_email", map[string]interface{}{"subject": "hello"})
m2 := randomTask("reindex", "default", nil) m2 := newTaskMessage("reindex", nil)
t1 := &EnqueuedTask{ID: m1.ID, Type: m1.Type, Payload: m1.Payload} t1 := &EnqueuedTask{ID: m1.ID, Type: m1.Type, Payload: m1.Payload}
t2 := &EnqueuedTask{ID: m2.ID, Type: m2.Type, Payload: m2.Payload} t2 := &EnqueuedTask{ID: m2.ID, Type: m2.Type, Payload: m2.Payload}
tests := []struct { tests := []struct {
@ -195,8 +195,8 @@ func TestListEnqueued(t *testing.T) {
func TestListInProgress(t *testing.T) { func TestListInProgress(t *testing.T) {
r := setup(t) r := setup(t)
m1 := randomTask("send_email", "default", map[string]interface{}{"subject": "hello"}) m1 := newTaskMessage("send_email", map[string]interface{}{"subject": "hello"})
m2 := randomTask("reindex", "default", nil) m2 := newTaskMessage("reindex", nil)
t1 := &InProgressTask{ID: m1.ID, Type: m1.Type, Payload: m1.Payload} t1 := &InProgressTask{ID: m1.ID, Type: m1.Type, Payload: m1.Payload}
t2 := &InProgressTask{ID: m2.ID, Type: m2.Type, Payload: m2.Payload} t2 := &InProgressTask{ID: m2.ID, Type: m2.Type, Payload: m2.Payload}
tests := []struct { tests := []struct {
@ -245,8 +245,8 @@ func TestListInProgress(t *testing.T) {
func TestListScheduled(t *testing.T) { func TestListScheduled(t *testing.T) {
r := setup(t) r := setup(t)
m1 := randomTask("send_email", "default", map[string]interface{}{"subject": "hello"}) m1 := newTaskMessage("send_email", map[string]interface{}{"subject": "hello"})
m2 := randomTask("reindex", "default", nil) m2 := newTaskMessage("reindex", nil)
p1 := time.Now().Add(30 * time.Minute) p1 := time.Now().Add(30 * time.Minute)
p2 := time.Now().Add(24 * time.Hour) p2 := time.Now().Add(24 * time.Hour)
t1 := &ScheduledTask{ID: m1.ID, Type: m1.Type, Payload: m1.Payload, ProcessAt: p1, Score: p1.Unix()} t1 := &ScheduledTask{ID: m1.ID, Type: m1.Type, Payload: m1.Payload, ProcessAt: p1, Score: p1.Unix()}
@ -474,8 +474,8 @@ var timeCmpOpt = EquateApproxTime(time.Second)
func TestEnqueueDeadTask(t *testing.T) { func TestEnqueueDeadTask(t *testing.T) {
r := setup(t) r := setup(t)
t1 := randomTask("send_email", "default", nil) t1 := newTaskMessage("send_email", nil)
t2 := randomTask("gen_thumbnail", "default", nil) t2 := newTaskMessage("gen_thumbnail", nil)
s1 := time.Now().Add(-5 * time.Minute).Unix() s1 := time.Now().Add(-5 * time.Minute).Unix()
s2 := time.Now().Add(-time.Hour).Unix() s2 := time.Now().Add(-time.Hour).Unix()
type deadEntry struct { type deadEntry struct {
@ -550,8 +550,8 @@ func TestEnqueueDeadTask(t *testing.T) {
func TestEnqueueRetryTask(t *testing.T) { func TestEnqueueRetryTask(t *testing.T) {
r := setup(t) r := setup(t)
t1 := randomTask("send_email", "default", nil) t1 := newTaskMessage("send_email", nil)
t2 := randomTask("gen_thumbnail", "default", nil) t2 := newTaskMessage("gen_thumbnail", nil)
s1 := time.Now().Add(-5 * time.Minute).Unix() s1 := time.Now().Add(-5 * time.Minute).Unix()
s2 := time.Now().Add(-time.Hour).Unix() s2 := time.Now().Add(-time.Hour).Unix()
type retryEntry struct { type retryEntry struct {
@ -626,8 +626,8 @@ func TestEnqueueRetryTask(t *testing.T) {
func TestEnqueueScheduledTask(t *testing.T) { func TestEnqueueScheduledTask(t *testing.T) {
r := setup(t) r := setup(t)
t1 := randomTask("send_email", "default", nil) t1 := newTaskMessage("send_email", nil)
t2 := randomTask("gen_thumbnail", "default", nil) t2 := newTaskMessage("gen_thumbnail", nil)
s1 := time.Now().Add(-5 * time.Minute).Unix() s1 := time.Now().Add(-5 * time.Minute).Unix()
s2 := time.Now().Add(-time.Hour).Unix() s2 := time.Now().Add(-time.Hour).Unix()
type scheduledEntry struct { type scheduledEntry struct {
@ -701,9 +701,9 @@ func TestEnqueueScheduledTask(t *testing.T) {
func TestEnqueueAllScheduledTasks(t *testing.T) { func TestEnqueueAllScheduledTasks(t *testing.T) {
r := setup(t) r := setup(t)
t1 := randomTask("send_email", "default", nil) t1 := newTaskMessage("send_email", nil)
t2 := randomTask("gen_thumbnail", "default", nil) t2 := newTaskMessage("gen_thumbnail", nil)
t3 := randomTask("reindex", "default", nil) t3 := newTaskMessage("reindex", nil)
tests := []struct { tests := []struct {
description string description string
@ -762,9 +762,9 @@ func TestEnqueueAllScheduledTasks(t *testing.T) {
func TestEnqueueAllRetryTasks(t *testing.T) { func TestEnqueueAllRetryTasks(t *testing.T) {
r := setup(t) r := setup(t)
t1 := randomTask("send_email", "default", nil) t1 := newTaskMessage("send_email", nil)
t2 := randomTask("gen_thumbnail", "default", nil) t2 := newTaskMessage("gen_thumbnail", nil)
t3 := randomTask("reindex", "default", nil) t3 := newTaskMessage("reindex", nil)
tests := []struct { tests := []struct {
description string description string
@ -823,9 +823,9 @@ func TestEnqueueAllRetryTasks(t *testing.T) {
func TestEnqueueAllDeadTasks(t *testing.T) { func TestEnqueueAllDeadTasks(t *testing.T) {
r := setup(t) r := setup(t)
t1 := randomTask("send_email", "default", nil) t1 := newTaskMessage("send_email", nil)
t2 := randomTask("gen_thumbnail", "default", nil) t2 := newTaskMessage("gen_thumbnail", nil)
t3 := randomTask("reindex", "default", nil) t3 := newTaskMessage("reindex", nil)
tests := []struct { tests := []struct {
description string description string
@ -884,8 +884,8 @@ func TestEnqueueAllDeadTasks(t *testing.T) {
func TestDeleteDeadTask(t *testing.T) { func TestDeleteDeadTask(t *testing.T) {
r := setup(t) r := setup(t)
m1 := randomTask("send_email", "default", nil) m1 := newTaskMessage("send_email", nil)
m2 := randomTask("reindex", "default", nil) m2 := newTaskMessage("reindex", nil)
t1 := time.Now().Add(-5 * time.Minute) t1 := time.Now().Add(-5 * time.Minute)
t2 := time.Now().Add(-time.Hour) t2 := time.Now().Add(-time.Hour)
@ -961,8 +961,8 @@ func TestDeleteDeadTask(t *testing.T) {
func TestDeleteRetryTask(t *testing.T) { func TestDeleteRetryTask(t *testing.T) {
r := setup(t) r := setup(t)
m1 := randomTask("send_email", "default", nil) m1 := newTaskMessage("send_email", nil)
m2 := randomTask("reindex", "default", nil) m2 := newTaskMessage("reindex", nil)
t1 := time.Now().Add(5 * time.Minute) t1 := time.Now().Add(5 * time.Minute)
t2 := time.Now().Add(time.Hour) t2 := time.Now().Add(time.Hour)
@ -1030,8 +1030,8 @@ func TestDeleteRetryTask(t *testing.T) {
func TestDeleteScheduledTask(t *testing.T) { func TestDeleteScheduledTask(t *testing.T) {
r := setup(t) r := setup(t)
m1 := randomTask("send_email", "default", nil) m1 := newTaskMessage("send_email", nil)
m2 := randomTask("reindex", "default", nil) m2 := newTaskMessage("reindex", nil)
t1 := time.Now().Add(5 * time.Minute) t1 := time.Now().Add(5 * time.Minute)
t2 := time.Now().Add(time.Hour) t2 := time.Now().Add(time.Hour)
@ -1099,9 +1099,9 @@ func TestDeleteScheduledTask(t *testing.T) {
func TestDeleteAllDeadTasks(t *testing.T) { func TestDeleteAllDeadTasks(t *testing.T) {
r := setup(t) r := setup(t)
m1 := randomTask("send_email", "default", nil) m1 := newTaskMessage("send_email", nil)
m2 := randomTask("reindex", "default", nil) m2 := newTaskMessage("reindex", nil)
m3 := randomTask("gen_thumbnail", "default", nil) m3 := newTaskMessage("gen_thumbnail", nil)
tests := []struct { tests := []struct {
initDead []*TaskMessage initDead []*TaskMessage
@ -1144,9 +1144,9 @@ func TestDeleteAllDeadTasks(t *testing.T) {
func TestDeleteAllRetryTasks(t *testing.T) { func TestDeleteAllRetryTasks(t *testing.T) {
r := setup(t) r := setup(t)
m1 := randomTask("send_email", "default", nil) m1 := newTaskMessage("send_email", nil)
m2 := randomTask("reindex", "default", nil) m2 := newTaskMessage("reindex", nil)
m3 := randomTask("gen_thumbnail", "default", nil) m3 := newTaskMessage("gen_thumbnail", nil)
tests := []struct { tests := []struct {
initRetry []*TaskMessage initRetry []*TaskMessage
@ -1189,9 +1189,9 @@ func TestDeleteAllRetryTasks(t *testing.T) {
func TestDeleteAllScheduledTasks(t *testing.T) { func TestDeleteAllScheduledTasks(t *testing.T) {
r := setup(t) r := setup(t)
m1 := randomTask("send_email", "default", nil) m1 := newTaskMessage("send_email", nil)
m2 := randomTask("reindex", "default", nil) m2 := newTaskMessage("reindex", nil)
m3 := randomTask("gen_thumbnail", "default", nil) m3 := newTaskMessage("gen_thumbnail", nil)
tests := []struct { tests := []struct {
initScheduled []*TaskMessage initScheduled []*TaskMessage

View File

@ -17,6 +17,7 @@ func init() {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
} }
// TODO(hibiken): Get Redis address and db number from ENV variables.
func setup(t *testing.T) *RDB { func setup(t *testing.T) *RDB {
t.Helper() t.Helper()
r := NewRDB(redis.NewClient(&redis.Options{ r := NewRDB(redis.NewClient(&redis.Options{
@ -25,7 +26,7 @@ func setup(t *testing.T) *RDB {
})) }))
// Start each test with a clean slate. // Start each test with a clean slate.
if err := r.client.FlushDB().Err(); err != nil { if err := r.client.FlushDB().Err(); err != nil {
panic(err) t.Fatal(err)
} }
return r return r
} }
@ -38,13 +39,13 @@ var sortMsgOpt = cmp.Transformer("SortMsg", func(in []*TaskMessage) []*TaskMessa
return out return out
}) })
func randomTask(taskType, qname string, payload map[string]interface{}) *TaskMessage { func newTaskMessage(taskType string, payload map[string]interface{}) *TaskMessage {
return &TaskMessage{ return &TaskMessage{
ID: xid.New(), ID: xid.New(),
Type: taskType, Type: taskType,
Queue: qname, Queue: "default",
Retry: 25, Retry: 25,
Payload: make(map[string]interface{}), Payload: payload,
} }
} }
@ -90,11 +91,9 @@ func TestEnqueue(t *testing.T) {
tests := []struct { tests := []struct {
msg *TaskMessage msg *TaskMessage
}{ }{
{msg: randomTask("send_email", "default", {msg: newTaskMessage("send_email", map[string]interface{}{"to": "exampleuser@gmail.com", "from": "noreply@example.com"})},
map[string]interface{}{"to": "exampleuser@gmail.com", "from": "noreply@example.com"})}, {msg: newTaskMessage("generate_csv", map[string]interface{}{})},
{msg: randomTask("generate_csv", "default", {msg: newTaskMessage("sync", nil)},
map[string]interface{}{})},
{msg: randomTask("sync", "default", nil)},
} }
for _, tc := range tests { for _, tc := range tests {
@ -120,7 +119,7 @@ func TestEnqueue(t *testing.T) {
func TestDequeue(t *testing.T) { func TestDequeue(t *testing.T) {
r := setup(t) r := setup(t)
t1 := randomTask("send_email", "default", map[string]interface{}{"subject": "hello!"}) t1 := newTaskMessage("send_email", map[string]interface{}{"subject": "hello!"})
tests := []struct { tests := []struct {
queued []*TaskMessage queued []*TaskMessage
want *TaskMessage want *TaskMessage
@ -153,8 +152,8 @@ func TestDequeue(t *testing.T) {
func TestDone(t *testing.T) { func TestDone(t *testing.T) {
r := setup(t) r := setup(t)
t1 := randomTask("send_email", "default", nil) t1 := newTaskMessage("send_email", nil)
t2 := randomTask("export_csv", "csv", nil) t2 := newTaskMessage("export_csv", nil)
tests := []struct { tests := []struct {
initial []*TaskMessage // initial state of the in-progress list initial []*TaskMessage // initial state of the in-progress list
@ -212,7 +211,7 @@ func TestDone(t *testing.T) {
func TestKill(t *testing.T) { func TestKill(t *testing.T) {
r := setup(t) r := setup(t)
t1 := randomTask("send_email", "default", nil) t1 := newTaskMessage("send_email", nil)
// TODO(hibiken): add test cases for trimming // TODO(hibiken): add test cases for trimming
tests := []struct { tests := []struct {
@ -257,9 +256,9 @@ func TestKill(t *testing.T) {
func TestRestoreUnfinished(t *testing.T) { func TestRestoreUnfinished(t *testing.T) {
r := setup(t) r := setup(t)
t1 := randomTask("send_email", "default", nil) t1 := newTaskMessage("send_email", nil)
t2 := randomTask("export_csv", "csv", nil) t2 := newTaskMessage("export_csv", nil)
t3 := randomTask("sync_stuff", "sync", nil) t3 := newTaskMessage("sync_stuff", nil)
tests := []struct { tests := []struct {
beforeSrc []*TaskMessage beforeSrc []*TaskMessage
@ -322,9 +321,9 @@ func TestRestoreUnfinished(t *testing.T) {
func TestCheckAndEnqueue(t *testing.T) { func TestCheckAndEnqueue(t *testing.T) {
r := setup(t) r := setup(t)
t1 := randomTask("send_email", "default", nil) t1 := newTaskMessage("send_email", nil)
t2 := randomTask("generate_csv", "default", nil) t2 := newTaskMessage("generate_csv", nil)
t3 := randomTask("gen_thumbnail", "default", nil) t3 := newTaskMessage("gen_thumbnail", nil)
secondAgo := time.Now().Add(-time.Second) secondAgo := time.Now().Add(-time.Second)
hourFromNow := time.Now().Add(time.Hour) hourFromNow := time.Now().Add(time.Hour)
@ -406,7 +405,7 @@ func TestSchedule(t *testing.T) {
processAt time.Time processAt time.Time
}{ }{
{ {
randomTask("send_email", "default", map[string]interface{}{"subject": "hello"}), newTaskMessage("send_email", map[string]interface{}{"subject": "hello"}),
time.Now().Add(15 * time.Minute), time.Now().Add(15 * time.Minute),
}, },
} }
@ -449,7 +448,7 @@ func TestRetryLater(t *testing.T) {
processAt time.Time processAt time.Time
}{ }{
{ {
randomTask("send_email", "default", map[string]interface{}{"subject": "hello"}), newTaskMessage("send_email", map[string]interface{}{"subject": "hello"}),
time.Now().Add(15 * time.Minute), time.Now().Add(15 * time.Minute),
}, },
} }