From 9924e56f4278d111d08fe9bddf36bc34dd291e1b Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 23 Jun 2023 13:53:44 +0200 Subject: [PATCH] pkg/epoch: fix tests on macOS These tests were failing on my macOS; could be the precision issue (like on Windows), or just because they're "too fast". === RUN TestSourceDateEpoch/WithoutSourceDateEpoch epoch_test.go:51: Error Trace: /Users/thajeztah/go/src/github.com/containerd/containerd/pkg/epoch/epoch_test.go:51 Error: Should be true Test: TestSourceDateEpoch/WithoutSourceDateEpoch Messages: now: 2023-06-23 11:47:09.93118 +0000 UTC, v: 2023-06-23 11:47:09.93118 +0000 UTC This patch: - updates the rightAfter utility to allow the timestamps to be "equal" - updates the asserts to provide some details about the timestamps - uses UTC for the value we're comparing to, to match the timestamps that are generated. Signed-off-by: Sebastiaan van Stijn --- pkg/epoch/epoch_test.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/pkg/epoch/epoch_test.go b/pkg/epoch/epoch_test.go index f25c77eb2..27535b78d 100644 --- a/pkg/epoch/epoch_test.go +++ b/pkg/epoch/epoch_test.go @@ -26,11 +26,15 @@ import ( ) func rightAfter(t1, t2 time.Time) bool { + if t2.Equal(t1) { + return true + } + threshold := 10 * time.Millisecond if runtime.GOOS == "windows" { // Low timer resolution on Windows - return (t2.After(t1) && t2.Before(t1.Add(100*time.Millisecond))) || t2.Equal(t1) + threshold *= 10 } - return t2.After(t1) && t2.Before(t1.Add(10*time.Millisecond)) + return t2.After(t1) && t2.Before(t1.Add(threshold)) } func TestSourceDateEpoch(t *testing.T) { @@ -46,9 +50,9 @@ func TestSourceDateEpoch(t *testing.T) { require.NoError(t, err) require.Nil(t, vp) - now := time.Now() + now := time.Now().UTC() v := SourceDateEpochOrNow() - require.True(t, rightAfter(now, v)) + require.True(t, rightAfter(now, v), "now: %s, v: %s", now, v) }) t.Run("WithEmptySourceDateEpoch", func(t *testing.T) { @@ -58,9 +62,9 @@ func TestSourceDateEpoch(t *testing.T) { require.NoError(t, err) require.Nil(t, vp) - now := time.Now() + now := time.Now().UTC() v := SourceDateEpochOrNow() - require.True(t, rightAfter(now, v)) + require.True(t, rightAfter(now, v), "now: %s, v: %s", now, v) }) t.Run("WithSourceDateEpoch", func(t *testing.T) { @@ -85,8 +89,8 @@ func TestSourceDateEpoch(t *testing.T) { require.ErrorContains(t, err, "invalid SOURCE_DATE_EPOCH value") require.Nil(t, vp) - now := time.Now() + now := time.Now().UTC() v := SourceDateEpochOrNow() - require.True(t, rightAfter(now, v)) + require.True(t, rightAfter(now, v), "now: %s, v: %s", now, v) }) }