|
16 | 16 |
|
17 | 17 | import io.opentelemetry.opamp.client.internal.connectivity.http.HttpErrorException; |
18 | 18 | import io.opentelemetry.opamp.client.internal.connectivity.http.HttpSender; |
| 19 | +import io.opentelemetry.opamp.client.internal.connectivity.http.RetryAfterParser; |
19 | 20 | import io.opentelemetry.opamp.client.internal.request.Request; |
20 | 21 | import io.opentelemetry.opamp.client.internal.request.delay.AcceptsDelaySuggestion; |
21 | 22 | import io.opentelemetry.opamp.client.internal.request.delay.PeriodicDelay; |
@@ -53,7 +54,12 @@ class HttpRequestServiceTest { |
53 | 54 | @BeforeEach |
54 | 55 | void setUp() { |
55 | 56 | httpRequestService = |
56 | | - new HttpRequestService(requestSender, executor, periodicRequestDelay, periodicRetryDelay); |
| 57 | + new HttpRequestService( |
| 58 | + requestSender, |
| 59 | + executor, |
| 60 | + periodicRequestDelay, |
| 61 | + periodicRetryDelay, |
| 62 | + RetryAfterParser.getInstance()); |
57 | 63 | } |
58 | 64 |
|
59 | 65 | @Test |
@@ -186,6 +192,23 @@ void verifySendingRequest_whenThereIsATooManyRequestsError() { |
186 | 192 |
|
187 | 193 | verify(callback).onRequestFailed(new HttpErrorException(429, "Error message")); |
188 | 194 | verify(executor).setPeriodicDelay(periodicRetryDelay); |
| 195 | + verify(periodicRetryDelay, never()).suggestDelay(any()); |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + void verifySendingRequest_whenThereIsATooManyRequestsError_withSuggestedDelay() { |
| 200 | + HttpSender.Response response = mock(); |
| 201 | + when(response.statusCode()).thenReturn(429); |
| 202 | + when(response.statusMessage()).thenReturn("Error message"); |
| 203 | + when(response.getHeader("Retry-After")).thenReturn("5"); |
| 204 | + prepareRequest(); |
| 205 | + enqueueResponse(response); |
| 206 | + |
| 207 | + httpRequestService.run(); |
| 208 | + |
| 209 | + verify(callback).onRequestFailed(new HttpErrorException(429, "Error message")); |
| 210 | + verify(executor).setPeriodicDelay(periodicRetryDelay); |
| 211 | + verify(periodicRetryDelay).suggestDelay(Duration.ofSeconds(5)); |
189 | 212 | } |
190 | 213 |
|
191 | 214 | @Test |
@@ -241,6 +264,23 @@ void verifySendingRequest_whenThereIsAServiceUnavailableError() { |
241 | 264 |
|
242 | 265 | verify(callback).onRequestFailed(new HttpErrorException(503, "Error message")); |
243 | 266 | verify(executor).setPeriodicDelay(periodicRetryDelay); |
| 267 | + verify(periodicRetryDelay, never()).suggestDelay(any()); |
| 268 | + } |
| 269 | + |
| 270 | + @Test |
| 271 | + void verifySendingRequest_whenThereIsAServiceUnavailableError_withSuggestedDelay() { |
| 272 | + HttpSender.Response response = mock(); |
| 273 | + when(response.getHeader("Retry-After")).thenReturn("2"); |
| 274 | + when(response.statusCode()).thenReturn(503); |
| 275 | + when(response.statusMessage()).thenReturn("Error message"); |
| 276 | + prepareRequest(); |
| 277 | + enqueueResponse(response); |
| 278 | + |
| 279 | + httpRequestService.run(); |
| 280 | + |
| 281 | + verify(callback).onRequestFailed(new HttpErrorException(503, "Error message")); |
| 282 | + verify(executor).setPeriodicDelay(periodicRetryDelay); |
| 283 | + verify(periodicRetryDelay).suggestDelay(Duration.ofSeconds(2)); |
244 | 284 | } |
245 | 285 |
|
246 | 286 | @Test |
|
0 commit comments