private async Task <ResponseMessage> GenerateSplitResponseAsync(ItemBatchOperation itemBatchOperation)
        {
            List <TransactionalBatchOperationResult> results = new List <TransactionalBatchOperationResult>();

            ItemBatchOperation[] arrayOperations = new ItemBatchOperation[1];
            results.Add(
                new TransactionalBatchOperationResult(HttpStatusCode.Gone)
            {
                ETag          = itemBatchOperation.Id,
                SubStatusCode = SubStatusCodes.PartitionKeyRangeGone
            });

            arrayOperations[0] = itemBatchOperation;

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

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

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

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

            responseMessage.Headers.SubStatusCode = SubStatusCodes.PartitionKeyRangeGone;
            return(responseMessage);
        }
示例#2
0
        public async Task BatchRequestSerializationAsync()
        {
            const string partitionKey1 = "pk1";

            ItemBatchOperation[] operations = new ItemBatchOperation[]
            {
                new ItemBatchOperation(
                    operationType: OperationType.Create,
                    operationIndex: 0)
                {
                    ResourceBody = new byte[] { 0x41, 0x42 }
                },
                new ItemBatchOperation(
                    id: "id2",
                    operationType: OperationType.Replace,
                    operationIndex: 1,
                    requestOptions: new TransactionalBatchItemRequestOptions()
                {
                    IfMatchEtag = "theCondition"
                })
            };

            ServerBatchRequest batchRequest = await SinglePartitionKeyServerBatchRequest.CreateAsync(
                new Cosmos.PartitionKey(partitionKey1),
                new ArraySegment <ItemBatchOperation>(operations),
                serializer : new CosmosJsonDotNetSerializer(),
                cancellationToken : CancellationToken.None);

            Assert.AreEqual(2, batchRequest.Operations.Count);

            using (MemoryStream payload = batchRequest.TransferBodyStream())
            {
                Assert.IsNotNull(payload);

                List <ItemBatchOperation> readOperations = await new BatchRequestPayloadReader().ReadPayloadAsync(payload);
                Assert.AreEqual(2, readOperations.Count);
                ItemBatchOperationEqualityComparer comparer = new ItemBatchOperationEqualityComparer();
                Assert.IsTrue(comparer.Equals(operations[0], readOperations[0]));
                Assert.IsTrue(comparer.Equals(operations[1], readOperations[1]));
            }
        }
        private async Task <ResponseMessage> GenerateCacheStaleResponseAsync(ItemBatchOperation itemBatchOperation)
        {
            List <TransactionalBatchOperationResult> results = new List <TransactionalBatchOperationResult>();

            ItemBatchOperation[] arrayOperations = new ItemBatchOperation[1];
            results.Add(
                new TransactionalBatchOperationResult(HttpStatusCode.Gone)
            {
                ETag          = itemBatchOperation.Id,
                SubStatusCode = SubStatusCodes.NameCacheIsStale
            });

            arrayOperations[0] = itemBatchOperation;

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

            SinglePartitionKeyServerBatchRequest batchRequest = await SinglePartitionKeyServerBatchRequest.CreateAsync(
                partitionKey : null,
                operations : new ArraySegment <ItemBatchOperation>(arrayOperations),
                serializer : new CosmosJsonDotNetSerializer(),
                cancellationToken : CancellationToken.None);

            ResponseMessage responseMessage = new ResponseMessage(HttpStatusCode.Gone)
            {
                Content     = responseContent,
                Diagnostics = new PointOperationStatistics(
                    activityId: Guid.NewGuid().ToString(),
                    statusCode: HttpStatusCode.Gone,
                    subStatusCode: SubStatusCodes.Unknown,
                    requestCharge: 0,
                    errorMessage: string.Empty,
                    method: HttpMethod.Get,
                    requestUri: new Uri("http://localhost"),
                    requestSessionToken: null,
                    responseSessionToken: null,
                    clientSideRequestStatistics: new CosmosClientSideRequestStatistics())
            };

            responseMessage.Headers.SubStatusCode = SubStatusCodes.NameCacheIsStale;
            return(responseMessage);
        }
        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, "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);

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

            return(result.IsSplit());
        }
        public async Task StatusCodesAreSetThroughResponseAsync()
        {
            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));

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

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

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        }
        public async Task DiagnosticsAreSetThroughResponseAsync()
        {
            List <BatchOperationResult> results = new List <BatchOperationResult>();

            ItemBatchOperation[] arrayOperations = new ItemBatchOperation[1];

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

            results.Add(
                new BatchOperationResult(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),
                maxBodyLength : 100,
                maxOperationCount : 1,
                serializer : new CosmosJsonDotNetSerializer(),
                cancellationToken : default(CancellationToken));

            CosmosDiagnostics diagnostics = new PointOperationStatistics(HttpStatusCode.OK, SubStatusCodes.Unknown, 0, string.Empty, HttpMethod.Get, new Uri("http://localhost"), new CosmosClientSideRequestStatistics());

            BatchResponse batchresponse = await BatchResponse.PopulateFromContentAsync(
                new ResponseMessage(HttpStatusCode.OK) { Content = responseContent, Diagnostics = diagnostics },
                batchRequest,
                new CosmosJsonDotNetSerializer());

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

            Assert.AreEqual(diagnostics, response.Diagnostics);
        }
示例#7
0
        private async Task <ResponseMessage> GenerateOkResponseAsync(ItemBatchOperation itemBatchOperation)
        {
            List <TransactionalBatchOperationResult> results = new List <TransactionalBatchOperationResult>();

            ItemBatchOperation[] arrayOperations = new ItemBatchOperation[1];
            results.Add(
                new TransactionalBatchOperationResult(HttpStatusCode.OK)
            {
                ETag = itemBatchOperation.Id
            });

            arrayOperations[0] = itemBatchOperation;

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

            SinglePartitionKeyServerBatchRequest batchRequest = await SinglePartitionKeyServerBatchRequest.CreateAsync(
                partitionKey : null,
                operations : new ArraySegment <ItemBatchOperation>(arrayOperations),
                serializer : new CosmosJsonDotNetSerializer(),
                cancellationToken : CancellationToken.None);

            ResponseMessage responseMessage = new ResponseMessage(HttpStatusCode.OK)
            {
                Content     = responseContent,
                Diagnostics = new PointOperationStatistics(
                    Guid.NewGuid().ToString(),
                    HttpStatusCode.OK,
                    SubStatusCodes.Unknown,
                    0,
                    string.Empty,
                    HttpMethod.Get,
                    new Uri("http://localhost"),
                    new CosmosClientSideRequestStatistics())
            };

            return(responseMessage);
        }
示例#8
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());
        }
示例#9
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: new Uri("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,
                false,
                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));
        }