use of io.undertow.util.CompletionLatchHandler in project undertow by undertow-io.
the class AccessLogFileTestCase method testLogLotsOfThreads.
@Test
public void testLogLotsOfThreads() throws IOException, InterruptedException, ExecutionException {
Path directory = logDirectory;
Path logFileName = directory.resolve("server2.log");
DefaultAccessLogReceiver logReceiver = new DefaultAccessLogReceiver(DefaultServer.getWorker(), directory, "server2.");
CompletionLatchHandler latchHandler;
DefaultServer.setRootHandler(latchHandler = new CompletionLatchHandler(NUM_REQUESTS * NUM_THREADS, new AccessLogHandler(HELLO_HANDLER, logReceiver, "REQ %{i,test-header}", AccessLogFileTestCase.class.getClassLoader())));
ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
try {
final List<Future<?>> futures = new ArrayList<>();
for (int i = 0; i < NUM_THREADS; ++i) {
final int threadNo = i;
futures.add(executor.submit(new Runnable() {
@Override
public void run() {
TestHttpClient client = new TestHttpClient();
try {
for (int i = 0; i < NUM_REQUESTS; ++i) {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.addHeader("test-header", "thread-" + threadNo + "-request-" + i);
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
final String response = HttpClientUtils.readResponse(result);
Assert.assertEquals("Hello", response);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
client.getConnectionManager().shutdown();
}
}
}));
}
for (Future<?> future : futures) {
future.get();
}
} finally {
executor.shutdown();
}
latchHandler.await();
logReceiver.awaitWrittenForTest();
String completeLog = new String(Files.readAllBytes(logFileName));
for (int i = 0; i < NUM_THREADS; ++i) {
for (int j = 0; j < NUM_REQUESTS; ++j) {
Assert.assertTrue(completeLog.contains("REQ thread-" + i + "-request-" + j));
}
}
}
use of io.undertow.util.CompletionLatchHandler in project undertow by undertow-io.
the class AccessLogFileTestCase method testForcedLogRotation.
@Test
public void testForcedLogRotation() throws IOException, InterruptedException {
Path logFileName = logDirectory.resolve("server.log");
DefaultAccessLogReceiver logReceiver = new DefaultAccessLogReceiver(DefaultServer.getWorker(), logDirectory, "server.");
CompletionLatchHandler latchHandler;
DefaultServer.setRootHandler(latchHandler = new CompletionLatchHandler(new AccessLogHandler(HELLO_HANDLER, logReceiver, "Remote address %a Code %s test-header %{i,test-header}", AccessLogFileTestCase.class.getClassLoader())));
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.addHeader("test-header", "v1");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("Hello", HttpClientUtils.readResponse(result));
latchHandler.await();
latchHandler.reset();
logReceiver.awaitWrittenForTest();
Assert.assertEquals("Remote address " + DefaultServer.getDefaultServerAddress().getAddress().getHostAddress() + " Code 200 test-header v1" + System.lineSeparator(), new String(Files.readAllBytes(logFileName)));
logReceiver.rotate();
logReceiver.awaitWrittenForTest();
Assert.assertFalse(Files.exists(logFileName));
Path firstLogRotate = logDirectory.resolve("server." + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + ".log");
Assert.assertEquals("Remote address " + DefaultServer.getDefaultServerAddress().getAddress().getHostAddress() + " Code 200 test-header v1" + System.lineSeparator(), new String(Files.readAllBytes(firstLogRotate)));
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.addHeader("test-header", "v2");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("Hello", HttpClientUtils.readResponse(result));
latchHandler.await();
latchHandler.reset();
logReceiver.awaitWrittenForTest();
Assert.assertEquals("Remote address " + DefaultServer.getDefaultServerAddress().getAddress().getHostAddress() + " Code 200 test-header v2" + System.lineSeparator(), new String(Files.readAllBytes(logFileName)));
logReceiver.rotate();
logReceiver.awaitWrittenForTest();
Assert.assertFalse(Files.exists(logFileName));
Path secondLogRotate = logDirectory.resolve("server." + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "-1.log");
Assert.assertEquals("Remote address " + DefaultServer.getDefaultServerAddress().getAddress().getHostAddress() + " Code 200 test-header v2" + System.lineSeparator(), new String(Files.readAllBytes(secondLogRotate)));
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.util.CompletionLatchHandler in project undertow by undertow-io.
the class MetricsHandlerTestCase method testMetrics.
@Test
public void testMetrics() throws IOException, InterruptedException {
MetricsHandler metricsHandler;
CompletionLatchHandler latchHandler;
DefaultServer.setRootHandler(latchHandler = new CompletionLatchHandler(metricsHandler = new MetricsHandler(new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
Thread.sleep(100);
if (exchange.getQueryString().contains("error")) {
throw new RuntimeException();
}
exchange.getResponseSender().send("Hello");
}
})));
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
TestHttpClient client = new TestHttpClient();
try {
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("Hello", HttpClientUtils.readResponse(result));
latchHandler.await();
latchHandler.reset();
MetricsHandler.MetricResult metrics = metricsHandler.getMetrics();
Assert.assertEquals(1, metrics.getTotalRequests());
Assert.assertTrue(metrics.getMaxRequestTime() > 0);
Assert.assertEquals(metrics.getMinRequestTime(), metrics.getMaxRequestTime());
Assert.assertEquals(metrics.getMaxRequestTime(), metrics.getTotalRequestTime());
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("Hello", HttpClientUtils.readResponse(result));
latchHandler.await();
latchHandler.reset();
metrics = metricsHandler.getMetrics();
Assert.assertEquals(2, metrics.getTotalRequests());
Assert.assertEquals(0, metrics.getTotalErrors());
result = client.execute(new HttpGet(DefaultServer.getDefaultServerURL() + "/path?error=true"));
Assert.assertEquals(StatusCodes.INTERNAL_SERVER_ERROR, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
latchHandler.await();
latchHandler.reset();
metrics = metricsHandler.getMetrics();
Assert.assertEquals(3, metrics.getTotalRequests());
Assert.assertEquals(1, metrics.getTotalErrors());
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.util.CompletionLatchHandler in project undertow by undertow-io.
the class ExtendedAccessLogFileTestCase method verifySingleLogMessageToFile.
private void verifySingleLogMessageToFile(Path logFileName, DefaultAccessLogReceiver logReceiver) throws IOException, InterruptedException {
CompletionLatchHandler latchHandler;
DefaultServer.setRootHandler(latchHandler = new CompletionLatchHandler(new AccessLogHandler(HELLO_HANDLER, logReceiver, PATTERN, new ExtendedAccessLogParser(ExtendedAccessLogFileTestCase.class.getClassLoader()).parse(PATTERN))));
TestHttpClient client = new TestHttpClient();
client.setSSLContext(DefaultServer.getClientSSLContext());
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerSSLAddress() + "/path");
get.addHeader("test-header", "single-val");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("Hello", HttpClientUtils.readResponse(result));
latchHandler.await();
logReceiver.awaitWrittenForTest();
String data = new String(Files.readAllBytes(logFileName));
String[] lines = data.split(System.lineSeparator());
Assert.assertEquals("#Fields: " + PATTERN, lines[0]);
Assert.assertEquals("#Version: 2.0", lines[1]);
Assert.assertEquals("#Software: " + Version.getFullVersionString(), lines[2]);
Assert.assertEquals("", lines[3]);
Assert.assertEquals("/path 'single-val' 'bb' true", lines[4]);
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.util.CompletionLatchHandler in project undertow by undertow-io.
the class AccessLogFileTestCase method verifySingleLogMessageToFile.
private void verifySingleLogMessageToFile(Path logFileName, DefaultAccessLogReceiver logReceiver) throws IOException, InterruptedException {
CompletionLatchHandler latchHandler;
DefaultServer.setRootHandler(latchHandler = new CompletionLatchHandler(new AccessLogHandler(HELLO_HANDLER, logReceiver, "Remote address %a Code %s test-header %{i,test-header} %{i,non-existent} %{i,dup}", AccessLogFileTestCase.class.getClassLoader())));
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.addHeader("test-header", "single-val");
// we can't rely on ordering, so we just send the same thing twice to make the comparison easy
get.addHeader("dup", "d");
get.addHeader("dup", "d");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("Hello", HttpClientUtils.readResponse(result));
latchHandler.await();
logReceiver.awaitWrittenForTest();
Assert.assertEquals("Remote address " + DefaultServer.getDefaultServerAddress().getAddress().getHostAddress() + " Code 200 test-header single-val - [d, d]" + System.lineSeparator(), new String(Files.readAllBytes(logFileName)));
} finally {
client.getConnectionManager().shutdown();
}
}
Aggregations