|
| 1 | +// -------------------------------------------------------------------------------------------------------------------- |
| 2 | +// SPDX-FileCopyrightText: 2025 Siemens AG |
| 3 | +// |
| 4 | +// SPDX-License-Identifier: MIT |
| 5 | +// -------------------------------------------------------------------------------------------------------------------- |
| 6 | + |
| 7 | +using log4net; |
| 8 | +using Moq.Protected; |
| 9 | +using Moq; |
| 10 | +using System.Net; |
| 11 | + |
| 12 | +namespace LCT.APICommunications.UTest |
| 13 | +{ |
| 14 | + public class RetryHttpClientHandlerUTest |
| 15 | + { |
| 16 | + private Mock<ILog> _mockLogger; |
| 17 | + |
| 18 | + [SetUp] |
| 19 | + public void SetUp() |
| 20 | + { |
| 21 | + // Mock the logger |
| 22 | + _mockLogger = new Mock<ILog>(); |
| 23 | + } |
| 24 | + [Test] |
| 25 | + public async Task SendAsync_ShouldRetry_OnTransientErrors() |
| 26 | + { |
| 27 | + // Arrange |
| 28 | + var handlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict); |
| 29 | + handlerMock |
| 30 | + .Protected() |
| 31 | + .SetupSequence<Task<HttpResponseMessage>>( |
| 32 | + "SendAsync", |
| 33 | + ItExpr.IsAny<HttpRequestMessage>(), |
| 34 | + ItExpr.IsAny<CancellationToken>()) |
| 35 | + .ThrowsAsync(new HttpRequestException()) |
| 36 | + .ThrowsAsync(new TaskCanceledException()) |
| 37 | + .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK)); |
| 38 | + |
| 39 | + var retryHandler = new RetryHttpClientHandler |
| 40 | + { |
| 41 | + InnerHandler = handlerMock.Object |
| 42 | + }; |
| 43 | + |
| 44 | + var httpClient = new HttpClient(retryHandler); |
| 45 | + |
| 46 | + // Act |
| 47 | + var response = await httpClient.GetAsync("http://test.com"); |
| 48 | + |
| 49 | + // Assert |
| 50 | + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); |
| 51 | + handlerMock.Protected().Verify( |
| 52 | + "SendAsync", |
| 53 | + Times.Exactly(3), // 2 retries + 1 initial call |
| 54 | + ItExpr.IsAny<HttpRequestMessage>(), |
| 55 | + ItExpr.IsAny<CancellationToken>()); |
| 56 | + } |
| 57 | + |
| 58 | + [Test] |
| 59 | + public async Task SendAsync_ShouldNotRetry_OnNonTransientErrors() |
| 60 | + { |
| 61 | + // Arrange |
| 62 | + var handlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict); |
| 63 | + handlerMock |
| 64 | + .Protected() |
| 65 | + .Setup<Task<HttpResponseMessage>>( |
| 66 | + "SendAsync", |
| 67 | + ItExpr.IsAny<HttpRequestMessage>(), |
| 68 | + ItExpr.IsAny<CancellationToken>()) |
| 69 | + .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.BadRequest)); |
| 70 | + |
| 71 | + var retryHandler = new RetryHttpClientHandler |
| 72 | + { |
| 73 | + InnerHandler = handlerMock.Object |
| 74 | + }; |
| 75 | + |
| 76 | + var httpClient = new HttpClient(retryHandler); |
| 77 | + |
| 78 | + // Act |
| 79 | + var response = await httpClient.GetAsync("http://test.com"); |
| 80 | + |
| 81 | + // Assert |
| 82 | + Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); |
| 83 | + handlerMock.Protected().Verify( |
| 84 | + "SendAsync", |
| 85 | + Times.Once(), // No retries |
| 86 | + ItExpr.IsAny<HttpRequestMessage>(), |
| 87 | + ItExpr.IsAny<CancellationToken>()); |
| 88 | + } |
| 89 | + |
| 90 | + [Test] |
| 91 | + public async Task SendAsync_ShouldLogRetryAttempts() |
| 92 | + { |
| 93 | + // Arrange |
| 94 | + var handlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict); |
| 95 | + handlerMock |
| 96 | + .Protected() |
| 97 | + .SetupSequence<Task<HttpResponseMessage>>( |
| 98 | + "SendAsync", |
| 99 | + ItExpr.IsAny<HttpRequestMessage>(), |
| 100 | + ItExpr.IsAny<CancellationToken>()) |
| 101 | + .ThrowsAsync(new HttpRequestException()) |
| 102 | + .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK)); |
| 103 | + |
| 104 | + |
| 105 | + var retryHandler = new RetryHttpClientHandler() |
| 106 | + { |
| 107 | + InnerHandler = handlerMock.Object |
| 108 | + }; |
| 109 | + |
| 110 | + var httpClient = new HttpClient(retryHandler); |
| 111 | + |
| 112 | + // Act |
| 113 | + var response = await httpClient.GetAsync("http://test.com"); |
| 114 | + |
| 115 | + // Assert |
| 116 | + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); |
| 117 | + } |
| 118 | + [Test] |
| 119 | + public async Task ExecuteWithRetryAsync_ShouldCompleteSuccessfully_WhenActionSucceedsAfterRetry() |
| 120 | + { |
| 121 | + // Arrange |
| 122 | + var attempts = 0; |
| 123 | + var action = new Func<Task>(() => |
| 124 | + { |
| 125 | + attempts++; |
| 126 | + if (attempts < ApiConstant.APIRetryIntervals.Count) |
| 127 | + { |
| 128 | + throw new WebException("Temporary error", WebExceptionStatus.Timeout); |
| 129 | + } |
| 130 | + return Task.CompletedTask; // Successfully completes after retries |
| 131 | + }); |
| 132 | + |
| 133 | + // Act |
| 134 | + await RetryHttpClientHandler.ExecuteWithRetryAsync(action); |
| 135 | + |
| 136 | + // Assert |
| 137 | + Assert.AreEqual(ApiConstant.APIRetryIntervals.Count, attempts, "Action should have been attempted the expected number of times."); |
| 138 | + } |
| 139 | + |
| 140 | + [Test] |
| 141 | + public async Task ExecuteWithRetryAsync_ShouldNotRetry_WhenNoWebExceptionIsThrown() |
| 142 | + { |
| 143 | + // Arrange |
| 144 | + var actionExecuted = false; |
| 145 | + var action = new Func<Task>(() => |
| 146 | + { |
| 147 | + actionExecuted = true; |
| 148 | + return Task.CompletedTask; |
| 149 | + }); |
| 150 | + |
| 151 | + // Act |
| 152 | + await RetryHttpClientHandler.ExecuteWithRetryAsync(action); |
| 153 | + |
| 154 | + // Assert |
| 155 | + Assert.IsTrue(actionExecuted, "Action should have been executed."); |
| 156 | + _mockLogger.Verify(logger => logger.Debug(It.IsAny<string>()), Times.Never, "Retry should not occur if there is no exception."); |
| 157 | + } |
| 158 | + |
| 159 | + } |
| 160 | +} |
0 commit comments