Skip to content
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

Ensure only a single action for a document is added to a batch #18469

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions sdk/search/Azure.Search.Documents/tests/Batching/BatchingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,65 @@ public async Task Behavior_BatchSize()
Assert.AreEqual(5, client.Submissions.Count);
}

[Test]
public async Task Behavior_SplitBatchByDocumentKey()
Mohit-Chakraborty marked this conversation as resolved.
Show resolved Hide resolved
{
int numberOfDocuments = 5;

await using SearchResources resources = await SearchResources.CreateWithEmptyIndexAsync<SimpleDocument>(this);
BatchingSearchClient client = GetBatchingSearchClient(resources);

SimpleDocument[] data = new SimpleDocument[numberOfDocuments];
for (int i = 0; i < numberOfDocuments; i++)
{
data[i] = new SimpleDocument() { Id = $"{i}", Name = $"Document #{i}" };
}

// 'SimpleDocument' has 'Id' set as its key field.
// Set the Ids of 2 documents in the group to be the same.
// We expect the batch to be split at this index, even though the size of the set is smaller than the batch size.
data[3].Id = data[0].Id;

await using SearchIndexingBufferedSender<SimpleDocument> indexer =
client.CreateIndexingBufferedSender(
new SearchIndexingBufferedSenderOptions<SimpleDocument>()
{
// Set the expected batch action count to be larger than the number of documents in the set.
InitialBatchActionCount = numberOfDocuments + 1,
});

// Throw from every handler
int sent = 0, completed = 0;
indexer.ActionSent += e =>
{
sent++;

// Batch will be split at the 4th document.
// So, 3 documents will be sent before any are submitted, but 3 submissions will be made before the last 2 are sent
Assert.AreEqual((sent <= 3) ? 0 : 3, completed);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a little tricky. Would it be easier to have a List<string> pendingKeys collection that you add/remove from in these methods and then CollectionAssert.AllItemsAreUnique in each?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Let me see how I can make the checks simpler.


throw new InvalidOperationException("ActionSentAsync: Should not be seen!");
};

indexer.ActionCompleted += e =>
{
completed++;

// Batch will be split at the 4th document.
// So, 3 documents will be submitted after 3 are sent, and the last 2 submissions will be made after all 5 are sent
Assert.AreEqual((completed <= 3) ? 3 : 5, sent);

throw new InvalidOperationException("ActionCompletedAsync: Should not be seen!");
};

AssertNoFailures(indexer);
await indexer.UploadDocumentsAsync(data);
await indexer.FlushAsync();

Assert.AreEqual(5, sent);
Assert.AreEqual(5, completed);
}

[Test]
[TestCase(409)]
[TestCase(422)]
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading