public async Task StatusCodesAreSetThroughResponseAsync()
        {
            List <TransactionalBatchOperationResult> results = new List <TransactionalBatchOperationResult>();

            ItemBatchOperation[] arrayOperations = new ItemBatchOperation[1];

            ItemBatchOperation operation = new ItemBatchOperation(OperationType.AddComputeGatewayRequestCharges, 0, "0");

            results.Add(
                new TransactionalBatchOperationResult(HttpStatusCode.OK)
            {
                ResourceStream = new MemoryStream(new byte[] { 0x41, 0x42 }, index: 0, count: 2, writable: false, publiclyVisible: true),
                ETag           = operation.Id
            });

            arrayOperations[0] = operation;

            MemoryStream responseContent = await new BatchResponsePayloadWriter(results).GeneratePayloadAsync();

            SinglePartitionKeyServerBatchRequest batchRequest = await SinglePartitionKeyServerBatchRequest.CreateAsync(
                partitionKey : null,
                operations : new ArraySegment <ItemBatchOperation>(arrayOperations),
                serializerCore : MockCosmosUtil.Serializer,
                cancellationToken : default(CancellationToken));

            TransactionalBatchResponse batchresponse = await TransactionalBatchResponse.FromResponseMessageAsync(
                new ResponseMessage(HttpStatusCode.OK) { Content = responseContent },
                batchRequest,
                MockCosmosUtil.Serializer);

            PartitionKeyRangeBatchResponse response = new PartitionKeyRangeBatchResponse(arrayOperations.Length, batchresponse, MockCosmosUtil.Serializer);

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        }
Beispiel #2
0
        public async Task BatchResponseDeserializationAsync()
        {
            using CosmosClient cosmosClient = MockCosmosUtil.CreateMockCosmosClient();
            ContainerInternal containerCore = (ContainerInlineCore)cosmosClient.GetDatabase("db").GetContainer("cont");
            List <TransactionalBatchOperationResult> results = new List <TransactionalBatchOperationResult>
            {
                new TransactionalBatchOperationResult(HttpStatusCode.Conflict),
                new TransactionalBatchOperationResult(HttpStatusCode.OK)
                {
                    ResourceStream = new MemoryStream(new byte[] { 0x41, 0x42 }, index: 0, count: 2, writable: false, publiclyVisible: true),
                    RequestCharge  = 2.5,
                    ETag           = "1234",
                    RetryAfter     = TimeSpan.FromMilliseconds(360)
                }
            };

            MemoryStream responseContent = await new BatchResponsePayloadWriter(results).GeneratePayloadAsync();

            CosmosSerializer serializer = new CosmosJsonDotNetSerializer();
            SinglePartitionKeyServerBatchRequest batchRequest = await SinglePartitionKeyServerBatchRequest.CreateAsync(
                partitionKey : Cosmos.PartitionKey.None,
                operations : new ArraySegment <ItemBatchOperation>(
                    new ItemBatchOperation[]
            {
                new ItemBatchOperation(OperationType.Read, operationIndex: 0, id: "someId", containerCore: containerCore),
                new ItemBatchOperation(OperationType.Read, operationIndex: 0, id: "someId", containerCore: containerCore)
            }),
                serializerCore : MockCosmosUtil.Serializer,
                trace : NoOpTrace.Singleton,
                cancellationToken : CancellationToken.None);

            ResponseMessage response = new ResponseMessage((HttpStatusCode)StatusCodes.MultiStatus)
            {
                Content = responseContent
            };

            response.Headers.Session    = Guid.NewGuid().ToString();
            response.Headers.ActivityId = Guid.NewGuid().ToString();
            TransactionalBatchResponse batchResponse = await TransactionalBatchResponse.FromResponseMessageAsync(
                response,
                batchRequest,
                MockCosmosUtil.Serializer,
                true,
                NoOpTrace.Singleton,
                CancellationToken.None);

            Assert.IsNotNull(batchRequest);
            Assert.AreEqual(HttpStatusCode.Conflict, batchResponse.StatusCode);
            Assert.AreEqual(2, batchResponse.Count);
            Assert.AreEqual(response.Headers.Session, batchResponse[0].SessionToken);
            Assert.AreEqual(response.Headers.Session, batchResponse[1].SessionToken);
            Assert.AreEqual(response.Headers.ActivityId, batchResponse[0].ActivityId);
            Assert.AreEqual(response.Headers.ActivityId, batchResponse[1].ActivityId);


            CosmosBatchOperationResultEqualityComparer comparer = new CosmosBatchOperationResultEqualityComparer();

            Assert.IsTrue(comparer.Equals(results[0], batchResponse[0]));
            Assert.IsTrue(comparer.Equals(results[1], batchResponse[1]));
        }
Beispiel #3
0
        public async Task DiagnosticsAreSetThroughResponseAsync()
        {
            List <TransactionalBatchOperationResult> results = new List <TransactionalBatchOperationResult>();

            ItemBatchOperation[] arrayOperations = new ItemBatchOperation[1];

            ItemBatchOperation operation = new ItemBatchOperation(OperationType.Read, 0, Cosmos.PartitionKey.Null, "0");

            results.Add(
                new TransactionalBatchOperationResult(HttpStatusCode.OK)
            {
                ResourceStream = new MemoryStream(new byte[] { 0x41, 0x42 }, index: 0, count: 2, writable: false, publiclyVisible: true),
                ETag           = operation.Id
            });

            arrayOperations[0] = operation;

            MemoryStream responseContent = await new BatchResponsePayloadWriter(results).GeneratePayloadAsync();

            SinglePartitionKeyServerBatchRequest batchRequest = await SinglePartitionKeyServerBatchRequest.CreateAsync(
                partitionKey : null,
                operations : new ArraySegment <ItemBatchOperation>(arrayOperations),
                serializerCore : MockCosmosUtil.Serializer,
                cancellationToken : default(CancellationToken));

            PointOperationStatistics diagnostics = new PointOperationStatistics(
                activityId: Guid.NewGuid().ToString(),
                statusCode: HttpStatusCode.OK,
                subStatusCode: SubStatusCodes.Unknown,
                responseTimeUtc: DateTime.UtcNow,
                requestCharge: 0,
                errorMessage: string.Empty,
                method: HttpMethod.Get,
                requestUri: "http://localhost",
                requestSessionToken: null,
                responseSessionToken: null);

            ResponseMessage responseMessage = new ResponseMessage(HttpStatusCode.OK)
            {
                Content = responseContent,
            };

            responseMessage.DiagnosticsContext.AddDiagnosticsInternal(diagnostics);

            TransactionalBatchResponse batchresponse = await TransactionalBatchResponse.FromResponseMessageAsync(
                responseMessage,
                batchRequest,
                MockCosmosUtil.Serializer,
                true,
                CancellationToken.None);

            PartitionKeyRangeBatchResponse response = new PartitionKeyRangeBatchResponse(arrayOperations.Length, batchresponse, MockCosmosUtil.Serializer);

            string pointDiagnosticString = diagnostics.ToString();

            pointDiagnosticString = pointDiagnosticString.Substring(1, pointDiagnosticString.Length - 2);
            string diagnosticContextString = response.DiagnosticsContext.ToString();

            Assert.IsTrue(diagnosticContextString.Contains(pointDiagnosticString));
        }
        public async Task DiagnosticsAreSetThroughResponseAsync()
        {
            List <TransactionalBatchOperationResult> results = new List <TransactionalBatchOperationResult>();

            ItemBatchOperation[] arrayOperations = new ItemBatchOperation[1];

            ItemBatchOperation operation = new ItemBatchOperation(OperationType.AddComputeGatewayRequestCharges, 0, "0");

            results.Add(
                new TransactionalBatchOperationResult(HttpStatusCode.OK)
            {
                ResourceStream = new MemoryStream(new byte[] { 0x41, 0x42 }, index: 0, count: 2, writable: false, publiclyVisible: true),
                ETag           = operation.Id
            });

            arrayOperations[0] = operation;

            MemoryStream responseContent = await new BatchResponsePayloadWriter(results).GeneratePayloadAsync();

            SinglePartitionKeyServerBatchRequest batchRequest = await SinglePartitionKeyServerBatchRequest.CreateAsync(
                partitionKey : null,
                operations : new ArraySegment <ItemBatchOperation>(arrayOperations),
                serializerCore : MockCosmosUtil.Serializer,
                cancellationToken : default(CancellationToken));

            CosmosDiagnostics diagnostics = new PointOperationStatistics(
                activityId: Guid.NewGuid().ToString(),
                statusCode: HttpStatusCode.OK,
                subStatusCode: SubStatusCodes.Unknown,
                requestCharge: 0,
                errorMessage: string.Empty,
                method: HttpMethod.Get,
                requestUri: new Uri("http://localhost"),
                requestSessionToken: null,
                responseSessionToken: null,
                clientSideRequestStatistics: new CosmosClientSideRequestStatistics());

            TransactionalBatchResponse batchresponse = await TransactionalBatchResponse.FromResponseMessageAsync(
                new ResponseMessage(HttpStatusCode.OK) { Content = responseContent, Diagnostics = diagnostics },
                batchRequest,
                MockCosmosUtil.Serializer);

            PartitionKeyRangeBatchResponse response = new PartitionKeyRangeBatchResponse(arrayOperations.Length, batchresponse, MockCosmosUtil.Serializer);

            Assert.AreEqual(diagnostics, response.Diagnostics);
        }
        private async Task <bool> ConstainsSplitIsTrueInternal(HttpStatusCode statusCode, SubStatusCodes subStatusCode)
        {
            List <TransactionalBatchOperationResult> results = new List <TransactionalBatchOperationResult>();

            ItemBatchOperation[] arrayOperations = new ItemBatchOperation[1];

            ItemBatchOperation operation = new ItemBatchOperation(OperationType.Read, 0, Cosmos.PartitionKey.Null, "0");

            results.Add(
                new TransactionalBatchOperationResult(HttpStatusCode.OK)
            {
                ResourceStream = new MemoryStream(new byte[] { 0x41, 0x42 }, index: 0, count: 2, writable: false, publiclyVisible: true),
                ETag           = operation.Id
            });

            arrayOperations[0] = operation;

            MemoryStream responseContent = await new BatchResponsePayloadWriter(results).GeneratePayloadAsync();

            SinglePartitionKeyServerBatchRequest batchRequest = await SinglePartitionKeyServerBatchRequest.CreateAsync(
                partitionKey : null,
                operations : new ArraySegment <ItemBatchOperation>(arrayOperations),
                serializerCore : MockCosmosUtil.Serializer,
                cancellationToken : default(CancellationToken));

            ResponseMessage response = new ResponseMessage(statusCode)
            {
                Content = responseContent
            };

            response.Headers.SubStatusCode = subStatusCode;

            TransactionalBatchResponse batchresponse = await TransactionalBatchResponse.FromResponseMessageAsync(
                response,
                batchRequest,
                MockCosmosUtil.Serializer,
                true,
                false,
                CancellationToken.None);

            PartitionKeyRangeBatchExecutionResult result = new PartitionKeyRangeBatchExecutionResult("0", arrayOperations, batchresponse);

            return(result.IsSplit());
        }
Beispiel #6
0
        public async Task BatchResponseDeserializationAsync()
        {
            List <TransactionalBatchOperationResult> results = new List <TransactionalBatchOperationResult>();

            results.Add(new TransactionalBatchOperationResult(HttpStatusCode.Conflict));

            results.Add(
                new TransactionalBatchOperationResult(HttpStatusCode.OK)
            {
                ResourceStream = new MemoryStream(new byte[] { 0x41, 0x42 }, index: 0, count: 2, writable: false, publiclyVisible: true),
                RequestCharge  = 2.5,
                ETag           = "1234",
                RetryAfter     = TimeSpan.FromMilliseconds(360)
            });

            MemoryStream responseContent = await new BatchResponsePayloadWriter(results).GeneratePayloadAsync();

            CosmosSerializer serializer = new CosmosJsonDotNetSerializer();
            SinglePartitionKeyServerBatchRequest batchRequest = await SinglePartitionKeyServerBatchRequest.CreateAsync(
                partitionKey : Cosmos.PartitionKey.None,
                operations : new ArraySegment <ItemBatchOperation>(
                    new ItemBatchOperation[]
            {
                new ItemBatchOperation(OperationType.Read, operationIndex: 0, id: "someId"),
                new ItemBatchOperation(OperationType.Read, operationIndex: 0, id: "someId")
            }),
                serializer : serializer,
                cancellationToken : CancellationToken.None);

            TransactionalBatchResponse batchResponse = await TransactionalBatchResponse.FromResponseMessageAsync(
                new ResponseMessage((HttpStatusCode)StatusCodes.MultiStatus) { Content = responseContent },
                batchRequest,
                serializer);

            Assert.IsNotNull(batchRequest);
            Assert.AreEqual(HttpStatusCode.Conflict, batchResponse.StatusCode);
            Assert.AreEqual(2, batchResponse.Count);

            CosmosBatchOperationResultEqualityComparer comparer = new CosmosBatchOperationResultEqualityComparer();

            Assert.IsTrue(comparer.Equals(results[0], batchResponse[0]));
            Assert.IsTrue(comparer.Equals(results[1], batchResponse[1]));
        }
Beispiel #7
0
        public async Task DiagnosticsAreSetThroughResponseAsync()
        {
            List <TransactionalBatchOperationResult> results = new List <TransactionalBatchOperationResult>();

            ItemBatchOperation[] arrayOperations = new ItemBatchOperation[1];

            ItemBatchOperation operation = new ItemBatchOperation(OperationType.Read, 0, Cosmos.PartitionKey.Null, "0");

            results.Add(
                new TransactionalBatchOperationResult(HttpStatusCode.OK)
            {
                ResourceStream = new MemoryStream(new byte[] { 0x41, 0x42 }, index: 0, count: 2, writable: false, publiclyVisible: true),
                ETag           = operation.Id
            });

            arrayOperations[0] = operation;

            MemoryStream responseContent = await new BatchResponsePayloadWriter(results).GeneratePayloadAsync();

            SinglePartitionKeyServerBatchRequest batchRequest = await SinglePartitionKeyServerBatchRequest.CreateAsync(
                partitionKey : null,
                operations : new ArraySegment <ItemBatchOperation>(arrayOperations),
                serializerCore : MockCosmosUtil.Serializer,
                trace : NoOpTrace.Singleton,
                cancellationToken : default(CancellationToken));

            ResponseMessage responseMessage = new ResponseMessage(HttpStatusCode.OK)
            {
                Content = responseContent,
            };

            PointOperationStatisticsTraceDatum diagnostics = new PointOperationStatisticsTraceDatum(
                activityId: Guid.NewGuid().ToString(),
                statusCode: HttpStatusCode.OK,
                subStatusCode: SubStatusCodes.Unknown,
                responseTimeUtc: DateTime.UtcNow,
                requestCharge: 0,
                errorMessage: string.Empty,
                method: HttpMethod.Get,
                requestUri: "http://localhost",
                requestSessionToken: null,
                responseSessionToken: null);

            TransactionalBatchResponse batchresponse;

            using (responseMessage.Trace = Trace.GetRootTrace("test trace"))
            {
                responseMessage.Trace.AddDatum("Point Operation Statistics", diagnostics);

                batchresponse = await TransactionalBatchResponse.FromResponseMessageAsync(
                    responseMessage,
                    batchRequest,
                    MockCosmosUtil.Serializer,
                    true,
                    responseMessage.Trace,
                    CancellationToken.None);
            }

            PartitionKeyRangeBatchResponse response = new PartitionKeyRangeBatchResponse(arrayOperations.Length, batchresponse, MockCosmosUtil.Serializer);

            if (!(response.Diagnostics is CosmosTraceDiagnostics cosmosTraceDiagnostics))
            {
                Assert.Fail();
                throw new Exception();
            }

            Assert.AreEqual(diagnostics, cosmosTraceDiagnostics.Value.Data.Values.First());
        }