Search in sources :

Example 6 with AbstractVerticle

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());
}
Also used : Context(io.vertx.core.Context) DeploymentOptions(io.vertx.core.DeploymentOptions) AbstractVerticle(io.vertx.core.AbstractVerticle) WebSocketHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketHandshakeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Test(org.junit.Test)

Example 7 with AbstractVerticle

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();
}
Also used : Context(io.vertx.core.Context) CompletableFuture(java.util.concurrent.CompletableFuture) DeploymentOptions(io.vertx.core.DeploymentOptions) AbstractVerticle(io.vertx.core.AbstractVerticle)

Example 8 with AbstractVerticle

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();
}
Also used : Context(io.vertx.core.Context) WorkerContext(io.vertx.core.impl.WorkerContext) EventLoopContext(io.vertx.core.impl.EventLoopContext) VertxException(io.vertx.core.VertxException) MultiMap(io.vertx.core.MultiMap) TimeoutException(java.util.concurrent.TimeoutException) Context(io.vertx.core.Context) InetAddress(java.net.InetAddress) HttpFrame(io.vertx.core.http.HttpFrame) HttpVersion(io.vertx.core.http.HttpVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TestLoggerFactory(io.vertx.test.netty.TestLoggerFactory) HttpHeaders(io.vertx.core.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) UUID(java.util.UUID) Future(io.vertx.core.Future) FileNotFoundException(java.io.FileNotFoundException) Nullable(io.vertx.codegen.annotations.Nullable) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) WorkerContext(io.vertx.core.impl.WorkerContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) IntStream(java.util.stream.IntStream) HeadersAdaptor(io.vertx.core.http.impl.HeadersAdaptor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) TestUtils.assertNullPointerException(io.vertx.test.core.TestUtils.assertNullPointerException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) OutputStreamWriter(java.io.OutputStreamWriter) Assume(org.junit.Assume) AsyncResult(io.vertx.core.AsyncResult) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpConnection(io.vertx.core.http.HttpConnection) BufferedWriter(java.io.BufferedWriter) Vertx(io.vertx.core.Vertx) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) EventLoopContext(io.vertx.core.impl.EventLoopContext) URLEncoder(java.net.URLEncoder) Rule(org.junit.Rule) DeploymentOptions(io.vertx.core.DeploymentOptions) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerOptions(io.vertx.core.http.HttpServerOptions) InternalLoggerFactory(io.netty.util.internal.logging.InternalLoggerFactory) Handler(io.vertx.core.Handler) Collections(java.util.Collections) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) TemporaryFolder(org.junit.rules.TemporaryFolder) TestUtils.assertIllegalArgumentException(io.vertx.test.core.TestUtils.assertIllegalArgumentException) DeploymentOptions(io.vertx.core.DeploymentOptions) HttpServerOptions(io.vertx.core.http.HttpServerOptions) WorkerContext(io.vertx.core.impl.WorkerContext) EventLoopContext(io.vertx.core.impl.EventLoopContext) AbstractVerticle(io.vertx.core.AbstractVerticle) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Example 9 with AbstractVerticle

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();
}
Also used : TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) DeploymentOptions(io.vertx.core.DeploymentOptions) AbstractVerticle(io.vertx.core.AbstractVerticle) VertxException(io.vertx.core.VertxException) TimeoutException(java.util.concurrent.TimeoutException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TestUtils.assertNullPointerException(io.vertx.test.core.TestUtils.assertNullPointerException) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) TestUtils.assertIllegalArgumentException(io.vertx.test.core.TestUtils.assertIllegalArgumentException) Test(org.junit.Test)

Example 10 with AbstractVerticle

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);
}
Also used : Vertx(io.vertx.core.Vertx) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) Context(io.vertx.core.Context) AtomicReference(java.util.concurrent.atomic.AtomicReference) WorkerExecutor(io.vertx.core.WorkerExecutor) HashSet(java.util.HashSet) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) DeploymentOptions(io.vertx.core.DeploymentOptions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractVerticle(io.vertx.core.AbstractVerticle) Collections(java.util.Collections) DeploymentOptions(io.vertx.core.DeploymentOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) AbstractVerticle(io.vertx.core.AbstractVerticle) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Test(org.junit.Test)

Aggregations

AbstractVerticle (io.vertx.core.AbstractVerticle)22 Test (org.junit.Test)21 DeploymentOptions (io.vertx.core.DeploymentOptions)16 Context (io.vertx.core.Context)12 CountDownLatch (java.util.concurrent.CountDownLatch)9 Vertx (io.vertx.core.Vertx)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 Verticle (io.vertx.core.Verticle)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Future (io.vertx.core.Future)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 WorkerExecutor (io.vertx.core.WorkerExecutor)4 Buffer (io.vertx.core.buffer.Buffer)4 Collections (java.util.Collections)4 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)4 TimeUnit (java.util.concurrent.TimeUnit)4 Consumer (java.util.function.Consumer)4 CharsetUtil (io.netty.util.CharsetUtil)3 VertxOptions (io.vertx.core.VertxOptions)3