Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates Sync Gateway’s test utilities and tests to eliminate data races when flushing the channel/change cache by stopping the caching DCP feed before clearing caches, and centralizes the flush helper at the database level.
Changes:
- Introduces
DatabaseContext.RestartChangeListener/DatabaseContext.FlushChannelCachetest helpers indb/util_testing.go, and removes the olderRestartListener/collection-level flush helpers. - Updates multiple REST/replicator tests to call the new database-level
FlushChannelCache(t)helper. - Skips
TestChangesLoopingWhenLowSequencewith a temporaryt.Skip("FIXME")marker.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| rest/replicatortest/replicator_test.go | Switches cache flush usage to database-level test helper. |
| rest/changestest/changes_api_test.go | Uses new FlushChannelCache(t) helper in many tests; adds a skip for one test. |
| rest/blip_api_crud_test.go | Uses new FlushChannelCache(t) helper. |
| rest/api_test.go | Switches listener restart to RestartChangeListener(t, false). |
| db/util_testing.go | Adds test helper APIs to stop/restart listener and flush caches safely. |
| db/database.go | Removes the old testing-only RestartListener method. |
| db/database_collection.go | Removes the collection-level FlushChannelCache helper. |
01d5646 to
6b174bd
Compare
gregns1
requested changes
May 27, 2026
Contributor
gregns1
left a comment
There was a problem hiding this comment.
LGTM, just one small change then ready I think
Contributor
There was a problem hiding this comment.
This comment I think needs updating now?
gregns1
approved these changes
May 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
CBG-5375 fix FlushChannelCache test data races
RestartListenerto db/util_testing.go so it could directly taketesting.TB. This function is moved verbatim except removing the unnecessary sleep.FlushChannelCacheto Database level, since this does not flush a collection level channel cache.The data race here occurs when flushing the cache resets
c.initialSequenceandprocessPrincipalDocumenthitssync_gateway/db/change_cache.go
Line 749 in e75b7c2
c.initialSequence. This is reproducible withSG_TEST_CACHING_FEED_DELAY=10msor some other non zero values. When I tried to fix one of the tests, I discovered there are issues with all tests that calledFlushChannelCache.Initially I thought that a
WaitForPendingSequencesafter creating a user would address the problem, but it is more subtle than this.Given a user "bernard" with access to "ch1", "ch2", "ch3"
If a user is created directly using an authenticator object, using:
the output would look like this:
This isn't allocated a sequence correctly, so waiting for a sequence isn't right because it would never come so a
WaitForPendingSequencewouldn't happen.If the instead the user is created using
rt.CreateUser("bernard", []string{"ch1", "ch2", "ch3"}then the object will look like this, with sequence=1 and cas=1:Naively, you might think that you could
WaitForPendingSequenceand have the DCP waiter find the principal document. However, this principal document gets mutated on the first login ofbernardto have cas=2 but sequence=1, in order to turnchannel_inval_seqtoall_channels.This means that we can't wait for sequence 1, since we already saw this with cas=1, but we can't be sure that cas=2 document will hit. So
WaitForPendingSequencesdoesn't solve this problem. TheDCPReceivedCountstat isn't incremented for principal documentssync_gateway/db/change_cache.go
Line 446 in f36131e
Since I don't think it is necessary to change the code for the authenticator, I solve both problems by stopping the DCP feed before flushing the cache, so that we know that second DCP entry isn't active. In these tests, it is expected that all the DCP events were received. This is a test-only change that preserves the functionality of the tests without modifying any other code.
Pre-review checklist
fmt.Print,log.Print, ...)base.UD(docID),base.MD(dbName))docs/apiIntegration Tests