[Ignore] // turn this back on when we have tracing diagnostics.
        public async Task ChangeFeedDiagnostics(bool disableDiagnostics)
        {
            string       pkValue   = "ChangeFeedDiagnostics";
            CosmosClient client    = TestCommon.CreateCosmosClient();
            Container    container = client.GetContainer(this.database.Id, this.Container.Id);
            List <Task <ItemResponse <ToDoActivity> > > createItemsTasks = new List <Task <ItemResponse <ToDoActivity> > >();

            for (int i = 0; i < 100; i++)
            {
                ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(pk: pkValue);
                createItemsTasks.Add(container.CreateItemAsync <ToDoActivity>(item, new PartitionKey(item.status)));
            }

            await Task.WhenAll(createItemsTasks);

            ChangeFeedRequestOptions requestOptions     = disableDiagnostics ? ChangeFeedRequestOptionDisableDiagnostic : null;
            FeedIterator             changeFeedIterator = ((ContainerCore)(container as ContainerInlineCore)).GetChangeFeedStreamIterator(
                ChangeFeedStartFrom.Beginning(),
                changeFeedRequestOptions: requestOptions);

            while (changeFeedIterator.HasMoreResults)
            {
                using (ResponseMessage response = await changeFeedIterator.ReadNextAsync())
                {
                    CosmosDiagnosticsTests.VerifyChangeFeedDiagnostics(
                        diagnostics: response.Diagnostics,
                        disableDiagnostics: disableDiagnostics);
                }
            }
        }
        public async Task BulkOperationDiagnostic()
        {
            string       pkValue       = "DiagnosticBulkTestPk";
            CosmosClient bulkClient    = TestCommon.CreateCosmosClient(builder => builder.WithBulkExecution(true));
            Container    bulkContainer = bulkClient.GetContainer(this.database.Id, this.Container.Id);
            List <Task <ItemResponse <ToDoActivity> > > createItemsTasks = new List <Task <ItemResponse <ToDoActivity> > >();

            for (int i = 0; i < 100; i++)
            {
                ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(pk: pkValue);
                createItemsTasks.Add(bulkContainer.CreateItemAsync <ToDoActivity>(item, new PartitionKey(item.status)));
            }

            await Task.WhenAll(createItemsTasks);

            foreach (Task <ItemResponse <ToDoActivity> > createTask in createItemsTasks)
            {
                ItemResponse <ToDoActivity> itemResponse = await createTask;
                Assert.IsNotNull(itemResponse);

                CosmosDiagnosticsTests.VerifyPointDiagnostics(
                    diagnostics: itemResponse.Diagnostics,
                    disableDiagnostics: false);
            }
        }
        public async Task BatchOperationDiagnostic(bool disableDiagnostics)
        {
            string                pkValue   = "DiagnosticTestPk";
            TransactionalBatch    batch     = this.Container.CreateTransactionalBatch(new PartitionKey(pkValue));
            BatchCore             batchCore = (BatchCore)batch;
            List <PatchOperation> patch     = new List <PatchOperation>()
            {
                PatchOperation.Remove("/cost")
            };

            List <ToDoActivity> createItems = new List <ToDoActivity>();

            for (int i = 0; i < 50; i++)
            {
                ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(pk: pkValue);
                createItems.Add(item);
                batch.CreateItem <ToDoActivity>(item);
            }

            for (int i = 0; i < 20; i++)
            {
                batch.ReadItem(createItems[i].id);
                batchCore.PatchItem(createItems[i].id, patch);
            }

            TransactionalBatchRequestOptions requestOptions = disableDiagnostics ? RequestOptionDisableDiagnostic : null;
            TransactionalBatchResponse       response       = await batch.ExecuteAsync(requestOptions);

            Assert.IsNotNull(response);
            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                diagnostics: response.Diagnostics,
                disableDiagnostics: disableDiagnostics);
        }
        public async Task PointOperationDiagnostic()
        {
            //Checking point operation diagnostics on typed operations
            ToDoActivity testItem = ToDoActivity.CreateRandomToDoActivity();
            ItemResponse <ToDoActivity> createResponse = await this.Container.CreateItemAsync <ToDoActivity>(item : testItem);

            CosmosDiagnosticsTests.VerifyPointDiagnostics(createResponse.Diagnostics);

            ItemResponse <ToDoActivity> readResponse = await this.Container.ReadItemAsync <ToDoActivity>(id : testItem.id, partitionKey : new PartitionKey(testItem.status));

            Assert.IsNotNull(readResponse.Diagnostics);

            testItem.description = "NewDescription";
            ItemResponse <ToDoActivity> replaceResponse = await this.Container.ReplaceItemAsync <ToDoActivity>(item : testItem, id : testItem.id, partitionKey : new PartitionKey(testItem.status));

            Assert.AreEqual(replaceResponse.Resource.description, "NewDescription");
            CosmosDiagnosticsTests.VerifyPointDiagnostics(replaceResponse.Diagnostics);

            ItemResponse <ToDoActivity> deleteResponse = await this.Container.DeleteItemAsync <ToDoActivity>(partitionKey : new Cosmos.PartitionKey(testItem.status), id : testItem.id);

            Assert.IsNotNull(deleteResponse);
            CosmosDiagnosticsTests.VerifyPointDiagnostics(deleteResponse.Diagnostics);

            //Checking point operation diagnostics on stream operations
            ResponseMessage createStreamResponse = await this.Container.CreateItemStreamAsync(
                partitionKey : new PartitionKey(testItem.status),
                streamPayload : TestCommon.SerializerCore.ToStream <ToDoActivity>(testItem));

            CosmosDiagnosticsTests.VerifyPointDiagnostics(createStreamResponse.Diagnostics);

            ResponseMessage readStreamResponse = await this.Container.ReadItemStreamAsync(
                id : testItem.id,
                partitionKey : new PartitionKey(testItem.status));

            CosmosDiagnosticsTests.VerifyPointDiagnostics(readStreamResponse.Diagnostics);

            ResponseMessage replaceStreamResponse = await this.Container.ReplaceItemStreamAsync(
                streamPayload : TestCommon.SerializerCore.ToStream <ToDoActivity>(testItem),
                id : testItem.id,
                partitionKey : new PartitionKey(testItem.status));

            CosmosDiagnosticsTests.VerifyPointDiagnostics(replaceStreamResponse.Diagnostics);

            ResponseMessage deleteStreamResponse = await this.Container.DeleteItemStreamAsync(
                id : testItem.id,
                partitionKey : new PartitionKey(testItem.status));

            CosmosDiagnosticsTests.VerifyPointDiagnostics(deleteStreamResponse.Diagnostics);

            // Ensure diagnostics are set even on failed operations
            testItem.description = new string('x', Microsoft.Azure.Documents.Constants.MaxResourceSizeInBytes + 1);
            ResponseMessage createTooBigStreamResponse = await this.Container.CreateItemStreamAsync(
                partitionKey : new PartitionKey(testItem.status),
                streamPayload : TestCommon.SerializerCore.ToStream <ToDoActivity>(testItem));

            Assert.IsFalse(createTooBigStreamResponse.IsSuccessStatusCode);
            CosmosDiagnosticsTests.VerifyPointDiagnostics(createTooBigStreamResponse.Diagnostics);
        }
Beispiel #5
0
        private void VerifyResponse <T>(
            Response <T> response,
            T expectedValue,
            QueryRequestOptions queryRequestOptions)
        {
            Assert.AreEqual <T>(expectedValue, response.Resource);
            Assert.IsTrue(response.RequestCharge > 0);

            bool disableDiagnostics = queryRequestOptions.DiagnosticContextFactory != null;

            CosmosDiagnosticsTests.VerifyQueryDiagnostics(
                diagnostics: response.Diagnostics,
                isFirstPage: false,
                disableDiagnostics: disableDiagnostics);
        }
        public async Task BatchOperationDiagnostic()
        {
            string             pkValue = "DiagnosticTestPk";
            TransactionalBatch batch   = this.Container.CreateTransactionalBatch(new PartitionKey(pkValue));

            List <ToDoActivity> createItems = new List <ToDoActivity>();

            for (int i = 0; i < 50; i++)
            {
                ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(pk: pkValue);
                createItems.Add(item);
                batch.CreateItem <ToDoActivity>(item);
            }

            for (int i = 0; i < 20; i++)
            {
                batch.ReadItem(createItems[i].id);
            }

            TransactionalBatchResponse response = await batch.ExecuteAsync();

            Assert.IsNotNull(response);
            CosmosDiagnosticsTests.VerifyPointDiagnostics(response.Diagnostics);
        }
        private async Task <long> ExecuteQueryAndReturnOutputDocumentCount(
            string queryText,
            int expectedItemCount,
            bool disableDiagnostics)
        {
            QueryDefinition sql = null;

            if (queryText != null)
            {
                sql = new QueryDefinition(queryText);
            }

            QueryRequestOptions requestOptions = new QueryRequestOptions()
            {
                MaxItemCount   = 1,
                MaxConcurrency = 1,
            };

            if (disableDiagnostics)
            {
                requestOptions.DiagnosticContextFactory = () => EmptyCosmosDiagnosticsContext.Singleton;
            }
            ;

            // Verify the typed query iterator
            FeedIterator <ToDoActivity> feedIterator = this.Container.GetItemQueryIterator <ToDoActivity>(
                sql,
                requestOptions: requestOptions);

            List <ToDoActivity> results = new List <ToDoActivity>();
            long totalOutDocumentCount  = 0;
            bool isFirst = true;

            while (feedIterator.HasMoreResults)
            {
                FeedResponse <ToDoActivity> response = await feedIterator.ReadNextAsync();

                results.AddRange(response);
                if (queryText == null)
                {
                    CosmosDiagnosticsTests.VerifyPointDiagnostics(
                        response.Diagnostics,
                        disableDiagnostics);
                }
                else
                {
                    VerifyQueryDiagnostics(
                        response.Diagnostics,
                        isFirst,
                        disableDiagnostics);
                }

                isFirst = false;
            }

            Assert.AreEqual(expectedItemCount, results.Count);

            // Verify the stream query iterator
            FeedIterator streamIterator = this.Container.GetItemQueryStreamIterator(
                sql,
                requestOptions: requestOptions);

            List <ToDoActivity> streamResults = new List <ToDoActivity>();
            long streamTotalOutDocumentCount  = 0;

            isFirst = true;
            while (streamIterator.HasMoreResults)
            {
                ResponseMessage response = await streamIterator.ReadNextAsync();

                Collection <ToDoActivity> result = TestCommon.SerializerCore.FromStream <CosmosFeedResponseUtil <ToDoActivity> >(response.Content).Data;
                streamResults.AddRange(result);
                if (queryText == null)
                {
                    CosmosDiagnosticsTests.VerifyPointDiagnostics(
                        response.Diagnostics,
                        disableDiagnostics);
                }
                else
                {
                    VerifyQueryDiagnostics(
                        response.Diagnostics,
                        isFirst,
                        disableDiagnostics);
                }

                isFirst = false;
            }

            Assert.AreEqual(expectedItemCount, streamResults.Count);
            Assert.AreEqual(totalOutDocumentCount, streamTotalOutDocumentCount);

            return(results.Count);
        }
        public async Task PointOperationDiagnostic(bool disableDiagnostics)
        {
            ItemRequestOptions requestOptions = new ItemRequestOptions();

            if (disableDiagnostics)
            {
                requestOptions.DiagnosticContextFactory = () => EmptyCosmosDiagnosticsContext.Singleton;
            }
            else
            {
                // Add 10 seconds to ensure CPU history is recorded
                await Task.Delay(TimeSpan.FromSeconds(10));
            }

            //Checking point operation diagnostics on typed operations
            ToDoActivity testItem = ToDoActivity.CreateRandomToDoActivity();
            ItemResponse <ToDoActivity> createResponse = await this.Container.CreateItemAsync <ToDoActivity>(
                item : testItem,
                requestOptions : requestOptions);

            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                createResponse.Diagnostics,
                disableDiagnostics);

            ItemResponse <ToDoActivity> readResponse = await this.Container.ReadItemAsync <ToDoActivity>(
                id : testItem.id,
                partitionKey : new PartitionKey(testItem.status),
                requestOptions);

            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                readResponse.Diagnostics,
                disableDiagnostics);

            testItem.description = "NewDescription";
            ItemResponse <ToDoActivity> replaceResponse = await this.Container.ReplaceItemAsync <ToDoActivity>(
                item : testItem,
                id : testItem.id,
                partitionKey : new PartitionKey(testItem.status),
                requestOptions : requestOptions);

            Assert.AreEqual(replaceResponse.Resource.description, "NewDescription");

            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                replaceResponse.Diagnostics,
                disableDiagnostics);

            testItem.description = "PatchedDescription";
            ContainerInternal     containerInternal = (ContainerInternal)this.Container;
            List <PatchOperation> patch             = new List <PatchOperation>()
            {
                PatchOperation.Replace("/description", testItem.description)
            };
            ItemResponse <ToDoActivity> patchResponse = await containerInternal.PatchItemAsync <ToDoActivity>(
                id : testItem.id,
                partitionKey : new PartitionKey(testItem.status),
                patchOperations : patch,
                requestOptions : requestOptions);

            Assert.AreEqual(patchResponse.Resource.description, "PatchedDescription");

            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                patchResponse.Diagnostics,
                disableDiagnostics);

            ItemResponse <ToDoActivity> deleteResponse = await this.Container.DeleteItemAsync <ToDoActivity>(
                partitionKey : new Cosmos.PartitionKey(testItem.status),
                id : testItem.id,
                requestOptions : requestOptions);

            Assert.IsNotNull(deleteResponse);
            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                deleteResponse.Diagnostics,
                disableDiagnostics);

            //Checking point operation diagnostics on stream operations
            ResponseMessage createStreamResponse = await this.Container.CreateItemStreamAsync(
                partitionKey : new PartitionKey(testItem.status),
                streamPayload : TestCommon.SerializerCore.ToStream <ToDoActivity>(testItem),
                requestOptions : requestOptions);

            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                createStreamResponse.Diagnostics,
                disableDiagnostics);

            ResponseMessage readStreamResponse = await this.Container.ReadItemStreamAsync(
                id : testItem.id,
                partitionKey : new PartitionKey(testItem.status),
                requestOptions : requestOptions);

            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                readStreamResponse.Diagnostics,
                disableDiagnostics);

            ResponseMessage replaceStreamResponse = await this.Container.ReplaceItemStreamAsync(
                streamPayload : TestCommon.SerializerCore.ToStream <ToDoActivity>(testItem),
                id : testItem.id,
                partitionKey : new PartitionKey(testItem.status),
                requestOptions : requestOptions);

            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                replaceStreamResponse.Diagnostics,
                disableDiagnostics);

            ResponseMessage patchStreamResponse = await containerInternal.PatchItemStreamAsync(
                id : testItem.id,
                partitionKey : new PartitionKey(testItem.status),
                patchOperations : patch,
                requestOptions : requestOptions);

            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                patchStreamResponse.Diagnostics,
                disableDiagnostics);

            ResponseMessage deleteStreamResponse = await this.Container.DeleteItemStreamAsync(
                id : testItem.id,
                partitionKey : new PartitionKey(testItem.status),
                requestOptions : requestOptions);

            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                deleteStreamResponse.Diagnostics,
                disableDiagnostics);

            // Ensure diagnostics are set even on failed operations
            testItem.description = new string('x', Microsoft.Azure.Documents.Constants.MaxResourceSizeInBytes + 1);
            ResponseMessage createTooBigStreamResponse = await this.Container.CreateItemStreamAsync(
                partitionKey : new PartitionKey(testItem.status),
                streamPayload : TestCommon.SerializerCore.ToStream <ToDoActivity>(testItem),
                requestOptions : requestOptions);

            Assert.IsFalse(createTooBigStreamResponse.IsSuccessStatusCode);
            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                createTooBigStreamResponse.Diagnostics,
                disableDiagnostics);
        }
 private void VerifyResponse <T>(Response <T> response, T expectedValue)
 {
     Assert.AreEqual <T>(expectedValue, response.Resource);
     Assert.IsTrue(response.RequestCharge > 0);
     CosmosDiagnosticsTests.VerifyQueryDiagnostics(response.Diagnostics);
 }