@@ -2,6 +2,8 @@ package verifier
22
33import (
44 "context"
5+ "github.com/rs/zerolog"
6+ "strings"
57 "testing"
68 "time"
79
@@ -198,3 +200,69 @@ func (suite *MultiSourceVersionTestSuite) TestNoStartAtTime() {
198200 suite .Require ().NotNil (verifier .srcStartAtTs )
199201 suite .Require ().LessOrEqual (origStartTs .Compare (* verifier .srcStartAtTs ), 0 )
200202}
203+
204+ func (suite * MultiSourceVersionTestSuite ) TestBatchInsertChangeEventRecheckDocs () {
205+ zerolog .SetGlobalLevel (zerolog .DebugLevel )
206+
207+ verifier := buildVerifier (suite .T (), suite .srcMongoInstance , suite .dstMongoInstance , suite .metaMongoInstance )
208+
209+ ctx := context .Background ()
210+ vCtx , cancel := context .WithCancel (ctx )
211+
212+ // Don't do a checkpoint for this test.
213+ origInterval := minChangeStreamCheckpointInterval
214+ minChangeStreamCheckpointInterval = 10 * time .Hour
215+ defer func () {
216+ minChangeStreamCheckpointInterval = origInterval
217+ }()
218+
219+ err := verifier .StartChangeStream (vCtx )
220+ suite .Require ().NoError (err )
221+
222+ // A large recheck document should be flushed immediately.
223+ _ , err = suite .srcMongoClient .Database ("testDb" ).Collection ("testColl" ).InsertOne (
224+ ctx ,
225+ bson.D {{"_id" , strings .Repeat ("a" , 4 * 1024 * 1024 )}},
226+ )
227+ suite .Require ().NoError (err )
228+ require .Eventually (
229+ suite .T (),
230+ func () bool {
231+ return len (suite .fetchVerifierRechecks (ctx , verifier )) == 1
232+ },
233+ time .Minute ,
234+ 500 * time .Millisecond ,
235+ "the verifier should flush a recheck" ,
236+ )
237+ suite .Require ().Empty (verifier .changeEventRecheckBuf .buf ["testDB.testColl" ])
238+
239+ // A small recheck document should be buffered in-memory.
240+ _ , err = suite .srcMongoClient .Database ("testDb" ).Collection ("testColl" ).InsertOne (
241+ ctx ,
242+ bson.D {{"_id" , 0 }},
243+ )
244+ suite .Require ().NoError (err )
245+ require .Eventually (
246+ suite .T (),
247+ func () bool {
248+ suite .Require ().Len (suite .fetchVerifierRechecks (ctx , verifier ), 1 )
249+ return len (verifier .changeEventRecheckBuf .buf ["testDB.testColl" ]) == 1
250+ },
251+ time .Minute ,
252+ 500 * time .Millisecond ,
253+ "the verifier should buffer a recheck" ,
254+ )
255+
256+ // Any recheck docs remaining in the buffer should be flushed before the change stream reader exits.
257+ cancel ()
258+ require .Eventually (
259+ suite .T (),
260+ func () bool {
261+ return len (suite .fetchVerifierRechecks (ctx , verifier )) == 2
262+ },
263+ time .Minute ,
264+ 500 * time .Millisecond ,
265+ "the verifier should have flushed all recheck docs" ,
266+ )
267+ suite .Require ().Empty (verifier .changeEventRecheckBuf .buf ["testDB.testColl" ])
268+ }
0 commit comments