TestFileCache_atomicWriteLeavesNoTempFiles verifies that the rename-based atomic write does not leak temporary files on the happy path. Only the JSON file and the persistent .lock sentinel are expected to remain.
(t *testing.T)
| 219 | // atomic write does not leak temporary files on the happy path. Only the |
| 220 | // JSON file and the persistent .lock sentinel are expected to remain. |
| 221 | func TestFileCache_atomicWriteLeavesNoTempFiles(t *testing.T) { |
| 222 | t.Parallel() |
| 223 | dir := t.TempDir() |
| 224 | path := filepath.Join(dir, "cache.json") |
| 225 | |
| 226 | c, err := New(Config{Enabled: true, Path: path}) |
| 227 | require.NoError(t, err) |
| 228 | |
| 229 | for i := range 5 { |
| 230 | c.Store(fmt.Sprintf("q%d", i), fmt.Sprintf("a%d", i)) |
| 231 | } |
| 232 | |
| 233 | entries, err := os.ReadDir(dir) |
| 234 | require.NoError(t, err) |
| 235 | |
| 236 | expected := map[string]bool{ |
| 237 | "cache.json": true, // the data file |
| 238 | "cache.json.lock": true, // the cross-process advisory lock |
| 239 | } |
| 240 | for _, e := range entries { |
| 241 | assert.True(t, expected[e.Name()], |
| 242 | "unexpected leftover in cache directory: %q", e.Name()) |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | // TestFileCache_concurrentStoreNeverYieldsTornFile verifies that concurrent |
| 247 | // Store calls always leave a fully valid JSON file behind — i.e. a parallel |