-
Notifications
You must be signed in to change notification settings - Fork 72
Add BatchSize parameter to MongoDB source to prevent cursor timeouts on large collections #234
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
Open
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/fix-mongo-cursor-error
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c585253
Initial plan
Copilot 8b57e69
Add BatchSize parameter to MongoDB source to prevent cursor timeout e…
Copilot e0f4cc2
Fix BatchSize support for collections without query filters
Copilot be43f2e
Refactor: Extract duplicate logging and add validation comment
Copilot 59a73bc
Merge branch 'main' into copilot/fix-mongo-cursor-error
philnach b861bf4
Address code review feedback: Add validation warning, better tests, f…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
174 changes: 174 additions & 0 deletions
174
Extensions/Mongo/Cosmos.DataTransfer.MongoExtension.UnitTests/MongoRepositoryTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| using MongoDB.Bson; | ||
| using MongoDB.Driver; | ||
| using Moq; | ||
|
|
||
| namespace Cosmos.DataTransfer.MongoExtension.UnitTests; | ||
|
|
||
| [TestClass] | ||
| public class MongoRepositoryTests | ||
| { | ||
| [TestMethod] | ||
| public async Task FindAsync_WithPositiveBatchSize_PassesBatchSizeToDriver() | ||
| { | ||
| // Arrange | ||
| var mockCollection = new Mock<IMongoCollection<BsonDocument>>(); | ||
| var mockCursor = new Mock<IAsyncCursor<BsonDocument>>(); | ||
|
|
||
| FindOptions<BsonDocument, BsonDocument>? capturedOptions = null; | ||
|
|
||
| mockCollection | ||
| .Setup(c => c.FindAsync( | ||
| It.IsAny<FilterDefinition<BsonDocument>>(), | ||
| It.IsAny<FindOptions<BsonDocument, BsonDocument>>(), | ||
| It.IsAny<CancellationToken>())) | ||
| .Callback<FilterDefinition<BsonDocument>, FindOptions<BsonDocument, BsonDocument>, CancellationToken>( | ||
| (filter, options, ct) => capturedOptions = options) | ||
| .ReturnsAsync(mockCursor.Object); | ||
|
|
||
| mockCursor.SetupSequence(c => c.MoveNextAsync(It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(true) | ||
| .ReturnsAsync(false); | ||
|
|
||
| mockCursor.Setup(c => c.Current).Returns(new List<BsonDocument> { new BsonDocument() }); | ||
| mockCursor.Setup(c => c.Dispose()); | ||
|
|
||
| var repository = new MongoRepository<BsonDocument>(mockCollection.Object); | ||
| var filter = Builders<BsonDocument>.Filter.Empty; | ||
| var batchSize = 1000; | ||
|
|
||
| // Act | ||
| var results = new List<BsonDocument>(); | ||
| await foreach (var doc in repository.FindAsync(filter, batchSize)) | ||
| { | ||
| results.Add(doc); | ||
| } | ||
|
|
||
| // Assert | ||
| Assert.IsNotNull(capturedOptions); | ||
| Assert.AreEqual(batchSize, capturedOptions.BatchSize); | ||
| Assert.AreEqual(1, results.Count); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task FindAsync_WithNullBatchSize_DoesNotSetBatchSizeOption() | ||
| { | ||
| // Arrange | ||
| var mockCollection = new Mock<IMongoCollection<BsonDocument>>(); | ||
| var mockCursor = new Mock<IAsyncCursor<BsonDocument>>(); | ||
|
|
||
| FindOptions<BsonDocument, BsonDocument>? capturedOptions = null; | ||
|
|
||
| mockCollection | ||
| .Setup(c => c.FindAsync( | ||
| It.IsAny<FilterDefinition<BsonDocument>>(), | ||
| It.IsAny<FindOptions<BsonDocument, BsonDocument>>(), | ||
| It.IsAny<CancellationToken>())) | ||
| .Callback<FilterDefinition<BsonDocument>, FindOptions<BsonDocument, BsonDocument>, CancellationToken>( | ||
| (filter, options, ct) => capturedOptions = options) | ||
| .ReturnsAsync(mockCursor.Object); | ||
|
|
||
| mockCursor.SetupSequence(c => c.MoveNextAsync(It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(true) | ||
| .ReturnsAsync(false); | ||
|
|
||
| mockCursor.Setup(c => c.Current).Returns(new List<BsonDocument> { new BsonDocument() }); | ||
| mockCursor.Setup(c => c.Dispose()); | ||
|
|
||
| var repository = new MongoRepository<BsonDocument>(mockCollection.Object); | ||
| var filter = Builders<BsonDocument>.Filter.Empty; | ||
|
|
||
| // Act | ||
| var results = new List<BsonDocument>(); | ||
| await foreach (var doc in repository.FindAsync(filter, null)) | ||
| { | ||
| results.Add(doc); | ||
| } | ||
|
|
||
| // Assert | ||
| Assert.IsNotNull(capturedOptions); | ||
| Assert.IsNull(capturedOptions.BatchSize); | ||
| Assert.AreEqual(1, results.Count); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task FindAsync_WithZeroBatchSize_DoesNotSetBatchSizeOption() | ||
| { | ||
| // Arrange | ||
| var mockCollection = new Mock<IMongoCollection<BsonDocument>>(); | ||
| var mockCursor = new Mock<IAsyncCursor<BsonDocument>>(); | ||
|
|
||
| FindOptions<BsonDocument, BsonDocument>? capturedOptions = null; | ||
|
|
||
| mockCollection | ||
| .Setup(c => c.FindAsync( | ||
| It.IsAny<FilterDefinition<BsonDocument>>(), | ||
| It.IsAny<FindOptions<BsonDocument, BsonDocument>>(), | ||
| It.IsAny<CancellationToken>())) | ||
| .Callback<FilterDefinition<BsonDocument>, FindOptions<BsonDocument, BsonDocument>, CancellationToken>( | ||
| (filter, options, ct) => capturedOptions = options) | ||
| .ReturnsAsync(mockCursor.Object); | ||
|
|
||
| mockCursor.SetupSequence(c => c.MoveNextAsync(It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(true) | ||
| .ReturnsAsync(false); | ||
|
|
||
| mockCursor.Setup(c => c.Current).Returns(new List<BsonDocument> { new BsonDocument() }); | ||
| mockCursor.Setup(c => c.Dispose()); | ||
|
|
||
| var repository = new MongoRepository<BsonDocument>(mockCollection.Object); | ||
| var filter = Builders<BsonDocument>.Filter.Empty; | ||
|
|
||
| // Act | ||
| var results = new List<BsonDocument>(); | ||
| await foreach (var doc in repository.FindAsync(filter, 0)) | ||
| { | ||
| results.Add(doc); | ||
| } | ||
|
|
||
| // Assert | ||
| Assert.IsNotNull(capturedOptions); | ||
| Assert.IsNull(capturedOptions.BatchSize); | ||
| Assert.AreEqual(1, results.Count); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task FindAsync_WithNegativeBatchSize_DoesNotSetBatchSizeOption() | ||
| { | ||
| // Arrange | ||
| var mockCollection = new Mock<IMongoCollection<BsonDocument>>(); | ||
| var mockCursor = new Mock<IAsyncCursor<BsonDocument>>(); | ||
|
|
||
| FindOptions<BsonDocument, BsonDocument>? capturedOptions = null; | ||
|
|
||
| mockCollection | ||
| .Setup(c => c.FindAsync( | ||
| It.IsAny<FilterDefinition<BsonDocument>>(), | ||
| It.IsAny<FindOptions<BsonDocument, BsonDocument>>(), | ||
| It.IsAny<CancellationToken>())) | ||
| .Callback<FilterDefinition<BsonDocument>, FindOptions<BsonDocument, BsonDocument>, CancellationToken>( | ||
| (filter, options, ct) => capturedOptions = options) | ||
| .ReturnsAsync(mockCursor.Object); | ||
|
|
||
| mockCursor.SetupSequence(c => c.MoveNextAsync(It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(true) | ||
| .ReturnsAsync(false); | ||
|
|
||
| mockCursor.Setup(c => c.Current).Returns(new List<BsonDocument> { new BsonDocument() }); | ||
| mockCursor.Setup(c => c.Dispose()); | ||
|
|
||
| var repository = new MongoRepository<BsonDocument>(mockCollection.Object); | ||
| var filter = Builders<BsonDocument>.Filter.Empty; | ||
|
|
||
| // Act | ||
| var results = new List<BsonDocument>(); | ||
| await foreach (var doc in repository.FindAsync(filter, -1)) | ||
| { | ||
| results.Add(doc); | ||
| } | ||
|
|
||
| // Assert | ||
| Assert.IsNotNull(capturedOptions); | ||
| Assert.IsNull(capturedOptions.BatchSize); | ||
| Assert.AreEqual(1, results.Count); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.