2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-24 23:02:18 +08:00

Fix flaky tests

Some tests were failing due to mismatch in Score in ZSetEntry.
Changed ZSetEntry Score to float64 type so that we can use
cmpopts.EquateApprox to allow for margin when comparing.
This commit is contained in:
Ken Hibino 2020-01-11 10:02:13 -08:00
parent 2631672575
commit 97316d6766
6 changed files with 152 additions and 150 deletions

View File

@ -58,7 +58,7 @@ func TestClient(t *testing.T) {
Retry: defaultMaxRetry, Retry: defaultMaxRetry,
Queue: "default", Queue: "default",
}, },
Score: time.Now().Add(2 * time.Hour).Unix(), Score: float64(time.Now().Add(2 * time.Hour).Unix()),
}, },
}, },
}, },

View File

@ -20,7 +20,7 @@ import (
// ZSetEntry is an entry in redis sorted set. // ZSetEntry is an entry in redis sorted set.
type ZSetEntry struct { type ZSetEntry struct {
Msg *base.TaskMessage Msg *base.TaskMessage
Score int64 Score float64
} }
// SortMsgOpt is a cmp.Option to sort base.TaskMessage for comparing slice of task messages. // SortMsgOpt is a cmp.Option to sort base.TaskMessage for comparing slice of task messages.
@ -245,7 +245,7 @@ func getZSetEntries(tb testing.TB, r *redis.Client, zset string) []ZSetEntry {
for _, z := range data { for _, z := range data {
entries = append(entries, ZSetEntry{ entries = append(entries, ZSetEntry{
Msg: MustUnmarshal(tb, z.Member.(string)), Msg: MustUnmarshal(tb, z.Member.(string)),
Score: int64(z.Score), Score: z.Score,
}) })
} }
return entries return entries

View File

@ -47,8 +47,8 @@ func TestCurrentStats(t *testing.T) {
}, },
inProgress: []*base.TaskMessage{m2}, inProgress: []*base.TaskMessage{m2},
scheduled: []h.ZSetEntry{ scheduled: []h.ZSetEntry{
{Msg: m3, Score: now.Add(time.Hour).Unix()}, {Msg: m3, Score: float64(now.Add(time.Hour).Unix())},
{Msg: m4, Score: now.Unix()}}, {Msg: m4, Score: float64(now.Unix())}},
retry: []h.ZSetEntry{}, retry: []h.ZSetEntry{},
dead: []h.ZSetEntry{}, dead: []h.ZSetEntry{},
processed: 120, processed: 120,
@ -72,12 +72,12 @@ func TestCurrentStats(t *testing.T) {
}, },
inProgress: []*base.TaskMessage{}, inProgress: []*base.TaskMessage{},
scheduled: []h.ZSetEntry{ scheduled: []h.ZSetEntry{
{Msg: m3, Score: now.Unix()}, {Msg: m3, Score: float64(now.Unix())},
{Msg: m4, Score: now.Unix()}}, {Msg: m4, Score: float64(now.Unix())}},
retry: []h.ZSetEntry{ retry: []h.ZSetEntry{
{Msg: m1, Score: now.Add(time.Minute).Unix()}}, {Msg: m1, Score: float64(now.Add(time.Minute).Unix())}},
dead: []h.ZSetEntry{ dead: []h.ZSetEntry{
{Msg: m2, Score: now.Add(-time.Hour).Unix()}}, {Msg: m2, Score: float64(now.Add(-time.Hour).Unix())}},
processed: 90, processed: 90,
failed: 10, failed: 10,
allQueues: []interface{}{base.DefaultQueue}, allQueues: []interface{}{base.DefaultQueue},
@ -364,8 +364,8 @@ func TestListScheduled(t *testing.T) {
}{ }{
{ {
scheduled: []h.ZSetEntry{ scheduled: []h.ZSetEntry{
{Msg: m1, Score: p1.Unix()}, {Msg: m1, Score: float64(p1.Unix())},
{Msg: m2, Score: p2.Unix()}, {Msg: m2, Score: float64(p2.Unix())},
}, },
want: []*ScheduledTask{t1, t2}, want: []*ScheduledTask{t1, t2},
}, },
@ -449,8 +449,8 @@ func TestListRetry(t *testing.T) {
}{ }{
{ {
retry: []h.ZSetEntry{ retry: []h.ZSetEntry{
{Msg: m1, Score: p1.Unix()}, {Msg: m1, Score: float64(p1.Unix())},
{Msg: m2, Score: p2.Unix()}, {Msg: m2, Score: float64(p2.Unix())},
}, },
want: []*RetryTask{t1, t2}, want: []*RetryTask{t1, t2},
}, },
@ -527,8 +527,8 @@ func TestListDead(t *testing.T) {
}{ }{
{ {
dead: []h.ZSetEntry{ dead: []h.ZSetEntry{
{Msg: m1, Score: f1.Unix()}, {Msg: m1, Score: float64(f1.Unix())},
{Msg: m2, Score: f2.Unix()}, {Msg: m2, Score: float64(f2.Unix())},
}, },
want: []*DeadTask{t1, t2}, want: []*DeadTask{t1, t2},
}, },
@ -582,8 +582,8 @@ func TestEnqueueDeadTask(t *testing.T) {
}{ }{
{ {
dead: []h.ZSetEntry{ dead: []h.ZSetEntry{
{Msg: t1, Score: s1}, {Msg: t1, Score: float64(s1)},
{Msg: t2, Score: s2}, {Msg: t2, Score: float64(s2)},
}, },
score: s2, score: s2,
id: t2.ID, id: t2.ID,
@ -595,8 +595,8 @@ func TestEnqueueDeadTask(t *testing.T) {
}, },
{ {
dead: []h.ZSetEntry{ dead: []h.ZSetEntry{
{Msg: t1, Score: s1}, {Msg: t1, Score: float64(s1)},
{Msg: t2, Score: s2}, {Msg: t2, Score: float64(s2)},
}, },
score: 123, score: 123,
id: t2.ID, id: t2.ID,
@ -608,9 +608,9 @@ func TestEnqueueDeadTask(t *testing.T) {
}, },
{ {
dead: []h.ZSetEntry{ dead: []h.ZSetEntry{
{Msg: t1, Score: s1}, {Msg: t1, Score: float64(s1)},
{Msg: t2, Score: s2}, {Msg: t2, Score: float64(s2)},
{Msg: t3, Score: s1}, {Msg: t3, Score: float64(s1)},
}, },
score: s1, score: s1,
id: t3.ID, id: t3.ID,
@ -666,8 +666,8 @@ func TestEnqueueRetryTask(t *testing.T) {
}{ }{
{ {
retry: []h.ZSetEntry{ retry: []h.ZSetEntry{
{Msg: t1, Score: s1}, {Msg: t1, Score: float64(s1)},
{Msg: t2, Score: s2}, {Msg: t2, Score: float64(s2)},
}, },
score: s2, score: s2,
id: t2.ID, id: t2.ID,
@ -679,8 +679,8 @@ func TestEnqueueRetryTask(t *testing.T) {
}, },
{ {
retry: []h.ZSetEntry{ retry: []h.ZSetEntry{
{Msg: t1, Score: s1}, {Msg: t1, Score: float64(s1)},
{Msg: t2, Score: s2}, {Msg: t2, Score: float64(s2)},
}, },
score: 123, score: 123,
id: t2.ID, id: t2.ID,
@ -692,9 +692,9 @@ func TestEnqueueRetryTask(t *testing.T) {
}, },
{ {
retry: []h.ZSetEntry{ retry: []h.ZSetEntry{
{Msg: t1, Score: s1}, {Msg: t1, Score: float64(s1)},
{Msg: t2, Score: s2}, {Msg: t2, Score: float64(s2)},
{Msg: t3, Score: s2}, {Msg: t3, Score: float64(s2)},
}, },
score: s2, score: s2,
id: t3.ID, id: t3.ID,
@ -750,8 +750,8 @@ func TestEnqueueScheduledTask(t *testing.T) {
}{ }{
{ {
scheduled: []h.ZSetEntry{ scheduled: []h.ZSetEntry{
{Msg: t1, Score: s1}, {Msg: t1, Score: float64(s1)},
{Msg: t2, Score: s2}, {Msg: t2, Score: float64(s2)},
}, },
score: s2, score: s2,
id: t2.ID, id: t2.ID,
@ -763,8 +763,8 @@ func TestEnqueueScheduledTask(t *testing.T) {
}, },
{ {
scheduled: []h.ZSetEntry{ scheduled: []h.ZSetEntry{
{Msg: t1, Score: s1}, {Msg: t1, Score: float64(s1)},
{Msg: t2, Score: s2}, {Msg: t2, Score: float64(s2)},
}, },
score: 123, score: 123,
id: t2.ID, id: t2.ID,
@ -776,9 +776,9 @@ func TestEnqueueScheduledTask(t *testing.T) {
}, },
{ {
scheduled: []h.ZSetEntry{ scheduled: []h.ZSetEntry{
{Msg: t1, Score: s1}, {Msg: t1, Score: float64(s1)},
{Msg: t2, Score: s2}, {Msg: t2, Score: float64(s2)},
{Msg: t3, Score: s1}, {Msg: t3, Score: float64(s1)},
}, },
score: s1, score: s1,
id: t3.ID, id: t3.ID,
@ -834,9 +834,9 @@ func TestEnqueueAllScheduledTasks(t *testing.T) {
{ {
desc: "with tasks in scheduled queue", desc: "with tasks in scheduled queue",
scheduled: []h.ZSetEntry{ scheduled: []h.ZSetEntry{
{Msg: t1, Score: time.Now().Add(time.Hour).Unix()}, {Msg: t1, Score: float64(time.Now().Add(time.Hour).Unix())},
{Msg: t2, Score: time.Now().Add(time.Hour).Unix()}, {Msg: t2, Score: float64(time.Now().Add(time.Hour).Unix())},
{Msg: t3, Score: time.Now().Add(time.Hour).Unix()}, {Msg: t3, Score: float64(time.Now().Add(time.Hour).Unix())},
}, },
want: 3, want: 3,
wantEnqueued: map[string][]*base.TaskMessage{ wantEnqueued: map[string][]*base.TaskMessage{
@ -854,11 +854,11 @@ func TestEnqueueAllScheduledTasks(t *testing.T) {
{ {
desc: "with custom queues", desc: "with custom queues",
scheduled: []h.ZSetEntry{ scheduled: []h.ZSetEntry{
{Msg: t1, Score: time.Now().Add(time.Hour).Unix()}, {Msg: t1, Score: float64(time.Now().Add(time.Hour).Unix())},
{Msg: t2, Score: time.Now().Add(time.Hour).Unix()}, {Msg: t2, Score: float64(time.Now().Add(time.Hour).Unix())},
{Msg: t3, Score: time.Now().Add(time.Hour).Unix()}, {Msg: t3, Score: float64(time.Now().Add(time.Hour).Unix())},
{Msg: t4, Score: time.Now().Add(time.Hour).Unix()}, {Msg: t4, Score: float64(time.Now().Add(time.Hour).Unix())},
{Msg: t5, Score: time.Now().Add(time.Hour).Unix()}, {Msg: t5, Score: float64(time.Now().Add(time.Hour).Unix())},
}, },
want: 5, want: 5,
wantEnqueued: map[string][]*base.TaskMessage{ wantEnqueued: map[string][]*base.TaskMessage{
@ -913,9 +913,9 @@ func TestEnqueueAllRetryTasks(t *testing.T) {
{ {
desc: "with tasks in retry queue", desc: "with tasks in retry queue",
retry: []h.ZSetEntry{ retry: []h.ZSetEntry{
{Msg: t1, Score: time.Now().Add(time.Hour).Unix()}, {Msg: t1, Score: float64(time.Now().Add(time.Hour).Unix())},
{Msg: t2, Score: time.Now().Add(time.Hour).Unix()}, {Msg: t2, Score: float64(time.Now().Add(time.Hour).Unix())},
{Msg: t3, Score: time.Now().Add(time.Hour).Unix()}, {Msg: t3, Score: float64(time.Now().Add(time.Hour).Unix())},
}, },
want: 3, want: 3,
wantEnqueued: map[string][]*base.TaskMessage{ wantEnqueued: map[string][]*base.TaskMessage{
@ -933,11 +933,11 @@ func TestEnqueueAllRetryTasks(t *testing.T) {
{ {
desc: "with custom queues", desc: "with custom queues",
retry: []h.ZSetEntry{ retry: []h.ZSetEntry{
{Msg: t1, Score: time.Now().Add(time.Hour).Unix()}, {Msg: t1, Score: float64(time.Now().Add(time.Hour).Unix())},
{Msg: t2, Score: time.Now().Add(time.Hour).Unix()}, {Msg: t2, Score: float64(time.Now().Add(time.Hour).Unix())},
{Msg: t3, Score: time.Now().Add(time.Hour).Unix()}, {Msg: t3, Score: float64(time.Now().Add(time.Hour).Unix())},
{Msg: t4, Score: time.Now().Add(time.Hour).Unix()}, {Msg: t4, Score: float64(time.Now().Add(time.Hour).Unix())},
{Msg: t5, Score: time.Now().Add(time.Hour).Unix()}, {Msg: t5, Score: float64(time.Now().Add(time.Hour).Unix())},
}, },
want: 5, want: 5,
wantEnqueued: map[string][]*base.TaskMessage{ wantEnqueued: map[string][]*base.TaskMessage{
@ -992,9 +992,9 @@ func TestEnqueueAllDeadTasks(t *testing.T) {
{ {
desc: "with tasks in dead queue", desc: "with tasks in dead queue",
dead: []h.ZSetEntry{ dead: []h.ZSetEntry{
{Msg: t1, Score: time.Now().Add(-time.Minute).Unix()}, {Msg: t1, Score: float64(time.Now().Add(-time.Minute).Unix())},
{Msg: t2, Score: time.Now().Add(-time.Minute).Unix()}, {Msg: t2, Score: float64(time.Now().Add(-time.Minute).Unix())},
{Msg: t3, Score: time.Now().Add(-time.Minute).Unix()}, {Msg: t3, Score: float64(time.Now().Add(-time.Minute).Unix())},
}, },
want: 3, want: 3,
wantEnqueued: map[string][]*base.TaskMessage{ wantEnqueued: map[string][]*base.TaskMessage{
@ -1012,11 +1012,11 @@ func TestEnqueueAllDeadTasks(t *testing.T) {
{ {
desc: "with custom queues", desc: "with custom queues",
dead: []h.ZSetEntry{ dead: []h.ZSetEntry{
{Msg: t1, Score: time.Now().Add(-time.Minute).Unix()}, {Msg: t1, Score: float64(time.Now().Add(-time.Minute).Unix())},
{Msg: t2, Score: time.Now().Add(-time.Minute).Unix()}, {Msg: t2, Score: float64(time.Now().Add(-time.Minute).Unix())},
{Msg: t3, Score: time.Now().Add(-time.Minute).Unix()}, {Msg: t3, Score: float64(time.Now().Add(-time.Minute).Unix())},
{Msg: t4, Score: time.Now().Add(-time.Minute).Unix()}, {Msg: t4, Score: float64(time.Now().Add(-time.Minute).Unix())},
{Msg: t5, Score: time.Now().Add(-time.Minute).Unix()}, {Msg: t5, Score: float64(time.Now().Add(-time.Minute).Unix())},
}, },
want: 5, want: 5,
wantEnqueued: map[string][]*base.TaskMessage{ wantEnqueued: map[string][]*base.TaskMessage{
@ -1070,35 +1070,35 @@ func TestKillRetryTask(t *testing.T) {
}{ }{
{ {
retry: []h.ZSetEntry{ retry: []h.ZSetEntry{
{Msg: m1, Score: t1.Unix()}, {Msg: m1, Score: float64(t1.Unix())},
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
dead: []h.ZSetEntry{}, dead: []h.ZSetEntry{},
id: m1.ID, id: m1.ID,
score: t1.Unix(), score: t1.Unix(),
want: nil, want: nil,
wantRetry: []h.ZSetEntry{ wantRetry: []h.ZSetEntry{
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
wantDead: []h.ZSetEntry{ wantDead: []h.ZSetEntry{
{Msg: m1, Score: time.Now().Unix()}, {Msg: m1, Score: float64(time.Now().Unix())},
}, },
}, },
{ {
retry: []h.ZSetEntry{ retry: []h.ZSetEntry{
{Msg: m1, Score: t1.Unix()}, {Msg: m1, Score: float64(t1.Unix())},
}, },
dead: []h.ZSetEntry{ dead: []h.ZSetEntry{
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
id: m2.ID, id: m2.ID,
score: t2.Unix(), score: t2.Unix(),
want: ErrTaskNotFound, want: ErrTaskNotFound,
wantRetry: []h.ZSetEntry{ wantRetry: []h.ZSetEntry{
{Msg: m1, Score: t1.Unix()}, {Msg: m1, Score: float64(t1.Unix())},
}, },
wantDead: []h.ZSetEntry{ wantDead: []h.ZSetEntry{
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
}, },
} }
@ -1147,35 +1147,35 @@ func TestKillScheduledTask(t *testing.T) {
}{ }{
{ {
scheduled: []h.ZSetEntry{ scheduled: []h.ZSetEntry{
{Msg: m1, Score: t1.Unix()}, {Msg: m1, Score: float64(t1.Unix())},
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
dead: []h.ZSetEntry{}, dead: []h.ZSetEntry{},
id: m1.ID, id: m1.ID,
score: t1.Unix(), score: t1.Unix(),
want: nil, want: nil,
wantScheduled: []h.ZSetEntry{ wantScheduled: []h.ZSetEntry{
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
wantDead: []h.ZSetEntry{ wantDead: []h.ZSetEntry{
{Msg: m1, Score: time.Now().Unix()}, {Msg: m1, Score: float64(time.Now().Unix())},
}, },
}, },
{ {
scheduled: []h.ZSetEntry{ scheduled: []h.ZSetEntry{
{Msg: m1, Score: t1.Unix()}, {Msg: m1, Score: float64(t1.Unix())},
}, },
dead: []h.ZSetEntry{ dead: []h.ZSetEntry{
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
id: m2.ID, id: m2.ID,
score: t2.Unix(), score: t2.Unix(),
want: ErrTaskNotFound, want: ErrTaskNotFound,
wantScheduled: []h.ZSetEntry{ wantScheduled: []h.ZSetEntry{
{Msg: m1, Score: t1.Unix()}, {Msg: m1, Score: float64(t1.Unix())},
}, },
wantDead: []h.ZSetEntry{ wantDead: []h.ZSetEntry{
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
}, },
} }
@ -1222,42 +1222,42 @@ func TestKillAllRetryTasks(t *testing.T) {
}{ }{
{ {
retry: []h.ZSetEntry{ retry: []h.ZSetEntry{
{Msg: m1, Score: t1.Unix()}, {Msg: m1, Score: float64(t1.Unix())},
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
dead: []h.ZSetEntry{}, dead: []h.ZSetEntry{},
want: 2, want: 2,
wantRetry: []h.ZSetEntry{}, wantRetry: []h.ZSetEntry{},
wantDead: []h.ZSetEntry{ wantDead: []h.ZSetEntry{
{Msg: m1, Score: time.Now().Unix()}, {Msg: m1, Score: float64(time.Now().Unix())},
{Msg: m2, Score: time.Now().Unix()}, {Msg: m2, Score: float64(time.Now().Unix())},
}, },
}, },
{ {
retry: []h.ZSetEntry{ retry: []h.ZSetEntry{
{Msg: m1, Score: t1.Unix()}, {Msg: m1, Score: float64(t1.Unix())},
}, },
dead: []h.ZSetEntry{ dead: []h.ZSetEntry{
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
want: 1, want: 1,
wantRetry: []h.ZSetEntry{}, wantRetry: []h.ZSetEntry{},
wantDead: []h.ZSetEntry{ wantDead: []h.ZSetEntry{
{Msg: m1, Score: time.Now().Unix()}, {Msg: m1, Score: float64(time.Now().Unix())},
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
}, },
{ {
retry: []h.ZSetEntry{}, retry: []h.ZSetEntry{},
dead: []h.ZSetEntry{ dead: []h.ZSetEntry{
{Msg: m1, Score: t1.Unix()}, {Msg: m1, Score: float64(t1.Unix())},
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
want: 0, want: 0,
wantRetry: []h.ZSetEntry{}, wantRetry: []h.ZSetEntry{},
wantDead: []h.ZSetEntry{ wantDead: []h.ZSetEntry{
{Msg: m1, Score: t1.Unix()}, {Msg: m1, Score: float64(t1.Unix())},
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
}, },
} }
@ -1304,42 +1304,42 @@ func TestKillAllScheduledTasks(t *testing.T) {
}{ }{
{ {
scheduled: []h.ZSetEntry{ scheduled: []h.ZSetEntry{
{Msg: m1, Score: t1.Unix()}, {Msg: m1, Score: float64(t1.Unix())},
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
dead: []h.ZSetEntry{}, dead: []h.ZSetEntry{},
want: 2, want: 2,
wantScheduled: []h.ZSetEntry{}, wantScheduled: []h.ZSetEntry{},
wantDead: []h.ZSetEntry{ wantDead: []h.ZSetEntry{
{Msg: m1, Score: time.Now().Unix()}, {Msg: m1, Score: float64(time.Now().Unix())},
{Msg: m2, Score: time.Now().Unix()}, {Msg: m2, Score: float64(time.Now().Unix())},
}, },
}, },
{ {
scheduled: []h.ZSetEntry{ scheduled: []h.ZSetEntry{
{Msg: m1, Score: t1.Unix()}, {Msg: m1, Score: float64(t1.Unix())},
}, },
dead: []h.ZSetEntry{ dead: []h.ZSetEntry{
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
want: 1, want: 1,
wantScheduled: []h.ZSetEntry{}, wantScheduled: []h.ZSetEntry{},
wantDead: []h.ZSetEntry{ wantDead: []h.ZSetEntry{
{Msg: m1, Score: time.Now().Unix()}, {Msg: m1, Score: float64(time.Now().Unix())},
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
}, },
{ {
scheduled: []h.ZSetEntry{}, scheduled: []h.ZSetEntry{},
dead: []h.ZSetEntry{ dead: []h.ZSetEntry{
{Msg: m1, Score: t1.Unix()}, {Msg: m1, Score: float64(t1.Unix())},
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
want: 0, want: 0,
wantScheduled: []h.ZSetEntry{}, wantScheduled: []h.ZSetEntry{},
wantDead: []h.ZSetEntry{ wantDead: []h.ZSetEntry{
{Msg: m1, Score: t1.Unix()}, {Msg: m1, Score: float64(t1.Unix())},
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
}, },
} }
@ -1386,8 +1386,8 @@ func TestDeleteDeadTask(t *testing.T) {
}{ }{
{ {
dead: []h.ZSetEntry{ dead: []h.ZSetEntry{
{Msg: m1, Score: t1.Unix()}, {Msg: m1, Score: float64(t1.Unix())},
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
id: m1.ID, id: m1.ID,
score: t1.Unix(), score: t1.Unix(),
@ -1396,8 +1396,8 @@ func TestDeleteDeadTask(t *testing.T) {
}, },
{ {
dead: []h.ZSetEntry{ dead: []h.ZSetEntry{
{Msg: m1, Score: t1.Unix()}, {Msg: m1, Score: float64(t1.Unix())},
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
id: m1.ID, id: m1.ID,
score: t2.Unix(), // id and score mismatch score: t2.Unix(), // id and score mismatch
@ -1446,8 +1446,8 @@ func TestDeleteRetryTask(t *testing.T) {
}{ }{
{ {
retry: []h.ZSetEntry{ retry: []h.ZSetEntry{
{Msg: m1, Score: t1.Unix()}, {Msg: m1, Score: float64(t1.Unix())},
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
id: m1.ID, id: m1.ID,
score: t1.Unix(), score: t1.Unix(),
@ -1456,7 +1456,7 @@ func TestDeleteRetryTask(t *testing.T) {
}, },
{ {
retry: []h.ZSetEntry{ retry: []h.ZSetEntry{
{Msg: m1, Score: t1.Unix()}, {Msg: m1, Score: float64(t1.Unix())},
}, },
id: m2.ID, id: m2.ID,
score: t2.Unix(), score: t2.Unix(),
@ -1498,8 +1498,8 @@ func TestDeleteScheduledTask(t *testing.T) {
}{ }{
{ {
scheduled: []h.ZSetEntry{ scheduled: []h.ZSetEntry{
{Msg: m1, Score: t1.Unix()}, {Msg: m1, Score: float64(t1.Unix())},
{Msg: m2, Score: t2.Unix()}, {Msg: m2, Score: float64(t2.Unix())},
}, },
id: m1.ID, id: m1.ID,
score: t1.Unix(), score: t1.Unix(),
@ -1508,7 +1508,7 @@ func TestDeleteScheduledTask(t *testing.T) {
}, },
{ {
scheduled: []h.ZSetEntry{ scheduled: []h.ZSetEntry{
{Msg: m1, Score: t1.Unix()}, {Msg: m1, Score: float64(t1.Unix())},
}, },
id: m2.ID, id: m2.ID,
score: t2.Unix(), score: t2.Unix(),
@ -1546,9 +1546,9 @@ func TestDeleteAllDeadTasks(t *testing.T) {
}{ }{
{ {
dead: []h.ZSetEntry{ dead: []h.ZSetEntry{
{Msg: m1, Score: time.Now().Unix()}, {Msg: m1, Score: float64(time.Now().Unix())},
{Msg: m2, Score: time.Now().Unix()}, {Msg: m2, Score: float64(time.Now().Unix())},
{Msg: m3, Score: time.Now().Unix()}, {Msg: m3, Score: float64(time.Now().Unix())},
}, },
wantDead: []*base.TaskMessage{}, wantDead: []*base.TaskMessage{},
}, },
@ -1582,9 +1582,9 @@ func TestDeleteAllRetryTasks(t *testing.T) {
}{ }{
{ {
retry: []h.ZSetEntry{ retry: []h.ZSetEntry{
{Msg: m1, Score: time.Now().Unix()}, {Msg: m1, Score: float64(time.Now().Unix())},
{Msg: m2, Score: time.Now().Unix()}, {Msg: m2, Score: float64(time.Now().Unix())},
{Msg: m3, Score: time.Now().Unix()}, {Msg: m3, Score: float64(time.Now().Unix())},
}, },
wantRetry: []*base.TaskMessage{}, wantRetry: []*base.TaskMessage{},
}, },
@ -1618,9 +1618,9 @@ func TestDeleteAllScheduledTasks(t *testing.T) {
}{ }{
{ {
scheduled: []h.ZSetEntry{ scheduled: []h.ZSetEntry{
{Msg: m1, Score: time.Now().Add(time.Minute).Unix()}, {Msg: m1, Score: float64(time.Now().Add(time.Minute).Unix())},
{Msg: m2, Score: time.Now().Add(time.Minute).Unix()}, {Msg: m2, Score: float64(time.Now().Add(time.Minute).Unix())},
{Msg: m3, Score: time.Now().Add(time.Minute).Unix()}, {Msg: m3, Score: float64(time.Now().Add(time.Minute).Unix())},
}, },
wantScheduled: []*base.TaskMessage{}, wantScheduled: []*base.TaskMessage{},
}, },

View File

@ -308,8 +308,8 @@ func TestSchedule(t *testing.T) {
t.Errorf("%s inserted %d items to %q, want 1 items inserted", desc, len(gotScheduled), base.ScheduledQueue) t.Errorf("%s inserted %d items to %q, want 1 items inserted", desc, len(gotScheduled), base.ScheduledQueue)
continue continue
} }
if gotScheduled[0].Score != tc.processAt.Unix() { if int64(gotScheduled[0].Score) != tc.processAt.Unix() {
t.Errorf("%s inserted an item with score %d, want %d", desc, gotScheduled[0].Score, tc.processAt.Unix()) t.Errorf("%s inserted an item with score %d, want %d", desc, int64(gotScheduled[0].Score), tc.processAt.Unix())
continue continue
} }
} }
@ -347,7 +347,7 @@ func TestRetry(t *testing.T) {
retry: []h.ZSetEntry{ retry: []h.ZSetEntry{
{ {
Msg: t3, Msg: t3,
Score: now.Add(time.Minute).Unix(), Score: float64(now.Add(time.Minute).Unix()),
}, },
}, },
msg: t1, msg: t1,
@ -357,11 +357,11 @@ func TestRetry(t *testing.T) {
wantRetry: []h.ZSetEntry{ wantRetry: []h.ZSetEntry{
{ {
Msg: t1AfterRetry, Msg: t1AfterRetry,
Score: now.Add(5 * time.Minute).Unix(), Score: float64(now.Add(5 * time.Minute).Unix()),
}, },
{ {
Msg: t3, Msg: t3,
Score: now.Add(time.Minute).Unix(), Score: float64(now.Add(time.Minute).Unix()),
}, },
}, },
}, },
@ -440,7 +440,7 @@ func TestKill(t *testing.T) {
dead: []h.ZSetEntry{ dead: []h.ZSetEntry{
{ {
Msg: t3, Msg: t3,
Score: now.Add(-time.Hour).Unix(), Score: float64(now.Add(-time.Hour).Unix()),
}, },
}, },
target: t1, target: t1,
@ -448,11 +448,11 @@ func TestKill(t *testing.T) {
wantDead: []h.ZSetEntry{ wantDead: []h.ZSetEntry{
{ {
Msg: t1AfterKill, Msg: t1AfterKill,
Score: now.Unix(), Score: float64(now.Unix()),
}, },
{ {
Msg: t3, Msg: t3,
Score: now.Add(-time.Hour).Unix(), Score: float64(now.Add(-time.Hour).Unix()),
}, },
}, },
}, },
@ -464,7 +464,7 @@ func TestKill(t *testing.T) {
wantDead: []h.ZSetEntry{ wantDead: []h.ZSetEntry{
{ {
Msg: t1AfterKill, Msg: t1AfterKill,
Score: now.Unix(), Score: float64(now.Unix()),
}, },
}, },
}, },
@ -593,11 +593,11 @@ func TestCheckAndEnqueue(t *testing.T) {
}{ }{
{ {
scheduled: []h.ZSetEntry{ scheduled: []h.ZSetEntry{
{Msg: t1, Score: secondAgo.Unix()}, {Msg: t1, Score: float64(secondAgo.Unix())},
{Msg: t2, Score: secondAgo.Unix()}, {Msg: t2, Score: float64(secondAgo.Unix())},
}, },
retry: []h.ZSetEntry{ retry: []h.ZSetEntry{
{Msg: t3, Score: secondAgo.Unix()}}, {Msg: t3, Score: float64(secondAgo.Unix())}},
wantEnqueued: map[string][]*base.TaskMessage{ wantEnqueued: map[string][]*base.TaskMessage{
"default": {t1, t2, t3}, "default": {t1, t2, t3},
}, },
@ -606,10 +606,10 @@ func TestCheckAndEnqueue(t *testing.T) {
}, },
{ {
scheduled: []h.ZSetEntry{ scheduled: []h.ZSetEntry{
{Msg: t1, Score: hourFromNow.Unix()}, {Msg: t1, Score: float64(hourFromNow.Unix())},
{Msg: t2, Score: secondAgo.Unix()}}, {Msg: t2, Score: float64(secondAgo.Unix())}},
retry: []h.ZSetEntry{ retry: []h.ZSetEntry{
{Msg: t3, Score: secondAgo.Unix()}}, {Msg: t3, Score: float64(secondAgo.Unix())}},
wantEnqueued: map[string][]*base.TaskMessage{ wantEnqueued: map[string][]*base.TaskMessage{
"default": {t2, t3}, "default": {t2, t3},
}, },
@ -618,10 +618,10 @@ func TestCheckAndEnqueue(t *testing.T) {
}, },
{ {
scheduled: []h.ZSetEntry{ scheduled: []h.ZSetEntry{
{Msg: t1, Score: hourFromNow.Unix()}, {Msg: t1, Score: float64(hourFromNow.Unix())},
{Msg: t2, Score: hourFromNow.Unix()}}, {Msg: t2, Score: float64(hourFromNow.Unix())}},
retry: []h.ZSetEntry{ retry: []h.ZSetEntry{
{Msg: t3, Score: hourFromNow.Unix()}}, {Msg: t3, Score: float64(hourFromNow.Unix())}},
wantEnqueued: map[string][]*base.TaskMessage{ wantEnqueued: map[string][]*base.TaskMessage{
"default": {}, "default": {},
}, },
@ -630,11 +630,11 @@ func TestCheckAndEnqueue(t *testing.T) {
}, },
{ {
scheduled: []h.ZSetEntry{ scheduled: []h.ZSetEntry{
{Msg: t1, Score: secondAgo.Unix()}, {Msg: t1, Score: float64(secondAgo.Unix())},
{Msg: t4, Score: secondAgo.Unix()}, {Msg: t4, Score: float64(secondAgo.Unix())},
}, },
retry: []h.ZSetEntry{ retry: []h.ZSetEntry{
{Msg: t5, Score: secondAgo.Unix()}}, {Msg: t5, Score: float64(secondAgo.Unix())}},
wantEnqueued: map[string][]*base.TaskMessage{ wantEnqueued: map[string][]*base.TaskMessage{
"default": {t1}, "default": {t1},
"critical": {t4}, "critical": {t4},

View File

@ -12,6 +12,7 @@ import (
"time" "time"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
h "github.com/hibiken/asynq/internal/asynqtest" h "github.com/hibiken/asynq/internal/asynqtest"
"github.com/hibiken/asynq/internal/base" "github.com/hibiken/asynq/internal/base"
"github.com/hibiken/asynq/internal/rdb" "github.com/hibiken/asynq/internal/rdb"
@ -128,9 +129,9 @@ func TestProcessorRetry(t *testing.T) {
delay: time.Minute, delay: time.Minute,
wait: time.Second, wait: time.Second,
wantRetry: []h.ZSetEntry{ wantRetry: []h.ZSetEntry{
{Msg: &r2, Score: now.Add(time.Minute).Unix()}, {Msg: &r2, Score: float64(now.Add(time.Minute).Unix())},
{Msg: &r3, Score: now.Add(time.Minute).Unix()}, {Msg: &r3, Score: float64(now.Add(time.Minute).Unix())},
{Msg: &r4, Score: now.Add(time.Minute).Unix()}, {Msg: &r4, Score: float64(now.Add(time.Minute).Unix())},
}, },
wantDead: []*base.TaskMessage{&r1}, wantDead: []*base.TaskMessage{&r1},
}, },
@ -161,8 +162,9 @@ func TestProcessorRetry(t *testing.T) {
time.Sleep(tc.wait) time.Sleep(tc.wait)
p.terminate() p.terminate()
cmpOpt := cmpopts.EquateApprox(0, float64(time.Second)) // allow up to second difference in zset score
gotRetry := h.GetRetryEntries(t, r) gotRetry := h.GetRetryEntries(t, r)
if diff := cmp.Diff(tc.wantRetry, gotRetry, h.SortZSetEntryOpt); diff != "" { if diff := cmp.Diff(tc.wantRetry, gotRetry, h.SortZSetEntryOpt, cmpOpt); diff != "" {
t.Errorf("mismatch found in %q after running processor; (-want, +got)\n%s", base.RetryQueue, diff) t.Errorf("mismatch found in %q after running processor; (-want, +got)\n%s", base.RetryQueue, diff)
} }

View File

@ -36,11 +36,11 @@ func TestScheduler(t *testing.T) {
}{ }{
{ {
initScheduled: []h.ZSetEntry{ initScheduled: []h.ZSetEntry{
{Msg: t1, Score: now.Add(time.Hour).Unix()}, {Msg: t1, Score: float64(now.Add(time.Hour).Unix())},
{Msg: t2, Score: now.Add(-2 * time.Second).Unix()}, {Msg: t2, Score: float64(now.Add(-2 * time.Second).Unix())},
}, },
initRetry: []h.ZSetEntry{ initRetry: []h.ZSetEntry{
{Msg: t3, Score: time.Now().Add(-500 * time.Millisecond).Unix()}, {Msg: t3, Score: float64(time.Now().Add(-500 * time.Millisecond).Unix())},
}, },
initQueue: []*base.TaskMessage{t4}, initQueue: []*base.TaskMessage{t4},
wait: pollInterval * 2, wait: pollInterval * 2,
@ -50,9 +50,9 @@ func TestScheduler(t *testing.T) {
}, },
{ {
initScheduled: []h.ZSetEntry{ initScheduled: []h.ZSetEntry{
{Msg: t1, Score: now.Unix()}, {Msg: t1, Score: float64(now.Unix())},
{Msg: t2, Score: now.Add(-2 * time.Second).Unix()}, {Msg: t2, Score: float64(now.Add(-2 * time.Second).Unix())},
{Msg: t3, Score: now.Add(-500 * time.Millisecond).Unix()}, {Msg: t3, Score: float64(now.Add(-500 * time.Millisecond).Unix())},
}, },
initRetry: []h.ZSetEntry{}, initRetry: []h.ZSetEntry{},
initQueue: []*base.TaskMessage{t4}, initQueue: []*base.TaskMessage{t4},