Search in sources :

Example 21 with Slow

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

the class BlockingArrayQueueTest method testConcurrentAccess.

@Test
@Slow
public void testConcurrentAccess() throws Exception {
    final int THREADS = 50;
    final int LOOPS = 1000;
    final BlockingArrayQueue<Integer> queue = new BlockingArrayQueue<>(1 + THREADS * LOOPS);
    final ConcurrentLinkedQueue<Integer> produced = new ConcurrentLinkedQueue<>();
    final ConcurrentLinkedQueue<Integer> consumed = new ConcurrentLinkedQueue<>();
    final AtomicBoolean running = new AtomicBoolean(true);
    // start consumers
    final CyclicBarrier barrier0 = new CyclicBarrier(THREADS + 1);
    for (int i = 0; i < THREADS; i++) {
        new Thread() {

            @Override
            public void run() {
                final Random random = new Random();
                setPriority(getPriority() - 1);
                try {
                    while (running.get()) {
                        int r = 1 + random.nextInt(10);
                        if (r % 2 == 0) {
                            Integer msg = queue.poll();
                            if (msg == null) {
                                Thread.sleep(1 + random.nextInt(10));
                                continue;
                            }
                            consumed.add(msg);
                        } else {
                            Integer msg = queue.poll(r, TimeUnit.MILLISECONDS);
                            if (msg != null)
                                consumed.add(msg);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        barrier0.await();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
    // start producers
    final CyclicBarrier barrier1 = new CyclicBarrier(THREADS + 1);
    for (int i = 0; i < THREADS; i++) {
        final int id = i;
        new Thread() {

            @Override
            public void run() {
                final Random random = new Random();
                try {
                    for (int j = 0; j < LOOPS; j++) {
                        Integer msg = random.nextInt();
                        produced.add(msg);
                        if (!queue.offer(msg))
                            throw new Exception(id + " FULL! " + queue.size());
                        Thread.sleep(1 + random.nextInt(10));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        barrier1.await();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
    barrier1.await();
    int size = queue.size();
    int last = size - 1;
    while (size > 0 && size != last) {
        last = size;
        Thread.sleep(500);
        size = queue.size();
    }
    running.set(false);
    barrier0.await();
    HashSet<Integer> prodSet = new HashSet<>(produced);
    HashSet<Integer> consSet = new HashSet<>(consumed);
    Assert.assertEquals(prodSet, consSet);
}
Also used : CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) HashSet(java.util.HashSet) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 22 with Slow

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

the class HttpClientContinueTest method test_Expect100Continue_WithContent_WithResponseFailure_Before100Continue.

@Slow
@Test
public void test_Expect100Continue_WithContent_WithResponseFailure_Before100Continue() throws Exception {
    final long idleTimeout = 1000;
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            try {
                TimeUnit.MILLISECONDS.sleep(2 * idleTimeout);
            } catch (InterruptedException x) {
                throw new ServletException(x);
            }
        }
    });
    client.setIdleTimeout(idleTimeout);
    byte[] content = new byte[1024];
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content)).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isFailed());
            Assert.assertNotNull(result.getRequestFailure());
            Assert.assertNotNull(result.getResponseFailure());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(3 * idleTimeout, TimeUnit.MILLISECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 23 with Slow

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

the class HttpClientContinueTest method test_Expect100Continue_WithContent_WithResponseFailure_After100Continue.

@Slow
@Test
public void test_Expect100Continue_WithContent_WithResponseFailure_After100Continue() throws Exception {
    final long idleTimeout = 1000;
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            // Send 100-Continue and consume the content
            IO.copy(request.getInputStream(), new ByteArrayOutputStream());
            try {
                TimeUnit.MILLISECONDS.sleep(2 * idleTimeout);
            } catch (InterruptedException x) {
                throw new ServletException(x);
            }
        }
    });
    client.setIdleTimeout(idleTimeout);
    byte[] content = new byte[1024];
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content)).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isFailed());
            Assert.assertNull(result.getRequestFailure());
            Assert.assertNotNull(result.getResponseFailure());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(3 * idleTimeout, TimeUnit.MILLISECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 24 with Slow

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

the class HttpClientContinueTest method test_Expect100Continue_WithInitialAndDeferredContent_Respond100Continue.

@Slow
@Test
public void test_Expect100Continue_WithInitialAndDeferredContent_Respond100Continue() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            // Send 100-Continue and echo the content
            IO.copy(request.getInputStream(), response.getOutputStream());
        }
    });
    final byte[] chunk1 = new byte[] { 0, 1, 2, 3 };
    final byte[] chunk2 = new byte[] { 4, 5, 6, 7 };
    final byte[] data = new byte[chunk1.length + chunk2.length];
    System.arraycopy(chunk1, 0, data, 0, chunk1.length);
    System.arraycopy(chunk2, 0, data, chunk1.length, chunk2.length);
    final CountDownLatch latch = new CountDownLatch(1);
    DeferredContentProvider content = new DeferredContentProvider(ByteBuffer.wrap(chunk1));
    client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(content).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertArrayEquals(data, getContent());
            latch.countDown();
        }
    });
    Thread.sleep(1000);
    content.offer(ByteBuffer.wrap(chunk2));
    content.close();
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 25 with Slow

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

the class ByteArrayEndPointTest method testIdle.

@Slow
@Test
public void testIdle() throws Exception {
    long idleTimeout = 1500;
    long halfIdleTimeout = idleTimeout / 2;
    long oneAndHalfIdleTimeout = idleTimeout + halfIdleTimeout;
    ByteArrayEndPoint endp = new ByteArrayEndPoint(_scheduler, idleTimeout);
    endp.setGrowOutput(false);
    endp.addInput("test");
    endp.setOutput(BufferUtil.allocate(5));
    assertTrue(endp.isOpen());
    Thread.sleep(oneAndHalfIdleTimeout);
    // Still open because it has not been oshut or closed explicitly
    // and there are no callbacks, so idle timeout is ignored.
    assertTrue(endp.isOpen());
    // Normal read is immediate, since there is data to read.
    ByteBuffer buffer = BufferUtil.allocate(1024);
    FutureCallback fcb = new FutureCallback();
    endp.fillInterested(fcb);
    fcb.get(idleTimeout, TimeUnit.MILLISECONDS);
    assertTrue(fcb.isDone());
    assertEquals(4, endp.fill(buffer));
    assertEquals("test", BufferUtil.toString(buffer));
    // Wait for a read timeout.
    fcb = new FutureCallback();
    endp.fillInterested(fcb);
    long start = System.nanoTime();
    try {
        fcb.get();
        fail();
    } catch (ExecutionException t) {
        assertThat(t.getCause(), instanceOf(TimeoutException.class));
    }
    assertThat(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start), greaterThan(halfIdleTimeout));
    assertThat("Endpoint open", endp.isOpen(), is(true));
    // We need to delay the write timeout test below from the read timeout test above.
    // The reason is that the scheduler thread that fails the endPoint WriteFlusher
    // because of the read timeout above runs concurrently with the write below, and
    // if it runs just after the write below, the test fails because the write callback
    // below fails immediately rather than after the idle timeout.
    Thread.sleep(halfIdleTimeout);
    // Write more than the output capacity, then wait for idle timeout.
    fcb = new FutureCallback();
    endp.write(fcb, BufferUtil.toBuffer("This is too long"));
    start = System.nanoTime();
    try {
        fcb.get();
        fail();
    } catch (ExecutionException t) {
        assertThat(t.getCause(), instanceOf(TimeoutException.class));
    }
    assertThat(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start), greaterThan(halfIdleTimeout));
    // Still open because it has not been oshut or closed explicitly.
    assertThat("Endpoint open", endp.isOpen(), is(true));
    // Make sure the endPoint is closed when the callback fails.
    endp.fillInterested(new Closer(endp));
    Thread.sleep(halfIdleTimeout);
    // Still open because it has not been oshut or closed explicitly.
    assertThat("Endpoint open", endp.isOpen(), is(true));
    // Shutdown output.
    endp.shutdownOutput();
    Thread.sleep(idleTimeout);
    assertThat("Endpoint closed", endp.isOpen(), is(false));
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) ByteBuffer(java.nio.ByteBuffer) FutureCallback(org.eclipse.jetty.util.FutureCallback) 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