Test that a late hit and a disabled cache are counted right (closes #56)
check / check (push) Failing after 2m49s
check / check (push) Failing after 2m49s
A hit served after the request context has ended must still move the hit count, and a disabled disk cache must report no items and no size even when its database holds rows from an earlier run. Model: opus-5-5
This commit is contained in:
@@ -217,6 +217,40 @@ func TestStats_TotalsCountSourcesAndVariants(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestStats_DisabledCacheReportsNoItems verifies that a disabled disk cache
|
||||||
|
// reports no items and no size, even when its database still holds the
|
||||||
|
// rows of an earlier run with the disk cache enabled.
|
||||||
|
func TestStats_DisabledCacheReportsNoItems(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
enabled, _ := newEvictionTestCache(t, 1<<30)
|
||||||
|
|
||||||
|
storeEvictionTestSource(t, enabled, testHostCDN, testPathCat,
|
||||||
|
bytes.Repeat([]byte{0xAA}, 1000))
|
||||||
|
storeEvictionTestVariant(t, enabled, testVariantKeyOne,
|
||||||
|
bytes.Repeat([]byte{0xAB}, 500))
|
||||||
|
|
||||||
|
disabled, err := NewCache(enabled.db, CacheConfig{
|
||||||
|
StateDir: t.TempDir(),
|
||||||
|
CacheTTL: time.Hour,
|
||||||
|
NegativeTTL: 5 * time.Minute,
|
||||||
|
DisableDiskCache: true,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
stats, err := disabled.Stats(t.Context())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Stats() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if stats.TotalItems != 0 || stats.TotalSizeBytes != 0 {
|
||||||
|
t.Errorf("TotalItems = %d, TotalSizeBytes = %d, want 0 and 0",
|
||||||
|
stats.TotalItems, stats.TotalSizeBytes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// cacheStatsCounters holds the counters of the cache_stats row, in column
|
// cacheStatsCounters holds the counters of the cache_stats row, in column
|
||||||
// order.
|
// order.
|
||||||
type cacheStatsCounters struct {
|
type cacheStatsCounters struct {
|
||||||
@@ -415,3 +449,51 @@ func TestService_Get_CountsInterruptedMisses(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestService_Get_CountsHitAfterRequestEnds checks every cache_stats counter
|
||||||
|
// after a hit served with a request context that has already ended: only
|
||||||
|
// the hit count moves.
|
||||||
|
func TestService_Get_CountsHitAfterRequestEnds(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
svc, fixtures := SetupTestService(t)
|
||||||
|
|
||||||
|
req := &ImageRequest{
|
||||||
|
SourceHost: fixtures.GoodHost,
|
||||||
|
SourcePath: testPathPhoto,
|
||||||
|
Size: Size{Width: 50, Height: 50},
|
||||||
|
Format: FormatJPEG,
|
||||||
|
Quality: 85,
|
||||||
|
FitMode: FitCover,
|
||||||
|
}
|
||||||
|
|
||||||
|
// A first request caches the variant.
|
||||||
|
resp, err := svc.Get(t.Context(), req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("first Get() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = resp.Content.Close()
|
||||||
|
|
||||||
|
want := readCacheStatsCounters(t, svc.cache)
|
||||||
|
want.hitCount++
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(t.Context())
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
resp, err = svc.Get(ctx, req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Get() with an ended request context: error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = resp.Content.Close()
|
||||||
|
|
||||||
|
if resp.CacheStatus != CacheHit {
|
||||||
|
t.Fatalf("CacheStatus = %v, want %v", resp.CacheStatus, CacheHit)
|
||||||
|
}
|
||||||
|
|
||||||
|
got := readCacheStatsCounters(t, svc.cache)
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("counters = %+v, want %+v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user