Skip to content

Commit 9a12e4a

Browse files
committed
CSHARP-1510: Fix GetMore throws InvalidOperationException under certain conditions.
1 parent e80807d commit 9a12e4a

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/MongoDB.Driver.Core.Tests/Core/Operations/FindOpcodeOperationTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,30 @@ public async Task ExecuteAsync_should_find_all_the_documents_matching_the_query(
230230
result.Should().HaveCount(5);
231231
}
232232

233+
[Test]
234+
[RequiresServer(VersionLessThan = "3.2.0")]
235+
public async Task ExecuteAsync_should_find_all_the_documents_matching_the_query_when_limit_is_used(
236+
[Values(1, 5, 6, 12)] int limit)
237+
{
238+
var collectionNamespace = CoreTestConfiguration.GetCollectionNamespaceForTestMethod();
239+
for (var id = 1; id <= limit + 1; id++)
240+
{
241+
var document = new BsonDocument { { "id", id }, { "filler", new string('x', 1000000) } }; // about 1MB big
242+
var requests = new[] { new InsertRequest(document) };
243+
var insertOperation = new BulkMixedWriteOperation(collectionNamespace, requests, new MessageEncoderSettings());
244+
ExecuteOperation(insertOperation);
245+
}
246+
var subject = new FindOpcodeOperation<BsonDocument>(collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
247+
{
248+
Limit = limit
249+
};
250+
251+
var cursor = await ExecuteOperationAsync(subject);
252+
var result = await ReadCursorToEndAsync(cursor);
253+
254+
result.Should().HaveCount(limit);
255+
}
256+
233257
[Test]
234258
[RequiresServer("EnsureTestData")]
235259
public async Task ExecuteAsync_should_find_all_the_documents_matching_the_query_when_split_across_batches()

src/MongoDB.Driver.Core/Core/Operations/AsyncCursor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ private int CalculateGetMoreProtocolNumberToReturn()
128128
var numberToReturn = _batchSize ?? 0;
129129
if (_limit > 0)
130130
{
131-
numberToReturn = _limit.Value - _count;
132-
if (_batchSize != 0 && numberToReturn > _batchSize.Value)
131+
var remaining = _limit.Value - _count;
132+
if (numberToReturn == 0 || numberToReturn > remaining)
133133
{
134-
numberToReturn = _batchSize.Value;
134+
numberToReturn = remaining;
135135
}
136136
}
137137
return numberToReturn;

0 commit comments

Comments
 (0)