use of io.vertx.core.AbstractVerticle in project vert.x by eclipse.
the class WebsocketTest method testRaceConditionWithWebsocketClientWorker.
@Test
public void testRaceConditionWithWebsocketClientWorker() throws Exception {
CompletableFuture<Context> fut = new CompletableFuture<>();
vertx.deployVerticle(new AbstractVerticle() {
@Override
public void start() throws Exception {
fut.complete(context);
}
}, new DeploymentOptions().setWorker(true), ar -> {
if (ar.failed()) {
fut.completeExceptionally(ar.cause());
}
});
testRaceConditionWithWebsocketClient(fut.get());
}
use of io.vertx.core.AbstractVerticle in project vert.x by eclipse.
the class VertxTestBase method createWorker.
/**
* Create a worker verticle for the current Vert.x and return its context.
*
* @return the context
* @throws Exception anything preventing the creation of the worker
*/
protected Context createWorker() throws Exception {
CompletableFuture<Context> fut = new CompletableFuture<>();
vertx.deployVerticle(new AbstractVerticle() {
@Override
public void start() throws Exception {
fut.complete(context);
}
}, new DeploymentOptions().setWorker(true), ar -> {
if (ar.failed()) {
fut.completeExceptionally(ar.cause());
}
});
return fut.get();
}
use of io.vertx.core.AbstractVerticle in project vert.x by eclipse.
the class HttpTest method testInVerticle.
private void testInVerticle(boolean worker) throws Exception {
client.close();
server.close();
class MyVerticle extends AbstractVerticle {
Context ctx;
@Override
public void start() {
ctx = Vertx.currentContext();
if (worker) {
assertTrue(ctx instanceof WorkerContext);
} else {
assertTrue(ctx instanceof EventLoopContext);
}
Thread thr = Thread.currentThread();
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT));
server.requestHandler(req -> {
req.response().end();
assertSame(ctx, Vertx.currentContext());
if (!worker) {
assertSame(thr, Thread.currentThread());
}
});
server.listen(ar -> {
assertTrue(ar.succeeded());
assertSame(ctx, Vertx.currentContext());
if (!worker) {
assertSame(thr, Thread.currentThread());
}
client = vertx.createHttpClient(new HttpClientOptions());
client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", res -> {
assertSame(ctx, Vertx.currentContext());
if (!worker) {
assertSame(thr, Thread.currentThread());
}
assertEquals(200, res.statusCode());
testComplete();
}).end();
});
}
}
MyVerticle verticle = new MyVerticle();
vertx.deployVerticle(verticle, new DeploymentOptions().setWorker(worker));
await();
}
use of io.vertx.core.AbstractVerticle in project vert.x by eclipse.
the class HttpTest method testInMultithreadedWorker.
@Test
public void testInMultithreadedWorker() throws Exception {
vertx.deployVerticle(new AbstractVerticle() {
@Override
public void start() throws Exception {
assertTrue(Vertx.currentContext().isWorkerContext());
assertTrue(Vertx.currentContext().isMultiThreadedWorkerContext());
assertTrue(Context.isOnWorkerThread());
try {
vertx.createHttpServer();
fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
vertx.createHttpClient();
fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
testComplete();
}
}, new DeploymentOptions().setWorker(true).setMultiThreaded(true));
await();
}
use of io.vertx.core.AbstractVerticle in project vert.x by eclipse.
the class NamedWorkerPoolTest method testDeployUsingNamedPool.
@Test
public void testDeployUsingNamedPool() throws Exception {
AtomicReference<Thread> thread = new AtomicReference<>();
String poolName = "vert.x-" + TestUtils.randomAlphaString(10);
vertx.deployVerticle(new AbstractVerticle() {
@Override
public void start() throws Exception {
vertx.executeBlocking(fut -> {
thread.set(Thread.currentThread());
assertTrue(Context.isOnVertxThread());
assertTrue(Context.isOnWorkerThread());
assertFalse(Context.isOnEventLoopThread());
assertTrue(Thread.currentThread().getName().startsWith(poolName + "-"));
fut.complete();
}, onSuccess(v -> {
vertx.undeploy(context.deploymentID());
}));
}
}, new DeploymentOptions().setWorkerPoolName(poolName), onSuccess(v -> {
}));
waitUntil(() -> thread.get() != null && thread.get().getState() == Thread.State.TERMINATED);
}
Aggregations