MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / TestLRURevisionCacheEvictionMemoryBased

Function TestLRURevisionCacheEvictionMemoryBased

db/revision_cache_test.go:280–377  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

278}
279
280func TestLRURevisionCacheEvictionMemoryBased(t *testing.T) {
281 testCases := []struct {
282 name string
283 UseCVCache bool
284 }{
285 {
286 name: "Rev cache pathway",
287 UseCVCache: false,
288 },
289 {
290 name: "CV cache pathway",
291 UseCVCache: true,
292 },
293 }
294 for _, testCase := range testCases {
295 t.Run(testCase.name, func(t *testing.T) {
296 dbcOptions := DatabaseContextOptions{
297 RevisionCacheOptions: &RevisionCacheOptions{
298 MaxBytes: 725,
299 MaxItemCount: 10,
300 },
301 }
302 db, ctx := SetupTestDBWithOptions(t, dbcOptions)
303 defer db.Close(ctx)
304 collection, ctx := GetSingleDatabaseCollectionWithUser(ctx, t, db)
305 cacheStats := db.DbStats.Cache()
306
307 smallBody := Body{
308 "channels": "_default", // add channel for default sync func in default collection test runs
309 }
310
311 var currMem, expValue, revZeroSize int64
312 var rev1Version *Version
313 for i := 0; i < 10; i++ {
314 currMem = cacheStats.RevisionCacheTotalMemory.Value()
315 revSize, _, docVersion := createDocAndReturnSizeAndRev(t, ctx, fmt.Sprint(i), collection, smallBody)
316 if i == 0 {
317 revZeroSize = int64(revSize)
318 }
319 if i == 1 {
320 rev1Version = docVersion
321 }
322 expValue = currMem + int64(revSize)
323 assert.Equal(t, expValue, cacheStats.RevisionCacheTotalMemory.Value())
324 }
325
326 // test eviction by number of items (adding new doc from createDocAndReturnSizeAndRev shouldn't take memory over threshold defined as 730 bytes)
327 expValue -= revZeroSize // for doc being evicted
328 docSize, rev, _ := createDocAndReturnSizeAndRev(t, ctx, fmt.Sprint(11), collection, smallBody)
329 expValue += int64(docSize)
330 // assert doc 0 been evicted
331 docRev, ok := db.revisionCache.Peek(ctx, "0", rev, collection.GetCollectionID())
332 assert.False(t, ok)
333 assert.Nil(t, docRev.BodyBytes)
334
335 currMem = cacheStats.RevisionCacheTotalMemory.Value()
336 // assert total memory is as expected
337 assert.Equal(t, expValue, currMem)

Callers

nothing calls this directly

Calls 13

SetupTestDBWithOptionsFunction · 0.85
CacheMethod · 0.80
RunMethod · 0.65
CloseMethod · 0.65
PeekMethod · 0.65
RemoveWithCVMethod · 0.65
RemoveWithRevMethod · 0.65
PutMethod · 0.65
ValueMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected