public async Task CreateAsync_ExecutesRetryPolicyAsExpected() { // Expect the parameters to be passed to the method of the inner rest client. // We'll throw an exception from within the callback method to trigger retries. this.mockRestClient .Setup(c => c.CreateAsync( It.Is <EndpointName>(e => e.Equals(this.endpointName)), It.Is <string>(j => j.Equals(this.inputData)), It.Is <LogContext>(l => l.Equals(this.logContext)), It.IsAny <CancellationToken>())) .Callback((EndpointName en, string jd, LogContext lc, CancellationToken ct) => throw new ServiceUnavailableException("Something went wrong.")); try { IRestClient restClient = GetRestClientWithRetries(); await restClient.CreateAsync(this.endpointName, this.inputData, this.logContext, default); Assert.Fail("Expected exception to be thrown."); } catch (ServiceUnavailableException) { } // Verify our inner rest client was called 4 times (original invocation + retries) this.mockRestClient.Verify(m => m.CreateAsync(this.endpointName, this.inputData, this.logContext, default), Times.Exactly(RetryCount + 1)); }
public async Task CreateAsync_FailsOnFirstTryWhenExceptionIsNotHandledByPolicy() { // Expect the parameters to be passed to the method of the inner rest client. // We'll throw an exception from within the callback method to trigger retries. this.mockRestClient .Setup(c => c.CreateAsync( It.Is <EndpointName>(e => e.Equals(this.endpointName)), It.Is <string>(j => j.Equals(this.inputData)), It.Is <LogContext>(l => l.Equals(this.logContext)), It.IsAny <CancellationToken>())) .Callback((EndpointName en, string jd, LogContext lc, CancellationToken ct) => throw new BadRequestException("Bad request.")); IRestClient restClient = GetRestClientWithRetries(); try { await restClient.CreateAsync(this.endpointName, this.inputData, this.logContext, default); Assert.Fail("Expected exception to be thrown."); } catch (BadRequestException) { } // Verify our inner rest client was called exactly once. this.mockRestClient.Verify(m => m.CreateAsync(this.endpointName, this.inputData, this.logContext, default), Times.Once); }
public async Task CreateAsync_SucceedsOnFirstTryWhenNoExceptionIsThrown() { // Expect the parameters to be passed to the method of the inner rest client. // We'll throw an exception from within the callback method to trigger retries. this.mockRestClient .Setup(c => c.CreateAsync( It.Is <EndpointName>(e => e.Equals(this.endpointName)), It.Is <string>(j => j.Equals(this.inputData)), It.Is <LogContext>(l => l.Equals(this.logContext)), It.IsAny <CancellationToken>())) .Returns(Task.FromResult("Done.")); IRestClient restClient = GetRestClientWithRetries(); string result = await restClient.CreateAsync(this.endpointName, this.inputData, this.logContext, default); Assert.AreEqual(SuccessResult, result, $"Expected result: '{SuccessResult}'."); // Verify our inner rest client was called only once. this.mockRestClient.Verify(m => m.CreateAsync(this.endpointName, this.inputData, this.logContext, default), Times.Once); }