|
| 1 | +/* |
| 2 | + * Copyright 2022 the original author or authors. |
| 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 | +package dev.failsafe.okhttp; |
| 17 | + |
| 18 | +import com.github.tomakehurst.wiremock.WireMockServer; |
| 19 | +import dev.failsafe.*; |
| 20 | +import dev.failsafe.okhttp.testing.OkHttpTesting; |
| 21 | +import okhttp3.Request; |
| 22 | +import okhttp3.Response; |
| 23 | +import okhttp3.ResponseBody; |
| 24 | +import org.testng.annotations.AfterMethod; |
| 25 | +import org.testng.annotations.BeforeMethod; |
| 26 | +import org.testng.annotations.Test; |
| 27 | + |
| 28 | +import java.time.Duration; |
| 29 | + |
| 30 | +import static com.github.tomakehurst.wiremock.client.WireMock.*; |
| 31 | +import static org.testng.Assert.assertEquals; |
| 32 | + |
| 33 | +@Test |
| 34 | +public class FailsafeOkHttpTest extends OkHttpTesting { |
| 35 | + public static final String URL = "http://localhost:8080"; |
| 36 | + |
| 37 | + WireMockServer server; |
| 38 | + |
| 39 | + @BeforeMethod |
| 40 | + protected void beforeMethod() { |
| 41 | + server = new WireMockServer(); |
| 42 | + server.start(); |
| 43 | + } |
| 44 | + |
| 45 | + @AfterMethod |
| 46 | + protected void afterMethod() { |
| 47 | + server.stop(); |
| 48 | + } |
| 49 | + |
| 50 | + public void testSuccess() { |
| 51 | + // Given |
| 52 | + mockResponse(200, "foo"); |
| 53 | + FailsafeExecutor<Response> failsafe = Failsafe.with(RetryPolicy.ofDefaults()); |
| 54 | + Request request = requestFor("/test"); |
| 55 | + |
| 56 | + // When / Then |
| 57 | + testRequest(failsafe, request, (f, e) -> { |
| 58 | + assertEquals(e.getAttemptCount(), 1); |
| 59 | + assertEquals(e.getExecutionCount(), 1); |
| 60 | + }, 200, "foo"); |
| 61 | + assertCalled("/test", 1); |
| 62 | + } |
| 63 | + |
| 64 | + public void testRetryPolicyOn400() { |
| 65 | + // Given |
| 66 | + mockResponse(400, "foo"); |
| 67 | + RetryPolicy<Response> retryPolicy = RetryPolicy.<Response>builder().handleResultIf(r -> r.code() == 400).build(); |
| 68 | + FailsafeExecutor<Response> failsafe = Failsafe.with(retryPolicy); |
| 69 | + Request request = requestFor("/test"); |
| 70 | + |
| 71 | + // When / Then |
| 72 | + testRequest(failsafe, request, (f, e) -> { |
| 73 | + assertEquals(e.getAttemptCount(), 3); |
| 74 | + assertEquals(e.getExecutionCount(), 3); |
| 75 | + }, 400, "foo"); |
| 76 | + assertCalled("/test", 3); |
| 77 | + } |
| 78 | + |
| 79 | + public void testRetryPolicyOnResult() { |
| 80 | + // Given |
| 81 | + mockResponse(200, "bad"); |
| 82 | + RetryPolicy<Response> retryPolicy = RetryPolicy.<Response>builder() |
| 83 | + .handleResultIf(r -> "bad".equals(r.peekBody(Long.MAX_VALUE).string())) |
| 84 | + .build(); |
| 85 | + FailsafeExecutor<Response> failsafe = Failsafe.with(retryPolicy); |
| 86 | + Request request = requestFor("/test"); |
| 87 | + |
| 88 | + // When / Then |
| 89 | + testRequest(failsafe, request, (f, e) -> { |
| 90 | + assertEquals(e.getAttemptCount(), 3); |
| 91 | + assertEquals(e.getExecutionCount(), 3); |
| 92 | + }, 200, "bad"); |
| 93 | + assertCalled("/test", 3); |
| 94 | + } |
| 95 | + |
| 96 | + public void testRetryPolicyFallback() { |
| 97 | + // Given |
| 98 | + mockResponse(400, "foo"); |
| 99 | + Fallback<Response> fallback = Fallback.<Response>builder(r -> { |
| 100 | + Response response = r.getLastResult(); |
| 101 | + ResponseBody body = ResponseBody.create("fallback", response.body().contentType()); |
| 102 | + return response.newBuilder().code(200).body(body).build(); |
| 103 | + }).handleResultIf(r -> r.code() == 400).build(); |
| 104 | + RetryPolicy<Response> retryPolicy = RetryPolicy.<Response>builder().handleResultIf(r -> r.code() == 400).build(); |
| 105 | + FailsafeExecutor<Response> failsafe = Failsafe.with(fallback, retryPolicy); |
| 106 | + Request request = requestFor("/test"); |
| 107 | + |
| 108 | + // When / Then |
| 109 | + testRequest(failsafe, request, (f, e) -> { |
| 110 | + assertEquals(e.getAttemptCount(), 3); |
| 111 | + assertEquals(e.getExecutionCount(), 3); |
| 112 | + }, 200, "fallback"); |
| 113 | + assertCalled("/test", 3); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Asserts that an open circuit breaker prevents executions from occurring, even with outer retries. |
| 118 | + */ |
| 119 | + public void testCircuitBreaker() { |
| 120 | + // Given |
| 121 | + mockResponse(200, "foo"); |
| 122 | + CircuitBreaker<Response> breaker = CircuitBreaker.ofDefaults(); |
| 123 | + FailsafeExecutor<Response> failsafe = Failsafe.with(RetryPolicy.ofDefaults(), breaker); |
| 124 | + Request request = requestFor("/test"); |
| 125 | + breaker.open(); |
| 126 | + |
| 127 | + // When / Then |
| 128 | + testFailure(failsafe, request, (f, e) -> { |
| 129 | + assertEquals(e.getAttemptCount(), 3); |
| 130 | + assertEquals(e.getExecutionCount(), 0); |
| 131 | + }, CircuitBreakerOpenException.class); |
| 132 | + assertCalled("/test", 0); |
| 133 | + } |
| 134 | + |
| 135 | + public void testTimeout() { |
| 136 | + // Given |
| 137 | + mockDelayedResponse(200, "foo", 1000); |
| 138 | + FailsafeExecutor<Response> failsafe = Failsafe.with(Timeout.of(Duration.ofMillis(100))); |
| 139 | + Request request = requestFor("/test"); |
| 140 | + |
| 141 | + // When / Then |
| 142 | + testFailure(failsafe, request, (f, e) -> { |
| 143 | + assertEquals(e.getAttemptCount(), 1); |
| 144 | + assertEquals(e.getExecutionCount(), 1); |
| 145 | + }, TimeoutExceededException.class); |
| 146 | + assertCalled("/test", 1); |
| 147 | + } |
| 148 | + |
| 149 | + private Request requestFor(String path) { |
| 150 | + return new Request.Builder().url(URL + path).build(); |
| 151 | + } |
| 152 | + |
| 153 | + private void mockResponse(int responseCode, String body) { |
| 154 | + stubFor(get(urlPathEqualTo("/test")).willReturn( |
| 155 | + aResponse().withStatus(responseCode).withHeader("Content-Type", "text/plain").withBody(body))); |
| 156 | + } |
| 157 | + |
| 158 | + private void mockDelayedResponse(int responseCode, String body, int delayMillis) { |
| 159 | + stubFor(get(urlEqualTo("/test")).willReturn( |
| 160 | + aResponse().withStatus(responseCode).withFixedDelay(delayMillis).withBody(body))); |
| 161 | + } |
| 162 | + |
| 163 | + private void assertCalled(String url, int times) { |
| 164 | + verify(times, getRequestedFor(urlPathEqualTo(url))); |
| 165 | + } |
| 166 | +} |
0 commit comments