Created
May 10, 2017 21:18
-
-
Save alexlehm/333405eccb14f57f0e252bb9509e9aaf 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(); | |
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/file.txt", resp -> { | |
log.info("response code: " + resp.statusCode()); | |
resp.bodyHandler(data -> { | |
log.info("finished"); | |
if (count < 5) { | |
vertx.setTimer(100, 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