public void TestGoneRanges()
        {
            CollectionRoutingMap routingMap = CollectionRoutingMap.TryCreateCompleteRoutingMap(
                new[]
            {
                Tuple.Create(new PartitionKeyRange {
                    Id = "2", MinInclusive = "", MaxExclusive = "0000000030", Parents = new Collection <string> {
                        "1", "0"
                    }
                }, (ServiceIdentity)null),
                Tuple.Create(new PartitionKeyRange {
                    Id = "3", MinInclusive = "0000000030", MaxExclusive = "0000000032", Parents = new Collection <string> {
                        "5"
                    }
                }, (ServiceIdentity)null),
                Tuple.Create(new PartitionKeyRange {
                    Id = "4", MinInclusive = "0000000032", MaxExclusive = "FF"
                }, (ServiceIdentity)null),
            },
                string.Empty);

            Assert.IsTrue(routingMap.IsGone("1"));
            Assert.IsTrue(routingMap.IsGone("0"));
            Assert.IsTrue(routingMap.IsGone("5"));

            Assert.IsFalse(routingMap.IsGone("2"));
            Assert.IsFalse(routingMap.IsGone("3"));
            Assert.IsFalse(routingMap.IsGone("4"));
            Assert.IsFalse(routingMap.IsGone("100"));
        }
        public void TestIncompleteRoutingMap()
        {
            CollectionRoutingMap routingMap = CollectionRoutingMap.TryCreateCompleteRoutingMap(
                new[]
            {
                Tuple.Create(new PartitionKeyRange {
                    Id = "2", MinInclusive = "", MaxExclusive = "0000000030"
                }, (ServiceIdentity)null),
                Tuple.Create(new PartitionKeyRange {
                    Id = "3", MinInclusive = "0000000031", MaxExclusive = "FF"
                }, (ServiceIdentity)null),
            },
                string.Empty);

            Assert.IsNull(routingMap);

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

            Assert.IsNotNull(routingMap);
        }
        [Ignore] /* Buffer cannot be null */
        public async Task TestAddFormattedContinuationToHeader()
        {
            using (Stream stream = new MemoryStream(Properties.Resources.BaselineTest_PartitionRoutingHelper_AddFormattedContinuationToHeader))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    AddFormattedContinuationToHeaderTestData testData = JsonConvert.DeserializeObject <AddFormattedContinuationToHeaderTestData>(reader.ReadToEnd());

                    CollectionRoutingMap routingMap =
                        CollectionRoutingMap.TryCreateCompleteRoutingMap(
                            testData.RoutingMap.Select(range => Tuple.Create(range, (ServiceIdentity)null)), string.Empty);
                    RoutingMapProvider routingMapProvider = new RoutingMapProvider(routingMap);

                    foreach (AddFormattedContinuationToHeaderTestUnit positiveTestData in testData.TestSet.Postive)
                    {
                        INameValueCollection headers;
                        List <CompositeContinuationToken> resolvedContinuationTokens;
                        List <PartitionKeyRange>          resolvedRanges;

                        this.AddFormattedContinuationHeaderHelper(positiveTestData, out headers, out resolvedRanges, out resolvedContinuationTokens);

                        bool answer = await this.partitionRoutingHelper.TryAddPartitionKeyRangeToContinuationTokenAsync(headers, positiveTestData.ProvidedRanges, routingMapProvider, null, new PartitionRoutingHelper.ResolvedRangeInfo(resolvedRanges[0], (resolvedContinuationTokens.Count > 0) ? resolvedContinuationTokens : null), NoOpTrace.Singleton);

                        Assert.AreEqual(positiveTestData.OutputCompositeContinuationToken, headers[HttpConstants.HttpHeaders.Continuation]);
                    }

                    foreach (AddFormattedContinuationToHeaderTestUnit negativeTestData in testData.TestSet.Negative)
                    {
                        try
                        {
                            INameValueCollection headers;
                            List <CompositeContinuationToken> resolvedContinuationTokens;
                            List <PartitionKeyRange>          resolvedRanges;

                            this.AddFormattedContinuationHeaderHelper(negativeTestData, out headers, out resolvedRanges, out resolvedContinuationTokens);

                            bool answer = await this.partitionRoutingHelper.TryAddPartitionKeyRangeToContinuationTokenAsync(headers, negativeTestData.ProvidedRanges, routingMapProvider, null, new PartitionRoutingHelper.ResolvedRangeInfo(resolvedRanges[0], (resolvedContinuationTokens.Count > 0) ? resolvedContinuationTokens : null), NoOpTrace.Singleton);

                            Assert.Fail("Expect BadRequestException");
                        }
                        catch (BadRequestException)
                        {
                        }
                        catch (InternalServerErrorException)
                        {
                        }
                    }
                }
            }
        }
 public void TestInvalidRoutingMap()
 {
     CollectionRoutingMap.TryCreateCompleteRoutingMap(
         new[]
     {
         Tuple.Create(new PartitionKeyRange {
             Id = "1", MinInclusive = "0000000020", MaxExclusive = "0000000030"
         }, (ServiceIdentity)null),
         Tuple.Create(new PartitionKeyRange {
             Id = "2", MinInclusive = "0000000025", MaxExclusive = "0000000035"
         }, (ServiceIdentity)null),
     },
         string.Empty);
 }
        public async Task TestGetPartitionRoutingInfo()
        {
            using (Stream stream = new MemoryStream(Properties.Resources.BaselineTest_PartitionRoutingHelper_GetPartitionRoutingInfo))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    GetPartitionRoutingInfoTestData testData = JsonConvert.DeserializeObject <GetPartitionRoutingInfoTestData>(reader.ReadToEnd());

                    CollectionRoutingMap routingMap =
                        CollectionRoutingMap.TryCreateCompleteRoutingMap(
                            testData.RoutingMap.Select(range => Tuple.Create(range, (ServiceIdentity)null)), string.Empty);

                    foreach (GetPartitionRoutingInfoTestCase testCase in testData.TestCases)
                    {
                        List <string> actualPartitionKeyRangeIds = new List <string>();

                        Range <string> startRange = Range <string> .GetEmptyRange(PartitionKeyInternal.MinimumInclusiveEffectivePartitionKey);

                        for (Range <string> currentRange = startRange; currentRange != null;)
                        {
                            RoutingMapProvider routingMapProvider = new RoutingMapProvider(routingMap);
                            PartitionRoutingHelper.ResolvedRangeInfo resolvedRangeInfo = await this.partitionRoutingHelper.TryGetTargetRangeFromContinuationTokenRangeAsync(testCase.ProvidedRanges, routingMapProvider, string.Empty, currentRange, null, NoOpTrace.Singleton);

                            actualPartitionKeyRangeIds.Add(resolvedRangeInfo.ResolvedRange.Id);
                            INameValueCollection headers = new StoreRequestNameValueCollection();

                            await this.partitionRoutingHelper.TryAddPartitionKeyRangeToContinuationTokenAsync(headers, testCase.ProvidedRanges, routingMapProvider, string.Empty, resolvedRangeInfo, NoOpTrace.Singleton);

                            List <CompositeContinuationToken> suppliedTokens;
                            Range <string> nextRange = this.partitionRoutingHelper.ExtractPartitionKeyRangeFromContinuationToken(headers, out suppliedTokens);
                            currentRange = nextRange.IsEmpty ? null : nextRange;
                        }

                        Assert.AreEqual(string.Join(", ", testCase.RoutingRangeIds), string.Join(", ", actualPartitionKeyRangeIds));
                    }
                }
            }
        }
        public async Task RetryOnNameStale()
        {
            ItemBatchOperation itemBatchOperation = CreateItem("test");

            Mock <CosmosClientContext> mockedContext = new Mock <CosmosClientContext>();

            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.PartitionKey?>(),
                               It.IsAny <Stream>(),
                               It.IsAny <Action <RequestMessage> >(),
                               It.IsAny <CosmosDiagnosticsContext>(),
                               It.IsAny <ITrace>(),
                               It.IsAny <CancellationToken>()))
            .Returns(this.GenerateCacheStaleResponseAsync(itemBatchOperation))
            .Returns(this.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.GetPartitionKeyDefinitionAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(new PartitionKeyDefinition()
            {
                Paths = new Collection <string>()
                {
                    "/id"
                }
            }));

            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);

            Mock.Get(mockContainer.Object)
            .Verify(x => x.GetPartitionKeyDefinitionAsync(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.PartitionKey?>(),
                        It.IsAny <Stream>(),
                        It.IsAny <Action <RequestMessage> >(),
                        It.IsAny <CosmosDiagnosticsContext>(),
                        It.IsAny <ITrace>(),
                        It.IsAny <CancellationToken>()), Times.Exactly(2));
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.IsNotNull(result.DiagnosticsContext);

            string diagnosticsString = result.DiagnosticsContext.ToString();

            Assert.IsTrue(diagnosticsString.Contains("PointOperationStatistics"), "Diagnostics might be missing");
        }
        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);
        }
Beispiel #8
0
 public MockRoutingMapProvider(IList <PartitionKeyRange> ranges)
 {
     this.routingMap = CollectionRoutingMap.TryCreateCompleteRoutingMap(ranges.Select(r => Tuple.Create(r, (ServiceIdentity)null)), "");
 }
        public void TestTryCombineRanges()
        {
            CollectionRoutingMap routingMap = CollectionRoutingMap.TryCreateCompleteRoutingMap(
                new[]
            {
                Tuple.Create(
                    new PartitionKeyRange {
                    Id           = "2",
                    MinInclusive = "0000000050",
                    MaxExclusive = "0000000070"
                },
                    (ServiceIdentity)null),

                Tuple.Create(
                    new PartitionKeyRange {
                    Id           = "0",
                    MinInclusive = "",
                    MaxExclusive = "0000000030"
                },
                    (ServiceIdentity)null),

                Tuple.Create(
                    new PartitionKeyRange {
                    Id           = "1",
                    MinInclusive = "0000000030",
                    MaxExclusive = "0000000050"
                },
                    (ServiceIdentity)null),

                Tuple.Create(
                    new PartitionKeyRange {
                    Id           = "3",
                    MinInclusive = "0000000070",
                    MaxExclusive = "FF"
                },
                    (ServiceIdentity)null),
            }, string.Empty);

            CollectionRoutingMap newRoutingMap = routingMap.TryCombine(
                new[]
            {
                Tuple.Create(
                    new PartitionKeyRange {
                    Id      = "4",
                    Parents = new Collection <string> {
                        "0"
                    },
                    MinInclusive = "",
                    MaxExclusive = "0000000010"
                },
                    (ServiceIdentity)null),

                Tuple.Create(
                    new PartitionKeyRange {
                    Id      = "5",
                    Parents = new Collection <string> {
                        "0"
                    },
                    MinInclusive = "0000000010",
                    MaxExclusive = "0000000030"
                },
                    (ServiceIdentity)null),
            },
                null);

            Assert.IsNotNull(newRoutingMap);

            newRoutingMap = routingMap.TryCombine(
                new[]
            {
                Tuple.Create(
                    new PartitionKeyRange {
                    Id      = "6",
                    Parents = new Collection <string> {
                        "0", "4"
                    },
                    MinInclusive = "",
                    MaxExclusive = "0000000005"
                },
                    (ServiceIdentity)null),

                Tuple.Create(
                    new PartitionKeyRange {
                    Id      = "7",
                    Parents = new Collection <string> {
                        "0", "4"
                    },
                    MinInclusive = "0000000005",
                    MaxExclusive = "0000000010"
                },
                    (ServiceIdentity)null),

                Tuple.Create(
                    new PartitionKeyRange {
                    Id      = "8",
                    Parents = new Collection <string> {
                        "0", "5"
                    },
                    MinInclusive = "0000000010",
                    MaxExclusive = "0000000015"
                },
                    (ServiceIdentity)null),

                Tuple.Create(
                    new PartitionKeyRange {
                    Id      = "9",
                    Parents = new Collection <string> {
                        "0", "5"
                    },
                    MinInclusive = "0000000015",
                    MaxExclusive = "0000000030"
                },
                    (ServiceIdentity)null),
            },
                null);

            Assert.IsNotNull(newRoutingMap);

            newRoutingMap = routingMap.TryCombine(
                new[]
            {
                Tuple.Create(
                    new PartitionKeyRange {
                    Id      = "10",
                    Parents = new Collection <string> {
                        "0", "4", "6"
                    },
                    MinInclusive = "",
                    MaxExclusive = "0000000002"
                },
                    (ServiceIdentity)null),
            },
                null);

            Assert.IsNull(newRoutingMap);
        }
        public void TestCollectionRoutingMap()
        {
            ServiceIdentity      serviceIdentity0 = new ServiceIdentity("1", new Uri("http://1"), false);
            ServiceIdentity      serviceIdentity1 = new ServiceIdentity("2", new Uri("http://2"), false);
            ServiceIdentity      serviceIdentity2 = new ServiceIdentity("3", new Uri("http://3"), false);
            ServiceIdentity      serviceIdentity3 = new ServiceIdentity("4", new Uri("http://4"), false);
            CollectionRoutingMap routingMap       = CollectionRoutingMap.TryCreateCompleteRoutingMap(
                new[]
            {
                Tuple.Create(
                    new PartitionKeyRange {
                    Id           = "2",
                    MinInclusive = "0000000050",
                    MaxExclusive = "0000000070"
                },
                    serviceIdentity2),

                Tuple.Create(
                    new PartitionKeyRange {
                    Id           = "0",
                    MinInclusive = "",
                    MaxExclusive = "0000000030"
                },
                    serviceIdentity0),

                Tuple.Create(
                    new PartitionKeyRange {
                    Id           = "1",
                    MinInclusive = "0000000030",
                    MaxExclusive = "0000000050"
                },
                    serviceIdentity1),

                Tuple.Create(
                    new PartitionKeyRange {
                    Id           = "3",
                    MinInclusive = "0000000070",
                    MaxExclusive = "FF"
                },
                    serviceIdentity3),
            }, string.Empty);

            Assert.AreEqual("0", routingMap.OrderedPartitionKeyRanges[0].Id);
            Assert.AreEqual("1", routingMap.OrderedPartitionKeyRanges[1].Id);
            Assert.AreEqual("2", routingMap.OrderedPartitionKeyRanges[2].Id);
            Assert.AreEqual("3", routingMap.OrderedPartitionKeyRanges[3].Id);

            Assert.AreEqual(serviceIdentity0, routingMap.TryGetInfoByPartitionKeyRangeId("0"));
            Assert.AreEqual(serviceIdentity1, routingMap.TryGetInfoByPartitionKeyRangeId("1"));
            Assert.AreEqual(serviceIdentity2, routingMap.TryGetInfoByPartitionKeyRangeId("2"));
            Assert.AreEqual(serviceIdentity3, routingMap.TryGetInfoByPartitionKeyRangeId("3"));

            Assert.AreEqual("0", routingMap.GetRangeByEffectivePartitionKey("").Id);
            Assert.AreEqual("0", routingMap.GetRangeByEffectivePartitionKey("0000000000").Id);
            Assert.AreEqual("1", routingMap.GetRangeByEffectivePartitionKey("0000000030").Id);
            Assert.AreEqual("1", routingMap.GetRangeByEffectivePartitionKey("0000000031").Id);
            Assert.AreEqual("3", routingMap.GetRangeByEffectivePartitionKey("0000000071").Id);

            Assert.AreEqual("0", routingMap.TryGetRangeByPartitionKeyRangeId("0").Id);
            Assert.AreEqual("1", routingMap.TryGetRangeByPartitionKeyRangeId("1").Id);

            Assert.AreEqual(4, routingMap.GetOverlappingRanges(new[] { new Range <string>(PartitionKeyInternal.MinimumInclusiveEffectivePartitionKey, PartitionKeyInternal.MaximumExclusiveEffectivePartitionKey, true, false) }).Count);
            Assert.AreEqual(0, routingMap.GetOverlappingRanges(new[] { new Range <string>(PartitionKeyInternal.MinimumInclusiveEffectivePartitionKey, PartitionKeyInternal.MinimumInclusiveEffectivePartitionKey, false, false) }).Count);
            IReadOnlyList <PartitionKeyRange> partitionKeyRanges =
                routingMap.GetOverlappingRanges(new[]
            {
                new Range <string>(
                    "0000000040",
                    "0000000040",
                    true,
                    true)
            });

            Assert.AreEqual(1, partitionKeyRanges.Count);
            Assert.AreEqual("1", partitionKeyRanges.ElementAt(0).Id);

            IReadOnlyList <PartitionKeyRange> partitionKeyRanges1 =
                routingMap.GetOverlappingRanges(new[]
            {
                new Range <string>(
                    "0000000040",
                    "0000000045",
                    true,
                    true),
                new Range <string>(
                    "0000000045",
                    "0000000046",
                    true,
                    true),
                new Range <string>(
                    "0000000046",
                    "0000000050",
                    true,
                    true)
            });

            Assert.AreEqual(2, partitionKeyRanges1.Count);
            Assert.AreEqual("1", partitionKeyRanges1.ElementAt(0).Id);
            Assert.AreEqual("2", partitionKeyRanges1.ElementAt(1).Id);
        }
Beispiel #11
0
        public async Task DoesNotRecalculatePartitionKeyRangeOnNoSplits()
        {
            ItemBatchOperation itemBatchOperation = CreateItem("test");

            Mock <CosmosClientContext> mockedContext = new Mock <CosmosClientContext>();

            mockedContext.Setup(c => c.ClientOptions).Returns(new CosmosClientOptions());
            mockedContext
            .Setup(c => c.ProcessResourceOperationStreamAsync(
                       It.IsAny <Uri>(),
                       It.IsAny <ResourceType>(),
                       It.IsAny <OperationType>(),
                       It.IsAny <RequestOptions>(),
                       It.IsAny <ContainerInternal>(),
                       It.IsAny <Cosmos.PartitionKey?>(),
                       It.IsAny <Stream>(),
                       It.IsAny <Action <RequestMessage> >(),
                       It.IsAny <CosmosDiagnosticsContext>(),
                       It.IsAny <CancellationToken>()))
            .Returns(this.GenerateOkResponseAsync(itemBatchOperation));

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

            Uri link = new Uri($"/dbs/db/colls/colls", UriKind.Relative);
            Mock <ContainerInternal> mockContainer = new Mock <ContainerInternal>();

            mockContainer.Setup(x => x.LinkUri).Returns(link);
            mockContainer.Setup(x => x.GetPartitionKeyDefinitionAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(new PartitionKeyDefinition()
            {
                Paths = new Collection <string>()
                {
                    "/id"
                }
            }));

            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, Constants.MaxDirectModeBatchRequestBodySizeInBytes, 1);
            TransactionalBatchOperationResult result   = await executor.AddAsync(itemBatchOperation);

            Mock.Get(mockContainer.Object)
            .Verify(x => x.GetPartitionKeyDefinitionAsync(It.IsAny <CancellationToken>()), Times.Once);
            Mock.Get(mockedContext.Object)
            .Verify(c => c.ProcessResourceOperationStreamAsync(
                        It.IsAny <Uri>(),
                        It.IsAny <ResourceType>(),
                        It.IsAny <OperationType>(),
                        It.IsAny <RequestOptions>(),
                        It.IsAny <ContainerInternal>(),
                        It.IsAny <Cosmos.PartitionKey?>(),
                        It.IsAny <Stream>(),
                        It.IsAny <Action <RequestMessage> >(),
                        It.IsAny <CosmosDiagnosticsContext>(),
                        It.IsAny <CancellationToken>()), Times.Once);
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
        }
Beispiel #12
0
        private void Init()
        {
            this.collectionCache = new Mock <ClientCollectionCache>(null, new ServerStoreModel(null), null, null);

            ContainerProperties containerProperties = ContainerProperties.CreateWithResourceId("test");

            containerProperties.PartitionKey = partitionKeyDefinition;
            this.collectionCache.Setup
                (m =>
                m.ResolveCollectionAsync(
                    It.IsAny <DocumentServiceRequest>(),
                    It.IsAny <CancellationToken>(),
                    It.IsAny <ITrace>()
                    )
                ).Returns(Task.FromResult(containerProperties));

            this.collectionCache.Setup(x =>
                                       x.ResolveByNameAsync(
                                           It.IsAny <string>(),
                                           It.IsAny <string>(),
                                           It.IsAny <bool>(),
                                           It.IsAny <ITrace>(),
                                           It.IsAny <IClientSideRequestStatistics>(),
                                           It.IsAny <CancellationToken>())).Returns(Task.FromResult(containerProperties));

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

            this.partitionKeyRangeCache = new Mock <PartitionKeyRangeCache>(null, null, null);
            this.partitionKeyRangeCache.Setup(
                m => m.TryLookupAsync(
                    It.IsAny <string>(),
                    It.IsAny <CollectionRoutingMap>(),
                    It.IsAny <DocumentServiceRequest>(),
                    It.IsAny <CancellationToken>(),
                    It.IsAny <ITrace>()
                    )
                ).Returns(Task.FromResult <CollectionRoutingMap>(routingMap));

            List <PartitionKeyRange> result = new List <PartitionKeyRange>
            {
                new PartitionKeyRange()
                {
                    MinInclusive = Documents.Routing.PartitionKeyInternal.MinimumInclusiveEffectivePartitionKey,
                    MaxExclusive = Documents.Routing.PartitionKeyInternal.MaximumExclusiveEffectivePartitionKey,
                    Id           = "0"
                }
            };

            this.partitionKeyRangeCache
            .Setup(m => m.TryGetOverlappingRangesAsync(
                       It.IsAny <string>(),
                       It.IsAny <Documents.Routing.Range <string> >(),
                       It.IsAny <ITrace>(),
                       It.IsAny <bool>()))
            .Returns(Task.FromResult((IReadOnlyList <PartitionKeyRange>)result));

            this.globalEndpointManager = new Mock <GlobalEndpointManager>(this, new ConnectionPolicy());

            this.InitStoreModels();
        }
        public async Task RetryOnSplit()
        {
            ItemBatchOperation itemBatchOperation = CreateItem("test");

            Mock <CosmosClientContext> mockedContext = new Mock <CosmosClientContext>();

            mockedContext.Setup(c => c.ClientOptions).Returns(new CosmosClientOptions());
            mockedContext
            .SetupSequence(c => c.ProcessResourceOperationStreamAsync(
                               It.IsAny <Uri>(),
                               It.IsAny <ResourceType>(),
                               It.IsAny <OperationType>(),
                               It.IsAny <RequestOptions>(),
                               It.IsAny <ContainerCore>(),
                               It.IsAny <Cosmos.PartitionKey?>(),
                               It.IsAny <Stream>(),
                               It.IsAny <Action <RequestMessage> >(),
                               It.IsAny <CancellationToken>()))
            .Returns(this.GenerateSplitResponseAsync(itemBatchOperation))
            .Returns(this.GenerateOkResponseAsync(itemBatchOperation));

            mockedContext.Setup(c => c.CosmosSerializer).Returns(new CosmosJsonDotNetSerializer());

            Uri link = new Uri($"/dbs/db/colls/colls", UriKind.Relative);
            Mock <ContainerCore> mockContainer = new Mock <ContainerCore>();

            mockContainer.Setup(x => x.LinkUri).Returns(link);
            mockContainer.Setup(x => x.GetPartitionKeyDefinitionAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(new PartitionKeyDefinition()
            {
                Paths = new Collection <string>()
                {
                    "/id"
                }
            }));

            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, Constants.MaxDirectModeBatchRequestBodySizeInBytes, 1);
            TransactionalBatchOperationResult result   = await executor.AddAsync(itemBatchOperation);

            Mock.Get(mockContainer.Object)
            .Verify(x => x.GetPartitionKeyDefinitionAsync(It.IsAny <CancellationToken>()), Times.Exactly(2));
            Mock.Get(mockedContext.Object)
            .Verify(c => c.ProcessResourceOperationStreamAsync(
                        It.IsAny <Uri>(),
                        It.IsAny <ResourceType>(),
                        It.IsAny <OperationType>(),
                        It.IsAny <RequestOptions>(),
                        It.IsAny <ContainerCore>(),
                        It.IsAny <Cosmos.PartitionKey?>(),
                        It.IsAny <Stream>(),
                        It.IsAny <Action <RequestMessage> >(),
                        It.IsAny <CancellationToken>()), Times.Exactly(2));
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.IsNotNull(result.Diagnostics);

            int    diagnosticsLines  = 0;
            string diagnosticsString = result.Diagnostics.ToString();
            int    index             = diagnosticsString.IndexOf(Environment.NewLine);

            while (index > -1)
            {
                diagnosticsLines++;
                index = diagnosticsString.IndexOf(Environment.NewLine, index + 1);
            }

            Assert.IsTrue(diagnosticsLines > 1, "Diagnostics might be missing");
        }