Search in sources :

Example 1 with CountDownLatch

use of java.util.concurrent.CountDownLatch in project jetty.project by eclipse.

the class WebSocketScopeSessionTest method testMultiSession_Overlapping.

@Test
public void testMultiSession_Overlapping() throws Exception {
    final CountDownLatch midLatch = new CountDownLatch(2);
    final CountDownLatch end1Latch = new CountDownLatch(1);
    Callable<Session> call1 = new Callable<Session>() {

        @Override
        public Session call() throws Exception {
            Session ret = null;
            ScopedInstance<WebSocketScopeContext> wsScope1Bean = newInstance(WebSocketScopeContext.class);
            WebSocketScopeContext wsScope1 = wsScope1Bean.instance;
            wsScope1.create();
            try {
                // Scope 1
                wsScope1.begin();
                BogusSession sess = new BogusSession("1");
                wsScope1.setSession(sess);
                midLatch.countDown();
                midLatch.await(1, TimeUnit.SECONDS);
                ScopedInstance<BogusSocket> sock1Bean = newInstance(BogusSocket.class);
                BogusSocket sock1 = sock1Bean.instance;
                assertThat("Socket 1 Session", sock1.getSession(), sameInstance((Session) sess));
                ret = sock1.getSession();
                sock1Bean.destroy();
            } finally {
                wsScope1.end();
            }
            wsScope1.destroy();
            wsScope1Bean.destroy();
            end1Latch.countDown();
            return ret;
        }
    };
    final CountDownLatch end2Latch = new CountDownLatch(1);
    Callable<Session> call2 = new Callable<Session>() {

        @Override
        public Session call() throws Exception {
            Session ret = null;
            ScopedInstance<WebSocketScopeContext> wsScope2Bean = newInstance(WebSocketScopeContext.class);
            WebSocketScopeContext wsScope2 = wsScope2Bean.instance;
            wsScope2.create();
            try {
                // Scope 2
                wsScope2.begin();
                BogusSession sess = new BogusSession("2");
                wsScope2.setSession(sess);
                ScopedInstance<BogusSocket> sock2Bean = newInstance(BogusSocket.class);
                midLatch.countDown();
                midLatch.await(1, TimeUnit.SECONDS);
                BogusSocket sock2 = sock2Bean.instance;
                ret = sock2.getSession();
                assertThat("Socket 2 Session", sock2.getSession(), sameInstance((Session) sess));
                sock2Bean.destroy();
            } finally {
                wsScope2.end();
            }
            wsScope2.destroy();
            wsScope2Bean.destroy();
            end2Latch.countDown();
            return ret;
        }
    };
    ExecutorService svc = Executors.newFixedThreadPool(4);
    Future<Session> fut1 = svc.submit(call1);
    Future<Session> fut2 = svc.submit(call2);
    Session sess1 = fut1.get(1, TimeUnit.SECONDS);
    Session sess2 = fut2.get(1, TimeUnit.SECONDS);
    assertThat("Sessions are different", sess1, not(sameInstance(sess2)));
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) WebSocketScopeContext(org.eclipse.jetty.cdi.websocket.WebSocketScopeContext) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 2 with CountDownLatch

use of java.util.concurrent.CountDownLatch in project jetty.project by eclipse.

the class HttpRedirector method redirect.

/**
     * Redirects the given {@code response}, blocking until the redirect is complete.
     *
     * @param request the original request that triggered the redirect
     * @param response the response to the original request
     * @return a {@link Result} object containing the request to the redirected location and its response
     * @throws InterruptedException if the thread is interrupted while waiting for the redirect to complete
     * @throws ExecutionException if the redirect failed
     * @see #redirect(Request, Response, Response.CompleteListener)
     */
public Result redirect(Request request, Response response) throws InterruptedException, ExecutionException {
    final AtomicReference<Result> resultRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    Request redirect = redirect(request, response, new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            resultRef.set(new Result(result.getRequest(), result.getRequestFailure(), new HttpContentResponse(result.getResponse(), getContent(), getMediaType(), getEncoding()), result.getResponseFailure()));
            latch.countDown();
        }
    });
    try {
        latch.await();
        Result result = resultRef.get();
        if (result.isFailed())
            throw new ExecutionException(result.getFailure());
        return result;
    } catch (InterruptedException x) {
        // If the application interrupts, we need to abort the redirect
        redirect.abort(x);
        throw x;
    }
}
Also used : Request(org.eclipse.jetty.client.api.Request) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) Result(org.eclipse.jetty.client.api.Result) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener)

Example 3 with CountDownLatch

use of java.util.concurrent.CountDownLatch in project jetty.project by eclipse.

the class HTTP2ServerTest method testRequestResponseNoContent.

@Test
public void testRequestResponseNoContent() throws Exception {
    final CountDownLatch latch = new CountDownLatch(3);
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            latch.countDown();
        }
    });
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    generator.control(lease, new PrefaceFrame());
    generator.control(lease, new SettingsFrame(new HashMap<>(), false));
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    generator.control(lease, new HeadersFrame(1, metaData, null, true));
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            output.write(BufferUtil.toArray(buffer));
        }
        final AtomicReference<HeadersFrame> frameRef = new AtomicReference<>();
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onSettings(SettingsFrame frame) {
                latch.countDown();
            }

            @Override
            public void onHeaders(HeadersFrame frame) {
                frameRef.set(frame);
                latch.countDown();
            }
        }, 4096, 8192);
        parseResponse(client, parser);
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
        HeadersFrame response = frameRef.get();
        Assert.assertNotNull(response);
        MetaData.Response responseMetaData = (MetaData.Response) response.getMetaData();
        Assert.assertEquals(200, responseMetaData.getStatus());
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) HttpServletResponse(javax.servlet.http.HttpServletResponse) Socket(java.net.Socket) Test(org.junit.Test)

Example 4 with CountDownLatch

use of java.util.concurrent.CountDownLatch in project jetty.project by eclipse.

the class HTTP2ServerTest method testRequestWithContinuationFrames.

private void testRequestWithContinuationFrames(PriorityFrame priorityFrame, Callable<ByteBufferPool.Lease> frames) throws Exception {
    final CountDownLatch serverLatch = new CountDownLatch(1);
    startServer(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            if (priorityFrame != null) {
                PriorityFrame priority = frame.getPriority();
                Assert.assertNotNull(priority);
                Assert.assertEquals(priorityFrame.getStreamId(), priority.getStreamId());
                Assert.assertEquals(priorityFrame.getParentStreamId(), priority.getParentStreamId());
                Assert.assertEquals(priorityFrame.getWeight(), priority.getWeight());
                Assert.assertEquals(priorityFrame.isExclusive(), priority.isExclusive());
            }
            serverLatch.countDown();
            MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
            HeadersFrame responseFrame = new HeadersFrame(stream.getId(), metaData, null, true);
            stream.headers(responseFrame, Callback.NOOP);
            return null;
        }
    });
    generator = new Generator(byteBufferPool, 4096, 4);
    ByteBufferPool.Lease lease = frames.call();
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
        output.flush();
        Assert.assertTrue(serverLatch.await(5, TimeUnit.SECONDS));
        final CountDownLatch clientLatch = new CountDownLatch(1);
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onHeaders(HeadersFrame frame) {
                if (frame.isEndStream())
                    clientLatch.countDown();
            }
        }, 4096, 8192);
        boolean closed = parseResponse(client, parser);
        Assert.assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
        Assert.assertFalse(closed);
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) OutputStream(java.io.OutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) PriorityFrame(org.eclipse.jetty.http2.frames.PriorityFrame) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) HttpServletResponse(javax.servlet.http.HttpServletResponse) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) OutputStream(java.io.OutputStream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Socket(java.net.Socket) Generator(org.eclipse.jetty.http2.generator.Generator)

Example 5 with CountDownLatch

use of java.util.concurrent.CountDownLatch in project jetty.project by eclipse.

the class HTTP2ServerTest method testBadPingWrongPayload.

@Test
public void testBadPingWrongPayload() throws Exception {
    startServer(new HttpServlet() {
    });
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    generator.control(lease, new PrefaceFrame());
    generator.control(lease, new SettingsFrame(new HashMap<>(), false));
    generator.control(lease, new PingFrame(new byte[8], false));
    // Modify the length of the frame to a wrong one.
    lease.getByteBuffers().get(2).putShort(0, (short) 7);
    final CountDownLatch latch = new CountDownLatch(1);
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            output.write(BufferUtil.toArray(buffer));
        }
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onGoAway(GoAwayFrame frame) {
                Assert.assertEquals(ErrorCode.FRAME_SIZE_ERROR.code, frame.getError());
                latch.countDown();
            }
        }, 4096, 8192);
        parseResponse(client, parser);
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HashMap(java.util.HashMap) PingFrame(org.eclipse.jetty.http2.frames.PingFrame) HttpServlet(javax.servlet.http.HttpServlet) OutputStream(java.io.OutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) Socket(java.net.Socket) Test(org.junit.Test)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)12609 Test (org.junit.Test)6674 AtomicReference (java.util.concurrent.atomic.AtomicReference)1509 IOException (java.io.IOException)1414 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1358 ArrayList (java.util.ArrayList)1255 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1086 ExecutorService (java.util.concurrent.ExecutorService)816 List (java.util.List)713 Test (org.testng.annotations.Test)688 TimeUnit (java.util.concurrent.TimeUnit)570 QuickTest (com.hazelcast.test.annotation.QuickTest)502 HashMap (java.util.HashMap)497 Test (org.junit.jupiter.api.Test)465 Map (java.util.Map)415 ExecutionException (java.util.concurrent.ExecutionException)408 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)400 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)375 HazelcastInstance (com.hazelcast.core.HazelcastInstance)367 Ignite (org.apache.ignite.Ignite)350