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();
}
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();
}
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();
}
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());
}
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());
}
Aggregations