Search in sources :

Example 16 with Slow

use of org.eclipse.jetty.toolchain.test.annotation.Slow in project jetty.project by eclipse.

the class HttpClientTimeoutTest method testTimeoutOnListener.

@Slow
@Test
public void testTimeoutOnListener() throws Exception {
    long timeout = 1000;
    start(new TimeoutHandler(2 * timeout));
    final CountDownLatch latch = new CountDownLatch(1);
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(timeout, TimeUnit.MILLISECONDS);
    request.send(new Response.CompleteListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isFailed());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(3 * timeout, TimeUnit.MILLISECONDS));
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 17 with Slow

use of org.eclipse.jetty.toolchain.test.annotation.Slow in project jetty.project by eclipse.

the class HttpClientTimeoutTest method testTimeoutIsCancelledOnSuccessWithExplicitConnection.

@Slow
@Test
public void testTimeoutIsCancelledOnSuccessWithExplicitConnection() throws Exception {
    long timeout = 1000;
    start(new TimeoutHandler(timeout));
    final CountDownLatch latch = new CountDownLatch(1);
    Destination destination = client.getDestination(scheme, "localhost", connector.getLocalPort());
    FuturePromise<Connection> futureConnection = new FuturePromise<>();
    destination.newConnection(futureConnection);
    try (Connection connection = futureConnection.get(5, TimeUnit.SECONDS)) {
        Request request = client.newRequest(destination.getHost(), destination.getPort()).scheme(scheme).timeout(2 * timeout, TimeUnit.MILLISECONDS);
        connection.send(request, new Response.CompleteListener() {

            @Override
            public void onComplete(Result result) {
                Response response = result.getResponse();
                Assert.assertEquals(200, response.getStatus());
                Assert.assertFalse(result.isFailed());
                latch.countDown();
            }
        });
        Assert.assertTrue(latch.await(3 * timeout, TimeUnit.MILLISECONDS));
        TimeUnit.MILLISECONDS.sleep(2 * timeout);
        Assert.assertNull(request.getAbortCause());
    }
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Destination(org.eclipse.jetty.client.api.Destination) FuturePromise(org.eclipse.jetty.util.FuturePromise) Connection(org.eclipse.jetty.client.api.Connection) SslConnection(org.eclipse.jetty.io.ssl.SslConnection) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 18 with Slow

use of org.eclipse.jetty.toolchain.test.annotation.Slow in project jetty.project by eclipse.

the class HttpConnectionLifecycleTest method test_BadRequest_WithSlowRequest_RemovesConnection.

@Slow
@Test
public void test_BadRequest_WithSlowRequest_RemovesConnection() throws Exception {
    start(new EmptyServerHandler());
    String host = "localhost";
    int port = connector.getLocalPort();
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    final Collection<Connection> idleConnections = connectionPool.getIdleConnections();
    Assert.assertEquals(0, idleConnections.size());
    final Collection<Connection> activeConnections = connectionPool.getActiveConnections();
    Assert.assertEquals(0, activeConnections.size());
    final long delay = 1000;
    final CountDownLatch successLatch = new CountDownLatch(3);
    client.newRequest(host, port).scheme(scheme).listener(new Request.Listener.Adapter() {

        @Override
        public void onBegin(Request request) {
            // Remove the host header, this will make the request invalid
            request.header(HttpHeader.HOST, null);
        }

        @Override
        public void onHeaders(Request request) {
            try {
                TimeUnit.MILLISECONDS.sleep(delay);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onSuccess(Request request) {
            successLatch.countDown();
        }
    }).send(new Response.Listener.Adapter() {

        @Override
        public void onSuccess(Response response) {
            Assert.assertEquals(400, response.getStatus());
            // 400 response also come with a Connection: close,
            // so the connection is closed and removed
            successLatch.countDown();
        }

        @Override
        public void onComplete(Result result) {
            Assert.assertFalse(result.isFailed());
            successLatch.countDown();
        }
    });
    Assert.assertTrue(successLatch.await(delay * 30, TimeUnit.MILLISECONDS));
    Assert.assertEquals(0, idleConnections.size());
    Assert.assertEquals(0, activeConnections.size());
}
Also used : Connection(org.eclipse.jetty.client.api.Connection) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 19 with Slow

use of org.eclipse.jetty.toolchain.test.annotation.Slow in project jetty.project by eclipse.

the class ResourceHandlerTest method testSlowBiggest.

@Test
@Slow
public void testSlowBiggest() throws Exception {
    _connector.setIdleTimeout(10000);
    File dir = MavenTestingUtils.getTargetFile("test-classes/simple");
    File biggest = new File(dir, "biggest.txt");
    try (OutputStream out = new FileOutputStream(biggest)) {
        for (int i = 0; i < 10; i++) {
            try (InputStream in = new FileInputStream(new File(dir, "bigger.txt"))) {
                IO.copy(in, out);
            }
        }
        out.write("\nTHE END\n".getBytes(StandardCharsets.ISO_8859_1));
    }
    biggest.deleteOnExit();
    try (Socket socket = new Socket("localhost", _connector.getLocalPort());
        OutputStream out = socket.getOutputStream();
        InputStream in = socket.getInputStream()) {
        socket.getOutputStream().write("GET /resource/biggest.txt HTTP/1.0\n\n".getBytes());
        byte[] array = new byte[102400];
        ByteBuffer buffer = null;
        while (true) {
            Thread.sleep(100);
            int len = in.read(array);
            if (len < 0)
                break;
            buffer = BufferUtil.toBuffer(array, 0, len);
        // System.err.println(++i+": "+BufferUtil.toDetailString(buffer));
        }
        Assert.assertEquals('E', buffer.get(buffer.limit() - 4));
        Assert.assertEquals('N', buffer.get(buffer.limit() - 3));
        Assert.assertEquals('D', buffer.get(buffer.limit() - 2));
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream) Socket(java.net.Socket) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 20 with Slow

use of org.eclipse.jetty.toolchain.test.annotation.Slow in project jetty.project by eclipse.

the class SameNodeLoadTest method testLoad.

@Test
@Slow
public void testLoad() throws Exception {
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory();
    String contextPath = "";
    String servletMapping = "/server";
    TestServer server1 = new TestServer(0, -1, 4, cacheFactory, storeFactory);
    server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
    try {
        server1.start();
        int port1 = server1.getPort();
        HttpClient client = new HttpClient();
        client.start();
        try {
            String url = "http://localhost:" + port1 + contextPath + servletMapping;
            //create session via first server
            ContentResponse response1 = client.GET(url + "?action=init");
            assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
            String sessionCookie = response1.getHeaders().get("Set-Cookie");
            assertTrue(sessionCookie != null);
            // Mangle the cookie, replacing Path with $Path, etc.
            sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
            //simulate 10 clients making 100 requests each
            ExecutorService executor = Executors.newCachedThreadPool();
            int clientsCount = 10;
            CyclicBarrier barrier = new CyclicBarrier(clientsCount + 1);
            int requestsCount = 100;
            Worker[] workers = new Worker[clientsCount];
            for (int i = 0; i < clientsCount; ++i) {
                workers[i] = new Worker(barrier, client, requestsCount, sessionCookie, url);
                executor.execute(workers[i]);
            }
            // Wait for all workers to be ready
            barrier.await();
            long start = System.nanoTime();
            // Wait for all workers to be done
            barrier.await();
            long end = System.nanoTime();
            long elapsed = TimeUnit.NANOSECONDS.toMillis(end - start);
            System.err.println("Elapsed ms:" + elapsed);
            executor.shutdownNow();
            // Perform one request to get the result
            Request request = client.newRequest(url + "?action=result");
            request.header("Cookie", sessionCookie);
            ContentResponse response2 = request.send();
            assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
            String response = response2.getContentAsString();
            assertEquals(response.trim(), String.valueOf(clientsCount * requestsCount));
        } finally {
            client.stop();
        }
    } finally {
        server1.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CyclicBarrier(java.util.concurrent.CyclicBarrier) HttpClient(org.eclipse.jetty.client.HttpClient) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Aggregations

Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)37 Test (org.junit.Test)37 HttpServletRequest (javax.servlet.http.HttpServletRequest)18 CountDownLatch (java.util.concurrent.CountDownLatch)17 HttpServletResponse (javax.servlet.http.HttpServletResponse)16 IOException (java.io.IOException)11 Result (org.eclipse.jetty.client.api.Result)11 ServletException (javax.servlet.ServletException)10 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)10 Request (org.eclipse.jetty.client.api.Request)9 Socket (java.net.Socket)7 OutputStream (java.io.OutputStream)6 ArrayList (java.util.ArrayList)6 Response (org.eclipse.jetty.client.api.Response)6 Connection (org.eclipse.jetty.client.api.Connection)5 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)5 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)5 Request (org.eclipse.jetty.server.Request)5 CloseInfo (org.eclipse.jetty.websocket.common.CloseInfo)5 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)5