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

Function TestChannelCacheStats

db/channel_cache_single_test.go:503–580  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

501}
502
503func TestChannelCacheStats(t *testing.T) {
504
505 base.SetUpTestLogging(t, base.LevelInfo, base.KeyCache)
506
507 db, ctx := setupTestDB(t)
508 defer db.Close(ctx)
509
510 stats, err := base.NewSyncGatewayStats()
511 require.NoError(t, err)
512
513 dbstats, err := stats.NewDBStats("", false, false, false, nil, nil)
514 require.NoError(t, err)
515
516 collection := GetSingleDatabaseCollection(t, db.DatabaseContext)
517 collectionID := collection.GetCollectionID()
518
519 testStats := dbstats.Cache()
520 cache := newSingleChannelCache(collection, channels.NewID("Test1", collectionID), 0, testStats)
521
522 // Add some entries to cache
523 cache.addToCache(ctx, testLogEntry(1, "doc1", "1-a"), false)
524 cache.addToCache(ctx, testLogEntry(2, "doc2", "1-a"), false)
525 cache.addToCache(ctx, testLogEntry(3, "doc3", "1-a"), false)
526
527 active, tombstones, removals := getCacheUtilization(testStats)
528 assert.Equal(t, 3, active)
529 assert.Equal(t, 0, tombstones)
530 assert.Equal(t, 0, removals)
531
532 // Update keys already present in the cache, shouldn't modify utilization
533 cache.addToCache(ctx, testLogEntry(4, "doc1", "2-a"), false)
534 cache.addToCache(ctx, testLogEntry(5, "doc2", "2-a"), false)
535 active, tombstones, removals = getCacheUtilization(testStats)
536 assert.Equal(t, 3, active)
537 assert.Equal(t, 0, tombstones)
538 assert.Equal(t, 0, removals)
539
540 // Add a removal rev for a doc not previously in the cache
541 cache.addToCache(ctx, testLogEntry(6, "doc4", "2-a"), true)
542 active, tombstones, removals = getCacheUtilization(testStats)
543 assert.Equal(t, 3, active)
544 assert.Equal(t, 0, tombstones)
545 assert.Equal(t, 1, removals)
546
547 // Add a removal rev for a doc previously in the cache
548 cache.addToCache(ctx, testLogEntry(7, "doc1", "3-a"), true)
549 active, tombstones, removals = getCacheUtilization(testStats)
550 assert.Equal(t, 2, active)
551 assert.Equal(t, 0, tombstones)
552 assert.Equal(t, 2, removals)
553
554 // Add a new tombstone to the cache
555 tombstone := testLogEntry(8, "doc5", "2-a")
556 tombstone.SetDeleted()
557 cache.addToCache(ctx, tombstone, false)
558 active, tombstones, removals = getCacheUtilization(testStats)
559 assert.Equal(t, 2, active)
560 assert.Equal(t, 1, tombstones)

Callers

nothing calls this directly

Calls 15

SetUpTestLoggingFunction · 0.92
NewSyncGatewayStatsFunction · 0.92
NewIDFunction · 0.92
setupTestDBFunction · 0.85
newSingleChannelCacheFunction · 0.85
testLogEntryFunction · 0.85
getCacheUtilizationFunction · 0.85
NewDBStatsMethod · 0.80
CacheMethod · 0.80
addToCacheMethod · 0.80
SetDeletedMethod · 0.80

Tested by

no test coverage detected