-
Notifications
You must be signed in to change notification settings - Fork 895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GODRIVER-1831 Avoid accidental extra getMore when using limit on Find #562
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -883,6 +883,20 @@ func TestCollection(t *testing.T) { | |
_, ok := err.(mongo.CommandError) | ||
assert.True(mt, ok, "expected error type %v, got %v", mongo.CommandError{}, err) | ||
}) | ||
mt.Run("unset batch size does not surpass limit", func(mt *mtest.T) { | ||
initCollection(mt, mt.Coll) | ||
for _, limit := range []int64{2, 3, 4} { | ||
opts := options.Find().SetSkip(0).SetLimit(limit) | ||
cursor, err := mt.Coll.Find(mtest.Background, bson.D{}, opts) | ||
assert.Nil(mt, err, "Find error with limit: %v", err) | ||
|
||
var docs []interface{} | ||
err = cursor.All(mtest.Background, &docs) | ||
assert.Nil(mt, err, "All error with limit: %v", err) | ||
|
||
assert.Equal(mt, int(limit), len(docs), "expected number of docs to be %v, got %v", limit, len(docs)) | ||
} | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that this is a bit tricky to reason about, I suggest:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added both! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! |
||
}) | ||
mt.RunOpts("find one", noClientOpts, func(mt *mtest.T) { | ||
mt.Run("limit", func(mt *mtest.T) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -241,7 +241,7 @@ func (bc *BatchCursor) getMore(ctx context.Context) { | |
|
||
// Required for legacy operations which don't support limit. | ||
numToReturn := bc.batchSize | ||
if bc.limit != 0 && bc.numReturned+bc.batchSize > bc.limit { | ||
if bc.limit != 0 && bc.numReturned+bc.batchSize >= bc.limit { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on my reading of https://github.com/mongodb/specifications/blob/master/source/crud/crud.rst#combining-limit-and-batch-size-for-the-wire-protocol I believe it is ok to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure either, but the tests now query a collection containing more than 101 documents and pass... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great, the new tests cover the case of a getMore being run. |
||
numToReturn = bc.limit - bc.numReturned | ||
if numToReturn <= 0 { | ||
err := bc.Close(ctx) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly, I suggest adding a name to the test cases. Otherwise, it'd be harder to track down which case failed if L949 errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good! Used the
limit
number as the name for each case.