Tests the eviction from the LRURevisionCache
(t *testing.T)
| 130 | |
| 131 | // Tests the eviction from the LRURevisionCache |
| 132 | func TestLRURevisionCacheEviction(t *testing.T) { |
| 133 | cacheHitCounter, cacheMissCounter, cacheNumItems, memoryBytesCounted := base.SgwIntStat{}, base.SgwIntStat{}, base.SgwIntStat{}, base.SgwIntStat{} |
| 134 | backingStoreMap := CreateTestSingleBackingStoreMap(&noopBackingStore{}, testCollectionID) |
| 135 | cacheOptions := &RevisionCacheOptions{ |
| 136 | MaxItemCount: 10, |
| 137 | MaxBytes: 0, |
| 138 | } |
| 139 | cache := NewLRURevisionCache(cacheOptions, backingStoreMap, &cacheHitCounter, &cacheMissCounter, &cacheNumItems, &memoryBytesCounted) |
| 140 | |
| 141 | ctx := base.TestCtx(t) |
| 142 | |
| 143 | // Fill up the rev cache with the first 10 docs |
| 144 | for docID := 0; docID < 10; docID++ { |
| 145 | id := strconv.Itoa(docID) |
| 146 | vrs := uint64(docID) |
| 147 | cache.Put(ctx, DocumentRevision{BodyBytes: []byte(`{}`), DocID: id, RevID: "1-abc", CV: &Version{Value: vrs, SourceID: "test"}, History: Revisions{"start": 1}}, testCollectionID) |
| 148 | } |
| 149 | assert.Equal(t, int64(10), cacheNumItems.Value()) |
| 150 | assert.Equal(t, int64(20), memoryBytesCounted.Value()) |
| 151 | assert.Equal(t, 10, len(cache.cache)) |
| 152 | assert.Equal(t, 10, len(cache.hlvCache)) |
| 153 | |
| 154 | // Get them back out |
| 155 | for i := 0; i < 10; i++ { |
| 156 | docID := strconv.Itoa(i) |
| 157 | docRev, err := cache.GetWithRev(ctx, docID, "1-abc", testCollectionID, RevCacheOmitDelta) |
| 158 | assert.NoError(t, err) |
| 159 | assert.NotNil(t, docRev.BodyBytes, "nil body for %s", docID) |
| 160 | assert.Equal(t, docID, docRev.DocID) |
| 161 | assert.Equal(t, int64(0), cacheMissCounter.Value()) |
| 162 | assert.Equal(t, int64(i+1), cacheHitCounter.Value()) |
| 163 | } |
| 164 | assert.Equal(t, int64(10), cacheNumItems.Value()) |
| 165 | assert.Equal(t, int64(20), memoryBytesCounted.Value()) |
| 166 | assert.Equal(t, 10, len(cache.cache)) |
| 167 | assert.Equal(t, 10, len(cache.hlvCache)) |
| 168 | |
| 169 | // Add 3 more docs to the now full revcache |
| 170 | for i := 10; i < 13; i++ { |
| 171 | docID := strconv.Itoa(i) |
| 172 | vrs := uint64(i) |
| 173 | cache.Put(ctx, DocumentRevision{BodyBytes: []byte(`{}`), DocID: docID, RevID: "1-abc", CV: &Version{Value: vrs, SourceID: "test"}, History: Revisions{"start": 1}}, testCollectionID) |
| 174 | } |
| 175 | assert.Equal(t, int64(10), cacheNumItems.Value()) |
| 176 | assert.Equal(t, int64(20), memoryBytesCounted.Value()) |
| 177 | assert.Equal(t, 10, len(cache.cache)) |
| 178 | assert.Equal(t, 10, len(cache.hlvCache)) |
| 179 | |
| 180 | // Check that the first 3 docs were evicted |
| 181 | prevCacheHitCount := cacheHitCounter.Value() |
| 182 | for i := 0; i < 3; i++ { |
| 183 | docID := strconv.Itoa(i) |
| 184 | docRev, ok := cache.Peek(ctx, docID, "1-abc", testCollectionID) |
| 185 | assert.False(t, ok) |
| 186 | assert.Nil(t, docRev.BodyBytes) |
| 187 | assert.Equal(t, int64(0), cacheMissCounter.Value()) // peek incurs no cache miss if not found |
| 188 | assert.Equal(t, prevCacheHitCount, cacheHitCounter.Value()) |
| 189 | } |
nothing calls this directly
no test coverage detected