Skip to content

Commit

Permalink
CosmosClientBuilder: Adds Warmup Option to Create/Initialize Containe…
Browse files Browse the repository at this point in the history
…rs (#3257)

* Add Warmup option for CosmosClientBuilder

* Add test method for new builder warmup option

* Public API Change

* rename method

* remove async/await

* update API

* API changes to remove async
  • Loading branch information
imanvt committed Jun 9, 2022
1 parent a39e23f commit dbbae92
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Microsoft.Azure.Cosmos/src/Fluent/CosmosClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Microsoft.Azure.Cosmos.Fluent
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using global::Azure.Core;
using Microsoft.Azure.Cosmos.Core.Trace;
using Microsoft.Azure.Documents;
Expand Down Expand Up @@ -130,6 +132,21 @@ public CosmosClient Build()
new CosmosClient(this.accountEndpoint, this.tokenCredential, this.clientOptions);
}

/// <summary>
/// A method to create the cosmos client and initialize the provided containers.
/// </summary>
/// <param name="containers">Containers to be initialized identified by it's database name and container name.</param>
/// <param name="cancellationToken">(Optional) Cancellation Token</param>
/// <returns>
/// A CosmosClient object.
/// </returns>
public Task<CosmosClient> BuildAndInitializeAsync(IReadOnlyList<(string databaseId, string containerId)> containers, CancellationToken cancellationToken = default)
{
return this.tokenCredential == null ?
CosmosClient.CreateAndInitializeAsync(this.accountEndpoint, this.accountKey, containers, this.clientOptions, cancellationToken) :
CosmosClient.CreateAndInitializeAsync(this.accountEndpoint, this.tokenCredential, containers, this.clientOptions, cancellationToken);
}

/// <summary>
/// A method to create the cosmos client
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Fluent;
using Microsoft.Azure.Cosmos.Routing;
using Microsoft.Azure.Documents;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -80,6 +81,34 @@ public async Task CreateAndInitializeTest()
cosmosClient.Dispose();
}

[TestMethod]
public async Task CreateAndInitializeWithCosmosClientBuilderTest()
{
int httpCallsMade = 0;
HttpClientHandlerHelper httpClientHandlerHelper = new HttpClientHandlerHelper
{
RequestCallBack = (request, cancellationToken) =>
{
httpCallsMade++;
return null;
}
};

(string endpoint, string authKey) = TestCommon.GetAccountInfo();
List<(string, string)> containers = new List<(string, string)>
{ ("ClientCreateAndInitializeDatabase", "ClientCreateAndInitializeContainer")};

CosmosClientBuilder builder = new CosmosClientBuilder(endpoint, authKey).WithHttpClientFactory(() => new HttpClient(httpClientHandlerHelper));
CosmosClient cosmosClient = await builder.BuildAndInitializeAsync(containers);
Assert.IsNotNull(cosmosClient);
int httpCallsMadeAfterCreation = httpCallsMade;

ContainerInternal container = (ContainerInternal)cosmosClient.GetContainer("ClientCreateAndInitializeDatabase", "ClientCreateAndInitializeContainer");
ItemResponse<ToDoActivity> readResponse = await container.ReadItemAsync<ToDoActivity>("1", new Cosmos.PartitionKey("Status1"));
Assert.AreEqual(httpCallsMade, httpCallsMadeAfterCreation);
cosmosClient.Dispose();
}

[TestMethod]
[ExpectedException(typeof(HttpRequestException))]
public async Task AuthIncorrectTest()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4356,6 +4356,11 @@
"Attributes": [],
"MethodInfo": "Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder WithThrottlingRetryOptions(System.TimeSpan, Int32);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.CosmosClient] BuildAndInitializeAsync(System.Collections.Generic.IReadOnlyList`1[System.ValueTuple`2[System.String,System.String]], System.Threading.CancellationToken)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.CosmosClient] BuildAndInitializeAsync(System.Collections.Generic.IReadOnlyList`1[System.ValueTuple`2[System.String,System.String]], System.Threading.CancellationToken);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"Void .ctor(System.String, Azure.Core.TokenCredential)": {
"Type": "Constructor",
"Attributes": [],
Expand Down

0 comments on commit dbbae92

Please sign in to comment.