/// <summary> /// Creates a new instance of an <see cref="IDocumentClient"/> implementation. /// </summary> /// <param name="options">The <see cref="UserStoreOptions"/> to use.</param> /// <param name="httpClientFactory">The <see cref="IHttpClientFactory"/> to use.</param> /// <param name="serializer">The optional <see cref="CosmosSerializer"/> to use.</param> /// <returns> /// The created instance of <see cref="IDocumentClient"/>. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="options"/> or <paramref name="httpClientFactory"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentException"> /// <paramref name="options"/> is invalid. /// </exception> internal static CosmosClient CreateClient( UserStoreOptions options, IHttpClientFactory httpClientFactory, CosmosSerializer?serializer = null) { if (options == null) { throw new ArgumentNullException(nameof(options)); } if (httpClientFactory == null) { throw new ArgumentNullException(nameof(httpClientFactory)); } if (options.ServiceUri == null) { throw new ArgumentException("No DocumentDB URI is configured.", nameof(options)); } if (!options.ServiceUri.IsAbsoluteUri) { throw new ArgumentException("The configured DocumentDB URI is as it is not an absolute URI.", nameof(options)); } if (string.IsNullOrEmpty(options.AccessKey)) { throw new ArgumentException("No DocumentDB access key is configured.", nameof(options)); } if (string.IsNullOrEmpty(options.DatabaseName)) { throw new ArgumentException("No DocumentDB database name is configured.", nameof(options)); } if (string.IsNullOrEmpty(options.CollectionName)) { throw new ArgumentException("No DocumentDB collection name is configured.", nameof(options)); } var builder = new CosmosClientBuilder(options.ServiceUri.AbsoluteUri, options.AccessKey) .WithApplicationName("london-travel") .WithCustomSerializer(serializer) .WithHttpClientFactory(httpClientFactory.CreateClient) .WithRequestTimeout(TimeSpan.FromSeconds(15)); if (!string.IsNullOrEmpty(options.CurrentLocation)) { builder = builder.WithApplicationRegion(options.CurrentLocation); } return(builder.Build()); }
public CosmosClient CreateClient( string connectionString, string currentRegion = null) { if (string.IsNullOrEmpty(connectionString)) { throw new ArgumentNullException(nameof(connectionString)); } CosmosClientBuilder clientBuilder = new CosmosClientBuilder(connectionString); if (!string.IsNullOrEmpty(currentRegion)) { clientBuilder.WithApplicationRegion(currentRegion); } return(clientBuilder.Build()); }
private static Lazy <CosmosClient> SetupClient(ICosmosContainerOptions options) { var csb = ValidatInput(options.ConnectionString, options.DatabaseId, options.ContainerId); var cacheKey = $"{options.DatabaseId}_{options.UseGatewayMode}_{ToSHA256(csb.Key)}"; return(new Lazy <CosmosClient>(() => { CosmosClient client = null; bool fromCache = false; if (_clientCache.ContainsKey(cacheKey)) { client = _clientCache[cacheKey]; if (client != null) { fromCache = true; } } if (!fromCache) { var builder = new CosmosClientBuilder(options.ConnectionString); builder .WithThrottlingRetryOptions(TimeSpan.FromSeconds(30), 10); if (options.UseGatewayMode) { builder.WithConnectionModeGateway(options.ConcurrentConnections); } foreach (var region in options.AzureRegions ?? new List <string>()) { builder.WithApplicationRegion(region); } client = builder.Build(); _clientCache[cacheKey] = client; } CreateDatabaseIfNotExistsAsync(client, options).GetAwaiter().GetResult(); return client; })); }
private static CosmosClient CreateCosmosClient(string connectionString, CosmosOptions cosmosOptions) { GuardEmptyString(connectionString, "cosmosDbConnectionString"); cosmosOptions.Guard(); var builder = new CosmosClientBuilder(connectionString); // ConnectionPolicy のDefault値は、SDKのConnectionPolicy.csで設定されています。 // ## Connection Mode について // Default で ConnectionMode.Direct/Protocol.Tcp で接続されます。 // もしGateway(ConnectionMode.Gateway/Protocol.Https) を使いたければ、以下メソッドを呼ぶ // builder.UseConnectionModeGateway(maxConnectionLimit: ??); // Default: CamelCase Serialize/Deserialize and ignore Readonly property // TODO: 設定変更用のconfigは未実装 //var settings = JsonSerializerSettingsFactory.CreateForReadonlyIgnoreAndCamelCase(); var settings = JsonSerializerSettingsFactory.CreateForCamelCase(); builder.WithCustomSerializer(new CustomizableCaseJsonSerializer(settings)); if (cosmosOptions.ThrottlingRetryOptions != null) { builder.WithThrottlingRetryOptions( cosmosOptions.ThrottlingRetryOptions.MaxRetryWaitTimeOnThrottledRequests, cosmosOptions.ThrottlingRetryOptions.MaxRetryAttemptsOnThrottledRequests); } // multi-master support if (!string.IsNullOrEmpty(cosmosOptions.CurrentRegion)) { builder.WithApplicationRegion(cosmosOptions.CurrentRegion); } return(builder.Build()); }
public void VerifyCosmosConfigurationPropertiesGetUpdated() { string endpoint = AccountEndpoint; string key = MockCosmosUtil.RandomInvalidCorrectlyFormatedAuthKey; string region = Regions.WestCentralUS; ConnectionMode connectionMode = ConnectionMode.Gateway; TimeSpan requestTimeout = TimeSpan.FromDays(1); int maxConnections = 9001; string userAgentSuffix = "testSuffix"; RequestHandler preProcessHandler = new TestHandler(); ApiType apiType = ApiType.Sql; int maxRetryAttemptsOnThrottledRequests = 9999; TimeSpan maxRetryWaitTime = TimeSpan.FromHours(6); bool enableTcpConnectionEndpointRediscovery = true; CosmosSerializationOptions cosmosSerializerOptions = new CosmosSerializationOptions() { IgnoreNullValues = true, PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase, }; TimeSpan idleTcpConnectionTimeout = new TimeSpan(0, 10, 0); TimeSpan openTcpConnectionTimeout = new TimeSpan(0, 0, 5); int maxRequestsPerTcpConnection = 30; int maxTcpConnectionsPerEndpoint = 65535; Cosmos.PortReuseMode portReuseMode = Cosmos.PortReuseMode.PrivatePortPool; IWebProxy webProxy = new TestWebProxy(); Cosmos.ConsistencyLevel consistencyLevel = Cosmos.ConsistencyLevel.ConsistentPrefix; CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder( accountEndpoint: endpoint, authKeyOrResourceToken: key); CosmosClient cosmosClient = cosmosClientBuilder.Build(new MockDocumentClient()); CosmosClientOptions clientOptions = cosmosClient.ClientOptions; Assert.AreEqual(endpoint, cosmosClient.Endpoint.OriginalString, "AccountEndpoint did not save correctly"); Assert.AreEqual(key, cosmosClient.AccountKey, "AccountKey did not save correctly"); //Verify the default values are different from the new values Assert.AreNotEqual(region, clientOptions.ApplicationRegion); Assert.IsNull(clientOptions.ApplicationPreferredRegions); Assert.AreNotEqual(connectionMode, clientOptions.ConnectionMode); Assert.AreNotEqual(maxConnections, clientOptions.GatewayModeMaxConnectionLimit); Assert.AreNotEqual(requestTimeout, clientOptions.RequestTimeout); Assert.AreNotEqual(userAgentSuffix, clientOptions.ApplicationName); Assert.AreNotEqual(apiType, clientOptions.ApiType); Assert.IsFalse(clientOptions.AllowBulkExecution); Assert.AreEqual(0, clientOptions.CustomHandlers.Count); Assert.IsNull(clientOptions.SerializerOptions); Assert.IsNotNull(clientOptions.Serializer); Assert.IsNull(clientOptions.WebProxy); Assert.IsFalse(clientOptions.LimitToEndpoint); Assert.IsTrue(clientOptions.EnableTcpConnectionEndpointRediscovery); Assert.IsNull(clientOptions.HttpClientFactory); Assert.AreNotEqual(consistencyLevel, clientOptions.ConsistencyLevel); Assert.IsFalse(clientOptions.EnablePartitionLevelFailover); //Verify GetConnectionPolicy returns the correct values for default ConnectionPolicy policy = clientOptions.GetConnectionPolicy(clientId: 0); Assert.AreEqual(ConnectionMode.Direct, policy.ConnectionMode); Assert.AreEqual(Protocol.Tcp, policy.ConnectionProtocol); Assert.AreEqual(clientOptions.GatewayModeMaxConnectionLimit, policy.MaxConnectionLimit); Assert.AreEqual(clientOptions.RequestTimeout, policy.RequestTimeout); Assert.IsNull(policy.IdleTcpConnectionTimeout); Assert.IsNull(policy.OpenTcpConnectionTimeout); Assert.IsNull(policy.MaxRequestsPerTcpConnection); Assert.IsNull(policy.MaxTcpConnectionsPerEndpoint); Assert.IsTrue(policy.EnableEndpointDiscovery); Assert.IsTrue(policy.EnableTcpConnectionEndpointRediscovery); Assert.IsNull(policy.HttpClientFactory); Assert.AreNotEqual(Cosmos.ConsistencyLevel.Session, clientOptions.ConsistencyLevel); Assert.IsFalse(policy.EnablePartitionLevelFailover); cosmosClientBuilder.WithApplicationRegion(region) .WithConnectionModeGateway(maxConnections, webProxy) .WithRequestTimeout(requestTimeout) .WithApplicationName(userAgentSuffix) .AddCustomHandlers(preProcessHandler) .WithApiType(apiType) .WithThrottlingRetryOptions(maxRetryWaitTime, maxRetryAttemptsOnThrottledRequests) .WithBulkExecution(true) .WithSerializerOptions(cosmosSerializerOptions) .WithConsistencyLevel(consistencyLevel) .WithPartitionLevelFailoverEnabled(); cosmosClient = cosmosClientBuilder.Build(new MockDocumentClient()); clientOptions = cosmosClient.ClientOptions; //Verify all the values are updated Assert.AreEqual(region, clientOptions.ApplicationRegion); Assert.IsNull(clientOptions.ApplicationPreferredRegions); Assert.AreEqual(connectionMode, clientOptions.ConnectionMode); Assert.AreEqual(maxConnections, clientOptions.GatewayModeMaxConnectionLimit); Assert.AreEqual(requestTimeout, clientOptions.RequestTimeout); Assert.AreEqual(userAgentSuffix, clientOptions.ApplicationName); Assert.AreEqual(preProcessHandler, clientOptions.CustomHandlers[0]); Assert.AreEqual(apiType, clientOptions.ApiType); Assert.AreEqual(maxRetryAttemptsOnThrottledRequests, clientOptions.MaxRetryAttemptsOnRateLimitedRequests); Assert.AreEqual(maxRetryWaitTime, clientOptions.MaxRetryWaitTimeOnRateLimitedRequests); Assert.AreEqual(cosmosSerializerOptions.IgnoreNullValues, clientOptions.SerializerOptions.IgnoreNullValues); Assert.AreEqual(cosmosSerializerOptions.PropertyNamingPolicy, clientOptions.SerializerOptions.PropertyNamingPolicy); Assert.AreEqual(cosmosSerializerOptions.Indented, clientOptions.SerializerOptions.Indented); Assert.IsTrue(object.ReferenceEquals(webProxy, clientOptions.WebProxy)); Assert.IsTrue(clientOptions.AllowBulkExecution); Assert.AreEqual(consistencyLevel, clientOptions.ConsistencyLevel); Assert.IsTrue(clientOptions.EnablePartitionLevelFailover); //Verify GetConnectionPolicy returns the correct values policy = clientOptions.GetConnectionPolicy(clientId: 0); Assert.AreEqual(region, policy.PreferredLocations[0]); Assert.AreEqual(ConnectionMode.Gateway, policy.ConnectionMode); Assert.AreEqual(Protocol.Https, policy.ConnectionProtocol); Assert.AreEqual(maxConnections, policy.MaxConnectionLimit); Assert.AreEqual(requestTimeout, policy.RequestTimeout); Assert.IsTrue(policy.UserAgentSuffix.Contains(userAgentSuffix)); Assert.IsTrue(policy.UseMultipleWriteLocations); Assert.AreEqual(maxRetryAttemptsOnThrottledRequests, policy.RetryOptions.MaxRetryAttemptsOnThrottledRequests); Assert.AreEqual((int)maxRetryWaitTime.TotalSeconds, policy.RetryOptions.MaxRetryWaitTimeInSeconds); Assert.AreEqual((Documents.ConsistencyLevel)consistencyLevel, clientOptions.GetDocumentsConsistencyLevel()); Assert.IsTrue(policy.EnablePartitionLevelFailover); IReadOnlyList <string> preferredLocations = new List <string>() { Regions.AustraliaCentral, Regions.AustraliaCentral2 }; //Verify Direct Mode settings cosmosClientBuilder = new CosmosClientBuilder( accountEndpoint: endpoint, authKeyOrResourceToken: key); cosmosClientBuilder.WithConnectionModeDirect( idleTcpConnectionTimeout, openTcpConnectionTimeout, maxRequestsPerTcpConnection, maxTcpConnectionsPerEndpoint, portReuseMode, enableTcpConnectionEndpointRediscovery) .WithApplicationPreferredRegions(preferredLocations); cosmosClient = cosmosClientBuilder.Build(new MockDocumentClient()); clientOptions = cosmosClient.ClientOptions; //Verify all the values are updated Assert.AreEqual(idleTcpConnectionTimeout, clientOptions.IdleTcpConnectionTimeout); Assert.AreEqual(openTcpConnectionTimeout, clientOptions.OpenTcpConnectionTimeout); Assert.AreEqual(maxRequestsPerTcpConnection, clientOptions.MaxRequestsPerTcpConnection); Assert.AreEqual(maxTcpConnectionsPerEndpoint, clientOptions.MaxTcpConnectionsPerEndpoint); Assert.AreEqual(portReuseMode, clientOptions.PortReuseMode); Assert.IsTrue(clientOptions.EnableTcpConnectionEndpointRediscovery); CollectionAssert.AreEqual(preferredLocations.ToArray(), clientOptions.ApplicationPreferredRegions.ToArray()); //Verify GetConnectionPolicy returns the correct values policy = clientOptions.GetConnectionPolicy(clientId: 0); Assert.AreEqual(idleTcpConnectionTimeout, policy.IdleTcpConnectionTimeout); Assert.AreEqual(openTcpConnectionTimeout, policy.OpenTcpConnectionTimeout); Assert.AreEqual(maxRequestsPerTcpConnection, policy.MaxRequestsPerTcpConnection); Assert.AreEqual(maxTcpConnectionsPerEndpoint, policy.MaxTcpConnectionsPerEndpoint); Assert.AreEqual(portReuseMode, policy.PortReuseMode); Assert.IsTrue(policy.EnableTcpConnectionEndpointRediscovery); CollectionAssert.AreEqual(preferredLocations.ToArray(), policy.PreferredLocations.ToArray()); }
public void VerifyCosmosConfigurationPropertiesGetUpdated() { string endpoint = AccountEndpoint; string key = Guid.NewGuid().ToString(); string region = CosmosRegions.WestCentralUS; ConnectionMode connectionMode = ConnectionMode.Gateway; TimeSpan requestTimeout = TimeSpan.FromDays(1); int maxConnections = 9001; string userAgentSuffix = "testSuffix"; RequestHandler preProcessHandler = new TestHandler(); ApiType apiType = ApiType.Sql; int maxRetryAttemptsOnThrottledRequests = 9999; TimeSpan maxRetryWaitTime = TimeSpan.FromHours(6); CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder( accountEndpoint: endpoint, accountKey: key); CosmosClient cosmosClient = cosmosClientBuilder.Build(new MockDocumentClient()); CosmosClientOptions clientOptions = cosmosClient.ClientOptions; Assert.AreEqual(endpoint, cosmosClient.Endpoint.OriginalString, "AccountEndpoint did not save correctly"); Assert.AreEqual(key, cosmosClient.AccountKey, "AccountKey did not save correctly"); //Verify the default values are different from the new values Assert.AreNotEqual(region, clientOptions.ApplicationRegion); Assert.AreNotEqual(connectionMode, clientOptions.ConnectionMode); Assert.AreNotEqual(maxConnections, clientOptions.GatewayModeMaxConnectionLimit); Assert.AreNotEqual(requestTimeout, clientOptions.RequestTimeout); Assert.AreNotEqual(userAgentSuffix, clientOptions.ApplicationName); Assert.AreNotEqual(apiType, clientOptions.ApiType); Assert.AreEqual(0, clientOptions.CustomHandlers.Count); //Verify GetConnectionPolicy returns the correct values for default ConnectionPolicy policy = clientOptions.GetConnectionPolicy(); Assert.AreEqual(ConnectionMode.Direct, policy.ConnectionMode); Assert.AreEqual(Protocol.Tcp, policy.ConnectionProtocol); Assert.AreEqual(clientOptions.GatewayModeMaxConnectionLimit, policy.MaxConnectionLimit); Assert.AreEqual(clientOptions.RequestTimeout, policy.RequestTimeout); cosmosClientBuilder.WithApplicationRegion(region) .WithConnectionModeGateway(maxConnections) .WithRequestTimeout(requestTimeout) .WithApplicationName(userAgentSuffix) .AddCustomHandlers(preProcessHandler) .WithApiType(apiType) .WithThrottlingRetryOptions(maxRetryWaitTime, maxRetryAttemptsOnThrottledRequests); cosmosClient = cosmosClientBuilder.Build(new MockDocumentClient()); clientOptions = cosmosClient.ClientOptions; //Verify all the values are updated Assert.AreEqual(region, clientOptions.ApplicationRegion); Assert.AreEqual(connectionMode, clientOptions.ConnectionMode); Assert.AreEqual(maxConnections, clientOptions.GatewayModeMaxConnectionLimit); Assert.AreEqual(requestTimeout, clientOptions.RequestTimeout); Assert.AreEqual(userAgentSuffix, clientOptions.ApplicationName); Assert.AreEqual(preProcessHandler, clientOptions.CustomHandlers[0]); Assert.AreEqual(apiType, clientOptions.ApiType); Assert.AreEqual(maxRetryAttemptsOnThrottledRequests, clientOptions.MaxRetryAttemptsOnThrottledRequests); Assert.AreEqual(maxRetryWaitTime, clientOptions.MaxRetryWaitTimeOnThrottledRequests); //Verify GetConnectionPolicy returns the correct values policy = clientOptions.GetConnectionPolicy(); Assert.AreEqual(region, policy.PreferredLocations[0]); Assert.AreEqual(ConnectionMode.Gateway, policy.ConnectionMode); Assert.AreEqual(Protocol.Https, policy.ConnectionProtocol); Assert.AreEqual(maxConnections, policy.MaxConnectionLimit); Assert.AreEqual(requestTimeout, policy.RequestTimeout); Assert.AreEqual(userAgentSuffix, policy.UserAgentSuffix); Assert.IsTrue(policy.UseMultipleWriteLocations); Assert.AreEqual(maxRetryAttemptsOnThrottledRequests, policy.RetryOptions.MaxRetryAttemptsOnThrottledRequests); Assert.AreEqual((int)maxRetryWaitTime.TotalSeconds, policy.RetryOptions.MaxRetryWaitTimeInSeconds); }
public void VerifyCosmosConfigurationPropertiesGetUpdated() { string endpoint = AccountEndpoint; string key = Guid.NewGuid().ToString(); string region = Regions.WestCentralUS; ConnectionMode connectionMode = ConnectionMode.Gateway; TimeSpan requestTimeout = TimeSpan.FromDays(1); int maxConnections = 9001; string userAgentSuffix = "testSuffix"; RequestHandler preProcessHandler = new TestHandler(); ApiType apiType = ApiType.Sql; int maxRetryAttemptsOnThrottledRequests = 9999; TimeSpan maxRetryWaitTime = TimeSpan.FromHours(6); CosmosSerializationOptions cosmosSerializerOptions = new CosmosSerializationOptions() { IgnoreNullValues = true, PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase, }; TimeSpan idleTcpConnectionTimeout = new TimeSpan(0, 10, 0); TimeSpan openTcpConnectionTimeout = new TimeSpan(0, 0, 5); int maxRequestsPerTcpConnection = 30; int maxTcpConnectionsPerEndpoint = 65535; CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder( accountEndpoint: endpoint, authKeyOrResourceToken: key); CosmosClient cosmosClient = cosmosClientBuilder.Build(new MockDocumentClient()); CosmosClientOptions clientOptions = cosmosClient.ClientOptions; Assert.AreEqual(endpoint, cosmosClient.Endpoint.OriginalString, "AccountEndpoint did not save correctly"); Assert.AreEqual(key, cosmosClient.AccountKey, "AccountKey did not save correctly"); //Verify the default values are different from the new values Assert.AreNotEqual(region, clientOptions.ApplicationRegion); Assert.AreNotEqual(connectionMode, clientOptions.ConnectionMode); Assert.AreNotEqual(maxConnections, clientOptions.GatewayModeMaxConnectionLimit); Assert.AreNotEqual(requestTimeout, clientOptions.RequestTimeout); Assert.AreNotEqual(userAgentSuffix, clientOptions.ApplicationName); Assert.AreNotEqual(apiType, clientOptions.ApiType); Assert.AreEqual(0, clientOptions.CustomHandlers.Count); Assert.IsNull(clientOptions.SerializerOptions); Assert.IsNull(clientOptions.Serializer); //Verify GetConnectionPolicy returns the correct values for default ConnectionPolicy policy = clientOptions.GetConnectionPolicy(); Assert.AreEqual(ConnectionMode.Direct, policy.ConnectionMode); Assert.AreEqual(Protocol.Tcp, policy.ConnectionProtocol); Assert.AreEqual(clientOptions.GatewayModeMaxConnectionLimit, policy.MaxConnectionLimit); Assert.AreEqual(clientOptions.RequestTimeout, policy.RequestTimeout); Assert.IsNull(policy.IdleTcpConnectionTimeout); Assert.IsNull(policy.OpenTcpConnectionTimeout); Assert.IsNull(policy.MaxRequestsPerTcpConnection); Assert.IsNull(policy.MaxTcpConnectionsPerEndpoint); cosmosClientBuilder.WithApplicationRegion(region) .WithConnectionModeGateway(maxConnections) .WithRequestTimeout(requestTimeout) .WithApplicationName(userAgentSuffix) .AddCustomHandlers(preProcessHandler) .WithApiType(apiType) .WithThrottlingRetryOptions(maxRetryWaitTime, maxRetryAttemptsOnThrottledRequests) .WithSerializerOptions(cosmosSerializerOptions); cosmosClient = cosmosClientBuilder.Build(new MockDocumentClient()); clientOptions = cosmosClient.ClientOptions; //Verify all the values are updated Assert.AreEqual(region, clientOptions.ApplicationRegion); Assert.AreEqual(connectionMode, clientOptions.ConnectionMode); Assert.AreEqual(maxConnections, clientOptions.GatewayModeMaxConnectionLimit); Assert.AreEqual(requestTimeout, clientOptions.RequestTimeout); Assert.AreEqual(userAgentSuffix, clientOptions.ApplicationName); Assert.AreEqual(preProcessHandler, clientOptions.CustomHandlers[0]); Assert.AreEqual(apiType, clientOptions.ApiType); Assert.AreEqual(maxRetryAttemptsOnThrottledRequests, clientOptions.MaxRetryAttemptsOnRateLimitedRequests); Assert.AreEqual(maxRetryWaitTime, clientOptions.MaxRetryWaitTimeOnRateLimitedRequests); Assert.AreEqual(cosmosSerializerOptions.IgnoreNullValues, clientOptions.SerializerOptions.IgnoreNullValues); Assert.AreEqual(cosmosSerializerOptions.PropertyNamingPolicy, clientOptions.SerializerOptions.PropertyNamingPolicy); Assert.AreEqual(cosmosSerializerOptions.Indented, clientOptions.SerializerOptions.Indented); //Verify GetConnectionPolicy returns the correct values policy = clientOptions.GetConnectionPolicy(); Assert.AreEqual(region, policy.PreferredLocations[0]); Assert.AreEqual(ConnectionMode.Gateway, policy.ConnectionMode); Assert.AreEqual(Protocol.Https, policy.ConnectionProtocol); Assert.AreEqual(maxConnections, policy.MaxConnectionLimit); Assert.AreEqual(requestTimeout, policy.RequestTimeout); Assert.IsTrue(policy.UserAgentSuffix.Contains(userAgentSuffix)); Assert.IsTrue(policy.UseMultipleWriteLocations); Assert.AreEqual(maxRetryAttemptsOnThrottledRequests, policy.RetryOptions.MaxRetryAttemptsOnThrottledRequests); Assert.AreEqual((int)maxRetryWaitTime.TotalSeconds, policy.RetryOptions.MaxRetryWaitTimeInSeconds); //Verify Direct Mode settings cosmosClientBuilder = new CosmosClientBuilder( accountEndpoint: endpoint, authKeyOrResourceToken: key); cosmosClientBuilder.WithConnectionModeDirect( idleTcpConnectionTimeout, openTcpConnectionTimeout, maxRequestsPerTcpConnection, maxTcpConnectionsPerEndpoint ); cosmosClient = cosmosClientBuilder.Build(new MockDocumentClient()); clientOptions = cosmosClient.ClientOptions; //Verify all the values are updated Assert.AreEqual(idleTcpConnectionTimeout, clientOptions.IdleTcpConnectionTimeout); Assert.AreEqual(openTcpConnectionTimeout, clientOptions.OpenTcpConnectionTimeout); Assert.AreEqual(maxRequestsPerTcpConnection, clientOptions.MaxRequestsPerTcpConnection); Assert.AreEqual(maxTcpConnectionsPerEndpoint, clientOptions.MaxTcpConnectionsPerEndpoint); //Verify GetConnectionPolicy returns the correct values policy = clientOptions.GetConnectionPolicy(); Assert.AreEqual(idleTcpConnectionTimeout, policy.IdleTcpConnectionTimeout); Assert.AreEqual(openTcpConnectionTimeout, policy.OpenTcpConnectionTimeout); Assert.AreEqual(maxRequestsPerTcpConnection, policy.MaxRequestsPerTcpConnection); Assert.AreEqual(maxTcpConnectionsPerEndpoint, policy.MaxTcpConnectionsPerEndpoint); }