use of io.javalin.embeddedserver.jetty.EmbeddedJettyFactory in project javalin by tipsy.
the class TestCustomJetty method test_embeddedServer_withCustomServer.
@Test
public void test_embeddedServer_withCustomServer() throws Exception {
Javalin app = Javalin.create().port(0).embeddedServer(new EmbeddedJettyFactory(() -> {
Server server = new Server();
server.setAttribute("is-custom-server", true);
return server;
})).start();
assertThat(app.embeddedServer().attribute("is-custom-server"), is(true));
app.stop();
}
use of io.javalin.embeddedserver.jetty.EmbeddedJettyFactory in project javalin by tipsy.
the class TestAsync method test_async.
@Test
@Ignore("Just for running manually")
public void test_async() throws Exception {
Javalin app = Javalin.create().embeddedServer(new EmbeddedJettyFactory(() -> new Server(new QueuedThreadPool(16, 10, 60_000)))).port(5454).start();
app.get("/test-async", ctx -> ctx.async(() -> {
CompletableFuture<Void> future = new CompletableFuture<>();
new Timer().schedule(new TimerTask() {
public void run() {
ctx.status(418);
future.complete(null);
}
}, 1000);
return future;
}));
long startTime = System.currentTimeMillis();
ForkJoinPool forkJoinPool = new ForkJoinPool(200);
forkJoinPool.submit(() -> IntStream.range(0, 50).parallel().forEach(i -> {
try {
assertThat(new SimpleHttpClient().http_GET("http://localhost:5454/test-async").getStatus(), is(418));
} catch (IOException e) {
e.printStackTrace();
}
})).get();
log.info("took " + (System.currentTimeMillis() - startTime) + " milliseconds");
app.stop();
}
use of io.javalin.embeddedserver.jetty.EmbeddedJettyFactory in project javalin by tipsy.
the class TestCustomJetty method test_embeddedServer_withStatisticsHandler.
@Test
public void test_embeddedServer_withStatisticsHandler() throws Exception {
StatisticsHandler statisticsHandler = new StatisticsHandler();
Javalin app = Javalin.create().port(0).embeddedServer(new EmbeddedJettyFactory(() -> {
Server server = new Server();
server.setHandler(statisticsHandler);
return server;
})).get("/", ctx -> ctx.result("Hello World")).start();
String origin = "http://localhost:" + app.port();
int requests = 5;
for (int i = 0; i < requests; i++) {
assertThat(Unirest.get(origin + "/").asString().getBody(), is("Hello World"));
assertThat(Unirest.get(origin + "/not_there").asString().getStatus(), is(404));
}
assertThat(statisticsHandler.getDispatched(), is(requests * 2));
assertThat(statisticsHandler.getResponses2xx(), is(requests));
assertThat(statisticsHandler.getResponses4xx(), is(requests));
app.stop();
}
use of io.javalin.embeddedserver.jetty.EmbeddedJettyFactory in project javalin by tipsy.
the class TestCustomJetty method test_embeddedServer_withHandlerChain.
@Test
public void test_embeddedServer_withHandlerChain() throws Exception {
AtomicLong logCount = new AtomicLong(0);
RequestLog requestLog = (request, response) -> logCount.incrementAndGet();
RequestLogHandler requestLogHandler = new RequestLogHandler();
requestLogHandler.setRequestLog(requestLog);
StatisticsHandler handlerChain = new StatisticsHandler();
handlerChain.setHandler(requestLogHandler);
Javalin app = Javalin.create().port(0).embeddedServer(new EmbeddedJettyFactory(() -> {
Server server = new Server();
server.setHandler(handlerChain);
return server;
})).get("/", ctx -> ctx.result("Hello World")).start();
String origin = "http://localhost:" + app.port();
int requests = 10;
for (int i = 0; i < requests; i++) {
assertThat(Unirest.get(origin + "/").asString().getBody(), is("Hello World"));
assertThat(Unirest.get(origin + "/not_there").asString().getStatus(), is(404));
}
assertThat(handlerChain.getDispatched(), is(requests * 2));
assertThat(handlerChain.getResponses2xx(), is(requests));
assertThat(handlerChain.getResponses4xx(), is(requests));
assertThat(logCount.get(), is((long) (requests * 2)));
app.stop();
}
Aggregations