package imgcache import ( "testing" ) // TestComputeDefaultMaxBytesCountsWhatTheCacheHolds verifies that the // default limit is 75% of the free space plus what the cache already // holds, so a cache filled to its limit keeps that limit across a // restart instead of shrinking to 75% of the space left free. func TestComputeDefaultMaxBytesCountsWhatTheCacheHolds(t *testing.T) { t.Parallel() cache, _ := newEvictionTestCache(t, 1<<30) // Empty cache, 4 GiB free -> 3 GiB default. got, err := cache.computeDefaultMaxBytes(t.Context(), func(string) (uint64, error) { return 4294967296, nil }) if err != nil { t.Fatalf("computeDefaultMaxBytes returned error: %v", err) } t.Logf("default for an empty cache with 4 GiB free: %d", got) if got != 3221225472 { t.Errorf("default for an empty cache = %d, want 3221225472 (75%% of 4 GiB)", got) } // The cache now holds those 3 GiB, which leaves 1 GiB free. _, err = cache.db.ExecContext(t.Context(), `INSERT INTO variant_content (cache_key, size_bytes, content_type) VALUES (?, ?, ?)`, string(testVariantKeyOne), 3221225472, testContentTypeWebP, ) if err != nil { t.Fatalf("failed to insert variant accounting row: %v", err) } got, err = cache.computeDefaultMaxBytes(t.Context(), func(string) (uint64, error) { return 1073741824, nil }) if err != nil { t.Fatalf("computeDefaultMaxBytes returned error: %v", err) } t.Logf("default for a cache holding 3 GiB with 1 GiB free: %d", got) if got != 3221225472 { t.Errorf("default for a cache holding 3 GiB with 1 GiB free = %d, "+ "want 3221225472 (75%% of 1 GiB + 3 GiB)", got) } }