public async Task ChangeFeedIteratorCore_PartitionKeyRangeId_ReadAll()
        {
            int totalDocuments = 200;

            await this.CreateRandomItems(this.LargerContainer, totalDocuments, randomPartitionKey : true);

            DocumentFeedResponse <Documents.PartitionKeyRange> ranges = await this.LargerContainer.ClientContext.DocumentClient.ReadPartitionKeyRangeFeedAsync(this.LargerContainer.LinkUri);

            List <FeedToken> tokens = new List <FeedToken>(ranges.Count);

            foreach (Documents.PartitionKeyRange range in ranges)
            {
                tokens.Add(new FeedTokenPartitionKeyRange(range.Id));
            }

            ContainerCore      itemsCore = this.LargerContainer;
            List <Task <int> > tasks     = tokens.Select(token => Task.Run(async() =>
            {
                int count = 0;
                ChangeFeedIteratorCore iteratorForToken =
                    itemsCore.GetChangeFeedStreamIterator(token, changeFeedRequestOptions: new ChangeFeedRequestOptions()
                {
                    StartTime = DateTime.MinValue.ToUniversalTime()
                }) as ChangeFeedIteratorCore;
                while (iteratorForToken.HasMoreResults)
                {
                    using (ResponseMessage responseMessage =
                               await iteratorForToken.ReadNextAsync(this.cancellationToken))
                    {
                        if (responseMessage.Content != null)
                        {
                            Collection <ToDoActivity> response = TestCommon.SerializerCore.FromStream <CosmosFeedResponseUtil <ToDoActivity> >(responseMessage.Content).Data;
                            count += response.Count;
                        }
                    }
                }

                return(count);
            })).ToList();

            await Task.WhenAll(tasks);

            int documentsRead = 0;

            foreach (Task <int> task in tasks)
            {
                documentsRead += task.Result;
            }

            Assert.AreEqual(totalDocuments, documentsRead);
        }
        public void ValidateUriGenerationForResources()
        {
            string databaseId = "db1234";
            string crId       = "cr42";

            CosmosClientContext context = this.CreateMockClientContext();
            DatabaseCore        db      = new DatabaseCore(context, databaseId);

            Assert.AreEqual(db.LinkUri.OriginalString, "dbs/" + databaseId);

            ContainerCore container = new ContainerCore(context, db, crId);

            Assert.AreEqual(container.LinkUri.OriginalString, "dbs/" + databaseId + "/colls/" + crId);
        }
        public async Task ConflictsFeedSetsPartitionKeyRangeIdentity()
        {
            ContainerCore container = CosmosConflictTests.GetMockedContainer((request, cancellationToken) => {
                Assert.IsNotNull(request.ToDocumentServiceRequest().PartitionKeyRangeIdentity);
                return(TestHandler.ReturnSuccess());
            });

            FeedIterator iterator = container.Conflicts.GetConflictQueryStreamIterator();

            while (iterator.HasMoreResults)
            {
                ResponseMessage responseMessage = await iterator.ReadNextAsync();
            }
        }
Esempio n. 4
0
        public async Task TestInitialize()
        {
            await base.TestInit();

            string            PartitionKey = "/status";
            ContainerResponse response     = await this.database.CreateContainerAsync(
                new ContainerProperties(id : Guid.NewGuid().ToString(), partitionKeyPath : PartitionKey),
                cancellationToken : this.cancellationToken);

            Assert.IsNotNull(response);
            Assert.IsNotNull(response.Container);
            Assert.IsNotNull(response.Resource);
            this.Container = (ContainerCore)response;
        }
        public async Task ReadFeedIteratorCore_WithNoInitialState_ReadNextAsync()
        {
            string          continuation    = "TBD";
            ResponseMessage responseMessage = new ResponseMessage(HttpStatusCode.OK);

            responseMessage.Headers.ContinuationToken = continuation;
            responseMessage.Headers[Documents.HttpConstants.HttpHeaders.ItemCount] = "1";

            MultiRangeMockDocumentClient documentClient = new MultiRangeMockDocumentClient();

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

            cosmosClientContext.Setup(c => c.ClientOptions).Returns(new CosmosClientOptions());
            cosmosClientContext.Setup(c => c.DocumentClient).Returns(documentClient);
            cosmosClientContext
            .Setup(c => c.ProcessResourceOperationStreamAsync(
                       It.IsAny <Uri>(),
                       It.IsAny <Documents.ResourceType>(),
                       It.IsAny <Documents.OperationType>(),
                       It.IsAny <RequestOptions>(),
                       It.IsAny <ContainerCore>(),
                       It.IsAny <PartitionKey?>(),
                       It.IsAny <Stream>(),
                       It.IsAny <Action <RequestMessage> >(),
                       It.IsAny <CosmosDiagnosticsContext>(),
                       It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(responseMessage));

            ContainerCore containerCore = Mock.Of <ContainerCore>();

            Mock.Get(containerCore)
            .Setup(c => c.ClientContext)
            .Returns(cosmosClientContext.Object);
            Mock.Get(containerCore)
            .Setup(c => c.GetRIDAsync(It.IsAny <CancellationToken>()))
            .ReturnsAsync(Guid.NewGuid().ToString());

            FeedRangeIteratorCore feedTokenIterator = FeedRangeIteratorCore.Create(containerCore, null, null, new QueryRequestOptions());
            ResponseMessage       response          = await feedTokenIterator.ReadNextAsync();

            Assert.IsTrue(FeedRangeContinuation.TryParse(response.ContinuationToken, out FeedRangeContinuation parsedToken));
            FeedRangeCompositeContinuation feedRangeCompositeContinuation = parsedToken as FeedRangeCompositeContinuation;
            FeedRangeEPK feedTokenEPKRange = feedRangeCompositeContinuation.FeedRange as FeedRangeEPK;

            // Assert that a FeedToken for the entire range is used
            Assert.AreEqual(Documents.Routing.PartitionKeyInternal.MinimumInclusiveEffectivePartitionKey, feedTokenEPKRange.Range.Min);
            Assert.AreEqual(Documents.Routing.PartitionKeyInternal.MaximumExclusiveEffectivePartitionKey, feedTokenEPKRange.Range.Max);
            Assert.AreEqual(continuation, feedRangeCompositeContinuation.CompositeContinuationTokens.Peek().Token);
            Assert.IsFalse(feedRangeCompositeContinuation.IsDone);
        }
        public async Task ReadFeedIteratorCore_ReadAll_StopResume()
        {
            int totalCount = 0;
            int batchSize  = 50;

            await this.CreateRandomItems(this.LargerContainer, batchSize, randomPartitionKey : true);

            ContainerCore itemsCore    = this.LargerContainer;
            FeedIterator  feedIterator = itemsCore.GetItemQueryStreamIterator(queryDefinition: null, requestOptions: new QueryRequestOptions()
            {
                MaxItemCount = 1
            });
            string continuation = null;

            while (feedIterator.HasMoreResults)
            {
                using (ResponseMessage responseMessage =
                           await feedIterator.ReadNextAsync(this.cancellationToken))
                {
                    if (responseMessage.IsSuccessStatusCode)
                    {
                        Collection <ToDoActivity> response = TestCommon.SerializerCore.FromStream <CosmosFeedResponseUtil <ToDoActivity> >(responseMessage.Content).Data;
                        totalCount += response.Count;
                    }

                    continuation = responseMessage.ContinuationToken;
                    break;
                }
            }

            feedIterator = itemsCore.GetItemQueryStreamIterator(queryDefinition: null, continuationToken: continuation, requestOptions: new QueryRequestOptions()
            {
                MaxItemCount = 1
            });
            while (feedIterator.HasMoreResults)
            {
                using (ResponseMessage responseMessage =
                           await feedIterator.ReadNextAsync(this.cancellationToken))
                {
                    if (responseMessage.IsSuccessStatusCode)
                    {
                        Collection <ToDoActivity> response = TestCommon.SerializerCore.FromStream <CosmosFeedResponseUtil <ToDoActivity> >(responseMessage.Content).Data;
                        totalCount += response.Count;
                    }
                }
            }

            Assert.AreEqual(batchSize, totalCount);
        }
        public async Task StandByFeedIterator_NoFetchNext()
        {
            int pkRangesCount = (await this.Container.ClientContext.DocumentClient.ReadPartitionKeyRangeFeedAsync(this.Container.LinkUri)).Count;

            int expected   = 25;
            int iterations = 0;

            await this.CreateRandomItems(this.Container, expected, randomPartitionKey : true);

            ContainerCore itemsCore         = this.Container;
            string        continuationToken = null;
            int           count             = 0;

            while (true)
            {
                ChangeFeedRequestOptions requestOptions = new ChangeFeedRequestOptions()
                {
                    StartTime = DateTime.MinValue
                };

                FeedIterator feedIterator = itemsCore.GetStandByFeedIterator(continuationToken, requestOptions: requestOptions);
                using (ResponseMessage responseMessage =
                           await feedIterator.ReadNextAsync(this.cancellationToken))
                {
                    continuationToken = responseMessage.Headers.ContinuationToken;
                    Assert.AreEqual(responseMessage.ContinuationToken, responseMessage.Headers.ContinuationToken);
                    if (responseMessage.IsSuccessStatusCode)
                    {
                        Collection <ToDoActivity> response = TestCommon.SerializerCore.FromStream <CosmosFeedResponseUtil <ToDoActivity> >(responseMessage.Content).Data;
                        count += response.Count;
                    }
                }

                if (count > expected)
                {
                    Assert.Fail($"{count} does not equal {expected}");
                }

                if (count.Equals(expected))
                {
                    break;
                }

                if (iterations++ > pkRangesCount)
                {
                    Assert.Fail("Feed does not contain all elements even after looping through PK ranges. Either the continuation is not moving forward or there is some state problem.");
                }
            }
        }
Esempio n. 8
0
        public async Task GetFeedRangesAsync_AllowsParallelProcessing()
        {
            int                     pkRangesCount = (await this.LargerContainer.ClientContext.DocumentClient.ReadPartitionKeyRangeFeedAsync(this.LargerContainer.LinkUri)).Count;
            ContainerCore           itemsCore     = this.LargerContainer;
            IEnumerable <FeedRange> tokens        = await itemsCore.GetFeedRangesAsync();

            Assert.IsTrue(pkRangesCount > 1, "Should have created a multi partition container.");
            Assert.AreEqual(pkRangesCount, tokens.Count());
            int totalDocuments = 200;

            await this.CreateRandomItems(this.LargerContainer, totalDocuments, randomPartitionKey : true);

            List <Task <int> > tasks = tokens.Select(token => Task.Run(async() =>
            {
                int count = 0;
                ChangeFeedIteratorCore iteratorForToken =
                    itemsCore.GetChangeFeedStreamIterator(token, changeFeedRequestOptions: new ChangeFeedRequestOptions()
                {
                    StartTime = DateTime.MinValue
                }) as ChangeFeedIteratorCore;
                while (true)
                {
                    using (ResponseMessage responseMessage =
                               await iteratorForToken.ReadNextAsync(this.cancellationToken))
                    {
                        if (!responseMessage.IsSuccessStatusCode)
                        {
                            break;
                        }

                        Collection <ToDoActivity> response = TestCommon.SerializerCore.FromStream <CosmosFeedResponseUtil <ToDoActivity> >(responseMessage.Content).Data;
                        count += response.Count;
                    }
                }

                return(count);
            })).ToList();

            await Task.WhenAll(tasks);

            int documentsRead = 0;

            foreach (Task <int> task in tasks)
            {
                documentsRead += task.Result;
            }

            Assert.AreEqual(totalDocuments, documentsRead);
        }
Esempio n. 9
0
        public async Task ExecuteNonePkTest()
        {
            // Create a container in v2 without a partition key
            string containerId = "SprocPkNone" + Guid.NewGuid().ToString();
            ContainerCore containerNonePk = await NonPartitionedContainerHelper.CreateNonPartitionedContainer(this.database, containerId);
            Scripts scriptsNonePk = containerNonePk.Scripts;

            string sprocId = Guid.NewGuid().ToString();
            string sprocBody = @"function() {
                var context = getContext();
                var response = context.getResponse();
                var collection = context.getCollection();
                var collectionLink = collection.getSelfLink();

                var filterQuery = 'SELECT * FROM c';

                collection.queryDocuments(collectionLink, filterQuery, { },
                    function(err, documents) {
                        response.setBody(documents);
                    }
                );
            }";

            StoredProcedureResponse storedProcedureResponse =
                await scriptsNonePk.CreateStoredProcedureAsync(new StoredProcedureProperties(sprocId, sprocBody));
            Assert.AreEqual(HttpStatusCode.Created, storedProcedureResponse.StatusCode);
            StoredProcedureTests.ValidateStoredProcedureSettings(sprocId, sprocBody, storedProcedureResponse);

            // Insert document and then query
            string testPartitionId = Guid.NewGuid().ToString();
            var payload = new { id = testPartitionId, user = testPartitionId };
            ItemResponse<dynamic> createItemResponse = await containerNonePk.CreateItemAsync<dynamic>(payload);
            Assert.AreEqual(HttpStatusCode.Created, createItemResponse.StatusCode);

            StoredProcedureProperties storedProcedure = storedProcedureResponse;
            StoredProcedureExecuteResponse<JArray> sprocResponse = await scriptsNonePk.ExecuteStoredProcedureAsync<JArray>(
                sprocId,
                Cosmos.PartitionKey.None,
                parameters: null);

            Assert.AreEqual(HttpStatusCode.OK, sprocResponse.StatusCode);

            JArray jArray = sprocResponse;
            Assert.AreEqual(1, jArray.Count);
            Assert.AreEqual(testPartitionId, jArray[0]["id"]);

            StoredProcedureResponse deleteResponse = await scriptsNonePk.DeleteStoredProcedureAsync(sprocId);
            Assert.AreEqual(HttpStatusCode.NoContent, deleteResponse.StatusCode);
        }
 public CosmosLinqQuery(
     ContainerCore container,
     CosmosSerializer cosmosJsonSerializer,
     CosmosQueryClientCore queryClient,
     QueryRequestOptions cosmosQueryRequestOptions,
     bool allowSynchronousQueryExecution)
     : this(
         container,
         cosmosJsonSerializer,
         queryClient,
         cosmosQueryRequestOptions,
         null,
         allowSynchronousQueryExecution)
 {
 }
Esempio n. 11
0
        private static async Task <ItemResponse <TestDoc> > CreateItemAsync(
            ContainerCore containerCore,
            string dekId,
            List <string> pathsToEncrypt)
        {
            TestDoc testDoc = TestDoc.Create();
            ItemResponse <TestDoc> createResponse = await containerCore.CreateItemAsync(
                testDoc,
                new PartitionKey(testDoc.PK),
                EncryptionTests.GetRequestOptions(containerCore, dekId, pathsToEncrypt));

            Assert.AreEqual(HttpStatusCode.Created, createResponse.StatusCode);
            Assert.AreEqual(testDoc, createResponse.Resource);
            return(createResponse);
        }
 public CosmosLinqQueryProvider(
     ContainerCore container,
     CosmosSerializer cosmosJsonSerializer,
     CosmosQueryClientCore queryClient,
     QueryRequestOptions cosmosQueryRequestOptions,
     bool allowSynchronousQueryExecution,
     Action <IQueryable> onExecuteScalarQueryCallback = null)
 {
     this.container                      = container;
     this.cosmosJsonSerializer           = cosmosJsonSerializer;
     this.queryClient                    = queryClient;
     this.cosmosQueryRequestOptions      = cosmosQueryRequestOptions;
     this.allowSynchronousQueryExecution = allowSynchronousQueryExecution;
     this.onExecuteScalarQueryCallback   = onExecuteScalarQueryCallback;
 }
        public async Task ChangeFeedIteratorCore_ReadNextAsync()
        {
            string          continuation    = "TBD";
            ResponseMessage responseMessage = new ResponseMessage(HttpStatusCode.OK);

            responseMessage.Headers.ETag = continuation;
            responseMessage.Headers[Documents.HttpConstants.HttpHeaders.ItemCount] = "1";

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

            cosmosClientContext.Setup(c => c.ClientOptions).Returns(new CosmosClientOptions());
            cosmosClientContext
            .Setup(c => c.ProcessResourceOperationStreamAsync(
                       It.IsAny <Uri>(),
                       It.IsAny <Documents.ResourceType>(),
                       It.IsAny <Documents.OperationType>(),
                       It.IsAny <RequestOptions>(),
                       It.IsAny <ContainerCore>(),
                       It.IsAny <PartitionKey?>(),
                       It.IsAny <Stream>(),
                       It.IsAny <Action <RequestMessage> >(),
                       It.IsAny <CosmosDiagnosticsContext>(),
                       It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(responseMessage));

            ContainerCore containerCore = Mock.Of <ContainerCore>();

            Mock.Get(containerCore)
            .Setup(c => c.ClientContext)
            .Returns(cosmosClientContext.Object);
            FeedTokenInternal feedToken = Mock.Of <FeedTokenInternal>();

            Mock.Get(feedToken)
            .Setup(f => f.EnrichRequest(It.IsAny <RequestMessage>()));
            Mock.Get(feedToken)
            .Setup(f => f.ShouldRetryAsync(It.Is <ContainerCore>(c => c == containerCore), It.IsAny <ResponseMessage>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(false));

            ChangeFeedIteratorCore changeFeedIteratorCore = new ChangeFeedIteratorCore(containerCore, feedToken, null);
            ResponseMessage        response = await changeFeedIteratorCore.ReadNextAsync();

            Assert.AreEqual(feedToken, changeFeedIteratorCore.FeedToken);
            Mock.Get(feedToken)
            .Verify(f => f.UpdateContinuation(It.Is <string>(ct => ct == continuation)), Times.Once);

            Mock.Get(feedToken)
            .Verify(f => f.ShouldRetryAsync(It.Is <ContainerCore>(c => c == containerCore), It.IsAny <ResponseMessage>(), It.IsAny <CancellationToken>()), Times.Once);
        }
Esempio n. 14
0
        private static async Task ValidateQueryResultsMultipleDocumentsAsync(
            ContainerCore containerCore,
            TestDoc testDoc1,
            TestDoc testDoc2)
        {
            string query = $"SELECT * FROM c WHERE c.PK in ('{testDoc1.PK}', '{testDoc2.PK}')";
            FeedIterator <TestDoc> queryResponseIterator = containerCore.GetItemQueryIterator <TestDoc>(query);
            FeedResponse <TestDoc> readDocs = await queryResponseIterator.ReadNextAsync();

            Assert.AreEqual(null, readDocs.ContinuationToken);
            Assert.AreEqual(2, readDocs.Count);
            foreach (TestDoc readDoc in readDocs)
            {
                Assert.AreEqual(readDoc, readDoc.Id.Equals(testDoc1.Id) ? testDoc1 : testDoc2);
            }
        }
Esempio n. 15
0
 private static ItemRequestOptions GetRequestOptions(
     ContainerCore containerCore,
     string dekId,
     List <string> pathsToEncrypt,
     string ifMatchEtag = null)
 {
     return(new ItemRequestOptions
     {
         EncryptionOptions = new EncryptionOptions
         {
             DataEncryptionKey = ((DatabaseCore)containerCore.Database).GetDataEncryptionKey(dekId),
             PathsToEncrypt = pathsToEncrypt
         },
         IfMatchEtag = ifMatchEtag
     });
 }
Esempio n. 16
0
        internal StandByFeedIteratorCore(
            CosmosClientContext clientContext,
            ContainerCore container,
            ChangeFeedStartFrom changeFeedStartFrom,
            ChangeFeedRequestOptions options)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            this.clientContext       = clientContext;
            this.container           = container;
            this.changeFeedStartFrom = changeFeedStartFrom ?? throw new ArgumentNullException(nameof(changeFeedStartFrom));
            this.changeFeedOptions   = options;
        }
Esempio n. 17
0
        private static async Task <ItemResponse <TestDoc> > UpsertItemAsync(
            ContainerCore containerCore,
            TestDoc testDoc,
            string dekId,
            List <string> pathsToEncrypt,
            HttpStatusCode expectedStatusCode)
        {
            ItemResponse <TestDoc> upsertResponse = await containerCore.UpsertItemAsync(
                testDoc,
                new PartitionKey(testDoc.PK),
                EncryptionTests.GetRequestOptions(containerCore, dekId, pathsToEncrypt));

            Assert.AreEqual(expectedStatusCode, upsertResponse.StatusCode);
            Assert.AreEqual(testDoc, upsertResponse.Resource);
            return(upsertResponse);
        }
        public async Task ReadFeedIteratorCore_PassingFeedToken_ReadAll()
        {
            int totalCount = 0;
            int batchSize  = 1000;

            await this.CreateRandomItems(this.LargerContainer, batchSize, randomPartitionKey : true);

            ContainerCore itemsCore = this.LargerContainer;

            FeedIteratorCore initialFeedIterator = itemsCore.GetItemQueryStreamIterator(queryDefinition: null, requestOptions: new QueryRequestOptions()
            {
                MaxItemCount = 1
            }) as FeedIteratorCore;

            while (initialFeedIterator.HasMoreResults)
            {
                using (ResponseMessage responseMessage =
                           await initialFeedIterator.ReadNextAsync(this.cancellationToken))
                {
                    if (responseMessage.IsSuccessStatusCode)
                    {
                        Collection <ToDoActivity> response = TestCommon.SerializerCore.FromStream <CosmosFeedResponseUtil <ToDoActivity> >(responseMessage.Content).Data;
                        totalCount += response.Count;
                    }
                    break;
                }
            }

            // Use the previous iterators FeedToken to continue
            FeedIteratorCore feedIterator = itemsCore.GetItemQueryStreamIterator(queryDefinition: null, feedToken: initialFeedIterator.FeedToken) as FeedIteratorCore;

            while (feedIterator.HasMoreResults)
            {
                using (ResponseMessage responseMessage =
                           await feedIterator.ReadNextAsync(this.cancellationToken))
                {
                    Assert.IsNotNull(feedIterator.FeedToken);
                    if (responseMessage.IsSuccessStatusCode)
                    {
                        Collection <ToDoActivity> response = TestCommon.SerializerCore.FromStream <CosmosFeedResponseUtil <ToDoActivity> >(responseMessage.Content).Data;
                        totalCount += response.Count;
                    }
                }
            }

            Assert.AreEqual(batchSize, totalCount);
        }
        public async Task BatchResponseDeserializationAsync()
        {
            ContainerCore containerCore = (ContainerInlineCore)MockCosmosUtil.CreateMockCosmosClient().GetDatabase("db").GetContainer("cont");
            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", containerCore: containerCore),
                new ItemBatchOperation(OperationType.Read, operationIndex: 0, id: "someId", containerCore: containerCore)
            }),
                serializerCore : MockCosmosUtil.Serializer,
                cancellationToken : CancellationToken.None);

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

            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]));
        }
        public async Task ReadFeedIteratorCore_WithNoInitialState_ReadNextAsync()
        {
            string          continuation    = "TBD";
            ResponseMessage responseMessage = new ResponseMessage(HttpStatusCode.OK);

            responseMessage.Headers.ContinuationToken = continuation;
            responseMessage.Headers[Documents.HttpConstants.HttpHeaders.ItemCount] = "1";

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

            cosmosClientContext.Setup(c => c.ClientOptions).Returns(new CosmosClientOptions());
            cosmosClientContext
            .Setup(c => c.ProcessResourceOperationStreamAsync(
                       It.IsAny <Uri>(),
                       It.IsAny <Documents.ResourceType>(),
                       It.IsAny <Documents.OperationType>(),
                       It.IsAny <RequestOptions>(),
                       It.IsAny <ContainerCore>(),
                       It.IsAny <PartitionKey?>(),
                       It.IsAny <Stream>(),
                       It.IsAny <Action <RequestMessage> >(),
                       It.IsAny <CosmosDiagnosticsContext>(),
                       It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(responseMessage));

            ContainerCore containerCore = Mock.Of <ContainerCore>();

            Mock.Get(containerCore)
            .Setup(c => c.ClientContext)
            .Returns(cosmosClientContext.Object);

            FeedIteratorCore feedTokenIterator = FeedIteratorCore.CreateForPartitionedResource(containerCore, new Uri("http://localhost"), Documents.ResourceType.Document, null, null, null, new QueryRequestOptions());
            ResponseMessage  response          = await feedTokenIterator.ReadNextAsync();

            FeedToken feedTokenOut = feedTokenIterator.FeedToken;

            Assert.IsNotNull(feedTokenOut);

            FeedTokenEPKRange feedTokenEPKRange = feedTokenOut as FeedTokenEPKRange;

            // Assert that a FeedToken for the entire range is used
            Assert.AreEqual(Documents.Routing.PartitionKeyInternal.MinimumInclusiveEffectivePartitionKey, feedTokenEPKRange.CompleteRange.Min);
            Assert.AreEqual(Documents.Routing.PartitionKeyInternal.MaximumExclusiveEffectivePartitionKey, feedTokenEPKRange.CompleteRange.Max);
            Assert.AreEqual(continuation, feedTokenEPKRange.CompositeContinuationTokens.Peek().Token);
            Assert.IsFalse(feedTokenEPKRange.IsDone);
        }
 public CosmosLinqQuery(
     ContainerCore container,
     CosmosResponseFactory responseFactory,
     CosmosQueryClientCore queryClient,
     string continuationToken,
     QueryRequestOptions cosmosQueryRequestOptions,
     bool allowSynchronousQueryExecution)
     : this(
         container,
         responseFactory,
         queryClient,
         continuationToken,
         cosmosQueryRequestOptions,
         null,
         allowSynchronousQueryExecution)
 {
 }
 private static TransactionalBatchItemRequestOptions GetBatchItemRequestOptions(
     ContainerCore containerCore,
     string dekId,
     List <string> pathsToEncrypt,
     string ifMatchEtag = null)
 {
     return(new TransactionalBatchItemRequestOptions
     {
         EncryptionOptions = new EncryptionOptions
         {
             DataEncryptionKeyId = dekId,
             EncryptionAlgorithm = CosmosEncryptionAlgorithm.AEAes256CbcHmacSha256Randomized,
             PathsToEncrypt = pathsToEncrypt
         },
         IfMatchEtag = ifMatchEtag
     });
 }
        public void ReadConflictContentDeserializesContent()
        {
            ContainerCore container = CosmosConflictTests.GetMockedContainer((request, cancellationToken) => {
                return(TestHandler.ReturnSuccess());
            });

            JObject someJsonObject = new JObject();

            someJsonObject["id"]      = Guid.NewGuid().ToString();
            someJsonObject["someInt"] = 2;

            ConflictProperties conflictSettings = new ConflictProperties();

            conflictSettings.Content = someJsonObject.ToString();

            Assert.AreEqual(someJsonObject.ToString(), container.Conflicts.ReadConflictContent <JObject>(conflictSettings).ToString());
        }
Esempio n. 24
0
 public CosmosLinqQueryProvider(
     ContainerCore container,
     CosmosResponseFactory responseFactory,
     CosmosQueryClientCore queryClient,
     string continuationToken,
     QueryRequestOptions cosmosQueryRequestOptions,
     bool allowSynchronousQueryExecution,
     Action <IQueryable> onExecuteScalarQueryCallback = null)
 {
     this.container                      = container;
     this.responseFactory                = responseFactory;
     this.queryClient                    = queryClient;
     this.continuationToken              = continuationToken;
     this.cosmosQueryRequestOptions      = cosmosQueryRequestOptions;
     this.allowSynchronousQueryExecution = allowSynchronousQueryExecution;
     this.onExecuteScalarQueryCallback   = onExecuteScalarQueryCallback;
 }
Esempio n. 25
0
        public async Task TestInitialize()
        {
            await base.TestInit();

            // Throughput needs to be large enough for multiple partitions
            ContainerResponse response = await this.database.CreateContainerAsync(
                Guid.NewGuid().ToString(),
                "/id",
                15000,
                cancellationToken : this.cancellationToken);

            Assert.IsNotNull(response);
            Assert.IsNotNull(response.Container);
            Assert.IsNotNull(response.Resource);
            this.Container       = (ContainerCore)response;
            this.queryClientCore = new CosmosQueryClientCore(this.Container.ClientContext, this.Container);
        }
Esempio n. 26
0
        private static async Task <ItemResponse <TestDoc> > ReplaceItemAsync(
            ContainerCore containerCore,
            TestDoc testDoc,
            string dekId,
            List <string> pathsToEncrypt,
            string etag = null)
        {
            ItemResponse <TestDoc> replaceResponse = await containerCore.ReplaceItemAsync(
                testDoc,
                testDoc.Id,
                new PartitionKey(testDoc.PK),
                EncryptionTests.GetRequestOptions(containerCore, dekId, pathsToEncrypt, etag));

            Assert.AreEqual(HttpStatusCode.OK, replaceResponse.StatusCode);
            Assert.AreEqual(testDoc, replaceResponse.Resource);
            return(replaceResponse);
        }
Esempio n. 27
0
        public async Task GetTargetPartitionKeyRangesAsyncWithFeedRange()
        {
            ContainerCore container = null;

            try
            {
                // Create a container large enough to have at least 2 partitions
                ContainerResponse containerResponse = await this.database.CreateContainerAsync(
                    id : Guid.NewGuid().ToString(),
                    partitionKeyPath : "/pk",
                    throughput : 15000);

                container = (ContainerInlineCore)containerResponse;

                // Get all the partition key ranges to verify there is more than one partition
                IRoutingMapProvider routingMapProvider = await this.cosmosClient.DocumentClient.GetPartitionKeyRangeCacheAsync();

                ContainerQueryProperties containerQueryProperties = new ContainerQueryProperties(
                    containerResponse.Resource.ResourceId,
                    null,
                    containerResponse.Resource.PartitionKey);

                IReadOnlyList <FeedRange> feedTokens = await container.GetFeedRangesAsync();

                Assert.IsTrue(feedTokens.Count > 1, " RUs of the container needs to be increased to ensure at least 2 partitions.");

                // There should only be one range since we get 1 FeedRange per range
                foreach (FeedRange feedToken in feedTokens)
                {
                    List <PartitionKeyRange> partitionKeyRanges = await CosmosQueryExecutionContextFactory.GetTargetPartitionKeyRangesAsync(
                        queryClient : new CosmosQueryClientCore(container.ClientContext, container),
                        resourceLink : container.LinkUri.OriginalString,
                        partitionedQueryExecutionInfo : null,
                        containerQueryProperties : containerQueryProperties,
                        properties : null,
                        feedRangeInternal : feedToken as FeedRangeInternal);

                    Assert.IsTrue(partitionKeyRanges.Count == 1, "Only 1 partition key range should be selected since the FeedRange represents a single range.");
                }
            }
            finally
            {
                await container?.DeleteContainerAsync();
            }
        }
        public async Task ReadFeedIteratorCore_HandlesSplitsThroughPipeline()
        {
            int executionCount = 0;
            CosmosClientContext cosmosClientContext = GetMockedClientContext((RequestMessage requestMessage, CancellationToken cancellationToken) =>
            {
                // Force OnBeforeRequestActions call
                requestMessage.ToDocumentServiceRequest();
                if (executionCount++ == 0)
                {
                    return(TestHandler.ReturnStatusCode(HttpStatusCode.Gone, Documents.SubStatusCodes.PartitionKeyRangeGone));
                }

                return(TestHandler.ReturnStatusCode(HttpStatusCode.OK));
            });

            ContainerCore containerCore = Mock.Of <ContainerCore>();

            Mock.Get(containerCore)
            .Setup(c => c.ClientContext)
            .Returns(cosmosClientContext);
            Mock.Get(containerCore)
            .Setup(c => c.LinkUri)
            .Returns(new Uri($"/dbs/db/colls/colls", UriKind.Relative));
            FeedRangeInternal range = Mock.Of <FeedRangeInternal>();

            Mock.Get(range)
            .Setup(f => f.Accept(It.IsAny <FeedRangeVisitor>()));
            FeedRangeContinuation feedToken = Mock.Of <FeedRangeContinuation>();

            Mock.Get(feedToken)
            .Setup(f => f.FeedRange)
            .Returns(range);
            Mock.Get(feedToken)
            .Setup(f => f.Accept(It.IsAny <FeedRangeVisitor>(), It.IsAny <Action <RequestMessage, string> >()));
            Mock.Get(feedToken)
            .Setup(f => f.HandleSplitAsync(It.Is <ContainerCore>(c => c == containerCore), It.IsAny <ResponseMessage>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(Documents.ShouldRetryResult.NoRetry()));

            FeedRangeIteratorCore changeFeedIteratorCore = new FeedRangeIteratorCore(containerCore, feedToken, new QueryRequestOptions());

            ResponseMessage response = await changeFeedIteratorCore.ReadNextAsync();

            Assert.AreEqual(1, executionCount, "Pipeline handled the Split");
            Assert.AreEqual(HttpStatusCode.Gone, response.StatusCode);
        }
        public async Task TestInitialize()
        {
            await base.TestInit();

            ContainerResponse response = await this.database.CreateContainerAsync(
                new ContainerProperties(id : Guid.NewGuid().ToString(), partitionKeyPath : PartitionKey),
                throughput : 50000,
                cancellationToken : this.cancellationToken);

            Assert.IsNotNull(response);
            Assert.IsNotNull(response.Container);
            Assert.IsNotNull(response.Resource);
            this.Container = (ContainerCore)response;

            DocumentFeedResponse <PartitionKeyRange> pkRangesFeed = await this.cosmosClient.DocumentClient.ReadPartitionKeyRangeFeedAsync(this.Container.LinkUri);

            Assert.IsTrue(pkRangesFeed.Count > 1, "Refresh container throughput to have at-least > 1 pk-range");
        }
Esempio n. 30
0
        [TestCategory("Quarantine")] // Not currently working with emulator
        public async Task ReadFixedWithAutoscaleTests()
        {
            DatabaseCore database = (DatabaseInlineCore)await this.cosmosClient.CreateDatabaseAsync(
                Guid.NewGuid().ToString());

            ContainerCore autoscaleContainer = (ContainerInlineCore)await database.CreateContainerAsync(
                new ContainerProperties(Guid.NewGuid().ToString(), "/pk"),
                ThroughputProperties.CreateAutoscaleProvionedThroughput(5000));

            Assert.IsNotNull(autoscaleContainer);

            // Reading a autoscale container with fixed results
            int?throughput = await autoscaleContainer.ReadThroughputAsync();

            Assert.IsNotNull(throughput);

            await database.DeleteAsync();
        }