@Override public synchronized MessageConsumer<T> endHandler(Handler<Void> endHandler) { if (endHandler != null) { // We should use the HandlerHolder context to properly do this (needs small refactoring) Context endCtx = vertx.getOrCreateContext(); this.endHandler = v1 -> endCtx.runOnContext(v2 -> endHandler.handle(null)); } else { this.endHandler = null; } return this; }
public synchronized <T> void executeBlocking(Handler<Future<T>> blockingCodeHandler, boolean ordered, Handler<AsyncResult<T>> asyncResultHandler) { if (closed) { throw new IllegalStateException("Worker executor closed"); } ContextImpl context = (ContextImpl) ctx.owner().getOrCreateContext(); context.executeBlocking(blockingCodeHandler, asyncResultHandler, pool.executor(), ordered ? context.orderedTasks : null, pool.metrics()); }
protected void startServer(SocketAddress remoteAddress) throws Exception { startServer(remoteAddress, vertx.getOrCreateContext()); }
protected void startServer(HttpServer server) throws Exception { startServer(vertx.getOrCreateContext(), server); }
protected void startServer(SocketAddress remoteAddress, NetServer server) throws Exception { startServer(remoteAddress, vertx.getOrCreateContext(), server); }
protected void startServer(NetServer server) throws Exception { startServer(testAddress, vertx.getOrCreateContext(), server); }
protected void startServer() throws Exception { startServer(vertx.getOrCreateContext()); }
protected void startServer() throws Exception { startServer(testAddress, vertx.getOrCreateContext()); }
FakeWaiter() { context = (ContextInternal) vertx.getOrCreateContext(); handler = ar -> { if (ar.succeeded()) { handleConnection(ar.result()); } else { handleFailure(ar.cause()); } }; }
@Test public void testRaceConditionWithWebsocketClientEventLoop() { testRaceConditionWithWebsocketClient(vertx.getOrCreateContext()); }
@Test public void testReportExceptionToContext() { ContextInternal ctx = (ContextInternal) vertx.getOrCreateContext(); RuntimeException expected = new RuntimeException(); AtomicReference<Throwable> err = new AtomicReference<>(); ctx.exceptionHandler(err::set); ctx.reportException(expected); assertSame(expected, err.get()); } }
@Override public void setUp() throws Exception { super.setUp(); context = vertx.getOrCreateContext(); sequence = new AtomicInteger(); context.runOnContext(v -> { Thread contextThread = Thread.currentThread(); contextChecker = () -> { assertSame(contextThread, Thread.currentThread()); }; }); waitUntil(() -> contextChecker != null); }
@Test public void testGettingContextContextUnderContextAnotherInstanceShouldReturnDifferentContext() throws Exception { Vertx other = vertx(); Context context = vertx.getOrCreateContext(); context.runOnContext(v -> { Context otherContext = other.getOrCreateContext(); assertNotSame(otherContext, context); testComplete(); }); await(); }
@Override public void start() throws Exception { vertx.eventBus().send("instanceCount", vertx.getOrCreateContext().getInstanceCount()); } }
@Test public void testExecuteBlockingThreadSyncComplete() throws Exception { Context context = vertx.getOrCreateContext(); context.<Void>runOnContext(v -> { Thread expected = Thread.currentThread(); context.executeBlocking(Future::complete, r -> { assertSame(expected, Thread.currentThread()); testComplete(); }); }); await(); }
@Test public void testDefaultContextExceptionHandler() { RuntimeException failure = new RuntimeException(); Context context = vertx.getOrCreateContext(); vertx.exceptionHandler(err -> { assertSame(failure, err); testComplete(); }); context.runOnContext(v -> { throw failure; }); await(); }
@Test public void testExecuteUnorderedBlocking() throws Exception { Context context = vertx.getOrCreateContext(); context.executeBlocking(f -> { assertTrue(Context.isOnWorkerThread()); f.complete(1 + 2); }, false, r -> { assertTrue(Context.isOnEventLoopThread()); assertEquals(r.result(), 3); testComplete(); }); await(); }
@Test public void testExecuteOrderedBlocking() throws Exception { Context context = vertx.getOrCreateContext(); context.executeBlocking(f -> { assertTrue(Context.isOnWorkerThread()); f.complete(1 + 2); }, r -> { assertTrue(Context.isOnEventLoopThread()); assertEquals(r.result(), 3); testComplete(); }); await(); }
@Test public void testContextExceptionHandler() { RuntimeException failure = new RuntimeException(); Context context = vertx.getOrCreateContext(); context.exceptionHandler(err -> { assertSame(context, Vertx.currentContext()); assertSame(failure, err); testComplete(); }); context.runOnContext(v -> { throw failure; }); await(); }
@Test public void testConsumerUnregisterDoesNotCancelTimer0() throws Exception { Context ctx = vertx.getOrCreateContext(); ctx.runOnContext(v -> { // The delay does not matter so much, it will always be executed after this task anyway vertx.setTimer(50, id -> { assertEquals(0, (long) id); testComplete(); }); eb.consumer(ADDRESS1).unregister(); }); await(); }