Skip to content

Commit 53dc4c0

Browse files
author
iwysiu
committed
GODRIVER-956 fix legacyGetMore
Change-Id: I28a7702b5f00f6de99cab45e9780c43574cc5b58
1 parent d6e987e commit 53dc4c0

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

mongo/collection_internal_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,38 @@ func TestCollection_Find_found(t *testing.T) {
16721672
require.Equal(t, results, []int{1, 2, 3, 4, 5})
16731673
}
16741674

1675+
func TestCollection_Find_With_Limit_And_BatchSize(t *testing.T) {
1676+
if testing.Short() {
1677+
t.Skip("skipping integration test in short mode")
1678+
}
1679+
1680+
coll := createTestCollection(t, nil, nil)
1681+
initCollection(t, coll)
1682+
1683+
batchSizes := []int32{2, 3, 4}
1684+
for _, b := range batchSizes {
1685+
name := fmt.Sprintf("Limit: 3, BatchSize: %v", b)
1686+
t.Run(name, func(t *testing.T) {
1687+
cursor, err := coll.Find(context.Background(), bson.D{}, options.Find().SetLimit(3).SetBatchSize(b))
1688+
1689+
numRecieved := 0
1690+
var doc bson.Raw
1691+
for cursor.Next(context.Background()) {
1692+
err = cursor.Decode(&doc)
1693+
require.NoError(t, err)
1694+
1695+
_, err = doc.LookupErr("_id")
1696+
require.NoError(t, err)
1697+
1698+
numRecieved++
1699+
}
1700+
1701+
require.NoError(t, cursor.Err())
1702+
require.Equal(t, 3, numRecieved)
1703+
})
1704+
}
1705+
}
1706+
16751707
func TestCollection_Find_notFound(t *testing.T) {
16761708
if testing.Short() {
16771709
t.Skip("skipping integration test in short mode")

x/mongo/driver/batch_cursor.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,13 @@ func (bc *BatchCursor) legacyGetMore(ctx context.Context) {
342342
numToReturn := bc.batchSize
343343
if bc.limit != 0 && bc.numReturned+bc.batchSize > bc.limit {
344344
numToReturn = bc.limit - bc.numReturned
345+
if numToReturn <= 0 {
346+
err = bc.Close(ctx)
347+
if err != nil {
348+
bc.err = err
349+
}
350+
return
351+
}
345352
}
346353
gm := wiremessage.GetMore{
347354
FullCollectionName: bc.namespace.DB + "." + bc.namespace.Collection,

0 commit comments

Comments
 (0)