Created
March 28, 2017 09:39
-
-
Save silmeth/f39ffcda87cf8c4855d70830826d714c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.Duration; | |
import java.util.concurrent.TimeoutException; | |
import io.vertx.core.Handler; | |
import io.vertx.core.Vertx; | |
import io.vertx.core.http.HttpClient; | |
import io.vertx.core.http.HttpClientOptions; | |
import io.vertx.core.http.HttpServer; | |
import io.vertx.core.http.HttpServerRequest; | |
import io.vertx.ext.unit.Async; | |
import io.vertx.ext.unit.TestContext; | |
import io.vertx.ext.unit.junit.RunTestOnContext; | |
import io.vertx.ext.unit.junit.VertxUnitRunner; | |
import org.apache.http.HttpStatus; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import org.junit.Before; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
@RunWith(VertxUnitRunner.class) | |
public class VertxHttpClientTimeoutTest { | |
private static final Logger LOGGER = LogManager.getLogger(); | |
private static final int PORT = 9300; | |
private static final Duration TIMEOUT = Duration.ofMillis(300L); | |
private Vertx vertx; | |
private Async async; | |
private HttpClient client; | |
@Rule | |
public final RunTestOnContext rule = new RunTestOnContext(); | |
@Before | |
public final void setUp(TestContext context) { | |
vertx = rule.vertx(); | |
client = vertx.createHttpClient(); // no matter if `new HttpOptions().setKeepAlive(false)` | |
// or `setKeepAlive(true)` is passed here or not | |
} | |
@Test | |
public void testVertxHttpClient(TestContext context) { | |
async = context.async(); | |
HttpServer server = vertx.createHttpServer(); | |
server.requestHandler(handler(server)) | |
.listen(PORT, s -> sendRequest(0)); | |
} | |
private Handler<HttpServerRequest> handler(HttpServer server) { | |
return new Handler<HttpServerRequest>() { | |
int requestNum = 0; | |
@Override | |
public void handle(HttpServerRequest req) { | |
if (requestNum < 1) { | |
vertx.setTimer(TIMEOUT.toMillis(), id -> req.response() | |
.setStatusCode(HttpStatus.SC_SERVICE_UNAVAILABLE) | |
.putHeader("X-Req-Num", Integer.toString(requestNum)) | |
.end()); | |
} else { | |
req.response() | |
.setStatusCode(HttpStatus.SC_OK) | |
.putHeader("X-Req-Num", Integer.toString(requestNum)) | |
.end(); | |
vertx.setTimer(Duration.ofMillis(10L).toMillis(), id -> server.close()); | |
} | |
++requestNum; | |
} | |
}; | |
} | |
private void sendRequest(int i) { | |
if (i < 2) { | |
LOGGER.info("Sending request {}.", i); | |
client.get(PORT, "localhost", "/test") | |
.setTimeout(TIMEOUT.toMillis()) | |
.handler(res -> { | |
LOGGER.info("Received response, status: {}, msg: {}, headers: {}", | |
res::statusCode, | |
res::statusMessage, | |
() -> res.headers().entries()); | |
// try sending once more to finish async | |
sendRequest(i + 1); | |
} | |
) | |
.exceptionHandler(e -> { | |
LOGGER.info("Exception thrown for request {}.", | |
i, | |
e); | |
// send another request after timeout | |
if (e instanceof TimeoutException) { | |
sendRequest(i + 1); | |
} | |
}) | |
.end(); | |
} else { | |
// wait for server closing connection and complete async after some more time | |
vertx.setTimer(Duration.ofMillis(300L).toMillis(), id -> async.complete()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment