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();
}
}
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();
}
}
Aggregations