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

Function TestRemoveFromRevLookup

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

Source from the content-addressed store, hash-verified

2345}
2346
2347func TestRemoveFromRevLookup(t *testing.T) {
2348 cacheHitCounter, cacheMissCounter, cacheNumItems, memoryBytesCounted := base.SgwIntStat{}, base.SgwIntStat{}, base.SgwIntStat{}, base.SgwIntStat{}
2349 backingStoreMap := CreateTestSingleBackingStoreMap(&noopBackingStore{}, testCollectionID)
2350 cacheOptions := &RevisionCacheOptions{
2351 MaxItemCount: 10,
2352 MaxBytes: 0,
2353 }
2354 cache := NewLRURevisionCache(cacheOptions, backingStoreMap, &cacheHitCounter, &cacheMissCounter, &cacheNumItems, &memoryBytesCounted)
2355
2356 ctx := base.TestCtx(t)
2357
2358 // Fill up the rev cache with the first 10 docs
2359 for docID := 0; docID < 10; docID++ {
2360 id := strconv.Itoa(docID)
2361 vrs := uint64(docID)
2362 cache.Put(ctx, DocumentRevision{BodyBytes: []byte(`{}`), DocID: id, RevID: "1-abc", CV: &Version{Value: vrs, SourceID: "test"}, History: Revisions{"start": 1}}, testCollectionID)
2363 }
2364 assert.Equal(t, int64(10), cacheNumItems.Value())
2365 assert.Equal(t, int64(20), memoryBytesCounted.Value())
2366 assert.Equal(t, 10, len(cache.cache))
2367 assert.Equal(t, 10, len(cache.hlvCache))
2368
2369 // simulate a user xattr update:
2370 // 1. Removes form rev lookup cache
2371 // 2. Enter new entry with same revID and docID but diff CV
2372 // 3. Assert that eviction eventually aligns the cache items in each lookup map
2373 cache.RemoveRevOnly(ctx, "1", "1-abc", testCollectionID)
2374 assert.Equal(t, int64(10), cacheNumItems.Value())
2375 assert.Equal(t, int64(20), memoryBytesCounted.Value())
2376 assert.Equal(t, 10, cache.lruList.Len())
2377 assert.Equal(t, 9, len(cache.cache))
2378 assert.Equal(t, 10, len(cache.hlvCache))
2379
2380 // add new entry to cache for docID 1 with a different CV but same revID
2381 cache.Put(ctx, DocumentRevision{BodyBytes: []byte(`{}`), DocID: "1", RevID: "1-abc", CV: &Version{Value: 1234, SourceID: "test"}, History: Revisions{"start": 1}}, testCollectionID)
2382 assert.Equal(t, int64(10), cacheNumItems.Value())
2383 assert.Equal(t, int64(20), memoryBytesCounted.Value())
2384 assert.Equal(t, 10, cache.lruList.Len())
2385 assert.Equal(t, 9, len(cache.cache)) // we should have 9 items in the rev lookup as we removed one item above
2386 assert.Equal(t, 10, len(cache.hlvCache))
2387
2388 // add new doc to trigger eviction of old doc "1" value in the cache that has entry in hlv cache to an item but the revID lookup
2389 // for this item will be to a different value (given the simulation of user xattr update above). This means that this
2390 // will trigger an eviction from CV lookup but no revID lookup thus aligning the lookup maps once again.
2391 cache.Put(ctx, DocumentRevision{BodyBytes: []byte(`{}`), DocID: "someNewDoc", RevID: "1-abc", CV: &Version{Value: 123456, SourceID: "test"}, History: Revisions{"start": 1}}, testCollectionID)
2392 assert.Equal(t, int64(10), cacheNumItems.Value())
2393 assert.Equal(t, int64(20), memoryBytesCounted.Value())
2394 assert.Equal(t, 10, cache.lruList.Len())
2395 assert.Equal(t, 10, len(cache.cache)) // we should now have 10 items in rev lookup given the item above aligned the lookups after eviction
2396 assert.Equal(t, 10, len(cache.hlvCache))
2397}
2398
2399func TestLoadFromBucketLegacyRevsThatAreBackedUpPreUpgrade(t *testing.T) {
2400 db, ctx := SetupTestDBWithOptions(t, DatabaseContextOptions{

Callers

nothing calls this directly

Calls 8

PutMethod · 0.95
RemoveRevOnlyMethod · 0.95
TestCtxFunction · 0.92
NewLRURevisionCacheFunction · 0.85
LenMethod · 0.65
EqualMethod · 0.45
ValueMethod · 0.45

Tested by

no test coverage detected