MCPcopy
hub / github.com/docker/docker-agent / TestFileCache_atomicWriteLeavesNoTempFiles

Function TestFileCache_atomicWriteLeavesNoTempFiles

pkg/cache/cache_test.go:221–244  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
221func 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

Callers

nothing calls this directly

Calls 3

NewFunction · 0.70
NameMethod · 0.65
StoreMethod · 0.45

Tested by

no test coverage detected