|
| 1 | +/* |
| 2 | + * Copyright 2019 Ipregistry (https://ipregistry.co). |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package co.ipregistry.api.client.request; |
| 18 | + |
| 19 | +import org.apache.hc.core5.http.HttpResponse; |
| 20 | +import org.apache.hc.core5.http.message.BasicHttpResponse; |
| 21 | +import org.junit.jupiter.api.Assertions; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import javax.net.ssl.SSLException; |
| 25 | +import java.io.IOException; |
| 26 | +import java.net.ConnectException; |
| 27 | +import java.net.SocketTimeoutException; |
| 28 | +import java.net.UnknownHostException; |
| 29 | + |
| 30 | +class IpregistryRetryStrategyTest { |
| 31 | + |
| 32 | + private static HttpResponse response(final int code) { |
| 33 | + return new BasicHttpResponse(code); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void testRetriesOnServerErrorWhenEnabled() { |
| 38 | + final IpregistryRetryStrategy strategy = new IpregistryRetryStrategy(3, 1000, true, false); |
| 39 | + Assertions.assertTrue(strategy.retryRequest(response(500), 1, null)); |
| 40 | + Assertions.assertTrue(strategy.retryRequest(response(503), 1, null)); |
| 41 | + Assertions.assertTrue(strategy.retryRequest(response(599), 1, null)); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void testDoesNotRetryServerErrorWhenDisabled() { |
| 46 | + final IpregistryRetryStrategy strategy = new IpregistryRetryStrategy(3, 1000, false, false); |
| 47 | + Assertions.assertFalse(strategy.retryRequest(response(500), 1, null)); |
| 48 | + Assertions.assertFalse(strategy.retryRequest(response(503), 1, null)); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void testRetriesTooManyRequestsOnlyWhenEnabled() { |
| 53 | + Assertions.assertFalse(new IpregistryRetryStrategy(3, 1000, true, false) |
| 54 | + .retryRequest(response(429), 1, null)); |
| 55 | + Assertions.assertTrue(new IpregistryRetryStrategy(3, 1000, true, true) |
| 56 | + .retryRequest(response(429), 1, null)); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void testDoesNotRetryClientErrorsOrSuccess() { |
| 61 | + final IpregistryRetryStrategy strategy = new IpregistryRetryStrategy(3, 1000, true, true); |
| 62 | + Assertions.assertFalse(strategy.retryRequest(response(200), 1, null)); |
| 63 | + Assertions.assertFalse(strategy.retryRequest(response(400), 1, null)); |
| 64 | + Assertions.assertFalse(strategy.retryRequest(response(401), 1, null)); |
| 65 | + Assertions.assertFalse(strategy.retryRequest(response(404), 1, null)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void testStopsAfterMaxAttempts() { |
| 70 | + final IpregistryRetryStrategy strategy = new IpregistryRetryStrategy(2, 1000, true, true); |
| 71 | + Assertions.assertTrue(strategy.retryRequest(response(500), 1, null)); |
| 72 | + Assertions.assertTrue(strategy.retryRequest(response(500), 2, null)); |
| 73 | + Assertions.assertFalse(strategy.retryRequest(response(500), 3, null)); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void testRetriesTransientIoExceptionButNotPermanentOnes() { |
| 78 | + final IpregistryRetryStrategy strategy = new IpregistryRetryStrategy(3, 1000, true, false); |
| 79 | + Assertions.assertTrue(strategy.retryRequest(null, new IOException("reset"), 1, null)); |
| 80 | + |
| 81 | + Assertions.assertFalse(strategy.retryRequest(null, new SocketTimeoutException(), 1, null)); |
| 82 | + Assertions.assertFalse(strategy.retryRequest(null, new UnknownHostException(), 1, null)); |
| 83 | + Assertions.assertFalse(strategy.retryRequest(null, new ConnectException(), 1, null)); |
| 84 | + Assertions.assertFalse(strategy.retryRequest(null, new SSLException("bad"), 1, null)); |
| 85 | + |
| 86 | + Assertions.assertFalse(strategy.retryRequest(null, new IOException("reset"), 4, null)); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void testExponentialBackoff() { |
| 91 | + final IpregistryRetryStrategy strategy = new IpregistryRetryStrategy(5, 1000, true, true); |
| 92 | + Assertions.assertEquals(1000, strategy.getRetryInterval(response(500), 1, null).toMilliseconds()); |
| 93 | + Assertions.assertEquals(2000, strategy.getRetryInterval(response(500), 2, null).toMilliseconds()); |
| 94 | + Assertions.assertEquals(4000, strategy.getRetryInterval(response(500), 3, null).toMilliseconds()); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void testRetryAfterHeaderTakesPrecedence() { |
| 99 | + final IpregistryRetryStrategy strategy = new IpregistryRetryStrategy(5, 1000, true, true); |
| 100 | + |
| 101 | + final HttpResponse withRetryAfter = response(429); |
| 102 | + withRetryAfter.setHeader("Retry-After", "5"); |
| 103 | + Assertions.assertEquals(5000, strategy.getRetryInterval(withRetryAfter, 1, null).toMilliseconds()); |
| 104 | + |
| 105 | + // A non-numeric (HTTP-date) Retry-After is not honored and falls back to exponential backoff. |
| 106 | + final HttpResponse httpDate = response(429); |
| 107 | + httpDate.setHeader("Retry-After", "Wed, 21 Oct 2026 07:28:00 GMT"); |
| 108 | + Assertions.assertEquals(1000, strategy.getRetryInterval(httpDate, 1, null).toMilliseconds()); |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments