private void checkDrained() { Handler<Void> handler = drainHandler; if (handler != null && credits >= maxSize / 2) { this.drainHandler = null; vertx.runOnContext(v -> handler.handle(null)); } }
@Override public synchronized void completionHandler(Handler<AsyncResult<Void>> completionHandler) { Objects.requireNonNull(completionHandler); if (result != null) { AsyncResult<Void> value = result; vertx.runOnContext(v -> completionHandler.handle(value)); } else { this.completionHandler = completionHandler; } }
private void callCompletionHandlerAsync(Handler<AsyncResult<Void>> completionHandler) { if (completionHandler != null) { vertx.runOnContext(v -> completionHandler.handle(Future.succeededFuture())); } }
@Override public void resolve(String identifier, DeploymentOptions deploymentOptions, ClassLoader classLoader, Future<String> resolution) { vertx.runOnContext(v -> { // Async resolution resolution.complete("whatever"); }); } @Override
public synchronized void setResult(AsyncResult<Void> result) { this.result = result; if (completionHandler != null) { if (metrics != null && result.succeeded()) { metric = metrics.handlerRegistered(address, repliedAddress); } Handler<AsyncResult<Void>> callback = completionHandler; vertx.runOnContext(v -> callback.handle(result)); } else if (result.failed()) { log.error("Failed to propagate registration for handler " + handler + " and address " + address); } else if (metrics != null) { metric = metrics.handlerRegistered(address, repliedAddress); } }
private void connectUntilWebsocketHandshakeException(HttpClient client, int count, Handler<AsyncResult<Void>> doneHandler) { vertx.runOnContext(v -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/some/path", ws -> { if (count < 100) { connectUntilWebsocketHandshakeException(client, count + 1, doneHandler); } else { doneHandler.handle(Future.failedFuture(new AssertionError())); } }, err -> { if (err instanceof WebSocketHandshakeException || err instanceof IOException) { doneHandler.handle(Future.succeededFuture()); } else if (count < 100) { connectUntilWebsocketHandshakeException(client, count + 1, doneHandler); } else { doneHandler.handle(Future.failedFuture(err)); } }); }); }
@Override public void start() throws Exception { getVertx().runOnContext(v -> sendMsg()); }
@Test public void testTimerStreamExceptionDuringHandle() throws Exception { vertx.runOnContext(v -> { ReadStream<Long> timer = vertx.timerStream(200); AtomicBoolean handled = new AtomicBoolean(); timer.handler(l -> { assertFalse(handled.get()); handled.set(true); throw new RuntimeException(); }); timer.endHandler(v2 -> { assertTrue(handled.get()); testComplete(); }); }); await(); }
@Test public void testTimerStreamSetHandlerSchedulesTheTimer() throws Exception { vertx.runOnContext(v -> { ReadStream<Long> timer = vertx.timerStream(200); AtomicBoolean handled = new AtomicBoolean(); timer.handler(l -> { assertFalse(handled.get()); handled.set(true); }); timer.endHandler(v2 -> { assertTrue(handled.get()); testComplete(); }); }); await(); }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(new HttpClientOptions().setMaxPoolSize(n)); for (int i = 0;i < n;i++) { AtomicBoolean responseReceived = new AtomicBoolean(); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> { try { Thread.sleep(150); } catch (InterruptedException e) { fail(e); } responseReceived.set(true); // Complete later, if some timeout tasks have been queued, this will be executed after vertx.runOnContext(v -> complete()); }).exceptionHandler(err -> { fail("Was not expecting to get a timeout after the response is received"); }).setTimeout(500).end(); } } }, new DeploymentOptions().setWorker(true));
@Test public void testTimerStreamCancellation() throws Exception { vertx.runOnContext(v -> { TimeoutStream timer = vertx.timerStream(200); AtomicBoolean called = new AtomicBoolean(); timer.handler(l -> { called.set(true); }); timer.cancel(); vertx.setTimer(500, id -> { assertFalse(called.get()); testComplete(); }); }); await(); }
@Test public void testTimerStreamCallingWithNullHandlerCancelsTheTimer() throws Exception { vertx.runOnContext(v -> { ReadStream<Long> timer = vertx.timerStream(200); AtomicInteger count = new AtomicInteger(); timer.handler(l -> { if (count.incrementAndGet() == 1) { timer.handler(null); vertx.setTimer(200, id -> { assertEquals(1, count.get()); testComplete(); }); } else { fail(); } }); }); await(); }
@Test public void testExceptionInInboundInterceptor() { AtomicInteger cnt = new AtomicInteger(); Handler<DeliveryContext<Object>> eb1 = dc -> { cnt.incrementAndGet(); vertx.runOnContext(v -> dc.next()); throw new RuntimeException("foo"); }; Handler<DeliveryContext<Object>> eb2 = dc -> { cnt.incrementAndGet(); dc.next(); }; eb.addInboundInterceptor(eb1).addInboundInterceptor(eb2); eb.consumer("some-address", msg -> { assertEquals("armadillo", msg.body()); assertEquals(2, cnt.get()); testComplete(); }); eb.send("some-address", "armadillo"); await(); }
@Test public void testExceptionInOutboundInterceptor() { AtomicInteger cnt = new AtomicInteger(); Handler<DeliveryContext<Object>> eb1 = sc -> { cnt.incrementAndGet(); vertx.runOnContext(v -> sc.next()); throw new RuntimeException("foo"); }; Handler<DeliveryContext<Object>> eb2 = sc -> { cnt.incrementAndGet(); sc.next(); }; eb.addOutboundInterceptor(eb1).addOutboundInterceptor(eb2); eb.consumer("some-address", msg -> { assertEquals("armadillo", msg.body()); assertEquals(2, cnt.get()); testComplete(); }); eb.send("some-address", "armadillo"); await(); }
@Test public void testFlowControl() { MessageProducer<String> prod = eb.sender("some-address"); int numBatches = 1000; int wqms = 2000; prod.setWriteQueueMaxSize(wqms); MessageConsumer<String> consumer = eb.consumer("some-address"); AtomicInteger cnt = new AtomicInteger(); consumer.handler(msg -> { int c = cnt.incrementAndGet(); if (c == numBatches * wqms) { testComplete(); } }); vertx.runOnContext(v -> { sendBatch(prod, wqms, numBatches, 0); }); await(); }
@Test public void testFlowControlWithOptions() { MessageProducer<String> prod = eb.sender("some-address"); prod.deliveryOptions(new DeliveryOptions().addHeader("foo", "bar")); int numBatches = 1000; int wqms = 2000; prod.setWriteQueueMaxSize(wqms); MessageConsumer<String> consumer = eb.consumer("some-address"); AtomicInteger cnt = new AtomicInteger(); consumer.handler(msg -> { int c = cnt.incrementAndGet(); if (c == numBatches * wqms) { testComplete(); } }); vertx.runOnContext(v -> { sendBatch(prod, wqms, numBatches, 0); }); await(); }
@Test public void testTimerPause() throws Exception { vertx.runOnContext(v -> { ReadStream<Long> timer = vertx.timerStream(10); timer.handler(l -> fail()); timer.endHandler(l -> testComplete()); timer.pause(); }); await(); }
@Test public void testTimerSetHandlerTwice() throws Exception { vertx.runOnContext(v -> { ReadStream<Long> timer = vertx.timerStream(200); timer.handler(l -> testComplete()); try { timer.handler(l -> fail()); fail(); } catch (IllegalStateException ignore) { } }); await(); }
@Test public void testPeriodicSetHandlerTwice() throws Exception { vertx.runOnContext(v -> { ReadStream<Long> timer = vertx.periodicStream(200); timer.handler(l -> testComplete()); try { timer.handler(l -> fail()); fail(); } catch (IllegalStateException ignore) { } }); await(); }
@Test public void testPutGetRemoveData() throws Exception { SomeObject obj = new SomeObject(); vertx.runOnContext(v -> { Context ctx = Vertx.currentContext(); ctx.put("foo", obj); ctx.runOnContext(v2 -> { assertEquals(obj, ctx.get("foo")); assertTrue(ctx.remove("foo")); ctx.runOnContext(v3 -> { assertNull(ctx.get("foo")); testComplete(); }); }); }); await(); }