MCPcopy
hub / github.com/kopia/kopia / verifyCacheExpiration

Function verifyCacheExpiration

internal/cache/content_cache_test.go:93–150  ·  view source on GitHub ↗

The test will fetch 4 items into the cache, named "a", "b", "c", "d", each 4000 bytes in size verify that the cache is evicting correct items based on the sweep settings. nolint:thelper

(t *testing.T, sweepSettings cache.SweepSettings, wantEvicted []blob.ID)

Source from the content-addressed store, hash-verified

91//
92//nolint:thelper
93func verifyCacheExpiration(t *testing.T, sweepSettings cache.SweepSettings, wantEvicted []blob.ID) {
94 cacheData := blobtesting.DataMap{}
95
96 // on Windows, the time does not always move forward (sometimes clock.Now() returns exactly the same value for consecutive invocations)
97 // this matters here so we return a fake clock.Now() function that always moves forward.
98 var currentTimeMutex sync.Mutex
99
100 currentTime := clock.Now()
101
102 movingTimeFunc := func() time.Time {
103 currentTimeMutex.Lock()
104 defer currentTimeMutex.Unlock()
105
106 currentTime = currentTime.Add(1 * time.Millisecond)
107
108 return currentTime
109 }
110
111 cacheStorage := testutil.EnsureType[cache.Storage](t, blobtesting.NewMapStorage(cacheData, nil, movingTimeFunc))
112 underlyingStorage := newUnderlyingStorageForContentCacheTesting(t)
113
114 ctx := testlogging.Context(t)
115 cc, err := cache.NewContentCache(ctx, underlyingStorage, cache.Options{
116 Storage: cacheStorage,
117 Sweep: sweepSettings,
118 TimeNow: movingTimeFunc,
119 }, nil)
120
121 require.NoError(t, err)
122
123 defer cc.Close(ctx)
124
125 var tmp gather.WriteBuffer
126 defer tmp.Close()
127
128 const underlyingBlobID = "content-4k"
129
130 err = cc.GetContent(ctx, "a", underlyingBlobID, 0, -1, &tmp) // 4k
131 require.NoError(t, err)
132 err = cc.GetContent(ctx, "b", underlyingBlobID, 0, -1, &tmp) // 4k
133 require.NoError(t, err)
134 err = cc.GetContent(ctx, "c", underlyingBlobID, 0, -1, &tmp) // 4k
135 require.NoError(t, err)
136 err = cc.GetContent(ctx, "d", underlyingBlobID, 0, -1, &tmp) // 4k
137 require.NoError(t, err)
138
139 // delete underlying storage blob to identify cache items that have been evicted
140 // all other items will be fetched from the cache.
141 require.NoError(t, underlyingStorage.DeleteBlob(ctx, underlyingBlobID))
142
143 for _, blobID := range []blob.ID{"a", "b", "c", "d"} {
144 if slices.Contains(wantEvicted, blobID) {
145 require.ErrorIs(t, cc.GetContent(ctx, string(blobID), underlyingBlobID, 0, -1, &tmp), blob.ErrBlobNotFound, "expected item not found %v", blobID)
146 } else {
147 require.NoError(t, cc.GetContent(ctx, string(blobID), underlyingBlobID, 0, -1, &tmp), "expected item to be found %v", blobID)
148 }
149 }
150}

Calls 14

CloseMethod · 0.95
CloseMethod · 0.95
GetContentMethod · 0.95
NowFunction · 0.92
EnsureTypeFunction · 0.92
NewMapStorageFunction · 0.92
ContextFunction · 0.92
NewContentCacheFunction · 0.92
LockMethod · 0.65
UnlockMethod · 0.65
DeleteBlobMethod · 0.65

Tested by

no test coverage detected