Skip to content

Instantly share code, notes, and snippets.

@alexlehm
Created May 14, 2017 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexlehm/2883ae691f5bc87bc03184fe6dc59466 to your computer and use it in GitHub Desktop.
Save alexlehm/2883ae691f5bc87bc03184fe6dc59466 to your computer and use it in GitHub Desktop.
HttpKeepAliveClientTest.java
package cx.lehmann.vertx;
import org.junit.Test;
import org.junit.runner.RunWith;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
/**
* @author <a href="https://oss.lehmann.cx/">Alexander Lehmann</a>
*
*/
@RunWith(VertxUnitRunner.class)
public class HttpKeepAliveClientTest {
private static final Logger log = LoggerFactory.getLogger(HttpKeepAliveClientTest.class);
@Test
public void testRequests(TestContext context) {
Async async = context.async();
Vertx vertx = Vertx.vertx();
HttpClientOptions options = new HttpClientOptions();
options.setIdleTimeout(1);
HttpClient client = vertx.createHttpClient(options);
doGet(context, async, vertx, client, 0);
}
private void doGet(TestContext context, Async async, Vertx vertx, HttpClient client, int count) {
client.getAbs("http://www.lehmann.cx/waiter.php", resp -> {
log.info("response code: " + resp.statusCode());
resp.bodyHandler(data -> {
log.info("finished");
if (count < 5) {
vertx.setTimer(2000, t -> {
doGet(context, async, vertx, client, count + 1);
});
} else {
async.complete();
}
});
resp.exceptionHandler(th -> context.fail(th));
}).exceptionHandler(th -> context.fail(th)).end();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment