From d40a5ac8902515f7cb6a580409a8dfacfa86c652 Mon Sep 17 00:00:00 2001 From: Mathias Bergqvist Date: Mon, 4 May 2026 15:50:01 +0200 Subject: [PATCH] test(routing): cover TTL expiry in fetcher Co-Authored-By: Claude Sonnet 4.6 --- internal/routing/passrate_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/internal/routing/passrate_test.go b/internal/routing/passrate_test.go index bd2c50e..f0b0f83 100644 --- a/internal/routing/passrate_test.go +++ b/internal/routing/passrate_test.go @@ -60,6 +60,27 @@ func TestFetcherCachesWithinTTL(t *testing.T) { assert.Equal(t, int32(1), atomic.LoadInt32(&calls), "should hit upstream once and serve four times from cache") } +func TestFetcherFetchesAgainAfterTTLExpires(t *testing.T) { + var calls int32 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + atomic.AddInt32(&calls, 1) + _ = json.NewEncoder(w).Encode(map[string]any{"pass_rate": 0.5}) + })) + defer srv.Close() + + // Tight TTL so the test stays fast. + f := routing.NewFetcher(srv.URL, "7d", 5*time.Millisecond) + _, err := f.Get(context.Background(), "tdd") + require.NoError(t, err) + assert.Equal(t, int32(1), atomic.LoadInt32(&calls)) + + // Sleep past TTL, then a second Get should hit upstream again. + time.Sleep(15 * time.Millisecond) + _, err = f.Get(context.Background(), "tdd") + require.NoError(t, err) + assert.Equal(t, int32(2), atomic.LoadInt32(&calls), "expected fresh upstream call after TTL expiry") +} + func TestFetcherSurfacesUpstreamError(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { http.Error(w, "boom", http.StatusInternalServerError)