|
| 1 | +package feign.vertx; |
| 2 | + |
| 3 | +import feign.VertxFeign; |
| 4 | +import feign.jackson.JacksonDecoder; |
| 5 | +import feign.jackson.JacksonEncoder; |
| 6 | +import feign.vertx.testcase.HelloServiceAPI; |
| 7 | +import io.vertx.core.CompositeFuture; |
| 8 | +import io.vertx.core.Future; |
| 9 | +import io.vertx.core.Vertx; |
| 10 | +import io.vertx.core.http.HttpClientOptions; |
| 11 | +import io.vertx.core.http.HttpServer; |
| 12 | +import io.vertx.core.http.HttpServerOptions; |
| 13 | +import io.vertx.ext.unit.Async; |
| 14 | +import io.vertx.ext.unit.TestContext; |
| 15 | +import io.vertx.ext.unit.junit.VertxUnitRunner; |
| 16 | +import org.junit.After; |
| 17 | +import org.junit.Before; |
| 18 | +import org.junit.Rule; |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.rules.ExpectedException; |
| 21 | +import org.junit.runner.RunWith; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +/** |
| 27 | + * Verify that Feign-Vertx are resilient to a server disconnect event. |
| 28 | + */ |
| 29 | +@RunWith(VertxUnitRunner.class) |
| 30 | +public class VertxHttp11ClientReconnectTest { |
| 31 | + protected Vertx vertx = Vertx.vertx(); |
| 32 | + protected HttpServer httpServer = null; |
| 33 | + HelloServiceAPI client = null; |
| 34 | + |
| 35 | + @Rule |
| 36 | + public ExpectedException expectedException = ExpectedException.none(); |
| 37 | + |
| 38 | + /** |
| 39 | + * Create a Feign Vertx client that is built once and used several times |
| 40 | + * during positive and negative test cases. |
| 41 | + * @param context |
| 42 | + */ |
| 43 | + @Before |
| 44 | + public void before(TestContext context) { |
| 45 | + // for HTTP 1.1 test, set up the pool size to 3. Then in the server side, there should be only 3 connections created per client. |
| 46 | + HttpClientOptions options = new HttpClientOptions(); |
| 47 | + options.setMaxPoolSize(3); |
| 48 | + |
| 49 | + client = VertxFeign |
| 50 | + .builder() |
| 51 | + .vertx(this.vertx) |
| 52 | + .options(options) |
| 53 | + .encoder(new JacksonEncoder()) |
| 54 | + .decoder(new JacksonDecoder()) |
| 55 | + .target(HelloServiceAPI.class, "http://localhost:8090"); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Shutdown the server |
| 60 | + * @param context |
| 61 | + */ |
| 62 | + @After |
| 63 | + public void after(TestContext context) { |
| 64 | + closeServer(); |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + /** |
| 69 | + * Create an HTTP Server and return a future for the startup result |
| 70 | + * @return Future for handling the serve open event |
| 71 | + */ |
| 72 | + protected Future<HttpServer> createServer() { |
| 73 | + Future<HttpServer> ret = Future.future(); |
| 74 | + |
| 75 | + HttpServerOptions serverOptions = |
| 76 | + new HttpServerOptions() |
| 77 | + .setLogActivity(true) |
| 78 | + .setPort(8090) |
| 79 | + .setSsl(false); |
| 80 | + |
| 81 | + httpServer = this.vertx.createHttpServer(serverOptions); |
| 82 | + |
| 83 | + // Simple 200 handler |
| 84 | + httpServer.requestHandler( req -> req.response().setStatusCode(200).end("Success!") ); |
| 85 | + |
| 86 | + // Listen! delegating to the future |
| 87 | + httpServer.listen( ret.completer() ); |
| 88 | + |
| 89 | + return ret; |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + /** |
| 94 | + * Verify Feign-Vertx's HttpClient automatically reconnects |
| 95 | + * to a server that severed connections temporarily. |
| 96 | + * @param context |
| 97 | + */ |
| 98 | + @Test |
| 99 | + public void testHttpReconnectTemporary(TestContext context) { |
| 100 | + // Async test context |
| 101 | + Async async = context.async(); |
| 102 | + |
| 103 | + Future<Void> futSuccess = Future.future(); |
| 104 | + |
| 105 | + // 1. Start the server |
| 106 | + // 2. Send a Volley of Requests to the server |
| 107 | + // 3. Close server! |
| 108 | + // 4. Start server again |
| 109 | + // 5. Send another Volley of Requests to the server |
| 110 | + // If all goes well the testFuture will signal success |
| 111 | + createServer() |
| 112 | + .compose( result -> requestVolley() ) |
| 113 | + .compose( result -> closeServer() ) |
| 114 | + .compose( result -> createServer() ) |
| 115 | + .compose( result -> requestVolley() ) |
| 116 | + .compose( result -> futSuccess.complete(), futSuccess); |
| 117 | + |
| 118 | + |
| 119 | + // This future will be triggered at the end of the test |
| 120 | + futSuccess.setHandler( result -> { |
| 121 | + if (result.succeeded()) { |
| 122 | + async.complete(); // success |
| 123 | + } else { |
| 124 | + context.fail(result.cause()); |
| 125 | + } |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Verify Feign-Vertx's HttpClient reconnects to a server |
| 131 | + * after an extended outage. |
| 132 | + * @param context |
| 133 | + */ |
| 134 | + @Test |
| 135 | + public void testHttpReconnectOutage(TestContext context) { |
| 136 | + // Async test context |
| 137 | + Async async = context.async(); |
| 138 | + |
| 139 | + Future<Void> futFailed = Future.future(); |
| 140 | + Future<Void> futSuccess = Future.future(); |
| 141 | + |
| 142 | + // 1. Start the server |
| 143 | + // 2. Send a Volley of Requests to the server |
| 144 | + // 3. Close server! |
| 145 | + // 4. Send another Volley of Requests to the server |
| 146 | + // Expect a failure and absorb it with futFailed |
| 147 | + createServer() |
| 148 | + .compose( result -> requestVolley() ) |
| 149 | + .compose( result -> closeServer() ) |
| 150 | + // do not restart the server, causing a failure on the next request |
| 151 | + .compose( result -> requestVolley() ) |
| 152 | + .compose( result -> futFailed.complete(), futFailed); |
| 153 | + |
| 154 | + // We expect a failure when this future is called. |
| 155 | + futFailed.setHandler( result -> { |
| 156 | + if ( result.failed() ) { |
| 157 | + // We expect HTTP/1.1 "Connection was refused" or HTTP/2 "Connection was closed" |
| 158 | + if ( result.cause().getMessage().startsWith("Connection ") ) { |
| 159 | + // This is what we expect for this future! |
| 160 | + // 1. Start server again |
| 161 | + // 2. Re-using the same Feign-Vertx client as the failure above |
| 162 | + // we send a new volley of requests |
| 163 | + // If all goes well futSuccess will succeed and end the unit test |
| 164 | + createServer() |
| 165 | + .compose( res2 -> requestVolley() ) |
| 166 | + .compose( res2 -> futSuccess.complete(), futSuccess ); |
| 167 | + } else { |
| 168 | + // Problem, we received an unexpected error |
| 169 | + context.fail("futFailed should have seen a \"Connection refused\" but received a \""+result.cause().getMessage()+"\""); |
| 170 | + } |
| 171 | + } |
| 172 | + else { |
| 173 | + // Problem, we unexpectedly succeeded! |
| 174 | + context.fail("futFailed should have received a failure but it actually succeeded!"); |
| 175 | + } |
| 176 | + }); |
| 177 | + |
| 178 | + // This is the final future that will be triggered upon a positive test case |
| 179 | + futSuccess.setHandler( result -> { |
| 180 | + if (result.succeeded()) { |
| 181 | + async.complete(); // success |
| 182 | + } else { |
| 183 | + context.fail(result.cause()); |
| 184 | + } |
| 185 | + }); |
| 186 | + } |
| 187 | + |
| 188 | + |
| 189 | + /** |
| 190 | + * This issues ten requests to the server via a shared |
| 191 | + * long lived Feign Vertx client. |
| 192 | + * @return |
| 193 | + */ |
| 194 | + private CompositeFuture requestVolley() { |
| 195 | + List<Future> requests = new ArrayList<>(10); |
| 196 | + |
| 197 | + // Initiate 10 requests |
| 198 | + for( int i=0; i<10; i++ ) { |
| 199 | + Future<feign.Response> request = Future.future(); |
| 200 | + client.hello().setHandler(request.completer()); |
| 201 | + requests.add(request); |
| 202 | + } |
| 203 | + |
| 204 | + // Composite future for all of the requests |
| 205 | + return CompositeFuture.all(requests); |
| 206 | + } |
| 207 | + |
| 208 | + |
| 209 | + /** |
| 210 | + * Close the server connection |
| 211 | + * @return future for close completion |
| 212 | + */ |
| 213 | + public Future<Void> closeServer() { |
| 214 | + // Close server |
| 215 | + Future<Void> fut = Future.future(); |
| 216 | + httpServer.close(fut.completer()); |
| 217 | + return fut; |
| 218 | + } |
| 219 | +} |
0 commit comments