Ejemplo n.º 1
0
        public void WithQuorumReadWithEventualConsistencyAccount()
        {
            CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder(
                accountEndpoint: AccountEndpoint,
                authKeyOrResourceToken: MockCosmosUtil.RandomInvalidCorrectlyFormatedAuthKey);

            CosmosClientOptions cosmosClientOptions = cosmosClientBuilder.Build(new MockDocumentClient()).ClientOptions;

            Assert.IsFalse(cosmosClientOptions.EnableUpgradeConsistencyToLocalQuorum);

            cosmosClientBuilder
            .AllowUpgradeConsistencyToLocalQuorum();

            cosmosClientOptions = cosmosClientBuilder.Build(new MockDocumentClient()).ClientOptions;
            Assert.IsTrue(cosmosClientOptions.EnableUpgradeConsistencyToLocalQuorum);
        }
Ejemplo n.º 2
0
        private async Task <Container> CreateContainer(bool isLocalQuorumConsistency)
        {
            HttpClientHandlerHelper httpHandler = new HttpClientHandlerHelper
            {
                ResponseIntercepter = async(response) =>
                {
                    string responseString = await response.Content.ReadAsStringAsync();

                    if (responseString.Contains("databaseAccountEndpoint"))
                    {
                        AccountProperties accountProperties =
                            JsonConvert.DeserializeObject <AccountProperties>(responseString);
                        accountProperties.Consistency.DefaultConsistencyLevel = Cosmos.ConsistencyLevel.Eventual;
                        response.Content = new StringContent(JsonConvert.SerializeObject(accountProperties), Encoding.UTF8, "application/json");
                    }
                    return(response);
                }
            };

            RequestHandlerHelper handlerHelper = new RequestHandlerHelper
            {
                UpdateRequestMessage = (request) =>
                {
                    if (request.OperationType == Documents.OperationType.Read)
                    {
                        Assert.AreEqual(Cosmos.ConsistencyLevel.Strong.ToString(), request.Headers[Documents.HttpConstants.HttpHeaders.ConsistencyLevel]);
                    }
                }
            };

            this.cosmosClient = TestCommon.CreateCosmosClient(x =>
            {
                CosmosClientBuilder builder = x.AddCustomHandlers(handlerHelper)
                                              .WithHttpClientFactory(() => new HttpClient(httpHandler));
                if (isLocalQuorumConsistency)
                {
                    builder.AllowUpgradeConsistencyToLocalQuorum();
                }
            });

            Database database = await this.cosmosClient.CreateDatabaseAsync(Guid.NewGuid().ToString(),
                                                                            cancellationToken : new CancellationTokenSource().Token);

            return(await database.CreateContainerAsync(id : Guid.NewGuid().ToString(),
                                                       partitionKeyPath : "/pk"));
        }