Skip to content

Commit 1e76182

Browse files
GODRIVER-2992 Add RemainingBatchLength to ChangeStream (#1626)
Co-authored-by: Preston Vasquez <[email protected]>
1 parent 7902daf commit 1e76182

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

mongo/change_stream.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,12 @@ func (cs *ChangeStream) ID() int64 {
531531
return cs.cursor.ID()
532532
}
533533

534+
// RemainingBatchLength returns the number of documents left in the current batch. If this returns zero, the subsequent
535+
// call to Next or TryNext will do a network request to fetch the next batch.
536+
func (cs *ChangeStream) RemainingBatchLength() int {
537+
return len(cs.batch)
538+
}
539+
534540
// SetBatchSize sets the number of documents to fetch from the database with
535541
// each iteration of the ChangeStream's "Next" or "TryNext" method. This setting
536542
// only affects subsequent document batches fetched from the database.

mongo/integration/change_stream_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func TestChangeStream_ReplicaSet(t *testing.T) {
9999
// cause an event to occur so the resume token is updated
100100
generateEvents(mt, 1)
101101
assert.True(mt, cs.Next(context.Background()), "expected next to return true, got false")
102+
assert.Equal(mt, 0, cs.RemainingBatchLength())
102103
firstToken := cs.ResumeToken()
103104

104105
// cause an event on a different collection than the one being watched so the server's PBRT is updated
@@ -374,6 +375,7 @@ func TestChangeStream_ReplicaSet(t *testing.T) {
374375

375376
// Iterate over one event to get resume token
376377
assert.True(mt, cs.Next(context.Background()), "expected Next to return true, got false")
378+
assert.Equal(mt, numEvents-1, cs.RemainingBatchLength())
377379
token := cs.ResumeToken()
378380
closeStream(cs)
379381

0 commit comments

Comments
 (0)