Search in sources :

Example 1 with CompletionLatchHandler

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));
        }
    }
}
Also used : Path(java.nio.file.Path) CompletionLatchHandler(io.undertow.util.CompletionLatchHandler) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Test(org.junit.Test)

Example 2 with CompletionLatchHandler

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();
    }
}
Also used : Path(java.nio.file.Path) CompletionLatchHandler(io.undertow.util.CompletionLatchHandler) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 3 with CompletionLatchHandler

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();
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) CompletionLatchHandler(io.undertow.util.CompletionLatchHandler) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 4 with CompletionLatchHandler

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();
    }
}
Also used : CompletionLatchHandler(io.undertow.util.CompletionLatchHandler) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) HttpString(io.undertow.util.HttpString) TestHttpClient(io.undertow.testutils.TestHttpClient)

Example 5 with CompletionLatchHandler

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();
    }
}
Also used : CompletionLatchHandler(io.undertow.util.CompletionLatchHandler) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient)

Aggregations

TestHttpClient (io.undertow.testutils.TestHttpClient)8 CompletionLatchHandler (io.undertow.util.CompletionLatchHandler)8 HttpResponse (org.apache.http.HttpResponse)8 HttpGet (org.apache.http.client.methods.HttpGet)8 Test (org.junit.Test)6 IOException (java.io.IOException)3 Path (java.nio.file.Path)2 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 HttpHandler (io.undertow.server.HttpHandler)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 MetricsHandler (io.undertow.server.handlers.MetricsHandler)1 PathHandler (io.undertow.server.handlers.PathHandler)1 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)1 DeploymentManager (io.undertow.servlet.api.DeploymentManager)1 FilterInfo (io.undertow.servlet.api.FilterInfo)1