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

Function TestPrependChanges

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

Source from the content-addressed store, hash-verified

244}
245
246func TestPrependChanges(t *testing.T) {
247
248 base.SetUpTestLogging(t, base.LevelInfo, base.KeyCache)
249
250 db, ctx := setupTestDB(t)
251 defer db.Close(ctx)
252
253 // 1. Test prepend to empty cache
254 stats, err := base.NewSyncGatewayStats()
255 require.NoError(t, err)
256
257 dbstats, err := stats.NewDBStats("", false, false, false, nil, nil)
258 require.NoError(t, err)
259
260 collection := GetSingleDatabaseCollection(t, db.DatabaseContext)
261
262 cache := newSingleChannelCache(collection, channels.NewID("PrependEmptyCache", collection.GetCollectionID()), 0, dbstats.Cache())
263 assert.NotNil(t, cache)
264
265 changesToPrepend := LogEntries{
266 testLogEntry(10, "doc3", "2-a"),
267 testLogEntry(12, "doc2", "2-a"),
268 testLogEntry(14, "doc1", "2-a"),
269 }
270
271 numPrepended := cache.prependChanges(ctx, changesToPrepend, 5, 14)
272 assert.Equal(t, 3, numPrepended)
273
274 // Validate cache
275 validFrom, cachedChanges := cache.GetCachedChanges(getChangesOptionsWithCtxOnly(t))
276 assert.Equal(t, uint64(5), validFrom)
277 require.Len(t, cachedChanges, 3)
278
279 // 2. Test prepend to populated cache, with overlap and duplicates
280 stats, err = base.NewSyncGatewayStats()
281 require.NoError(t, err)
282 dbstats, err = stats.NewDBStats("", false, false, false, nil, nil)
283 require.NoError(t, err)
284 cache = newSingleChannelCache(collection, channels.NewID("PrependPopulatedCache", collection.GetCollectionID()), 0, dbstats.Cache())
285 cache.validFrom = 13
286 cache.addToCache(ctx, testLogEntry(14, "doc1", "2-a"), false)
287 cache.addToCache(ctx, testLogEntry(20, "doc2", "3-a"), false)
288
289 // Prepend
290 changesToPrepend = LogEntries{
291 testLogEntry(10, "doc3", "2-a"),
292 testLogEntry(11, "doc5", "2-a"),
293 testLogEntry(12, "doc2", "2-a"),
294 testLogEntry(14, "doc1", "2-a"),
295 }
296
297 numPrepended = cache.prependChanges(ctx, changesToPrepend, 5, 14)
298 assert.Equal(t, 2, numPrepended)
299
300 // Validate cache
301 validFrom, cachedChanges = cache.GetCachedChanges(getChangesOptionsWithCtxOnly(t))
302 assert.Equal(t, uint64(5), validFrom)
303 require.Len(t, cachedChanges, 4)

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
NewDBStatsMethod · 0.80
CacheMethod · 0.80
prependChangesMethod · 0.80
addToCacheMethod · 0.80

Tested by

no test coverage detected