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

Function TestUpdateDeltaRevCacheMemoryStat

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

Source from the content-addressed store, hash-verified

823}
824
825func TestUpdateDeltaRevCacheMemoryStat(t *testing.T) {
826 testCases := []struct {
827 name string
828 UseCVCache bool
829 }{
830 {
831 name: "Rev cache pathway",
832 UseCVCache: false,
833 },
834 {
835 name: "CV cache pathway",
836 UseCVCache: true,
837 },
838 }
839 for _, testCase := range testCases {
840 t.Run(testCase.name, func(t *testing.T) {
841 cacheHitCounter, cacheMissCounter, getDocumentCounter, getRevisionCounter, cacheNumItems, memoryBytesCounted := base.SgwIntStat{}, base.SgwIntStat{}, base.SgwIntStat{}, base.SgwIntStat{}, base.SgwIntStat{}, base.SgwIntStat{}
842 backingStoreMap := CreateTestSingleBackingStoreMap(&testBackingStore{nil, &getDocumentCounter, &getRevisionCounter}, testCollectionID)
843 cacheOptions := &RevisionCacheOptions{
844 MaxItemCount: 10,
845 MaxBytes: 140,
846 }
847 cache := NewLRURevisionCache(cacheOptions, backingStoreMap, &cacheHitCounter, &cacheMissCounter, &cacheNumItems, &memoryBytesCounted)
848
849 firstDelta := []byte("delta")
850 secondDelta := []byte("modified delta")
851 thirdDelta := []byte("another delta further modified")
852 ctx := base.TestCtx(t)
853
854 // Trigger load into cache
855 docRev, err := cache.GetWithRev(ctx, "doc1", "1-abc", testCollectionID, RevCacheIncludeDelta)
856 assert.NoError(t, err, "Error adding to cache")
857
858 revCacheMem := memoryBytesCounted.Value()
859 revCacheDelta := newRevCacheDelta(firstDelta, "1-abc", docRev, false, nil)
860 cache.UpdateDelta(ctx, "doc1", "1-abc", testCollectionID, revCacheDelta)
861 // assert that rev cache memory increases by expected amount
862 newMem := revCacheMem + revCacheDelta.totalDeltaBytes
863 assert.Equal(t, newMem, memoryBytesCounted.Value())
864 oldDeltaSize := revCacheDelta.totalDeltaBytes
865
866 newMem = memoryBytesCounted.Value()
867 revCacheDelta = newRevCacheDelta(secondDelta, "1-abc", docRev, false, nil)
868 cache.UpdateDelta(ctx, "doc1", "1-abc", testCollectionID, revCacheDelta)
869
870 // assert the overall memory stat is correctly updated (by the diff between the old delta and the new delta)
871 newMem += revCacheDelta.totalDeltaBytes - oldDeltaSize
872 assert.Equal(t, newMem, memoryBytesCounted.Value())
873
874 revCacheDelta = newRevCacheDelta(thirdDelta, "1-abc", docRev, false, nil)
875 cache.UpdateDelta(ctx, "doc1", "1-abc", testCollectionID, revCacheDelta)
876
877 // assert that eviction took place and as result stat is now 0 (only item in cache was doc1)
878 assert.Equal(t, int64(0), memoryBytesCounted.Value())
879 assert.Equal(t, 0, cache.lruList.Len())
880 })
881 }
882}

Callers

nothing calls this directly

Calls 10

GetWithRevMethod · 0.95
UpdateDeltaMethod · 0.95
TestCtxFunction · 0.92
NewLRURevisionCacheFunction · 0.85
newRevCacheDeltaFunction · 0.85
RunMethod · 0.65
LenMethod · 0.65
ValueMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected