MCPcopy Index your code
hub / github.com/docker/docker-agent / TestFileCache_dedupSkipsRedundantWrite

Function TestFileCache_dedupSkipsRedundantWrite

pkg/cache/cache_test.go:95–120  ·  view source on GitHub ↗

TestFileCache_dedupSkipsRedundantWrite verifies that storing the exact same (question, response) pair twice is treated as a no-op, so the underlying JSON file is rewritten only on the first Store. This is what keeps cache replays free of redundant disk traffic.

(t *testing.T)

Source from the content-addressed store, hash-verified

93// underlying JSON file is rewritten only on the first Store. This is
94// what keeps cache replays free of redundant disk traffic.
95func TestFileCache_dedupSkipsRedundantWrite(t *testing.T) {
96 t.Parallel()
97 dir := t.TempDir()
98 path := filepath.Join(dir, "cache.json")
99
100 c, err := New(Config{Enabled: true, Path: path})
101 require.NoError(t, err)
102
103 c.Store("q", "a")
104 infoBefore, err := os.Stat(path)
105 require.NoError(t, err)
106
107 // Same pair: must not rewrite the file (mtime stays the same).
108 c.Store("q", "a")
109 infoAfter, err := os.Stat(path)
110 require.NoError(t, err)
111 assert.Equal(t, infoBefore.ModTime(), infoAfter.ModTime(),
112 "identical Store must not rewrite the cache file")
113
114 // Different value: must rewrite.
115 c.Store("q", "b")
116 infoChanged, err := os.Stat(path)
117 require.NoError(t, err)
118 assert.True(t, infoChanged.ModTime().After(infoBefore.ModTime()) || infoChanged.Size() != infoBefore.Size(),
119 "different Store must rewrite the cache file")
120}
121
122func TestFileCache_persists(t *testing.T) {
123 t.Parallel()

Callers

nothing calls this directly

Calls 3

NewFunction · 0.70
StoreMethod · 0.45
SizeMethod · 0.45

Tested by

no test coverage detected