use of org.eclipse.jetty.util.resource.Resource in project jetty.project by eclipse.
the class ResourceCacheTest method testResourceCache.
@Test
public void testResourceCache() throws Exception {
final Resource directory;
File[] files = new File[10];
String[] names = new String[files.length];
CachedContentFactory cache;
for (int i = 0; i < files.length; i++) {
files[i] = File.createTempFile("R-" + i + "-", ".txt");
files[i].deleteOnExit();
names[i] = files[i].getName();
try (OutputStream out = new FileOutputStream(files[i])) {
for (int j = 0; j < (i * 10 - 1); j++) out.write(' ');
out.write('\n');
}
}
directory = Resource.newResource(files[0].getParentFile().getAbsolutePath());
cache = new CachedContentFactory(null, directory, new MimeTypes(), false, false, CompressedContentFormat.NONE);
cache.setMaxCacheSize(95);
cache.setMaxCachedFileSize(85);
cache.setMaxCachedFiles(4);
assertTrue(cache.getContent("does not exist", 4096) == null);
assertTrue(cache.getContent(names[9], 4096) instanceof ResourceHttpContent);
assertTrue(cache.getContent(names[9], 4096).getIndirectBuffer() != null);
HttpContent content;
content = cache.getContent(names[8], 4096);
assertTrue(content != null);
assertEquals(80, content.getContentLengthValue());
assertEquals(0, cache.getCachedSize());
if (OS.IS_LINUX) {
// Initially not using memory mapped files
content.getDirectBuffer();
assertEquals(80, cache.getCachedSize());
// with both types of buffer loaded, this is too large for cache
content.getIndirectBuffer();
assertEquals(0, cache.getCachedSize());
assertEquals(0, cache.getCachedFiles());
cache = new CachedContentFactory(null, directory, new MimeTypes(), true, false, CompressedContentFormat.NONE);
cache.setMaxCacheSize(95);
cache.setMaxCachedFileSize(85);
cache.setMaxCachedFiles(4);
content = cache.getContent(names[8], 4096);
content.getDirectBuffer();
assertEquals(cache.isUseFileMappedBuffer() ? 0 : 80, cache.getCachedSize());
// with both types of buffer loaded, this is not too large for cache because
// mapped buffers don't count, so we can continue
}
content.getIndirectBuffer();
assertEquals(80, cache.getCachedSize());
assertEquals(1, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[1], 4096);
assertEquals(80, cache.getCachedSize());
content.getIndirectBuffer();
assertEquals(90, cache.getCachedSize());
assertEquals(2, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[2], 4096);
content.getIndirectBuffer();
assertEquals(30, cache.getCachedSize());
assertEquals(2, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[3], 4096);
content.getIndirectBuffer();
assertEquals(60, cache.getCachedSize());
assertEquals(3, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[4], 4096);
content.getIndirectBuffer();
assertEquals(90, cache.getCachedSize());
assertEquals(3, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[5], 4096);
content.getIndirectBuffer();
assertEquals(90, cache.getCachedSize());
assertEquals(2, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[6], 4096);
content.getIndirectBuffer();
assertEquals(60, cache.getCachedSize());
assertEquals(1, cache.getCachedFiles());
Thread.sleep(200);
try (OutputStream out = new FileOutputStream(files[6])) {
out.write(' ');
}
content = cache.getContent(names[7], 4096);
content.getIndirectBuffer();
assertEquals(70, cache.getCachedSize());
assertEquals(1, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[6], 4096);
content.getIndirectBuffer();
assertEquals(71, cache.getCachedSize());
assertEquals(2, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[0], 4096);
content.getIndirectBuffer();
assertEquals(72, cache.getCachedSize());
assertEquals(3, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[1], 4096);
content.getIndirectBuffer();
assertEquals(82, cache.getCachedSize());
assertEquals(4, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[2], 4096);
content.getIndirectBuffer();
assertEquals(32, cache.getCachedSize());
assertEquals(4, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[3], 4096);
content.getIndirectBuffer();
assertEquals(61, cache.getCachedSize());
assertEquals(4, cache.getCachedFiles());
Thread.sleep(200);
cache.flushCache();
assertEquals(0, cache.getCachedSize());
assertEquals(0, cache.getCachedFiles());
cache.flushCache();
}
use of org.eclipse.jetty.util.resource.Resource in project jetty.project by eclipse.
the class HttpOutputTest method testAsyncWriteMed.
@Test
public void testAsyncWriteMed() throws Exception {
final Resource big = Resource.newClassPathResource("simple/big.txt");
_handler._writeLengthIfKnown = false;
_handler._content = BufferUtil.toBuffer(big, false);
_handler._arrayBuffer = new byte[4000];
_handler._async = true;
String response = _connector.getResponse("GET / HTTP/1.0\nHost: localhost:80\n\n");
assertThat(response, containsString("HTTP/1.1 200 OK"));
assertThat(response, Matchers.not(containsString("Content-Length")));
assertThat(response, endsWith(toUTF8String(big)));
}
use of org.eclipse.jetty.util.resource.Resource in project jetty.project by eclipse.
the class HttpOutputTest method testWriteLargeKnown.
@Test
public void testWriteLargeKnown() throws Exception {
final Resource big = Resource.newClassPathResource("simple/big.txt");
_handler._writeLengthIfKnown = true;
_handler._content = BufferUtil.toBuffer(big, false);
_handler._arrayBuffer = new byte[8192];
String response = _connector.getResponse("GET / HTTP/1.0\nHost: localhost:80\n\n");
assertThat(response, containsString("HTTP/1.1 200 OK"));
assertThat(response, containsString("Content-Length"));
assertThat(response, endsWith(toUTF8String(big)));
}
use of org.eclipse.jetty.util.resource.Resource in project jetty.project by eclipse.
the class HttpOutputTest method testWriteByteKnown.
@Test
public void testWriteByteKnown() throws Exception {
final Resource big = Resource.newClassPathResource("simple/big.txt");
_handler._writeLengthIfKnown = true;
_handler._content = BufferUtil.toBuffer(big, false);
_handler._arrayBuffer = new byte[1];
String response = _connector.getResponse("GET / HTTP/1.0\nHost: localhost:80\n\n");
assertThat(response, containsString("HTTP/1.1 200 OK"));
assertThat(response, containsString("Content-Length"));
assertThat(response, endsWith(toUTF8String(big)));
}
use of org.eclipse.jetty.util.resource.Resource in project jetty.project by eclipse.
the class HttpOutputTest method testAsyncWriteSimpleKnown.
@Test
public void testAsyncWriteSimpleKnown() throws Exception {
final Resource big = Resource.newClassPathResource("simple/simple.txt");
_handler._async = true;
_handler._writeLengthIfKnown = true;
_handler._content = BufferUtil.toBuffer(big, false);
_handler._arrayBuffer = new byte[4000];
String response = _connector.getResponse("GET / HTTP/1.0\nHost: localhost:80\n\n");
assertThat(response, containsString("HTTP/1.1 200 OK"));
assertThat(response, containsString("Content-Length: 11"));
assertThat(response, containsString("simple text"));
}
Aggregations