Search in sources :

Example 1 with CacheHandler

use of io.undertow.server.handlers.cache.CacheHandler in project undertow by undertow-io.

the class FileHandlerStressTestCase method simpleFileStressTest.

@Test
public void simpleFileStressTest() throws IOException, ExecutionException, InterruptedException, URISyntaxException {
    ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
    try {
        Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent();
        final ResourceHandler handler = new ResourceHandler(new PathResourceManager(rootPath, 10485760));
        final CacheHandler cacheHandler = new CacheHandler(new DirectBufferCache(1024, 10, 10480), handler);
        final PathHandler path = new PathHandler();
        path.addPrefixPath("/path", cacheHandler);
        final CanonicalPathHandler root = new CanonicalPathHandler();
        root.setNext(path);
        DefaultServer.setRootHandler(root);
        final List<Future<?>> futures = new ArrayList<>();
        for (int i = 0; i < NUM_THREADS; ++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/page.html");
                            HttpResponse result = client.execute(get);
                            Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
                            final String response = HttpClientUtils.readResponse(result);
                            Assert.assertTrue(response, response.contains("A web page"));
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    } finally {
                        client.getConnectionManager().shutdown();
                    }
                }
            }));
        }
        for (Future<?> future : futures) {
            future.get();
        }
    } finally {
        executor.shutdown();
    }
}
Also used : Path(java.nio.file.Path) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) HttpGet(org.apache.http.client.methods.HttpGet) DirectBufferCache(io.undertow.server.handlers.cache.DirectBufferCache) ArrayList(java.util.ArrayList) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) PathHandler(io.undertow.server.handlers.PathHandler) HttpResponse(org.apache.http.HttpResponse) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) IOException(java.io.IOException) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) TestHttpClient(io.undertow.testutils.TestHttpClient) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) CacheHandler(io.undertow.server.handlers.cache.CacheHandler) Test(org.junit.Test)

Example 2 with CacheHandler

use of io.undertow.server.handlers.cache.CacheHandler in project undertow by undertow-io.

the class CacheHandlerTestCase method testBasicPathBasedCaching.

@Test
public void testBasicPathBasedCaching() throws IOException {
    final AtomicInteger responseCount = new AtomicInteger();
    final HttpHandler messageHandler = new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            final ResponseCache cache = exchange.getAttachment(ResponseCache.ATTACHMENT_KEY);
            if (!cache.tryServeResponse()) {
                final String data = "Response " + responseCount.incrementAndGet();
                exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, data.length() + "");
                exchange.getResponseSender().send(data);
            }
        }
    };
    final CacheHandler cacheHandler = new CacheHandler(new DirectBufferCache(100, 10, 1000), messageHandler);
    DefaultServer.setRootHandler(cacheHandler);
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        //it takes 5 hits to make an entry actually get cached
        for (int i = 1; i <= 5; ++i) {
            HttpResponse result = client.execute(get);
            Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
            Assert.assertEquals("Response " + i, HttpClientUtils.readResponse(result));
        }
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("Response 5", HttpClientUtils.readResponse(result));
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("Response 5", HttpClientUtils.readResponse(result));
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("Response 5", HttpClientUtils.readResponse(result));
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path2");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("Response 6", HttpClientUtils.readResponse(result));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpGet(org.apache.http.client.methods.HttpGet) DirectBufferCache(io.undertow.server.handlers.cache.DirectBufferCache) HttpResponse(org.apache.http.HttpResponse) ResponseCache(io.undertow.server.handlers.cache.ResponseCache) CacheHandler(io.undertow.server.handlers.cache.CacheHandler) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Aggregations

CacheHandler (io.undertow.server.handlers.cache.CacheHandler)2 DirectBufferCache (io.undertow.server.handlers.cache.DirectBufferCache)2 TestHttpClient (io.undertow.testutils.TestHttpClient)2 HttpResponse (org.apache.http.HttpResponse)2 HttpGet (org.apache.http.client.methods.HttpGet)2 Test (org.junit.Test)2 HttpHandler (io.undertow.server.HttpHandler)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 CanonicalPathHandler (io.undertow.server.handlers.CanonicalPathHandler)1 PathHandler (io.undertow.server.handlers.PathHandler)1 ResponseCache (io.undertow.server.handlers.cache.ResponseCache)1 PathResourceManager (io.undertow.server.handlers.resource.PathResourceManager)1 ResourceHandler (io.undertow.server.handlers.resource.ResourceHandler)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1