Search in sources :

Example 31 with VertxInternal

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

the class NetTest method testContexts.

@Test
public void testContexts() throws Exception {
    int numConnections = 10;
    CountDownLatch serverLatch = new CountDownLatch(numConnections);
    AtomicReference<Context> serverConnectContext = new AtomicReference<>();
    server.connectHandler(sock -> {
        // Server connect handler should always be called with same context
        Context serverContext = Vertx.currentContext();
        if (serverConnectContext.get() != null) {
            assertSame(serverConnectContext.get(), serverContext);
        } else {
            serverConnectContext.set(serverContext);
        }
        serverLatch.countDown();
    });
    CountDownLatch listenLatch = new CountDownLatch(1);
    AtomicReference<Context> listenContext = new AtomicReference<>();
    server.listen(testAddress, onSuccess(v -> {
        listenContext.set(Vertx.currentContext());
        listenLatch.countDown();
    }));
    awaitLatch(listenLatch);
    Set<Context> contexts = new ConcurrentHashSet<>();
    AtomicInteger connectCount = new AtomicInteger();
    CountDownLatch clientLatch = new CountDownLatch(1);
    // Each connect should be in its own context
    for (int i = 0; i < numConnections; i++) {
        Context context = ((VertxInternal) vertx).createEventLoopContext();
        context.runOnContext(v -> {
            client.connect(testAddress, conn -> {
                contexts.add(Vertx.currentContext());
                if (connectCount.incrementAndGet() == numConnections) {
                    assertEquals(numConnections, contexts.size());
                    clientLatch.countDown();
                }
            });
        });
    }
    awaitLatch(clientLatch);
    awaitLatch(serverLatch);
    // Close should be in own context
    server.close(ar -> {
        assertTrue(ar.succeeded());
        Context closeContext = Vertx.currentContext();
        assertFalse(contexts.contains(closeContext));
        assertSame(serverConnectContext.get(), closeContext);
        assertFalse(contexts.contains(listenContext.get()));
        assertSame(serverConnectContext.get(), listenContext.get());
        testComplete();
    });
    await();
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HAProxyMessageCompletionHandler(io.vertx.core.net.impl.HAProxyMessageCompletionHandler) CoreMatchers(org.hamcrest.CoreMatchers) NetServerImpl(io.vertx.core.net.impl.NetServerImpl) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Utils(io.vertx.core.impl.Utils) Unpooled(io.netty.buffer.Unpooled) PlatformDependent(io.netty.util.internal.PlatformDependent) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReadStream(io.vertx.core.streams.ReadStream) JsonObject(io.vertx.core.json.JsonObject) CheckingSender(io.vertx.test.core.CheckingSender) Logger(io.vertx.core.impl.logging.Logger) VertxHandler(io.vertx.core.net.impl.VertxHandler) ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) ConnectTimeoutException(io.netty.channel.ConnectTimeoutException) TestLoggerFactory(io.vertx.test.netty.TestLoggerFactory) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) java.util.concurrent(java.util.concurrent) Message(io.vertx.core.eventbus.Message) Trust(io.vertx.test.tls.Trust) ChannelPipeline(io.netty.channel.ChannelPipeline) InetSocketAddress(java.net.InetSocketAddress) io.vertx.core.http(io.vertx.core.http) StandardCharsets(java.nio.charset.StandardCharsets) io.netty.handler.codec.http(io.netty.handler.codec.http) SSLException(javax.net.ssl.SSLException) Certificate(java.security.cert.Certificate) Buffer(io.vertx.core.buffer.Buffer) Cert(io.vertx.test.tls.Cert) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) NetSocketInternal(io.vertx.core.net.impl.NetSocketInternal) HttpVersion(io.netty.handler.codec.http.HttpVersion) java.util(java.util) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) io.vertx.core(io.vertx.core) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VertxTestBase(io.vertx.test.core.VertxTestBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) SSLSession(javax.net.ssl.SSLSession) ByteBuf(io.netty.buffer.ByteBuf) io.vertx.test.proxy(io.vertx.test.proxy) OutputStreamWriter(java.io.OutputStreamWriter) Assume(org.junit.Assume) VertxInternal(io.vertx.core.impl.VertxInternal) ConcurrentHashSet(io.vertx.core.impl.ConcurrentHashSet) BufferedWriter(java.io.BufferedWriter) HttpMethod(io.netty.handler.codec.http.HttpMethod) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) File(java.io.File) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) AtomicLong(java.util.concurrent.atomic.AtomicLong) Rule(org.junit.Rule) TemporaryFolder(org.junit.rules.TemporaryFolder) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) VertxInternal(io.vertx.core.impl.VertxInternal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashSet(io.vertx.core.impl.ConcurrentHashSet) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 32 with VertxInternal

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

the class SSLHelperTest method testPreserveEnabledCipherSuitesOrder.

@Test
public void testPreserveEnabledCipherSuitesOrder() throws Exception {
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, null, null);
    SSLEngine engine = context.createSSLEngine();
    HttpServerOptions options = new HttpServerOptions();
    for (String suite : engine.getEnabledCipherSuites()) {
        options.addEnabledCipherSuite(suite);
    }
    assertEquals(new ArrayList<>(options.getEnabledCipherSuites()), Arrays.asList(engine.getEnabledCipherSuites()));
    assertEquals(new ArrayList<>(new HttpServerOptions(options).getEnabledCipherSuites()), Arrays.asList(engine.getEnabledCipherSuites()));
    JsonObject json = options.toJson();
    assertEquals(new ArrayList<>(new HttpServerOptions(json).getEnabledCipherSuites()), Arrays.asList(engine.getEnabledCipherSuites()));
    SSLHelper helper = new SSLHelper(options, Cert.SERVER_JKS.get(), null);
    assertEquals(Arrays.asList(helper.createEngine((VertxInternal) vertx).getEnabledCipherSuites()), Arrays.asList(engine.getEnabledCipherSuites()));
}
Also used : SSLHelper(io.vertx.core.net.impl.SSLHelper) VertxInternal(io.vertx.core.impl.VertxInternal) SSLEngine(javax.net.ssl.SSLEngine) HttpServerOptions(io.vertx.core.http.HttpServerOptions) JsonObject(io.vertx.core.json.JsonObject) SSLContext(javax.net.ssl.SSLContext) Test(org.junit.Test)

Example 33 with VertxInternal

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

the class ClusteredAsynchronousLockTest method testLockReleasedForKilledNode.

/**
 * Cannot run with the fake cluster manager.
 * Subclasses need to override the method and call <code>super.testLockReleasedForKilledNode()</code>.
 */
@Test
@Ignore
public void testLockReleasedForKilledNode() throws Exception {
    testLockReleased(latch -> {
        VertxInternal vi = (VertxInternal) vertices[0];
        Promise<Void> promise = vi.getOrCreateContext().promise();
        vi.getClusterManager().leave(promise);
        promise.future().onComplete(onSuccess(v -> {
            latch.countDown();
        }));
    });
}
Also used : Consumer(java.util.function.Consumer) CompositeFuture(io.vertx.core.CompositeFuture) CountDownLatch(java.util.concurrent.CountDownLatch) VertxInternal(io.vertx.core.impl.VertxInternal) Ignore(org.junit.Ignore) ClusterManager(io.vertx.core.spi.cluster.ClusterManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) FakeClusterManager(io.vertx.test.fakecluster.FakeClusterManager) Future(io.vertx.core.Future) VertxInternal(io.vertx.core.impl.VertxInternal) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 34 with VertxInternal

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

the class HostnameResolutionTest method testResolveMissingLocalhost.

@Test
public void testResolveMissingLocalhost() throws Exception {
    InetAddress localhost = InetAddress.getByName("localhost");
    // Set a dns resolver that won't resolve localhost
    dnsServer.testResolveASameServer("127.0.0.1");
    // Test using the resolver API
    VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()).setOptResourceEnabled(false)));
    CompletableFuture<Void> test1 = new CompletableFuture<>();
    vertx.resolveAddress("localhost", ar -> {
        if (ar.succeeded()) {
            InetAddress resolved = ar.result();
            if (resolved.equals(localhost)) {
                test1.complete(null);
            } else {
                test1.completeExceptionally(new AssertionError("Unexpected localhost value " + resolved));
            }
        } else {
            test1.completeExceptionally(ar.cause());
        }
    });
    test1.get(10, TimeUnit.SECONDS);
    CompletableFuture<Void> test2 = new CompletableFuture<>();
    vertx.resolveAddress("LOCALHOST", ar -> {
        if (ar.succeeded()) {
            InetAddress resolved = ar.result();
            if (resolved.equals(localhost)) {
                test2.complete(null);
            } else {
                test2.completeExceptionally(new AssertionError("Unexpected localhost value " + resolved));
            }
        } else {
            test2.completeExceptionally(ar.cause());
        }
    });
    test2.get(10, TimeUnit.SECONDS);
    // Test using bootstrap
    CompletableFuture<Void> test3 = new CompletableFuture<>();
    NetServer server = vertx.createNetServer(new NetServerOptions().setPort(1234).setHost(localhost.getHostAddress()));
    try {
        server.connectHandler(so -> {
            so.end(Buffer.buffer("hello"));
        });
        server.listen(ar -> {
            if (ar.succeeded()) {
                test3.complete(null);
            } else {
                test3.completeExceptionally(ar.cause());
            }
        });
        test3.get(10, TimeUnit.SECONDS);
        CompletableFuture<Void> test4 = new CompletableFuture<>();
        NetClient client = vertx.createNetClient();
        client.connect(1234, "localhost", ar -> {
            if (ar.succeeded()) {
                test4.complete(null);
            } else {
                test4.completeExceptionally(ar.cause());
            }
        });
        test4.get(10, TimeUnit.SECONDS);
    } finally {
        server.close();
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) VertxInternal(io.vertx.core.impl.VertxInternal) NetClient(io.vertx.core.net.NetClient) NetServerOptions(io.vertx.core.net.NetServerOptions) NetServer(io.vertx.core.net.NetServer) InetAddress(java.net.InetAddress) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test)

Example 35 with VertxInternal

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

the class HostnameResolutionTest method testAsyncResolveConnectIsNotifiedOnChannelEventLoop.

@Test
public void testAsyncResolveConnectIsNotifiedOnChannelEventLoop() throws Exception {
    CountDownLatch listenLatch = new CountDownLatch(1);
    NetServer server = vertx.createNetServer().connectHandler(so -> {
    });
    try {
        server.listen(1234, "localhost", onSuccess(v -> listenLatch.countDown()));
        awaitLatch(listenLatch);
        AtomicReference<Thread> channelThread = new AtomicReference<>();
        CountDownLatch connectLatch = new CountDownLatch(1);
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.channelFactory(((VertxInternal) vertx).transport().channelFactory(false));
        bootstrap.group(vertx.nettyEventLoopGroup());
        bootstrap.resolver(((VertxInternal) vertx).nettyAddressResolverGroup());
        bootstrap.handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                channelThread.set(Thread.currentThread());
                connectLatch.countDown();
            }
        });
        ChannelFuture channelFut = bootstrap.connect("localhost", 1234);
        awaitLatch(connectLatch);
        channelFut.addListener(v -> {
            assertTrue(v.isSuccess());
            assertEquals(channelThread.get(), Thread.currentThread());
            testComplete();
        });
        await();
    } finally {
        server.close();
    }
}
Also used : VertxException(io.vertx.core.VertxException) RecordClass(org.apache.directory.server.dns.messages.RecordClass) java.util(java.util) HttpServer(io.vertx.core.http.HttpServer) CompletableFuture(java.util.concurrent.CompletableFuture) VertxTestBase(io.vertx.test.core.VertxTestBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) InetAddress(java.net.InetAddress) TestUtils(io.vertx.test.core.TestUtils) JsonObject(io.vertx.core.json.JsonObject) FakeDNSServer(io.vertx.test.fakedns.FakeDNSServer) NetClient(io.vertx.core.net.NetClient) ResourceRecord(org.apache.directory.server.dns.messages.ResourceRecord) VertxImpl(io.vertx.core.impl.VertxImpl) VertxInternal(io.vertx.core.impl.VertxInternal) ChannelInitializer(io.netty.channel.ChannelInitializer) AddressResolver(io.vertx.core.impl.AddressResolver) DnsAttribute(org.apache.directory.server.dns.store.DnsAttribute) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) File(java.io.File) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Bootstrap(io.netty.bootstrap.Bootstrap) CountDownLatch(java.util.concurrent.CountDownLatch) NetServerOptions(io.vertx.core.net.NetServerOptions) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) HttpMethod(io.vertx.core.http.HttpMethod) RecordType(org.apache.directory.server.dns.messages.RecordType) HttpClient(io.vertx.core.http.HttpClient) ChannelFuture(io.netty.channel.ChannelFuture) VertxInternal(io.vertx.core.impl.VertxInternal) Channel(io.netty.channel.Channel) NetServer(io.vertx.core.net.NetServer) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) VertxException(io.vertx.core.VertxException) UnknownHostException(java.net.UnknownHostException) Bootstrap(io.netty.bootstrap.Bootstrap) Test(org.junit.Test)

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