Search in sources :

Example 41 with VertxInternal

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

the class Http1xTest method testContexts.

@Test
public void testContexts() throws Exception {
    int numReqs = 4;
    Buffer data = randomBuffer(1024);
    AtomicInteger cnt = new AtomicInteger();
    // Server connect handler should always be called with same context
    Context serverCtx = vertx.getOrCreateContext();
    CountDownLatch latch = new CountDownLatch(1);
    List<HttpServerRequest> requests = Collections.synchronizedList(new ArrayList<>());
    Map<String, CompletionStage<Long>> requestResumeMap = new HashMap<>();
    Map<String, CompletionStage<Void>> responseResumeMap = new HashMap<>();
    serverCtx.runOnContext(v1 -> {
        server.requestHandler(req -> {
            req.pause();
            assertSameEventLoop(serverCtx, Vertx.currentContext());
            Buffer body = Buffer.buffer();
            requestResumeMap.get(req.path()).thenAccept(amount -> {
                req.resume();
                req.endHandler(v2 -> {
                    assertSameEventLoop(serverCtx, Vertx.currentContext());
                    assertEquals((long) amount, body.length());
                    requests.add(req);
                    if (requests.size() == numReqs) {
                        requests.forEach(req_ -> {
                            HttpServerResponse resp = req_.response();
                            CompletableFuture<Void> cf = new CompletableFuture<>();
                            responseResumeMap.put(req_.path(), cf);
                            resp.setChunked(true);
                            fill(data, resp, sent -> {
                                cf.complete(null);
                                resp.drainHandler(v -> {
                                    assertSameEventLoop(serverCtx, Vertx.currentContext());
                                    resp.end();
                                });
                            });
                        });
                    }
                });
            });
            req.handler(chunk -> {
                assertSameEventLoop(serverCtx, Vertx.currentContext());
                body.appendBuffer(chunk);
            });
        });
        server.listen(testAddress, onSuccess(s -> {
            assertSameEventLoop(serverCtx, Vertx.currentContext());
            latch.countDown();
        }));
    });
    awaitLatch(latch);
    CountDownLatch latch2 = new CountDownLatch(1);
    int numConns = 4;
    client.close();
    client = null;
    Context clientCtx = vertx.getOrCreateContext();
    clientCtx.runOnContext(v -> {
        client = vertx.createHttpClient(createBaseClientOptions().setMaxPoolSize(numConns));
    });
    waitUntil(() -> client != null);
    // There should be a context per request
    List<EventLoopContext> contexts = Stream.generate(() -> ((VertxInternal) vertx).createEventLoopContext()).limit(4).collect(Collectors.toList());
    Set<Thread> expectedThreads = new HashSet<>();
    for (Context ctx : contexts) {
        CompletableFuture<Thread> th = new CompletableFuture<>();
        ctx.runOnContext(v -> {
            th.complete(Thread.currentThread());
        });
        expectedThreads.add(th.get());
    }
    Set<Thread> threads = new ConcurrentHashSet<>();
    for (int i = 0; i < numReqs; i++) {
        Context requestCtx = contexts.get(i);
        CompletableFuture<Long> cf = new CompletableFuture<>();
        String path = "/" + i;
        requestResumeMap.put(path, cf);
        requestCtx.runOnContext(v -> {
            Thread t = Thread.currentThread();
            client.request(new RequestOptions(requestOptions).setURI(path)).onComplete(onSuccess(req -> {
                assertSame(t, Thread.currentThread());
                req.response(onSuccess(resp -> {
                    assertSameEventLoop(requestCtx, Vertx.currentContext());
                    assertEquals(200, resp.statusCode());
                    threads.add(Thread.currentThread());
                    resp.pause();
                    responseResumeMap.get(path).thenAccept(v2 -> resp.resume());
                    resp.handler(chunk -> {
                        assertSameEventLoop(requestCtx, Vertx.currentContext());
                    });
                    resp.exceptionHandler(this::fail);
                    resp.endHandler(v2 -> {
                        assertSameEventLoop(requestCtx, Vertx.currentContext());
                        if (cnt.incrementAndGet() == numReqs) {
                            assertEquals(expectedThreads, new HashSet<>(threads));
                            latch2.countDown();
                        }
                    });
                })).setChunked(true).exceptionHandler(this::fail);
                req.drainHandler(v2 -> {
                    assertSameEventLoop(requestCtx, Vertx.currentContext());
                    req.end();
                });
                req.sendHead(version -> {
                    assertSameEventLoop(requestCtx, Vertx.currentContext());
                    fill(data, req, cf::complete);
                });
            }));
        });
    }
    awaitLatch(latch2, 40, TimeUnit.SECONDS);
    // Close should be in own context
    new Thread(() -> {
        server.close(onSuccess(v -> {
            ContextInternal closeContext = ((VertxInternal) vertx).getContext();
            assertFalse(contexts.contains(closeContext));
            assertNotSame(serverCtx, closeContext);
            assertFalse(contexts.contains(serverCtx));
            testComplete();
        }));
        server = null;
    }).start();
    await();
}
Also used : HttpServerImpl(io.vertx.core.http.impl.HttpServerImpl) IntStream(java.util.stream.IntStream) java.util(java.util) io.vertx.core(io.vertx.core) ContextInternal(io.vertx.core.impl.ContextInternal) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Http1xOrH2CHandler(io.vertx.core.http.impl.Http1xOrH2CHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) SimpleServer(io.vertx.test.verticles.SimpleServer) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WriteStream(io.vertx.core.streams.WriteStream) BiConsumer(java.util.function.BiConsumer) RecordParser(io.vertx.core.parsetools.RecordParser) JsonObject(io.vertx.core.json.JsonObject) Assume(org.junit.Assume) CheckingSender(io.vertx.test.core.CheckingSender) VertxInternal(io.vertx.core.impl.VertxInternal) Http1xUpgradeToH2CHandler(io.vertx.core.http.impl.Http1xUpgradeToH2CHandler) ConcurrentHashSet(io.vertx.core.impl.ConcurrentHashSet) java.util.concurrent(java.util.concurrent) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) Test(org.junit.Test) Http1xServerConnection(io.vertx.core.http.impl.Http1xServerConnection) EventLoop(io.netty.channel.EventLoop) Future(io.vertx.core.Future) io.vertx.core.net(io.vertx.core.net) Collectors(java.util.stream.Collectors) File(java.io.File) LongConsumer(java.util.function.LongConsumer) Channel(io.netty.channel.Channel) Consumer(java.util.function.Consumer) EventLoopContext(io.vertx.core.impl.EventLoopContext) JsonArray(io.vertx.core.json.JsonArray) Repeat(io.vertx.test.core.Repeat) Stream(java.util.stream.Stream) Buffer(io.vertx.core.buffer.Buffer) HttpUtils(io.vertx.core.http.impl.HttpUtils) Cert(io.vertx.test.tls.Cert) ChannelHandler(io.netty.channel.ChannelHandler) PUT(io.vertx.core.http.HttpMethod.PUT) VertxInternal(io.vertx.core.impl.VertxInternal) ContextInternal(io.vertx.core.impl.ContextInternal) EventLoopContext(io.vertx.core.impl.EventLoopContext) ConcurrentHashSet(io.vertx.core.impl.ConcurrentHashSet) ConcurrentHashSet(io.vertx.core.impl.ConcurrentHashSet) Buffer(io.vertx.core.buffer.Buffer) EventLoopContext(io.vertx.core.impl.EventLoopContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 42 with VertxInternal

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

the class FileResolverTestBase method testCacheDirDeletedOnVertxClose.

@Test
public void testCacheDirDeletedOnVertxClose() {
    VertxInternal vertx2 = (VertxInternal) vertx();
    File file = vertx2.resolveFile("webroot/somefile.html");
    assertTrue(file.exists());
    File cacheDir = file.getParentFile().getParentFile();
    assertTrue(cacheDir.exists());
    vertx2.close(onSuccess(v -> {
        assertFalse(cacheDir.exists());
        testComplete();
    }));
    await();
}
Also used : java.util(java.util) URL(java.net.URL) VertxTestBase(io.vertx.test.core.VertxTestBase) Function(java.util.function.Function) Utils(io.vertx.core.impl.Utils) HttpClientRequest(io.vertx.core.http.HttpClientRequest) PosixFilePermissions(java.nio.file.attribute.PosixFilePermissions) FileResolver(io.vertx.core.spi.file.FileResolver) Assume(org.junit.Assume) HttpClientOptions(io.vertx.core.http.HttpClientOptions) VertxInternal(io.vertx.core.impl.VertxInternal) Files(java.nio.file.Files) Vertx(io.vertx.core.Vertx) StandardOpenOption(java.nio.file.StandardOpenOption) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) CountDownLatch(java.util.concurrent.CountDownLatch) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) FileResolverImpl(io.vertx.core.file.impl.FileResolverImpl) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Assert(org.junit.Assert) VertxInternal(io.vertx.core.impl.VertxInternal) File(java.io.File) Test(org.junit.Test)

Example 43 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) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Test(org.junit.Test) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Cert(io.vertx.test.core.tls.Cert) VertxTestBase(io.vertx.test.core.VertxTestBase) HttpClient(io.vertx.core.http.HttpClient) Trust(io.vertx.test.core.tls.Trust) VertxInternal(io.vertx.core.impl.VertxInternal) Test(org.junit.Test)

Example 44 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);
    try {
        server = vertx.createHttpServer(options);
    } catch (VertxException e) {
        e.printStackTrace();
        if (error == null) {
            fail(e);
        } else {
            assertEquals(error, e.getMessage());
            if (expectCause) {
                assertNotSame(e, e.getCause());
            }
        }
        return;
    }
    server.requestHandler(req -> {
        assertEquals(req.version(), version);
        assertTrue(req.isSSL());
        req.response().end();
    });
    server.listen(onSuccess(s -> {
        HttpServerImpl impl = (HttpServerImpl) s;
        SSLHelper sslHelper = impl.getSslHelper();
        SslContext ctx = sslHelper.getContext((VertxInternal) vertx);
        switch(expectedSslContext) {
            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.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
            assertEquals(200, resp.statusCode());
            testComplete();
        });
    }));
    await();
}
Also used : 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) Cert(io.vertx.test.core.tls.Cert) SSLHelper(io.vertx.core.net.impl.SSLHelper) OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) HttpTestBase(io.vertx.test.core.HttpTestBase) HttpVersion(io.vertx.core.http.HttpVersion) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) JdkSSLEngineOptions(io.vertx.core.net.JdkSSLEngineOptions) SSLHelper(io.vertx.core.net.impl.SSLHelper) VertxInternal(io.vertx.core.impl.VertxInternal) JdkSslContext(io.netty.handler.ssl.JdkSslContext) VertxException(io.vertx.core.VertxException) OpenSslContext(io.netty.handler.ssl.OpenSslContext) HttpServerOptions(io.vertx.core.http.HttpServerOptions) 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)

Example 45 with VertxInternal

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

the class AsyncMultiMapTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    startNodes(1);
    clusterManager = ((VertxInternal) vertices[0]).getClusterManager();
    CountDownLatch latch = new CountDownLatch(1);
    clusterManager.<String, ServerID>getAsyncMultiMap("mymap", onSuccess(res -> {
        map = res;
        latch.countDown();
    }));
    awaitLatch(latch);
}
Also used : HashSet(java.util.HashSet) CountDownLatch(java.util.concurrent.CountDownLatch) VertxInternal(io.vertx.core.impl.VertxInternal) ClusterManager(io.vertx.core.spi.cluster.ClusterManager) Set(java.util.Set) AsyncMultiMap(io.vertx.core.spi.cluster.AsyncMultiMap) Test(org.junit.Test) ServerID(io.vertx.core.net.impl.ServerID) ServerID(io.vertx.core.net.impl.ServerID) CountDownLatch(java.util.concurrent.CountDownLatch)

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