From 572eb338d53217b4d38ca575bb228c17096435df Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Fri, 28 Aug 2020 06:04:17 -0700 Subject: [PATCH] Fix flaky ProcessorRetry test --- internal/asynqtest/asynqtest.go | 9 +++++++++ processor_test.go | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/internal/asynqtest/asynqtest.go b/internal/asynqtest/asynqtest.go index abc0992..2c34f5b 100644 --- a/internal/asynqtest/asynqtest.go +++ b/internal/asynqtest/asynqtest.go @@ -7,6 +7,7 @@ package asynqtest import ( "encoding/json" + "math" "sort" "testing" @@ -17,6 +18,14 @@ import ( "github.com/hibiken/asynq/internal/base" ) +// EquateInt64Approx returns a Comparer option that treats int64 values +// to be equal if they are within the given margin. +func EquateInt64Approx(margin int64) cmp.Option { + return cmp.Comparer(func(a, b int64) bool { + return math.Abs(float64(a-b)) < float64(margin) + }) +} + // SortMsgOpt is a cmp.Option to sort base.TaskMessage for comparing slice of task messages. var SortMsgOpt = cmp.Transformer("SortTaskMessages", func(in []*base.TaskMessage) []*base.TaskMessage { out := append([]*base.TaskMessage(nil), in...) // Copy input to avoid mutating it diff --git a/processor_test.go b/processor_test.go index ac2728f..603a1bd 100644 --- a/processor_test.go +++ b/processor_test.go @@ -386,7 +386,7 @@ func TestProcessorRetry(t *testing.T) { time.Sleep(tc.wait) // FIXME: This makes test flaky. p.terminate() - cmpOpt := cmpopts.EquateApprox(0, float64(time.Second)) // allow up to a second difference in zset score + cmpOpt := h.EquateInt64Approx(1) // allow up to a second difference in zset score gotRetry := h.GetRetryEntries(t, r, base.DefaultQueueName) 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.RetryKey(base.DefaultQueueName), diff)