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

Bulk: Adds validation for ItemRequestOptions.Properties #2746

Merged
merged 7 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ internal virtual async Task ValidateOperationAsync(
|| itemRequestOptions.PreTriggers != null
|| itemRequestOptions.PostTriggers != null
|| itemRequestOptions.SessionToken != null
|| itemRequestOptions.Properties != null
|| itemRequestOptions.DedicatedGatewayRequestOptions?.MaxIntegratedCacheStaleness != null)
{
throw new InvalidOperationException(ClientResources.UnsupportedBulkRequestOptions);
Expand Down
8 changes: 3 additions & 5 deletions Microsoft.Azure.Cosmos/src/ClientResources.Designer.cs

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

2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/src/ClientResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@
<value>Instantiation of only value types, anonymous types and spatial types are supported.</value>
</data>
<data name="UnsupportedBulkRequestOptions" xml:space="preserve">
<value>Consistency, Session, and Triggers are not allowed when AllowBulkExecution is set to true.</value>
<value>Consistency, Session, Properties, and Triggers are not allowed when AllowBulkExecution is set to true.</value>
</data>
<data name="EncryptorNotConfigured" xml:space="preserve">
<value>The client was not configured to allow for encryption. Create the client by using cosmosClientBuilder.WithEncryptor.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Tracing;
using Microsoft.Azure.Documents;
Expand All @@ -16,7 +15,7 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
[TestClass]
public class BatchAsyncContainerExecutorTests
{
private static CosmosSerializer cosmosDefaultJsonSerializer = new CosmosJsonDotNetSerializer();
private readonly static CosmosSerializer cosmosDefaultJsonSerializer = new CosmosJsonDotNetSerializer();
private CosmosClient cosmosClient;
private ContainerInternal cosmosContainer;

Expand Down Expand Up @@ -79,6 +78,9 @@ public async Task ValidateInvalidRequestOptionsAsync()
MyDocument myDocument = new MyDocument() { id = id, Status = id };

await Assert.ThrowsExceptionAsync<InvalidOperationException>(() => executor.ValidateOperationAsync(new ItemBatchOperation(OperationType.Replace, 0, new Cosmos.PartitionKey(id), id, cosmosDefaultJsonSerializer.ToStream(myDocument)), new ItemRequestOptions() { SessionToken = "something" }));
await Assert.ThrowsExceptionAsync<InvalidOperationException>(() => executor.ValidateOperationAsync(
new ItemBatchOperation(OperationType.Replace, 0, new Cosmos.PartitionKey(id), id, cosmosDefaultJsonSerializer.ToStream(myDocument)),
new ItemRequestOptions() { Properties = new Dictionary<string, object>() { { "test", "test" } } }));
await Assert.ThrowsExceptionAsync<InvalidOperationException>(() => executor.ValidateOperationAsync(
new ItemBatchOperation(OperationType.Replace, 0, new Cosmos.PartitionKey(id), id, cosmosDefaultJsonSerializer.ToStream(myDocument)),
new ItemRequestOptions() { DedicatedGatewayRequestOptions = new DedicatedGatewayRequestOptions { MaxIntegratedCacheStaleness = TimeSpan.FromMinutes(3) } }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
public class CosmosItemBulkTests
{
private Container container;
private Container nonBulkContainer;
private Database database;

[TestInitialize]
Expand All @@ -26,14 +25,12 @@ public async Task TestInitialize()
AllowBulkExecution = true
};
CosmosClient client = TestCommon.CreateCosmosClient(clientOptions);
CosmosClient nonBulkClient = TestCommon.CreateCosmosClient();

DatabaseResponse response = await client.CreateDatabaseIfNotExistsAsync(Guid.NewGuid().ToString());
this.database = response.Database;

ContainerResponse containerResponse = await this.database.CreateContainerAsync(Guid.NewGuid().ToString(), "/pk", 10000);
this.container = containerResponse;
this.nonBulkContainer = nonBulkClient.GetContainer(this.database.Id, this.container.Id);
}

[TestCleanup]
Expand All @@ -42,6 +39,38 @@ public async Task Cleanup()
await this.database.DeleteAsync();
}

[TestMethod]
public async Task ValidateRequestOptions()
{
List<Task<ResponseMessage>> tasks = new List<Task<ResponseMessage>>();
for (int i = 0; i < 100; i++)
{
tasks.Add(ExecuteCreateStreamAsync(this.container, CreateItem(i.ToString()),
ealsur marked this conversation as resolved.
Show resolved Hide resolved
new ItemRequestOptions() {
Properties = new Dictionary<string, object>() { { "test", "test" } },
DedicatedGatewayRequestOptions = new DedicatedGatewayRequestOptions { MaxIntegratedCacheStaleness = TimeSpan.FromMinutes(3) },
SessionToken = Guid.NewGuid().ToString(),
PreTriggers = new List<string>() { "preTrigger" },
PostTriggers = new List<string>() { "postTrigger" }
}));
}

try
{
await Task.WhenAll(tasks);
}
catch (InvalidOperationException)
ealsur marked this conversation as resolved.
Show resolved Hide resolved
{
}

for (int i = 0; i < 100; i++)
{
Task<ResponseMessage> task = tasks[i];
Assert.IsTrue(task.IsFaulted);
Assert.IsTrue(task.Exception.InnerException is InvalidOperationException _);
}
}

[TestMethod]
public async Task CreateItemStream_WithBulk()
{
Expand Down Expand Up @@ -532,9 +561,9 @@ private static Task<ItemResponse<ToDoActivity>> ExecuteReadAsync(Container conta
return container.ReadItemAsync<ToDoActivity>(item.id, new PartitionKey(item.pk));
}

private static Task<ResponseMessage> ExecuteCreateStreamAsync(Container container, ToDoActivity item)
private static Task<ResponseMessage> ExecuteCreateStreamAsync(Container container, ToDoActivity item, ItemRequestOptions itemRequestOptions = null)
{
return container.CreateItemStreamAsync(TestCommon.SerializerCore.ToStream(item), new PartitionKey(item.pk));
return container.CreateItemStreamAsync(TestCommon.SerializerCore.ToStream(item), new PartitionKey(item.pk), itemRequestOptions);
}

private static Task<ResponseMessage> ExecuteUpsertStreamAsync(Container container, ToDoActivity item)
Expand Down