From f952062e30fbccc692e7168209d1f27d1f54a46d Mon Sep 17 00:00:00 2001 From: Simon Chrzanowski Date: Sun, 22 Nov 2026 10:01:45 +0100 Subject: [PATCH 1/2] Bound the expected rewrite expiry by the test's own start and end The test compared with the time after the request, allowing two seconds. On a slow Windows runner the request took 1.7s and it failed. --- internal/handler/cooldown_expiry_test.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/internal/handler/cooldown_expiry_test.go b/internal/handler/cooldown_expiry_test.go index fd558455..7f23c36a 100644 --- a/internal/handler/cooldown_expiry_test.go +++ b/internal/handler/cooldown_expiry_test.go @@ -148,7 +148,10 @@ func TestHandlersAskCooldownThroughRecorder(t *testing.T) { func TestHandlerRewriteExpiresWithWithheldVersion(t *testing.T) { for _, c := range validatorCases { t.Run(c.ecosystem, func(t *testing.T) { - u := cooldownUpstream(t) // the newest version was published 2h ago + // The newest version is published 2h before a time between + // start and the end of the request, to the second. + start := time.Now() + u := cooldownUpstream(t) proxy, _, clock := validatorProxyWithDB(t, u, time.Hour) // Cooldown compares with the wall clock, so the proxy's clock // follows it here. @@ -160,6 +163,7 @@ func TestHandlerRewriteExpiresWithWithheldVersion(t *testing.T) { if rec := serve(handler, c.path, nil); rec.Code != http.StatusOK { t.Fatalf("status %d", rec.Code) } + end := time.Now() proxy.rewrites.mu.Lock() defer proxy.rewrites.mu.Unlock() if len(proxy.rewrites.entries) != 1 { @@ -167,10 +171,10 @@ func TestHandlerRewriteExpiresWithWithheldVersion(t *testing.T) { } for _, el := range proxy.rewrites.entries { expires := el.Value.(*rewriteEntry).expires - // Published 2h ago with a 3h cooldown: available in an hour, - // to the second the publish time was given in. - if want := time.Now().Add(time.Hour); expires.Before(want.Add(-2*time.Second)) || expires.After(want.Add(time.Second)) { - t.Errorf("rewrite expires %v, want about %v when the withheld version is available", expires, want) + // With a 3h cooldown it is available an hour after then. + earliest, latest := start.Add(time.Hour).Add(-time.Second), end.Add(time.Hour) + if expires.Before(earliest) || expires.After(latest) { + t.Errorf("rewrite expires %v, want between %v and %v, when the withheld version is available", expires, earliest, latest) } } }) From 89be00d6c2e15f48a079495eeb6fbe687bc7034e Mon Sep 17 00:00:00 2001 From: Simon Chrzanowski Date: Sun, 22 Nov 2026 10:13:28 +0100 Subject: [PATCH 2/2] Compare the rewrite expiry with the fixture's publish time A window between the test's start and end let a wrong expiry through when the request was slow. --- internal/handler/cooldown_expiry_test.go | 12 +++--------- internal/handler/metadata_validators_test.go | 13 +++++++++++-- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/internal/handler/cooldown_expiry_test.go b/internal/handler/cooldown_expiry_test.go index 7f23c36a..6856d83b 100644 --- a/internal/handler/cooldown_expiry_test.go +++ b/internal/handler/cooldown_expiry_test.go @@ -148,10 +148,7 @@ func TestHandlersAskCooldownThroughRecorder(t *testing.T) { func TestHandlerRewriteExpiresWithWithheldVersion(t *testing.T) { for _, c := range validatorCases { t.Run(c.ecosystem, func(t *testing.T) { - // The newest version is published 2h before a time between - // start and the end of the request, to the second. - start := time.Now() - u := cooldownUpstream(t) + u, published := cooldownUpstreamPublished(t) proxy, _, clock := validatorProxyWithDB(t, u, time.Hour) // Cooldown compares with the wall clock, so the proxy's clock // follows it here. @@ -163,7 +160,6 @@ func TestHandlerRewriteExpiresWithWithheldVersion(t *testing.T) { if rec := serve(handler, c.path, nil); rec.Code != http.StatusOK { t.Fatalf("status %d", rec.Code) } - end := time.Now() proxy.rewrites.mu.Lock() defer proxy.rewrites.mu.Unlock() if len(proxy.rewrites.entries) != 1 { @@ -171,10 +167,8 @@ func TestHandlerRewriteExpiresWithWithheldVersion(t *testing.T) { } for _, el := range proxy.rewrites.entries { expires := el.Value.(*rewriteEntry).expires - // With a 3h cooldown it is available an hour after then. - earliest, latest := start.Add(time.Hour).Add(-time.Second), end.Add(time.Hour) - if expires.Before(earliest) || expires.After(latest) { - t.Errorf("rewrite expires %v, want between %v and %v, when the withheld version is available", expires, earliest, latest) + if want := published.Add(3 * time.Hour); !expires.Equal(want) { + t.Errorf("rewrite expires %v, want %v, when the withheld version is available", expires, want) } } }) diff --git a/internal/handler/metadata_validators_test.go b/internal/handler/metadata_validators_test.go index 340e3db6..8d469fe1 100644 --- a/internal/handler/metadata_validators_test.go +++ b/internal/handler/metadata_validators_test.go @@ -688,7 +688,16 @@ func TestRewrittenMetadataLastModifiedWithClockSkew(t *testing.T) { // and a cooldown of an hour keeps it. func cooldownUpstream(t *testing.T) *validatorUpstream { t.Helper() - recent := time.Now().Add(-2 * time.Hour).UTC().Format(time.RFC3339) + u, _ := cooldownUpstreamPublished(t) + return u +} + +// cooldownUpstreamPublished is cooldownUpstream, also returning when the +// newest version was published. +func cooldownUpstreamPublished(t *testing.T) (*validatorUpstream, time.Time) { + t.Helper() + published := time.Now().Add(-2 * time.Hour).UTC().Truncate(time.Second) + recent := published.Format(time.RFC3339) u := newValidatorUpstream(t) u.set( `{"name":"left-pad","dist-tags":{"latest":"1.4.0"},"versions":{`+ @@ -699,7 +708,7 @@ func cooldownUpstream(t *testing.T) *validatorUpstream { `{"version":"1.0.0","time":"2016-01-01T00:00:00Z","dist":{"type":"zip","url":"https://example.com/pkg-1.0.0.zip"}},`+ `{"version":"1.1.0","time":"`+recent+`","dist":{"type":"zip","url":"https://example.com/pkg-1.1.0.zip"}}]}}`, ) - return u + return u, published } // TestRewrittenMetadataETagWithCooldownFollowsKeptVersions checks that with