public void EnsureCorrectStatusCode() { string testMessage = "Test" + Guid.NewGuid().ToString(); string activityId = Guid.NewGuid().ToString(); int substatuscode = 9000; string substatus = substatuscode.ToString(); double requestCharge = 42; double retryAfter = 9000; string retryAfterLiteral = retryAfter.ToString(); List <(HttpStatusCode statusCode, CosmosException exception)> exceptionsToStatusCodes = new List <(HttpStatusCode, CosmosException)>() { (HttpStatusCode.NotFound, CosmosExceptionFactory.CreateNotFoundException(testMessage, new Headers() { SubStatusCodeLiteral = substatus, ActivityId = activityId, RequestCharge = requestCharge, RetryAfterLiteral = retryAfterLiteral })), (HttpStatusCode.InternalServerError, CosmosExceptionFactory.CreateInternalServerErrorException(testMessage, new Headers() { SubStatusCodeLiteral = substatus, ActivityId = activityId, RequestCharge = requestCharge, RetryAfterLiteral = retryAfterLiteral })), (HttpStatusCode.BadRequest, CosmosExceptionFactory.CreateBadRequestException(testMessage, new Headers() { SubStatusCodeLiteral = substatus, ActivityId = activityId, RequestCharge = requestCharge, RetryAfterLiteral = retryAfterLiteral })), (HttpStatusCode.RequestTimeout, CosmosExceptionFactory.CreateRequestTimeoutException(testMessage, new Headers() { SubStatusCodeLiteral = substatus, ActivityId = activityId, RequestCharge = requestCharge, RetryAfterLiteral = retryAfterLiteral })), ((HttpStatusCode)429, CosmosExceptionFactory.CreateThrottledException(testMessage, new Headers() { SubStatusCodeLiteral = substatus, ActivityId = activityId, RequestCharge = requestCharge, RetryAfterLiteral = retryAfterLiteral })), }; foreach ((HttpStatusCode statusCode, CosmosException exception) in exceptionsToStatusCodes) { this.ValidateExceptionInfo( exception, statusCode, substatus, testMessage, activityId, requestCharge, retryAfter); } CosmosException cosmosException = CosmosExceptionFactory.CreateNotFoundException(testMessage, new Headers() { SubStatusCodeLiteral = ((int)SubStatusCodes.ReadSessionNotAvailable).ToString(), ActivityId = activityId, RequestCharge = requestCharge, RetryAfterLiteral = retryAfterLiteral }); this.ValidateExceptionInfo( cosmosException, HttpStatusCode.NotFound, ((int)SubStatusCodes.ReadSessionNotAvailable).ToString(), testMessage, activityId, requestCharge, retryAfter); }
public async Task FeedEstimatorRunner_TransientErrorsShouldContinue() { const long estimation = 10; bool detectedEstimationCorrectly = false; CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(500); Task estimatorDispatcher(long detectedEstimation, CancellationToken token) { detectedEstimationCorrectly = estimation == detectedEstimation; cancellationTokenSource.Cancel(); return(Task.CompletedTask); } Mock <FeedResponse <ChangeFeedProcessorState> > mockedResponse = new Mock <FeedResponse <ChangeFeedProcessorState> >(); mockedResponse.Setup(r => r.Count).Returns(1); mockedResponse.Setup(r => r.GetEnumerator()).Returns(new List <ChangeFeedProcessorState>() { new ChangeFeedProcessorState(string.Empty, estimation, string.Empty) }.GetEnumerator()); CosmosException exception = CosmosExceptionFactory.CreateThrottledException("throttled", new Headers()); Mock <FeedIterator <ChangeFeedProcessorState> > mockedIterator = new Mock <FeedIterator <ChangeFeedProcessorState> >(); mockedIterator.SetupSequence(i => i.ReadNextAsync(It.IsAny <CancellationToken>())) .ThrowsAsync(exception) .ReturnsAsync(mockedResponse.Object); Mock <ChangeFeedEstimator> mockedEstimator = new Mock <ChangeFeedEstimator>(); mockedEstimator.Setup(e => e.GetCurrentStateIterator(It.IsAny <ChangeFeedEstimatorRequestOptions>())).Returns(mockedIterator.Object); Mock <ChangeFeedProcessorHealthMonitor> healthMonitor = new Mock <ChangeFeedProcessorHealthMonitor>(); FeedEstimatorRunner estimatorCore = new FeedEstimatorRunner(estimatorDispatcher, mockedEstimator.Object, healthMonitor.Object, TimeSpan.FromMilliseconds(10)); try { await estimatorCore.RunAsync(cancellationTokenSource.Token); } catch (TaskCanceledException) { // expected } Assert.IsTrue(detectedEstimationCorrectly); mockedIterator.Verify(i => i.ReadNextAsync(It.IsAny <CancellationToken>()), Times.Exactly(2)); healthMonitor .Verify(m => m.NotifyErrorAsync(It.IsAny <string>(), exception), Times.Once); }
public async Task ProcessChanges_WhenCheckpointThrows_ShouldThrow() { CosmosException original = CosmosExceptionFactory.CreateThrottledException("throttled", new Headers()); Mock <PartitionCheckpointer> checkpointer = new Mock <PartitionCheckpointer>(); checkpointer.Setup(c => c.CheckpointPartitionAsync(It.IsAny <string>())).ThrowsAsync(original); ResponseMessage responseMessage = new ResponseMessage(); responseMessage.Headers.ContinuationToken = Guid.NewGuid().ToString(); ChangeFeedObserverContextCore observerContext = new ChangeFeedObserverContextCore(Guid.NewGuid().ToString(), feedResponse: responseMessage, checkpointer.Object); CosmosException caught = await Assert.ThrowsExceptionAsync <CosmosException>(() => this.sut.ProcessChangesAsync(observerContext, this.stream, CancellationToken.None)); Assert.AreEqual(original, caught); }
public async Task TryCheckpoint_OnCosmosException() { CosmosException cosmosException = CosmosExceptionFactory.CreateThrottledException("throttled", new Headers()); string leaseToken = Guid.NewGuid().ToString(); string continuation = Guid.NewGuid().ToString(); ResponseMessage responseMessage = new ResponseMessage(HttpStatusCode.OK); responseMessage.Headers.ContinuationToken = continuation; Mock <PartitionCheckpointer> checkpointer = new Mock <PartitionCheckpointer>(); checkpointer.Setup(c => c.CheckpointPartitionAsync(It.Is <string>(s => s == continuation))).ThrowsAsync(cosmosException); ChangeFeedObserverContextCore changeFeedObserverContextCore = new ChangeFeedObserverContextCore(leaseToken, responseMessage, checkpointer.Object); CosmosException exception = await Assert.ThrowsExceptionAsync <CosmosException>(() => changeFeedObserverContextCore.CheckpointAsync()); Assert.ReferenceEquals(cosmosException, exception); }
public void EnsureCorrectStatusCode() { string testMessage = "Test" + Guid.NewGuid().ToString(); List <(HttpStatusCode statusCode, CosmosException exception)> exceptionsToStatusCodes = new List <(HttpStatusCode, CosmosException)>() { (HttpStatusCode.NotFound, CosmosExceptionFactory.CreateNotFoundException(testMessage, activityId: Guid.NewGuid().ToString())), (HttpStatusCode.InternalServerError, CosmosExceptionFactory.CreateInternalServerErrorException(testMessage, activityId: Guid.NewGuid().ToString())), (HttpStatusCode.BadRequest, CosmosExceptionFactory.CreateBadRequestException(testMessage, activityId: Guid.NewGuid().ToString())), (HttpStatusCode.RequestTimeout, CosmosExceptionFactory.CreateRequestTimeoutException(testMessage, activityId: Guid.NewGuid().ToString())), ((HttpStatusCode)429, CosmosExceptionFactory.CreateThrottledException(testMessage, activityId: Guid.NewGuid().ToString())), }; foreach ((HttpStatusCode statusCode, CosmosException exception)item in exceptionsToStatusCodes) { this.ValidateExceptionInfo(item.exception, item.statusCode, testMessage); } }