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

Function TestShardedMemoryEviction

db/revision_cache_test.go:962–1007  ·  view source on GitHub ↗

TestShardedMemoryEviction: - Test adding a doc to each shard in the test - Assert that each shard has individual count for memory usage as expected - Add new doc that will take over the shard memory capacity and assert that that eviction takes place and all stats are as expected

(t *testing.T)

Source from the content-addressed store, hash-verified

960// - Add new doc that will take over the shard memory capacity and assert that that eviction takes place and
961// all stats are as expected
962func TestShardedMemoryEviction(t *testing.T) {
963 dbcOptions := DatabaseContextOptions{
964 RevisionCacheOptions: &RevisionCacheOptions{
965 MaxBytes: 160,
966 MaxItemCount: 10,
967 ShardCount: 2,
968 },
969 }
970 db, ctx := SetupTestDBWithOptions(t, dbcOptions)
971 defer db.Close(ctx)
972 collection, ctx := GetSingleDatabaseCollectionWithUser(ctx, t, db)
973 cacheStats := db.DbStats.Cache()
974
975 docBody := Body{
976 "channels": "_default",
977 }
978
979 // add doc that will be added to one shard
980 size, _, _ := createDocAndReturnSizeAndRev(t, ctx, "doc1", collection, docBody)
981 assert.Equal(t, int64(size), cacheStats.RevisionCacheTotalMemory.Value())
982 // grab this particular shard + assert that the shard memory usage is as expected
983 shardedCache := db.revisionCache.(*ShardedLRURevisionCache)
984 doc1Shard := shardedCache.getShard("doc1")
985 assert.Equal(t, int64(size), doc1Shard.currMemoryUsage.Value())
986
987 // add new doc in diff shard + assert that the shard memory usage is as expected
988 size, _, _ = createDocAndReturnSizeAndRev(t, ctx, "doc2", collection, docBody)
989 doc2Shard := shardedCache.getShard("doc2")
990 assert.Equal(t, int64(size), doc2Shard.currMemoryUsage.Value())
991 // overall mem usage should be combination oif the two added docs
992 assert.Equal(t, int64(size*2), cacheStats.RevisionCacheTotalMemory.Value())
993
994 // two docs should reside in cache at this time
995 assert.Equal(t, int64(2), cacheStats.RevisionCacheNumItems.Value())
996
997 docBody = Body{
998 "channels": "_default",
999 "some": "field",
1000 }
1001 // add new doc to trigger eviction and assert stats are as expected
1002 newDocSize, _, _ := createDocAndReturnSizeAndRev(t, ctx, "doc3", collection, docBody)
1003 doc3Shard := shardedCache.getShard("doc3")
1004 assert.Equal(t, int64(newDocSize), doc3Shard.currMemoryUsage.Value())
1005 assert.Equal(t, int64(2), cacheStats.RevisionCacheNumItems.Value())
1006 assert.Equal(t, int64(size+newDocSize), cacheStats.RevisionCacheTotalMemory.Value())
1007}
1008
1009// TestShardedMemoryEvictionWhenShardEmpty:
1010// - Test adding a doc to sharded revision cache that will immediately be evicted due to size

Callers

nothing calls this directly

Calls 8

SetupTestDBWithOptionsFunction · 0.85
CacheMethod · 0.80
getShardMethod · 0.80
CloseMethod · 0.65
EqualMethod · 0.45
ValueMethod · 0.45

Tested by

no test coverage detected