Search in sources :

Example 81 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.

the class EventBusInterceptorTest method testRemoveInterceptor.

@Test
public void testRemoveInterceptor() {
    AtomicInteger cnt1 = new AtomicInteger();
    AtomicInteger cnt2 = new AtomicInteger();
    Handler<SendContext> eb1 = sc -> {
        cnt1.incrementAndGet();
        sc.next();
    };
    Handler<SendContext> eb2 = sc -> {
        cnt2.incrementAndGet();
        sc.next();
    };
    eb.addInterceptor(eb1).addInterceptor(eb2);
    eb.consumer("some-address", msg -> {
        if (msg.body().equals("armadillo")) {
            assertEquals(1, cnt1.get());
            assertEquals(1, cnt2.get());
            eb.removeInterceptor(eb2);
            eb.send("some-address", "aardvark");
        } else if (msg.body().equals("aardvark")) {
            assertEquals(2, cnt1.get());
            assertEquals(1, cnt2.get());
            testComplete();
        } else {
            fail("wrong body");
        }
    });
    eb.send("some-address", "armadillo");
    await();
}
Also used : EventBus(io.vertx.core.eventbus.EventBus) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SendContext(io.vertx.core.eventbus.SendContext) Test(org.junit.Test) Handler(io.vertx.core.Handler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SendContext(io.vertx.core.eventbus.SendContext) Test(org.junit.Test)

Example 82 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.

the class DatagramTest method testPauseResume.

@Test
public void testPauseResume() {
    peer1 = vertx.createDatagramSocket(new DatagramSocketOptions());
    peer2 = vertx.createDatagramSocket(new DatagramSocketOptions());
    peer2.exceptionHandler(t -> fail(t.getMessage()));
    peer2.listen(1234, "127.0.0.1", ar -> {
        Buffer buffer = TestUtils.randomBuffer(128);
        AtomicBoolean received = new AtomicBoolean();
        peer2.handler(packet -> received.set(true));
        peer2.pause();
        peer1.send(buffer, 1234, "127.0.0.1", ar2 -> {
            assertTrue(ar2.succeeded());
        });
        vertx.setTimer(1000, l -> {
            AtomicInteger count = new AtomicInteger();
            peer2.handler(packet -> {
                switch(count.getAndIncrement()) {
                    case 0:
                        assertEquals(buffer, packet.data());
                        peer1.send(buffer, 1234, "127.0.0.1", ar2 -> {
                            assertTrue(ar2.succeeded());
                        });
                        break;
                    case 1:
                        assertFalse(received.get());
                        assertEquals(buffer, packet.data());
                        testComplete();
                        break;
                    default:
                        fail();
                }
            });
            peer2.resume();
        });
    });
    await();
}
Also used : DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) Buffer(io.vertx.core.buffer.Buffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 83 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.

the class ContextTest method testContextExceptionHandlerFailing.

@Test
public void testContextExceptionHandlerFailing() {
    RuntimeException failure = new RuntimeException();
    Context context = vertx.getOrCreateContext();
    AtomicInteger count = new AtomicInteger();
    context.exceptionHandler(err -> {
        if (count.getAndIncrement() == 0) {
            throw new RuntimeException();
        } else {
            assertSame(failure, err);
            testComplete();
        }
    });
    context.runOnContext(v -> {
        throw new RuntimeException();
    });
    context.runOnContext(v -> {
        throw failure;
    });
    await();
}
Also used : Context(io.vertx.core.Context) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 84 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project jetty.project by eclipse.

the class HttpClientTest method testResponseListenerForMultipleEventsIsInvokedOncePerEvent.

@Test
public void testResponseListenerForMultipleEventsIsInvokedOncePerEvent() throws Exception {
    start(new EmptyServerHandler());
    final AtomicInteger counter = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(1);
    Response.Listener listener = new Response.Listener() {

        @Override
        public void onBegin(Response response) {
            counter.incrementAndGet();
        }

        @Override
        public boolean onHeader(Response response, HttpField field) {
            // Number of header may vary, so don't count
            return true;
        }

        @Override
        public void onHeaders(Response response) {
            counter.incrementAndGet();
        }

        @Override
        public void onContent(Response response, ByteBuffer content) {
            // Should not be invoked
            counter.incrementAndGet();
        }

        @Override
        public void onContent(Response response, ByteBuffer content, Callback callback) {
            // Should not be invoked
            counter.incrementAndGet();
        }

        @Override
        public void onSuccess(Response response) {
            counter.incrementAndGet();
        }

        @Override
        public void onFailure(Response response, Throwable failure) {
            // Should not be invoked
            counter.incrementAndGet();
        }

        @Override
        public void onComplete(Result result) {
            Assert.assertEquals(200, result.getResponse().getStatus());
            counter.incrementAndGet();
            latch.countDown();
        }
    };
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onResponseBegin(listener).onResponseHeader(listener).onResponseHeaders(listener).onResponseContent(listener).onResponseContentAsync(listener).onResponseSuccess(listener).onResponseFailure(listener).send(listener);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    int expectedEventsTriggeredByOnResponseXXXListeners = 3;
    int expectedEventsTriggeredByCompletionListener = 4;
    int expected = expectedEventsTriggeredByOnResponseXXXListeners + expectedEventsTriggeredByCompletionListener;
    Assert.assertEquals(expected, counter.get());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Callback(org.eclipse.jetty.util.Callback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpField(org.eclipse.jetty.http.HttpField) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) EndPoint(org.eclipse.jetty.io.EndPoint) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test)

Example 85 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project jetty.project by eclipse.

the class HttpResponseAbortTest method testAbortOnContentBeforeRequestTermination.

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

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            try {
                baseRequest.setHandled(true);
                OutputStream output = response.getOutputStream();
                output.write(1);
                output.flush();
                output.write(2);
                output.flush();
            } catch (IOException ignored) {
            // The client may have already closed, and we'll get an exception here, but it's expected
            }
        }
    });
    final CountDownLatch abortLatch = new CountDownLatch(1);
    final AtomicInteger completes = new AtomicInteger();
    final CountDownLatch completeLatch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestSuccess(new org.eclipse.jetty.client.api.Request.SuccessListener() {

        @Override
        public void onSuccess(org.eclipse.jetty.client.api.Request request) {
            try {
                abortLatch.await(5, TimeUnit.SECONDS);
            } catch (InterruptedException x) {
                x.printStackTrace();
            }
        }
    }).onResponseContent(new Response.ContentListener() {

        @Override
        public void onContent(Response response, ByteBuffer content) {
            try {
                response.abort(new Exception());
                abortLatch.countDown();
                // Delay to let the request side to finish its processing.
                Thread.sleep(1000);
            } catch (InterruptedException x) {
                x.printStackTrace();
            }
        }
    }).send(new Response.CompleteListener() {

        @Override
        public void onComplete(Result result) {
            completes.incrementAndGet();
            Assert.assertTrue(result.isFailed());
            completeLatch.countDown();
        }
    });
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
    // Wait to be sure that the complete event is only notified once.
    Thread.sleep(1000);
    Assert.assertEquals(1, completes.get());
}
Also used : OutputStream(java.io.OutputStream) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) 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) ByteBuffer(java.nio.ByteBuffer) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.eclipse.jetty.client.api.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7986 Test (org.junit.Test)3775 CountDownLatch (java.util.concurrent.CountDownLatch)1072 ArrayList (java.util.ArrayList)1018 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)849 List (java.util.List)740 IOException (java.io.IOException)719 AtomicReference (java.util.concurrent.atomic.AtomicReference)574 HashMap (java.util.HashMap)499 Map (java.util.Map)460 Test (org.testng.annotations.Test)419 File (java.io.File)337 ExecutorService (java.util.concurrent.ExecutorService)337 Test (org.junit.jupiter.api.Test)334 AtomicLong (java.util.concurrent.atomic.AtomicLong)329 TimeUnit (java.util.concurrent.TimeUnit)323 HashSet (java.util.HashSet)315 Arrays (java.util.Arrays)308 Set (java.util.Set)284 Collections (java.util.Collections)266