test(routing): cover TTL expiry in fetcher

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Mathias Bergqvist
2026-05-04 15:50:01 +02:00
parent b77820534a
commit d40a5ac890

View File

@@ -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)