TestPutStreamOverwrite verifies that re-streaming a key replaces it without double-counting currentSize or leaving a duplicate LRU entry (finding #2).
(t *testing.T)
| 278 | func TestDiskCacheStartupCountsCommittedBytesAsReclaimable(t *testing.T) { |
| 279 | dir := t.TempDir() |
| 280 | keys := []string{strings.Repeat("a", 64), strings.Repeat("b", 64)} |
| 281 | for _, key := range keys { |
| 282 | if err := os.WriteFile(filepath.Join(dir, key), make([]byte, 400), 0600); err != nil { |
| 283 | t.Fatal(err) |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | c, err := NewDiskCache(dir, 80, DiskCacheOptions{ |
| 288 | MaxEntries: 2, |
| 289 | CapacityProvider: func(string) (diskSpace, error) { |
| 290 | // The disk is 80% full only because of the two committed cache files. |
| 291 | return diskSpace{TotalBytes: 1000, FreeBytes: 200}, nil |
| 292 | }, |
| 293 | }) |
| 294 | if err != nil { |
| 295 | t.Fatal(err) |
| 296 | } |
| 297 | if c.currentSize != 800 || c.maxBytes != 800 { |
| 298 | t.Fatalf("startup cache size/capacity = %d/%d, want 800/800; committed entries must not be mistaken for external disk use", c.currentSize, c.maxBytes) |
| 299 | } |
| 300 | for _, key := range keys { |
| 301 | if !c.Has(key) { |
| 302 | t.Fatalf("startup pruned committed cache key %s", key) |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | func TestDiskCacheScanSeparatesTemporaryInvalidAndCommittedFiles(t *testing.T) { |
| 308 | dir := t.TempDir() |
| 309 | key := strings.Repeat("c", 64) |
| 310 | if err := os.WriteFile(filepath.Join(dir, key), []byte("committed"), 0600); err != nil { |
| 311 | t.Fatal(err) |
nothing calls this directly
no test coverage detected