Search in sources :

Example 21 with VertxInternal

use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.

the class VertxTest method testCloseVertxShouldWaitConcurrentCloseHook.

@Test
public void testCloseVertxShouldWaitConcurrentCloseHook() throws Exception {
    VertxInternal vertx = (VertxInternal) Vertx.vertx();
    AtomicReference<Promise<Void>> ref = new AtomicReference<>();
    CloseFuture fut = new CloseFuture();
    fut.add(ref::set);
    vertx.addCloseHook(fut);
    Promise<Void> p = Promise.promise();
    fut.close(p);
    AtomicBoolean closed = new AtomicBoolean();
    vertx.close(ar -> {
        closed.set(true);
    });
    Thread.sleep(500);
    assertFalse(closed.get());
    ref.get().complete();
    assertWaitUntil(closed::get);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CloseFuture(io.vertx.core.impl.CloseFuture) VertxInternal(io.vertx.core.impl.VertxInternal) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 22 with VertxInternal

use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.

the class MetricsTest method testThreadPoolMetricsWithWorkerVerticle.

@Test
public void testThreadPoolMetricsWithWorkerVerticle() throws Exception {
    AtomicInteger counter = new AtomicInteger();
    Map<String, PoolMetrics> all = FakePoolMetrics.getPoolMetrics();
    FakePoolMetrics metrics = (FakePoolMetrics) all.get("vert.x-worker-thread");
    assertThat(metrics.getPoolSize(), is(getOptions().getInternalBlockingPoolSize()));
    assertThat(metrics.numberOfIdleThreads(), is(getOptions().getWorkerPoolSize()));
    AtomicBoolean hadWaitingQueue = new AtomicBoolean();
    AtomicBoolean hadIdle = new AtomicBoolean();
    AtomicBoolean hadRunning = new AtomicBoolean();
    int count = 100;
    AtomicInteger msg = new AtomicInteger();
    CountDownLatch latch1 = new CountDownLatch(1);
    CountDownLatch latch2 = new CountDownLatch(1);
    ContextInternal ctx = ((VertxInternal) vertx).createWorkerContext();
    ctx.runOnContext(v -> {
        vertx.eventBus().localConsumer("message", d -> {
            msg.incrementAndGet();
            try {
                Thread.sleep(10);
                if (metrics.numberOfWaitingTasks() > 0) {
                    hadWaitingQueue.set(true);
                }
                if (metrics.numberOfIdleThreads() > 0) {
                    hadIdle.set(true);
                }
                if (metrics.numberOfRunningTasks() > 0) {
                    hadRunning.set(true);
                }
                if (counter.incrementAndGet() == count) {
                    latch2.countDown();
                }
            } catch (InterruptedException e) {
                Thread.currentThread().isInterrupted();
            }
        });
        latch1.countDown();
    });
    awaitLatch(latch1);
    for (int i = 0; i < count; i++) {
        vertx.eventBus().send("message", i);
    }
    awaitLatch(latch2);
    // The verticle deployment is also executed on the worker thread pool
    assertWaitUntil(() -> count + 1 == metrics.numberOfCompletedTasks());
    assertEquals(count + 1, metrics.numberOfSubmittedTask());
    assertEquals(count + 1, metrics.numberOfCompletedTasks());
    assertTrue("Had no idle threads", hadIdle.get());
    assertTrue("Had no waiting tasks", hadWaitingQueue.get());
    assertTrue("Had running tasks", hadRunning.get());
    assertEquals(getOptions().getWorkerPoolSize(), metrics.numberOfIdleThreads());
    assertEquals(0, metrics.numberOfRunningTasks());
    assertEquals(0, metrics.numberOfWaitingTasks());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VertxInternal(io.vertx.core.impl.VertxInternal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContextInternal(io.vertx.core.impl.ContextInternal) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 23 with VertxInternal

use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.

the class FileResolverFactoryTest method testResolver.

@Test
public void testResolver() {
    FileResolver resolver = ((VertxInternal) vertx).fileResolver();
    assertEquals(CustomFileResolver.class, resolver.getClass());
}
Also used : VertxInternal(io.vertx.core.impl.VertxInternal) FileResolver(io.vertx.core.spi.file.FileResolver) Test(org.junit.Test)

Example 24 with VertxInternal

use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.

the class NettyCompatTest method testAddressResolver.

@Test
public void testAddressResolver() {
    VertxInternal vertx = (VertxInternal) super.vertx;
    vertx.resolveAddress("localhost", onSuccess(v -> testComplete()));
    await();
}
Also used : OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) VertxInternal(io.vertx.core.impl.VertxInternal) HttpMethod(io.vertx.core.http.HttpMethod) Cert(io.vertx.test.tls.Cert) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Trust(io.vertx.test.tls.Trust) Test(org.junit.Test) HttpClientOptions(io.vertx.core.http.HttpClientOptions) VertxTestBase(io.vertx.test.core.VertxTestBase) HttpClient(io.vertx.core.http.HttpClient) VertxInternal(io.vertx.core.impl.VertxInternal) Test(org.junit.Test)

Example 25 with VertxInternal

use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.

the class SSLEngineTest method doTest.

private void doTest(SSLEngineOptions engine, boolean useAlpn, HttpVersion version, String error, String expectedSslContext, boolean expectCause) {
    server.close();
    HttpServerOptions options = new HttpServerOptions().setSslEngineOptions(engine).setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTP_HOST).setKeyCertOptions(Cert.SERVER_PEM.get()).setSsl(true).setUseAlpn(useAlpn);
    server = vertx.createHttpServer(options);
    server.requestHandler(req -> {
        assertEquals(req.version(), version);
        assertTrue(req.isSSL());
        req.response().end();
    });
    try {
        startServer();
        if (error != null) {
            fail("Was expecting failure: " + error);
        }
    } catch (Exception e) {
        if (error == null) {
            fail(e);
        } else {
            assertEquals(error, e.getMessage());
            if (expectCause) {
                assertNotSame(e, e.getCause());
            }
            return;
        }
    }
    SSLHelper sslHelper = ((HttpServerImpl) server).sslHelper();
    SslContext ctx = sslHelper.getContext((VertxInternal) vertx);
    switch(expectedSslContext != null ? expectedSslContext : "jdk") {
        case "jdk":
            assertTrue(ctx instanceof JdkSslContext);
            break;
        case "openssl":
            assertTrue(ctx instanceof OpenSslContext);
            break;
    }
    client = vertx.createHttpClient(new HttpClientOptions().setSslEngineOptions(engine).setSsl(true).setUseAlpn(useAlpn).setTrustAll(true).setProtocolVersion(version));
    client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(req -> {
        req.send(onSuccess(resp -> {
            assertEquals(200, resp.statusCode());
            testComplete();
        }));
    }));
    await();
}
Also used : SSLHelper(io.vertx.core.net.impl.SSLHelper) VertxException(io.vertx.core.VertxException) HttpServerImpl(io.vertx.core.http.impl.HttpServerImpl) SSLEngineOptions(io.vertx.core.net.SSLEngineOptions) VertxInternal(io.vertx.core.impl.VertxInternal) JdkSslContext(io.netty.handler.ssl.JdkSslContext) SslContext(io.netty.handler.ssl.SslContext) OpenSslContext(io.netty.handler.ssl.OpenSslContext) Test(org.junit.Test) SSLHelper(io.vertx.core.net.impl.SSLHelper) OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) HttpTestBase(io.vertx.core.http.HttpTestBase) HttpVersion(io.vertx.core.http.HttpVersion) HttpMethod(io.vertx.core.http.HttpMethod) Cert(io.vertx.test.tls.Cert) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) JdkSSLEngineOptions(io.vertx.core.net.JdkSSLEngineOptions) JdkSslContext(io.netty.handler.ssl.JdkSslContext) OpenSslContext(io.netty.handler.ssl.OpenSslContext) HttpServerOptions(io.vertx.core.http.HttpServerOptions) VertxException(io.vertx.core.VertxException) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpServerImpl(io.vertx.core.http.impl.HttpServerImpl) JdkSslContext(io.netty.handler.ssl.JdkSslContext) SslContext(io.netty.handler.ssl.SslContext) OpenSslContext(io.netty.handler.ssl.OpenSslContext)

Aggregations

VertxInternal (io.vertx.core.impl.VertxInternal)100 Test (org.junit.Test)73 CountDownLatch (java.util.concurrent.CountDownLatch)46 VertxOptions (io.vertx.core.VertxOptions)30 JsonObject (io.vertx.core.json.JsonObject)29 Buffer (io.vertx.core.buffer.Buffer)28 File (java.io.File)27 AtomicReference (java.util.concurrent.atomic.AtomicReference)27 VertxException (io.vertx.core.VertxException)24 HttpClient (io.vertx.core.http.HttpClient)24 NetClient (io.vertx.core.net.NetClient)24 TimeUnit (java.util.concurrent.TimeUnit)24 NetServerOptions (io.vertx.core.net.NetServerOptions)23 InetAddress (java.net.InetAddress)23 Channel (io.netty.channel.Channel)22 InetSocketAddress (java.net.InetSocketAddress)22 CompletableFuture (java.util.concurrent.CompletableFuture)22 NetServer (io.vertx.core.net.NetServer)21 ChannelFuture (io.netty.channel.ChannelFuture)20 Bootstrap (io.netty.bootstrap.Bootstrap)19