(t *testing.T)
| 244 | } |
| 245 | |
| 246 | func 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) |
nothing calls this directly
no test coverage detected