Trigger initialization of the channel cache under load via getChanges. Ensures validFrom handling correctly sets query/cache boundaries
(t *testing.T)
| 1559 | // Trigger initialization of the channel cache under load via getChanges. Ensures validFrom handling correctly |
| 1560 | // sets query/cache boundaries |
| 1561 | func TestInitializeCacheUnderLoad(t *testing.T) { |
| 1562 | |
| 1563 | base.SetUpTestLogging(t, base.LevelInfo, base.KeyChanges) |
| 1564 | |
| 1565 | // Increase the cache max size |
| 1566 | cacheOptions := DefaultCacheOptions() |
| 1567 | cacheOptions.ChannelCacheMaxLength = 50 |
| 1568 | |
| 1569 | db, ctx := setupTestDBWithCacheOptions(t, cacheOptions) |
| 1570 | defer db.Close(ctx) |
| 1571 | collection, ctx := GetSingleDatabaseCollectionWithUser(ctx, t, db) |
| 1572 | collection.ChannelMapper = channels.NewChannelMapper(ctx, channels.DocChannelsSyncFunction, db.Options.JavascriptTimeout) |
| 1573 | |
| 1574 | // Writes [docCount] documents. Use wait group (writesDone)to identify when all docs have been written. |
| 1575 | // Use another waitGroup (writesInProgress) to trigger getChanges midway through writes |
| 1576 | docCount := 1000 |
| 1577 | inProgressCount := 100 |
| 1578 | |
| 1579 | cacheWaiter := db.NewDCPCachingCountWaiter(t) |
| 1580 | cacheWaiter.Add(docCount) |
| 1581 | var writesInProgress sync.WaitGroup |
| 1582 | writesInProgress.Add(inProgressCount) |
| 1583 | |
| 1584 | // Start writing docs |
| 1585 | go func() { |
| 1586 | for i := 0; i < docCount; i++ { |
| 1587 | channels := []string{"zero"} |
| 1588 | body := Body{"serialnumber": int64(i), "channels": channels} |
| 1589 | docID := fmt.Sprintf("loadCache-%d", i) |
| 1590 | _, _, err := collection.Put(ctx, docID, body) |
| 1591 | require.NoError(t, err, "Couldn't create document") |
| 1592 | if i < inProgressCount { |
| 1593 | writesInProgress.Done() |
| 1594 | } |
| 1595 | } |
| 1596 | }() |
| 1597 | |
| 1598 | // Wait for writes to be in progress, then getChanges for channel zero |
| 1599 | writesInProgress.Wait() |
| 1600 | changes := getChanges(t, collection, channels.BaseSetOf(t, "zero"), getChangesOptionsWithCtxOnly(t)) |
| 1601 | firstChangesCount := len(changes) |
| 1602 | var lastSeq SequenceID |
| 1603 | if firstChangesCount > 0 { |
| 1604 | lastSeq = changes[len(changes)-1].Seq |
| 1605 | } |
| 1606 | |
| 1607 | // Wait for all writes to be cached, then getChanges again |
| 1608 | cacheWaiter.Wait() |
| 1609 | |
| 1610 | changes = getChanges(t, collection, channels.BaseSetOf(t, "zero"), getChangesOptionsWithSeq(t, lastSeq)) |
| 1611 | secondChangesCount := len(changes) |
| 1612 | assert.Equal(t, docCount, firstChangesCount+secondChangesCount) |
| 1613 | |
| 1614 | } |
| 1615 | |
| 1616 | // Verify that notifyChangeFunc for channel zero is sent even when the channel isn't active in the cache. |
| 1617 | func TestNotifyForInactiveChannel(t *testing.T) { |
nothing calls this directly
no test coverage detected