@Slow @Test public void testProxyWithBigResponseContentWithSlowReader() throws Exception { prepareProxy(); // Create a 6 MiB file final int length = 6 * 1024; Path targetTestsDir = MavenTestingUtils.getTargetTestingDir().toPath(); Files.createDirectories(targetTestsDir); final Path temp = Files.createTempFile(targetTestsDir, "test_", null); byte[] kb = new byte[1024]; new Random().nextBytes(kb); try (OutputStream output = Files.newOutputStream(temp, StandardOpenOption.CREATE)) { for (int i = 0; i < length; ++i) output.write(kb); } prepareServer( new HttpServlet() { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try (InputStream input = Files.newInputStream(temp)) { IO.copy(input, response.getOutputStream()); } } }); Request request = client.newRequest("localhost", serverConnector.getLocalPort()).path("/proxy/test"); final CountDownLatch latch = new CountDownLatch(1); request.send( new BufferingResponseListener(2 * length * 1024) { @Override public void onContent(Response response, ByteBuffer content) { try { // Slow down the reader TimeUnit.MILLISECONDS.sleep(5); super.onContent(response, content); } catch (InterruptedException x) { response.abort(x); } } @Override public void onComplete(Result result) { Assert.assertFalse(result.isFailed()); Assert.assertEquals(200, result.getResponse().getStatus()); Assert.assertEquals(length * 1024, getContent().length); latch.countDown(); } }); Assert.assertTrue(latch.await(30, TimeUnit.SECONDS)); }
@BeforeClass public static void startServer() throws Exception { server = new Server(); ServerConnector connector = new ServerConnector(server); connector.setPort(0); server.addConnector(connector); ContextHandlerCollection contexts = new ContextHandlerCollection(); File dir = MavenTestingUtils.getTargetTestingDir(ResourceHandlerRangeTest.class.getSimpleName()); FS.ensureEmpty(dir); File rangeFile = new File(dir, "range.txt"); try (FileWriter writer = new FileWriter(rangeFile)) { writer.append("0123456789"); writer.flush(); } ContextHandler contextHandler = new ContextHandler(); ResourceHandler contentResourceHandler = new ResourceHandler(); contextHandler.setBaseResource(Resource.newResource(dir.getAbsolutePath())); contextHandler.setHandler(contentResourceHandler); contextHandler.setContextPath("/"); contexts.addHandler(contextHandler); server.setHandler(contexts); server.start(); String host = connector.getHost(); if (host == null) { host = "localhost"; } int port = connector.getLocalPort(); serverUri = new URI(String.format("http://%s:%d/", host, port)); }