|
| 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.feign; |
| 17 | + |
| 18 | +import com.github.tomakehurst.wiremock.WireMockServer; |
| 19 | +import dev.failsafe.*; |
| 20 | +import dev.failsafe.feign.testing.FeignTesting; |
| 21 | +import feign.FeignException.BadRequest; |
| 22 | +import org.testng.annotations.AfterMethod; |
| 23 | +import org.testng.annotations.BeforeMethod; |
| 24 | +import org.testng.annotations.Test; |
| 25 | + |
| 26 | +import java.time.Duration; |
| 27 | + |
| 28 | +import static com.github.tomakehurst.wiremock.client.WireMock.*; |
| 29 | +import static org.testng.Assert.assertEquals; |
| 30 | +import static org.testng.Assert.assertNotNull; |
| 31 | + |
| 32 | +@Test |
| 33 | +public class FailsafeFeignTest extends FeignTesting { |
| 34 | + WireMockServer server; |
| 35 | + |
| 36 | + @BeforeMethod |
| 37 | + protected void beforeMethod() { |
| 38 | + server = new WireMockServer(); |
| 39 | + server.start(); |
| 40 | + } |
| 41 | + |
| 42 | + @AfterMethod |
| 43 | + protected void afterMethod() { |
| 44 | + server.stop(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Asserts that equals, hashcode, and toString work as expected. |
| 49 | + */ |
| 50 | + public void testObjectMethods() { |
| 51 | + FeignService service1 = FailsafeFeign.builder(RetryPolicy.ofDefaults()).target(FeignService.class, URL); |
| 52 | + FeignService service2 = FailsafeFeign.builder(RetryPolicy.ofDefaults()).target(FeignService.class, URL); |
| 53 | + |
| 54 | + assertEquals(service1, service1); |
| 55 | + assertEquals(service2, service1); // Targets are compared by their internal values |
| 56 | + assertEquals(service1.hashCode(), service2.hashCode()); // Hashcodes are computed from internal values |
| 57 | + assertNotNull(service1.toString()); |
| 58 | + } |
| 59 | + |
| 60 | + public void testSuccess() { |
| 61 | + // Given |
| 62 | + mockResponse(200, "foo"); |
| 63 | + FailsafeExecutor<String> failsafe = Failsafe.with(RetryPolicy.ofDefaults()); |
| 64 | + |
| 65 | + // When / Then |
| 66 | + testSuccess(failsafe, FeignService::test, (f, e) -> { |
| 67 | + assertEquals(e.getAttemptCount(), 1); |
| 68 | + assertEquals(e.getExecutionCount(), 1); |
| 69 | + }, "foo"); |
| 70 | + assertCalled("/test", 1); |
| 71 | + } |
| 72 | + |
| 73 | + public void testSuccessDefault() { |
| 74 | + // Given |
| 75 | + mockResponse(200, "foo"); |
| 76 | + FailsafeExecutor<String> failsafe = Failsafe.with(RetryPolicy.ofDefaults()); |
| 77 | + |
| 78 | + // When / Then |
| 79 | + testSuccess(failsafe, FeignService::testDefault, (f, e) -> { |
| 80 | + assertEquals(e.getAttemptCount(), 1); |
| 81 | + assertEquals(e.getExecutionCount(), 1); |
| 82 | + }, "foo"); |
| 83 | + assertCalled("/test", 1); |
| 84 | + } |
| 85 | + |
| 86 | + public void testRetryPolicyOnFailure() { |
| 87 | + // Given |
| 88 | + mockResponse(400, "foo"); |
| 89 | + FailsafeExecutor<String> failsafe = Failsafe.with(RetryPolicy.ofDefaults()); |
| 90 | + |
| 91 | + // When / Then |
| 92 | + testFailure(failsafe, FeignService::test, (f, e) -> { |
| 93 | + assertEquals(e.getAttemptCount(), 3); |
| 94 | + assertEquals(e.getExecutionCount(), 3); |
| 95 | + }, BadRequest.class); |
| 96 | + assertCalled("/test", 3); |
| 97 | + } |
| 98 | + |
| 99 | + public void testRetryPolicyOnResult() { |
| 100 | + // Given |
| 101 | + mockResponse(200, "bad"); |
| 102 | + FailsafeExecutor<String> failsafe = Failsafe.with(RetryPolicy.<String>builder().handleResult("bad").build()); |
| 103 | + |
| 104 | + // When / Then |
| 105 | + testSuccess(failsafe, FeignService::test, (f, e) -> { |
| 106 | + assertEquals(e.getAttemptCount(), 3); |
| 107 | + assertEquals(e.getExecutionCount(), 3); |
| 108 | + }, "bad"); |
| 109 | + assertCalled("/test", 3); |
| 110 | + } |
| 111 | + |
| 112 | + public void testRetryPolicyFallback() { |
| 113 | + // Given |
| 114 | + mockResponse(400, "foo"); |
| 115 | + FailsafeExecutor<String> failsafe = Failsafe.with(Fallback.of("fallback"), RetryPolicy.ofDefaults()); |
| 116 | + |
| 117 | + // When / Then |
| 118 | + testSuccess(failsafe, FeignService::test, (f, e) -> { |
| 119 | + assertEquals(e.getAttemptCount(), 3); |
| 120 | + assertEquals(e.getExecutionCount(), 3); |
| 121 | + }, "fallback"); |
| 122 | + assertCalled("/test", 3); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Asserts that an open circuit breaker prevents executions from occurring, even with outer retries. |
| 127 | + */ |
| 128 | + public void testCircuitBreaker() { |
| 129 | + // Given |
| 130 | + mockResponse(200, "foo"); |
| 131 | + CircuitBreaker<String> breaker = CircuitBreaker.ofDefaults(); |
| 132 | + FailsafeExecutor<String> failsafe = Failsafe.with(RetryPolicy.ofDefaults(), breaker); |
| 133 | + breaker.open(); |
| 134 | + |
| 135 | + // When / Then |
| 136 | + testFailure(failsafe, FeignService::test, (f, e) -> { |
| 137 | + assertEquals(e.getAttemptCount(), 3); |
| 138 | + assertEquals(e.getExecutionCount(), 0); |
| 139 | + }, CircuitBreakerOpenException.class); |
| 140 | + assertCalled("/test", 0); |
| 141 | + } |
| 142 | + |
| 143 | + public void testTimeout() { |
| 144 | + // Given |
| 145 | + mockDelayedResponse(200, "foo", 200); |
| 146 | + FailsafeExecutor<String> failsafe = Failsafe.with(Timeout.of(Duration.ofMillis(100))); |
| 147 | + |
| 148 | + // When / Then |
| 149 | + testFailure(failsafe, FeignService::test, (f, e) -> { |
| 150 | + assertEquals(e.getAttemptCount(), 1); |
| 151 | + assertEquals(e.getExecutionCount(), 1); |
| 152 | + }, TimeoutExceededException.class); |
| 153 | + assertCalled("/test", 1); |
| 154 | + } |
| 155 | + |
| 156 | + private void mockResponse(int responseCode, String body) { |
| 157 | + stubFor(get(urlPathEqualTo("/test")).willReturn( |
| 158 | + aResponse().withStatus(responseCode).withHeader("Content-Type", "text/plain").withBody(body))); |
| 159 | + } |
| 160 | + |
| 161 | + private void mockDelayedResponse(int responseCode, String body, int delayMillis) { |
| 162 | + stubFor(get(urlEqualTo("/test")).willReturn( |
| 163 | + aResponse().withStatus(responseCode).withFixedDelay(delayMillis).withBody(body))); |
| 164 | + } |
| 165 | + |
| 166 | + private void assertCalled(String url, int times) { |
| 167 | + verify(times, getRequestedFor(urlPathEqualTo(url))); |
| 168 | + } |
| 169 | +} |
0 commit comments