public void ToResponseMessage_MapsProperties()
        {
            TransactionalBatchOperationResult result = new TransactionalBatchOperationResult(HttpStatusCode.OK)
            {
                ResourceStream = new MemoryStream(new byte[] { 0x41, 0x42 }, index: 0, count: 2, writable: false, publiclyVisible: true),
                ETag           = "1234",
                SubStatusCode  = SubStatusCodes.CompletingSplit,
                RetryAfter     = TimeSpan.FromSeconds(10),
                RequestCharge  = 4.3,
                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())
            };

            ResponseMessage response = result.ToResponseMessage();

            Assert.AreEqual(result.ResourceStream, response.Content);
            Assert.AreEqual(result.SubStatusCode, response.Headers.SubStatusCode);
            Assert.AreEqual(result.RetryAfter, response.Headers.RetryAfter);
            Assert.AreEqual(result.StatusCode, response.StatusCode);
            Assert.AreEqual(result.RequestCharge, response.Headers.RequestCharge);
            Assert.AreEqual(result.Diagnostics, response.Diagnostics);
        }
Ejemplo n.º 2
0
        public async Task RetriesOnSplits()
        {
            IDocumentClientRetryPolicy retryPolicy = new BulkPartitionKeyRangeGoneRetryPolicy(
                new ResourceThrottleRetryPolicy(1));

            TransactionalBatchOperationResult result = new TransactionalBatchOperationResult(HttpStatusCode.Gone)
            {
                SubStatusCode = SubStatusCodes.PartitionKeyRangeGone
            };
            ShouldRetryResult shouldRetryResult = await retryPolicy.ShouldRetryAsync(result.ToResponseMessage(), default(CancellationToken));

            Assert.IsTrue(shouldRetryResult.ShouldRetry);
        }
        public async Task RetriesOnCompletingPartitionMigrationSplits()
        {
            IDocumentClientRetryPolicy retryPolicy = new BulkPartitionKeyRangeGoneRetryPolicy(
                GetSplitEnabledContainer(),
                new ResourceThrottleRetryPolicy(1));

            TransactionalBatchOperationResult result = new TransactionalBatchOperationResult(HttpStatusCode.Gone)
            {
                SubStatusCode = SubStatusCodes.CompletingPartitionMigration
            };
            ShouldRetryResult shouldRetryResult = await retryPolicy.ShouldRetryAsync(result.ToResponseMessage(), default);

            Assert.IsTrue(shouldRetryResult.ShouldRetry);
        }
        public void ToResponseMessageHasPropertiesMapped()
        {
            TransactionalBatchOperationResult result = CreateTestResult();

            ResponseMessage response = result.ToResponseMessage();

            Assert.AreEqual(result.StatusCode, response.StatusCode);
            Assert.AreEqual(result.SubStatusCode, response.Headers.SubStatusCode);
            Assert.AreEqual(result.ETag, response.Headers.ETag);
            Assert.AreEqual(result.RequestCharge, response.Headers.RequestCharge);
            Assert.AreEqual(result.RetryAfter, response.Headers.RetryAfter);
            Assert.AreSame(result.ResourceStream, response.Content);
            Assert.IsNotNull(response.Diagnostics);
        }
Ejemplo n.º 5
0
        public async Task RetriesOnSplits_UpToMax()
        {
            IDocumentClientRetryPolicy retryPolicy = new BulkExecutionRetryPolicy(
                GetSplitEnabledContainer(),
                OperationType.Read,
                new ResourceThrottleRetryPolicy(1));

            TransactionalBatchOperationResult result = new TransactionalBatchOperationResult(HttpStatusCode.Gone)
            {
                SubStatusCode = SubStatusCodes.PartitionKeyRangeGone
            };
            ShouldRetryResult shouldRetryResult;

            for (int i = 0; i < 10; i++)
            {
                shouldRetryResult = await retryPolicy.ShouldRetryAsync(result.ToResponseMessage(), default);

                Assert.IsTrue(shouldRetryResult.ShouldRetry);
            }

            shouldRetryResult = await retryPolicy.ShouldRetryAsync(result.ToResponseMessage(), default);

            Assert.IsFalse(shouldRetryResult.ShouldRetry);
        }
Ejemplo n.º 6
0
        public async Task RetriesOn413_3204()
        {
            IDocumentClientRetryPolicy retryPolicy = new BulkExecutionRetryPolicy(
                Mock.Of <ContainerInternal>(),
                OperationType.Read,
                new ResourceThrottleRetryPolicy(1));

            TransactionalBatchOperationResult result = new TransactionalBatchOperationResult(HttpStatusCode.RequestEntityTooLarge)
            {
                SubStatusCode = (SubStatusCodes)3402
            };
            ShouldRetryResult shouldRetryResult = await retryPolicy.ShouldRetryAsync(result.ToResponseMessage(), default);

            Assert.IsTrue(shouldRetryResult.ShouldRetry);
        }
        public void ToResponseMessage_MapsProperties()
        {
            PointOperationStatistics pointOperationStatistics = 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);
            CosmosDiagnosticsContext context = new CosmosDiagnosticsContextCore();

            context.AddDiagnosticsInternal(pointOperationStatistics);

            TransactionalBatchOperationResult result = new TransactionalBatchOperationResult(HttpStatusCode.OK)
            {
                ResourceStream     = new MemoryStream(new byte[] { 0x41, 0x42 }, index: 0, count: 2, writable: false, publiclyVisible: true),
                ETag               = "1234",
                SubStatusCode      = SubStatusCodes.CompletingSplit,
                RetryAfter         = TimeSpan.FromSeconds(10),
                RequestCharge      = 4.3,
                DiagnosticsContext = context
            };

            ResponseMessage response = result.ToResponseMessage();

            Assert.AreEqual(result.ResourceStream, response.Content);
            Assert.AreEqual(result.SubStatusCode, response.Headers.SubStatusCode);
            Assert.AreEqual(result.RetryAfter, response.Headers.RetryAfter);
            Assert.AreEqual(result.StatusCode, response.StatusCode);
            Assert.AreEqual(result.RequestCharge, response.Headers.RequestCharge);
            string diagnostics = response.Diagnostics.ToString();

            Assert.IsNotNull(diagnostics);
            Assert.IsTrue(diagnostics.Contains(pointOperationStatistics.ActivityId));
        }
        public async Task RetryOnSplit()
        {
            ItemBatchOperation itemBatchOperation = CreateItem("test");

            Mock <CosmosClientContext> mockedContext = this.MockClientContext();

            mockedContext.Setup(c => c.ClientOptions).Returns(new CosmosClientOptions());
            mockedContext
            .SetupSequence(c => c.ProcessResourceOperationStreamAsync(
                               It.IsAny <string>(),
                               It.IsAny <ResourceType>(),
                               It.IsAny <OperationType>(),
                               It.IsAny <RequestOptions>(),
                               It.IsAny <ContainerInternal>(),
                               It.IsAny <Cosmos.FeedRange>(),
                               It.IsAny <Stream>(),
                               It.IsAny <Action <RequestMessage> >(),
                               It.IsAny <ITrace>(),
                               It.IsAny <CancellationToken>()))
            .Returns(GenerateSplitResponseAsync(itemBatchOperation))
            .Returns(GenerateOkResponseAsync(itemBatchOperation));

            mockedContext.Setup(c => c.SerializerCore).Returns(MockCosmosUtil.Serializer);

            string link = "/dbs/db/colls/colls";
            Mock <ContainerInternal> mockContainer = new Mock <ContainerInternal>();

            mockContainer.Setup(x => x.LinkUri).Returns(link);
            mockContainer.Setup(x => x.GetCachedContainerPropertiesAsync(It.IsAny <bool>(), It.IsAny <ITrace>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new ContainerProperties()
            {
                PartitionKey = new PartitionKeyDefinition()
                {
                    Paths = new Collection <string>()
                    {
                        "/id"
                    }
                }
            }));
            Mock <CosmosClientContext> context = this.MockClientContext();

            mockContainer.Setup(c => c.ClientContext).Returns(context.Object);
            context.Setup(c => c.DocumentClient).Returns(new ClientWithSplitDetection());


            CollectionRoutingMap routingMap = CollectionRoutingMap.TryCreateCompleteRoutingMap(
                new[]
            {
                Tuple.Create(new PartitionKeyRange {
                    Id = "0", MinInclusive = "", MaxExclusive = "FF"
                }, (ServiceIdentity)null)
            },
                string.Empty);

            mockContainer.Setup(x => x.GetRoutingMapAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(routingMap));
            BatchAsyncContainerExecutor       executor = new BatchAsyncContainerExecutor(mockContainer.Object, mockedContext.Object, 20, BatchAsyncContainerExecutorCache.DefaultMaxBulkRequestBodySizeInBytes);
            TransactionalBatchOperationResult result   = await executor.AddAsync(itemBatchOperation, NoOpTrace.Singleton);

            Mock.Get(mockContainer.Object)
            .Verify(x => x.GetCachedContainerPropertiesAsync(It.IsAny <bool>(), It.IsAny <ITrace>(), It.IsAny <CancellationToken>()), Times.Exactly(2));
            Mock.Get(mockedContext.Object)
            .Verify(c => c.ProcessResourceOperationStreamAsync(
                        It.IsAny <string>(),
                        It.IsAny <ResourceType>(),
                        It.IsAny <OperationType>(),
                        It.IsAny <RequestOptions>(),
                        It.IsAny <ContainerInternal>(),
                        It.IsAny <Cosmos.FeedRange>(),
                        It.IsAny <Stream>(),
                        It.IsAny <Action <RequestMessage> >(),
                        It.IsAny <ITrace>(),
                        It.IsAny <CancellationToken>()), Times.Exactly(2));
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.IsNotNull(result.ToResponseMessage().Trace);
        }
        public async Task RetriesOn429()
        {
            IDocumentClientRetryPolicy retryPolicy = new BulkPartitionKeyRangeGoneRetryPolicy(
                Mock.Of <ContainerInternal>(),
                new ResourceThrottleRetryPolicy(1));

            TransactionalBatchOperationResult result = new TransactionalBatchOperationResult((HttpStatusCode)StatusCodes.TooManyRequests);
            ShouldRetryResult shouldRetryResult      = await retryPolicy.ShouldRetryAsync(result.ToResponseMessage(), default);

            Assert.IsTrue(shouldRetryResult.ShouldRetry);
        }
        public async Task NotRetryOnSuccess()
        {
            IDocumentClientRetryPolicy retryPolicy = new BulkPartitionKeyRangeGoneRetryPolicy(
                Mock.Of <ContainerInternal>(),
                new ResourceThrottleRetryPolicy(1));

            TransactionalBatchOperationResult result = new TransactionalBatchOperationResult(HttpStatusCode.OK);
            ShouldRetryResult shouldRetryResult      = await retryPolicy.ShouldRetryAsync(result.ToResponseMessage(), default);

            Assert.IsFalse(shouldRetryResult.ShouldRetry);
        }