From ded9b6d4646cbf9f6f773935dd74f52a77dcfba6 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Fri, 6 Mar 2026 22:00:29 +0300 Subject: [PATCH] cache: include nar_url in ListRecentRoutes; test expired TTL in GetRouteByNarURL Signed-off-by: NotAShelf Change-Id: I952d08726f34d0d9b7c4c1880d60222c6a6a6964 --- internal/cache/db.go | 4 ++-- internal/cache/db_test.go | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/internal/cache/db.go b/internal/cache/db.go index 556b047..ae9c8b1 100644 --- a/internal/cache/db.go +++ b/internal/cache/db.go @@ -197,7 +197,7 @@ func (d *DB) ExpireOldRoutes() error { // Returns up to n non-expired routes ordered by most-recently-verified. func (d *DB) ListRecentRoutes(n int) ([]RouteEntry, error) { rows, err := d.db.Query(` - SELECT store_path, upstream_url, latency_ema, last_verified, ttl, nar_hash, nar_size + SELECT store_path, upstream_url, latency_ema, last_verified, ttl, nar_hash, nar_size, nar_url FROM routes WHERE ttl > ? ORDER BY last_verified DESC LIMIT ?`, time.Now().Unix(), n) if err != nil { @@ -210,7 +210,7 @@ func (d *DB) ListRecentRoutes(n int) ([]RouteEntry, error) { var lastVerifiedUnix, ttlUnix int64 if err := rows.Scan( &e.StorePath, &e.UpstreamURL, &e.LatencyEMA, - &lastVerifiedUnix, &ttlUnix, &e.NarHash, &e.NarSize, + &lastVerifiedUnix, &ttlUnix, &e.NarHash, &e.NarSize, &e.NarURL, ); err != nil { return nil, err } diff --git a/internal/cache/db_test.go b/internal/cache/db_test.go index 8ec1f99..b74dbab 100644 --- a/internal/cache/db_test.go +++ b/internal/cache/db_test.go @@ -276,6 +276,24 @@ func TestGetRouteByNarURL(t *testing.T) { if got2 != nil { t.Error("expected nil for missing NarURL") } + + // Expired entry must not be returned (tests the AND ttl > ? predicate). + expired := &cache.RouteEntry{ + StorePath: "abc456", + UpstreamURL: "https://cache.nixos.org", + NarURL: "nar/abc456.nar.xz", + TTL: time.Now().Add(-time.Hour), // already in the past + } + if err := db.SetRoute(expired); err != nil { + t.Fatalf("SetRoute expired: %v", err) + } + got3, err := db.GetRouteByNarURL("nar/abc456.nar.xz") + if err != nil { + t.Fatalf("GetRouteByNarURL for expired: %v", err) + } + if got3 != nil { + t.Error("GetRouteByNarURL should return nil for an expired entry") + } } func TestLRUEviction(t *testing.T) {