예제 #1
0
        public async Task TestCustomPropertyWithHandler()
        {
            RequestHandlerHelper testHandler = new RequestHandlerHelper();

            // Add the random guid to the property
            Guid   randomGuid  = Guid.NewGuid();
            string propertyKey = "Test";

            testHandler.UpdateRequestMessage = x => x.Properties[propertyKey] = randomGuid;

            CosmosClient customClient = TestCommon.CreateCosmosClient(
                (cosmosClientBuilder) => cosmosClientBuilder.AddCustomHandlers(testHandler));

            ToDoActivity testItem = CreateRandomToDoActivity();

            using (ResponseMessage response = await customClient.GetContainer(this.database.Id, this.Container.Id).CreateItemStreamAsync(
                       partitionKey: new Cosmos.PartitionKey(testItem.status),
                       streamPayload: TestCommon.Serializer.ToStream(testItem)))
            {
                Assert.IsNotNull(response);
                Assert.IsNotNull(response.RequestMessage);
                Assert.IsNotNull(response.RequestMessage.Properties);
                Assert.AreEqual(randomGuid, response.RequestMessage.Properties[propertyKey]);
            }
        }
        public async Task TestBatchRequiredHeadersWithHandler()
        {
            RequestHandlerHelper testHandler = new RequestHandlerHelper();

            // Get the headers from request message for testing.
            Headers requestHeaders = null;

            testHandler.UpdateRequestMessage = x => requestHeaders = x.Headers;

            CosmosClient customClient = TestCommon.CreateCosmosClient(
                (cosmosClientBuilder) => cosmosClientBuilder.AddCustomHandlers(testHandler).WithBulkExecution(true));

            ToDoActivity testItem = this.CreateRandomToDoActivity();

            using (ResponseMessage response = await customClient.GetContainer(this.database.Id, this.Container.Id).CreateItemStreamAsync(
                       partitionKey: new Cosmos.PartitionKey(testItem.status),
                       streamPayload: TestCommon.SerializerCore.ToStream(testItem)))
            {
                Assert.IsNotNull(response);
                Assert.IsNotNull(requestHeaders);

                string isBatchAtomic = requestHeaders[HttpConstants.HttpHeaders.IsBatchAtomic];
                Assert.IsNotNull(isBatchAtomic);
                Assert.IsFalse(bool.Parse(isBatchAtomic));

                string isBatchRequest = requestHeaders[HttpConstants.HttpHeaders.IsBatchRequest];
                Assert.IsNotNull(isBatchRequest);
                Assert.IsTrue(bool.Parse(isBatchRequest));

                string shouldBatchContinueOnError = requestHeaders[HttpConstants.HttpHeaders.ShouldBatchContinueOnError];
                Assert.IsNotNull(shouldBatchContinueOnError);
                Assert.IsTrue(bool.Parse(shouldBatchContinueOnError));
            }
        }
예제 #3
0
        public async Task CheckTracesIncludedWithAllExceptionsTestAsync()
        {
            RequestHandlerHelper requestHandlerHelper = new RequestHandlerHelper();
            CosmosClient         cosmosClient         = TestCommon.CreateCosmosClient(
                customizeClientBuilder: builder => builder.AddCustomHandlers(requestHandlerHelper));
            Container containerWithFailure = cosmosClient.GetContainer(this.database.Id, this.Container.Id);

            requestHandlerHelper.UpdateRequestMessage = (request) => throw new ObjectDisposedException("Mock ObjectDisposedException");
            await this.CheckForTracesAsync <ObjectDisposedException>(containerWithFailure, isClientDisposed : false);

            cosmosClient.Dispose();
            requestHandlerHelper.UpdateRequestMessage = (request) => throw new ObjectDisposedException("Mock ObjectDisposedException");
            await this.CheckForTracesAsync <ObjectDisposedException>(containerWithFailure, isClientDisposed : true);
        }
예제 #4
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"));
        }
예제 #5
0
        public async Task CreateIfNotExists()
        {
            RequestChargeHandlerHelper requestChargeHandler = new RequestChargeHandlerHelper();
            RequestHandlerHelper       requestHandlerHelper = new RequestHandlerHelper();

            CosmosClient client = TestCommon.CreateCosmosClient(x => x.AddCustomHandlers(requestChargeHandler, requestHandlerHelper));

            // Create a new database
            requestChargeHandler.TotalRequestCharges = 0;
            DatabaseResponse createResponse = await client.CreateDatabaseIfNotExistsAsync(Guid.NewGuid().ToString());

            Assert.AreEqual(requestChargeHandler.TotalRequestCharges, createResponse.RequestCharge);
            Assert.AreEqual(HttpStatusCode.Created, createResponse.StatusCode);

            requestChargeHandler.TotalRequestCharges = 0;
            DatabaseResponse createExistingResponse = await client.CreateDatabaseIfNotExistsAsync(createResponse.Resource.Id);

            Assert.AreEqual(HttpStatusCode.OK, createExistingResponse.StatusCode);
            Assert.AreEqual(requestChargeHandler.TotalRequestCharges, createExistingResponse.RequestCharge);
            Assert.IsNotNull(createExistingResponse.Diagnostics);
            string diagnostics = createExistingResponse.Diagnostics.ToString();

            Assert.IsFalse(string.IsNullOrEmpty(diagnostics));
            Assert.IsTrue(diagnostics.Contains("CreateDatabaseIfNotExistsAsync"));

            bool conflictReturned = false;

            requestHandlerHelper.CallBackOnResponse = (request, response) =>
            {
                if (request.OperationType == Documents.OperationType.Create &&
                    request.ResourceType == Documents.ResourceType.Database)
                {
                    conflictReturned = true;
                    // Simulate a race condition which results in a 409
                    return(CosmosExceptionFactory.Create(
                               statusCode: HttpStatusCode.Conflict,
                               message: "Fake 409 conflict",
                               stackTrace: string.Empty,
                               headers: response.Headers,
                               error: default,