diff --git a/internal/handler/cooldown_expiry_test.go b/internal/handler/cooldown_expiry_test.go index fd558455..6856d83b 100644 --- a/internal/handler/cooldown_expiry_test.go +++ b/internal/handler/cooldown_expiry_test.go @@ -148,7 +148,7 @@ 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 + 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. @@ -167,10 +167,8 @@ 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) + 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