Created
May 14, 2017 11:44
-
-
Save alexlehm/2883ae691f5bc87bc03184fe6dc59466 to your computer and use it in GitHub Desktop.
HttpKeepAliveClientTest.java
This file contains 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
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