internal static Container GetContainerWithItemTransportException(
     string databaseId,
     string containerId,
     Guid activityId,
     string transportExceptionSourceDescription)
 {
     return(GetContainerWithIntercepter(
                databaseId,
                containerId,
                (uri, resourceOperation, request) => TransportClientHelper.ThrowTransportExceptionOnItemOperation(
                    uri,
                    resourceOperation,
                    request,
                    activityId,
                    transportExceptionSourceDescription)));
 }
Example #2
0
        internal static Container GetContainerWithItemTransportException(
            string databaseId,
            string containerId,
            Guid activityId,
            string transportExceptionSourceDescription)
        {
            CosmosClient clientWithIntercepter = TestCommon.CreateCosmosClient(
                builder =>
            {
                builder.WithTransportClientHandlerFactory(transportClient => new TransportClientWrapper(
                                                              transportClient,
                                                              (uri, resourceOperation, request) => TransportClientHelper.ThrowTransportExceptionOnItemOperation(
                                                                  uri,
                                                                  resourceOperation,
                                                                  request,
                                                                  activityId,
                                                                  transportExceptionSourceDescription)));
            });

            return(clientWithIntercepter.GetContainer(databaseId, containerId));
        }
        public async Task PointOperationForbiddenDiagnostic()
        {
            List <int> stringLength = new List <int>();

            foreach (int maxCount in new int[] { 1, 2, 4 })
            {
                int count = 0;
                List <(string, string)> activityIdAndErrorMessage = new List <(string, string)>(maxCount);
                Guid   transportExceptionActivityId = Guid.NewGuid();
                string transportErrorMessage        = $"TransportErrorMessage{Guid.NewGuid()}";
                Guid   activityIdScope = Guid.Empty;
                Action <Uri, Documents.ResourceOperation, Documents.DocumentServiceRequest> interceptor =
                    (uri, operation, request) =>
                {
                    Assert.AreNotEqual(Trace.CorrelationManager.ActivityId, Guid.Empty, "Activity scope should be set");

                    if (request.ResourceType == Documents.ResourceType.Document)
                    {
                        if (activityIdScope == Guid.Empty)
                        {
                            activityIdScope = Trace.CorrelationManager.ActivityId;
                        }
                        else
                        {
                            Assert.AreEqual(Trace.CorrelationManager.ActivityId, activityIdScope, "Activity scope should match on retries");
                        }

                        if (count >= maxCount)
                        {
                            TransportClientHelper.ThrowTransportExceptionOnItemOperation(
                                uri,
                                operation,
                                request,
                                transportExceptionActivityId,
                                transportErrorMessage);
                        }

                        count++;
                        string activityId   = Guid.NewGuid().ToString();
                        string errorMessage = $"Error{Guid.NewGuid()}";

                        activityIdAndErrorMessage.Add((activityId, errorMessage));
                        TransportClientHelper.ThrowForbiddendExceptionOnItemOperation(
                            uri,
                            request,
                            activityId,
                            errorMessage);
                    }
                };

                Container containerWithTransportException = TransportClientHelper.GetContainerWithIntercepter(
                    this.database.Id,
                    this.Container.Id,
                    interceptor);
                //Checking point operation diagnostics on typed operations
                ToDoActivity testItem = ToDoActivity.CreateRandomToDoActivity();

                try
                {
                    ItemResponse <ToDoActivity> createResponse = await containerWithTransportException.CreateItemAsync <ToDoActivity>(
                        item : testItem);

                    Assert.Fail("Should have thrown a request timeout exception");
                }
                catch (CosmosException ce) when(ce.StatusCode == System.Net.HttpStatusCode.RequestTimeout)
                {
                    string exceptionToString = ce.ToString();

                    Assert.IsNotNull(exceptionToString);
                    stringLength.Add(exceptionToString.Length);

                    // Request timeout info will be in the exception message and in the diagnostic info.
                    Assert.AreEqual(2, Regex.Matches(exceptionToString, transportExceptionActivityId.ToString()).Count);
                    Assert.AreEqual(2, Regex.Matches(exceptionToString, transportErrorMessage).Count);

                    // Check to make sure the diagnostics does not include duplicate info.
                    foreach ((string activityId, string errorMessage) in activityIdAndErrorMessage)
                    {
                        Assert.AreEqual(1, Regex.Matches(exceptionToString, activityId).Count);
                        Assert.AreEqual(1, Regex.Matches(exceptionToString, errorMessage).Count);
                    }
                }
            }

            // Check if the exception message is not growing exponentially
            Assert.IsTrue(stringLength.Count > 2);
            for (int i = 0; i < stringLength.Count - 1; i++)
            {
                int currLength = stringLength[i];
                int nextLength = stringLength[i + 1];
                Assert.IsTrue(nextLength < currLength * 2,
                              $"The diagnostic string is growing faster than linear. Length: {currLength}, Next Length: {nextLength}");
            }
        }