Skip to content

Instantly share code, notes, and snippets.

@dmolesUC
Created November 16, 2017 23:26
Show Gist options
  • Save dmolesUC/b7e0593469dae4c89edebdec675e23a3 to your computer and use it in GitHub Desktop.
Save dmolesUC/b7e0593469dae4c89edebdec675e23a3 to your computer and use it in GitHub Desktop.
Vert.x test example with thread debugging
/*
Output (with org.slf4j:slf4j-simple:1.7.25):
[main] INFO VertxThreadTest - setUp running
[main] INFO VertxThreadTest - deploying on port 53539
[main] INFO VertxThreadTest - Test status200 running
[vert.x-eventloop-thread-0] INFO VertxThreadTest - Handling request
[vert.x-eventloop-thread-2] INFO VertxThreadTest - Test status200 got response
[main] INFO VertxThreadTest - tearDown running
[main] INFO VertxThreadTest - setUp running
[main] INFO VertxThreadTest - deploying on port 53542
[main] INFO VertxThreadTest - Test bodyHello running
[vert.x-eventloop-thread-0] INFO VertxThreadTest - Handling request
[vert.x-eventloop-thread-2] INFO VertxThreadTest - Test bodyHello got response
[vert.x-eventloop-thread-2] INFO VertxThreadTest - Test bodyHello got body
[main] INFO VertxThreadTest - tearDown running
*/
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import io.vertx.reactivex.core.AbstractVerticle;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.ServerSocket;
@RunWith(VertxUnitRunner.class)
public class VertxThreadTest {
private int port;
private Vertx vertx;
private final Logger log = LoggerFactory.getLogger(VertxThreadTest.class);
private int findOpenPort() {
try {
try (ServerSocket s = new ServerSocket(0)) {
return s.getLocalPort();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@Before
public void setUp(TestContext tc) {
log.info("setUp running");
port = findOpenPort();
log.info(String.format("deploying on port %d", port));
DeploymentOptions deploymentOptions = new DeploymentOptions()
.setConfig(new JsonObject().put("http.port", port));
vertx = Vertx.vertx();
vertx.deployVerticle(
HelloVerticle::new,
deploymentOptions,
tc.asyncAssertSuccess());
}
@Test
public void status200(TestContext tc) {
Async async = tc.async();
log.info("Test status200 running");
HttpClient httpClient = vertx.createHttpClient();
httpClient.getNow(port, "localhost", "/", r -> {
log.info("Test status200 got response");
tc.assertTrue(r.statusCode() == 200);
async.complete();
}
);
}
@Test
public void bodyHello(TestContext tc) {
Async async = tc.async();
log.info("Test bodyHello running");
HttpClient httpClient = vertx.createHttpClient();
httpClient.getNow(port, "localhost", "/", r -> {
log.info("Test bodyHello got response");
r.bodyHandler(b -> {
log.info("Test bodyHello got body");
tc.assertEquals("hello", b.toString());
async.complete();
}
);
}
);
}
@After
public void tearDown(TestContext tc) {
log.info("tearDown running");
vertx.close(tc.asyncAssertSuccess());
}
}
class HelloVerticle extends AbstractVerticle {
private final Logger log = LoggerFactory.getLogger(VertxThreadTest.class);
@Override
public void start() throws Exception {
int httpPort = config().getInteger("http.port");
vertx.createHttpServer()
.requestHandler(r -> {
log.info("Handling request");
r.response().end("hello");
}).listen(httpPort);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment