|
| 1 | +package feign.hystrix; |
| 2 | + |
| 3 | +import feign.FeignException; |
| 4 | +import feign.RequestLine; |
| 5 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 6 | +import java.util.logging.Level; |
| 7 | +import java.util.logging.Logger; |
| 8 | +import okhttp3.mockwebserver.MockResponse; |
| 9 | +import okhttp3.mockwebserver.MockWebServer; |
| 10 | +import org.junit.Rule; |
| 11 | +import org.junit.Test; |
| 12 | +import org.junit.rules.ExpectedException; |
| 13 | + |
| 14 | +import static feign.assertj.MockWebServerAssertions.assertThat; |
| 15 | + |
| 16 | +public class FallbackFactoryTest { |
| 17 | + |
| 18 | + interface TestInterface { |
| 19 | + @RequestLine("POST /") String invoke(); |
| 20 | + } |
| 21 | + |
| 22 | + @Rule |
| 23 | + public final ExpectedException thrown = ExpectedException.none(); |
| 24 | + @Rule |
| 25 | + public final MockWebServer server = new MockWebServer(); |
| 26 | + |
| 27 | + @Test |
| 28 | + public void fallbackFactory_example_lambda() { |
| 29 | + server.enqueue(new MockResponse().setResponseCode(500)); |
| 30 | + server.enqueue(new MockResponse().setResponseCode(404)); |
| 31 | + |
| 32 | + TestInterface api = target(cause -> () -> { |
| 33 | + assertThat(cause).isInstanceOf(FeignException.class); |
| 34 | + return ((FeignException) cause).status() == 500 ? "foo" : "bar"; |
| 35 | + }); |
| 36 | + |
| 37 | + assertThat(api.invoke()).isEqualTo("foo"); |
| 38 | + assertThat(api.invoke()).isEqualTo("bar"); |
| 39 | + } |
| 40 | + |
| 41 | + static class FallbackApiWithCtor implements TestInterface { |
| 42 | + final Throwable cause; |
| 43 | + |
| 44 | + FallbackApiWithCtor(Throwable cause) { |
| 45 | + this.cause = cause; |
| 46 | + } |
| 47 | + |
| 48 | + @Override public String invoke() { |
| 49 | + return "foo"; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void fallbackFactory_example_ctor() { |
| 55 | + server.enqueue(new MockResponse().setResponseCode(500)); |
| 56 | + |
| 57 | + // method reference |
| 58 | + TestInterface api = target(FallbackApiWithCtor::new); |
| 59 | + |
| 60 | + assertThat(api.invoke()).isEqualTo("foo"); |
| 61 | + |
| 62 | + server.enqueue(new MockResponse().setResponseCode(500)); |
| 63 | + |
| 64 | + // lambda factory |
| 65 | + api = target(throwable -> new FallbackApiWithCtor(throwable)); |
| 66 | + |
| 67 | + server.enqueue(new MockResponse().setResponseCode(500)); |
| 68 | + |
| 69 | + // old school |
| 70 | + api = target(new FallbackFactory<TestInterface>() { |
| 71 | + @Override public TestInterface create(Throwable cause) { |
| 72 | + return new FallbackApiWithCtor(cause); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + assertThat(api.invoke()).isEqualTo("foo"); |
| 77 | + } |
| 78 | + |
| 79 | + // retrofit so people don't have to track 2 classes |
| 80 | + static class FallbackApiRetro implements TestInterface, FallbackFactory<FallbackApiRetro> { |
| 81 | + |
| 82 | + @Override public FallbackApiRetro create(Throwable cause) { |
| 83 | + return new FallbackApiRetro(cause); |
| 84 | + } |
| 85 | + |
| 86 | + final Throwable cause; // nullable |
| 87 | + |
| 88 | + public FallbackApiRetro() { |
| 89 | + this(null); |
| 90 | + } |
| 91 | + |
| 92 | + FallbackApiRetro(Throwable cause) { |
| 93 | + this.cause = cause; |
| 94 | + } |
| 95 | + |
| 96 | + @Override public String invoke() { |
| 97 | + return cause != null ? cause.getMessage() : "foo"; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void fallbackFactory_example_retro() { |
| 103 | + server.enqueue(new MockResponse().setResponseCode(500)); |
| 104 | + |
| 105 | + // method reference |
| 106 | + TestInterface api = target(new FallbackApiRetro()); |
| 107 | + |
| 108 | + assertThat(api.invoke()).isEqualTo("status 500 reading TestInterface#invoke()"); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void defaultFallbackFactory_delegates() { |
| 113 | + server.enqueue(new MockResponse().setResponseCode(500)); |
| 114 | + |
| 115 | + TestInterface api = target(new FallbackFactory.Default<>(() -> "foo")); |
| 116 | + |
| 117 | + assertThat(api.invoke()) |
| 118 | + .isEqualTo("foo"); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void defaultFallbackFactory_doesntLogByDefault() { |
| 123 | + server.enqueue(new MockResponse().setResponseCode(500)); |
| 124 | + |
| 125 | + Logger logger = new Logger("", null) { |
| 126 | + @Override public void log(Level level, String msg, Throwable thrown) { |
| 127 | + throw new AssertionError("logged eventhough not FINE level"); |
| 128 | + } |
| 129 | + }; |
| 130 | + |
| 131 | + target(new FallbackFactory.Default<>(() -> "foo", logger)).invoke(); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void defaultFallbackFactory_logsAtFineLevel() { |
| 136 | + server.enqueue(new MockResponse().setResponseCode(500)); |
| 137 | + |
| 138 | + AtomicBoolean logged = new AtomicBoolean(); |
| 139 | + Logger logger = new Logger("", null) { |
| 140 | + @Override public void log(Level level, String msg, Throwable thrown) { |
| 141 | + logged.set(true); |
| 142 | + |
| 143 | + assertThat(msg).isEqualTo("fallback due to: status 500 reading TestInterface#invoke()"); |
| 144 | + assertThat(thrown).isInstanceOf(FeignException.class); |
| 145 | + } |
| 146 | + }; |
| 147 | + logger.setLevel(Level.FINE); |
| 148 | + |
| 149 | + target(new FallbackFactory.Default<>(() -> "foo", logger)).invoke(); |
| 150 | + assertThat(logged.get()).isTrue(); |
| 151 | + } |
| 152 | + |
| 153 | + TestInterface target(FallbackFactory<? extends TestInterface> factory) { |
| 154 | + return HystrixFeign.builder() |
| 155 | + .target(TestInterface.class, "http://localhost:" + server.getPort(), factory); |
| 156 | + } |
| 157 | +} |
0 commit comments