Search in sources :

Example 96 with VertxInternal

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

the class HttpNetSocket method sendFile.

@Override
public NetSocket sendFile(String filename, long offset, long length, Handler<AsyncResult<Void>> resultHandler) {
    VertxInternal vertx = conn.getContext().owner();
    Handler<AsyncResult<Void>> h;
    if (resultHandler != null) {
        Context resultCtx = vertx.getOrCreateContext();
        h = ar -> {
            resultCtx.runOnContext((v) -> {
                resultHandler.handle(ar);
            });
        };
    } else {
        h = ar -> {
        };
    }
    HttpUtils.resolveFile(vertx, filename, offset, length, ar -> {
        if (ar.succeeded()) {
            AsyncFile file = ar.result();
            file.pipe().endOnComplete(false).to(this, ar1 -> file.close(ar2 -> {
                Throwable failure = ar1.failed() ? ar1.cause() : ar2.failed() ? ar2.cause() : null;
                if (failure == null)
                    h.handle(ar1);
                else
                    h.handle(Future.failedFuture(failure));
            }));
        } else {
            h.handle(ar.mapEmpty());
        }
    });
    return this;
}
Also used : Context(io.vertx.core.Context) VertxException(io.vertx.core.VertxException) AsyncFile(io.vertx.core.file.AsyncFile) VertxInternal(io.vertx.core.impl.VertxInternal) ClosedChannelException(java.nio.channels.ClosedChannelException) Promise(io.vertx.core.Promise) ContextInternal(io.vertx.core.impl.ContextInternal) ConnectionBase(io.vertx.core.net.impl.ConnectionBase) X509Certificate(javax.security.cert.X509Certificate) Context(io.vertx.core.Context) Future(io.vertx.core.Future) Nullable(io.vertx.codegen.annotations.Nullable) List(java.util.List) Pipe(io.vertx.core.streams.Pipe) SSLSession(javax.net.ssl.SSLSession) Certificate(java.security.cert.Certificate) Buffer(io.vertx.core.buffer.Buffer) WriteStream(io.vertx.core.streams.WriteStream) ReadStream(io.vertx.core.streams.ReadStream) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) StreamResetException(io.vertx.core.http.StreamResetException) NetSocket(io.vertx.core.net.NetSocket) SocketAddress(io.vertx.core.net.SocketAddress) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) VertxInternal(io.vertx.core.impl.VertxInternal) AsyncFile(io.vertx.core.file.AsyncFile) AsyncResult(io.vertx.core.AsyncResult)

Example 97 with VertxInternal

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

the class NodeInfoTest method testFailedFutureForUnknownNode.

@Test
public void testFailedFutureForUnknownNode() {
    startNodes(2);
    ClusterManager clusterManager = ((VertxInternal) vertices[0]).getClusterManager();
    // Create unknown node identifier
    String unknown = String.join("", clusterManager.getNodes());
    // Needed as callback might be done from non Vert.x thread
    disableThreadChecks();
    Promise<NodeInfo> promise = Promise.promise();
    clusterManager.getNodeInfo(unknown, promise);
    promise.future().onComplete(onFailure(t -> testComplete()));
    await();
}
Also used : VertxInternal(io.vertx.core.impl.VertxInternal) ClusterManager(io.vertx.core.spi.cluster.ClusterManager) Promise(io.vertx.core.Promise) Test(org.junit.Test) NodeInfo(io.vertx.core.spi.cluster.NodeInfo) FakeClusterManager(io.vertx.test.fakecluster.FakeClusterManager) VertxTestBase(io.vertx.test.core.VertxTestBase) VertxInternal(io.vertx.core.impl.VertxInternal) NodeInfo(io.vertx.core.spi.cluster.NodeInfo) ClusterManager(io.vertx.core.spi.cluster.ClusterManager) FakeClusterManager(io.vertx.test.fakecluster.FakeClusterManager) Test(org.junit.Test)

Example 98 with VertxInternal

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

the class FileResolverTestBase method testCaching.

private void testCaching(boolean enabled) throws Exception {
    VertxInternal vertx = (VertxInternal) Vertx.vertx(new VertxOptions().setFileSystemOptions(new FileSystemOptions().setFileCachingEnabled(enabled)));
    File tmp = File.createTempFile("vertx", ".bin");
    tmp.deleteOnExit();
    URL url = tmp.toURI().toURL();
    Files.write(tmp.toPath(), "foo".getBytes());
    ClassLoader old = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(new ClassLoader() {

            @Override
            public URL getResource(String name) {
                if ("foo".equals(name)) {
                    return url;
                }
                return super.getResource(name);
            }
        });
        File f = vertx.resolveFile("foo");
        assertEquals("foo", new String(Files.readAllBytes(f.toPath())));
        Files.write(tmp.toPath(), "bar".getBytes());
        f = vertx.resolveFile("foo");
        if (enabled) {
            assertEquals("foo", new String(Files.readAllBytes(f.toPath())));
        } else {
            assertEquals("bar", new String(Files.readAllBytes(f.toPath())));
        }
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
}
Also used : VertxInternal(io.vertx.core.impl.VertxInternal) VertxOptions(io.vertx.core.VertxOptions) File(java.io.File) URL(java.net.URL)

Example 99 with VertxInternal

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

the class FileResolverTestBase method testResolveFileFromClasspathDisableCaching.

@Test
public void testResolveFileFromClasspathDisableCaching() throws Exception {
    VertxInternal vertx = (VertxInternal) Vertx.vertx(new VertxOptions().setFileSystemOptions(new FileSystemOptions().setFileCachingEnabled(true)));
    try {
        for (int i = 0; i < 2; i++) {
            File file = vertx.resolveFile("afile.html");
            assertTrue(file.exists());
            assertTrue(file.getPath().startsWith(cacheBaseDir + "-"));
            assertFalse(file.isDirectory());
            assertEquals("<html><body>afile</body></html>", readFile(file));
        }
    } finally {
        vertx.close();
    }
}
Also used : VertxInternal(io.vertx.core.impl.VertxInternal) VertxOptions(io.vertx.core.VertxOptions) File(java.io.File) Test(org.junit.Test)

Example 100 with VertxInternal

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

the class MetricsTest method testThreadPoolMetricsWithInternalExecuteBlocking.

@Test
public void testThreadPoolMetricsWithInternalExecuteBlocking() {
    Map<String, PoolMetrics> all = FakePoolMetrics.getPoolMetrics();
    FakePoolMetrics metrics = (FakePoolMetrics) all.get("vert.x-internal-blocking");
    assertThat(metrics.getPoolSize(), is(getOptions().getInternalBlockingPoolSize()));
    assertThat(metrics.numberOfIdleThreads(), is(getOptions().getInternalBlockingPoolSize()));
    int num = VertxOptions.DEFAULT_INTERNAL_BLOCKING_POOL_SIZE;
    int count = num * 5;
    AtomicBoolean hadWaitingQueue = new AtomicBoolean();
    AtomicBoolean hadIdle = new AtomicBoolean();
    AtomicBoolean hadRunning = new AtomicBoolean();
    VertxInternal v = (VertxInternal) vertx;
    Map<Integer, CountDownLatch> latches = new HashMap<>();
    for (int i = 0; i < count; i++) {
        CountDownLatch latch = latches.computeIfAbsent(i / num, k -> new CountDownLatch(num));
        v.executeBlockingInternal(fut -> {
            latch.countDown();
            try {
                awaitLatch(latch);
            } catch (InterruptedException e) {
                fail(e);
                Thread.currentThread().interrupt();
            }
            if (metrics.numberOfRunningTasks() > 0) {
                hadRunning.set(true);
            }
            if (metrics.numberOfWaitingTasks() > 0) {
                hadWaitingQueue.set(true);
            }
            fut.complete();
        }, false, ar -> {
            if (metrics.numberOfIdleThreads() > 0) {
                hadIdle.set(true);
            }
        });
    }
    assertWaitUntil(() -> metrics.numberOfSubmittedTask() == 100);
    assertWaitUntil(() -> metrics.numberOfCompletedTasks() == 100);
    assertTrue(hadIdle.get());
    assertTrue(hadWaitingQueue.get());
    assertTrue(hadRunning.get());
    assertEquals(metrics.numberOfIdleThreads(), getOptions().getWorkerPoolSize());
    assertEquals(metrics.numberOfRunningTasks(), 0);
    assertEquals(metrics.numberOfWaitingTasks(), 0);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VertxInternal(io.vertx.core.impl.VertxInternal) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

VertxInternal (io.vertx.core.impl.VertxInternal)102 Test (org.junit.Test)73 CountDownLatch (java.util.concurrent.CountDownLatch)46 VertxOptions (io.vertx.core.VertxOptions)30 Buffer (io.vertx.core.buffer.Buffer)29 JsonObject (io.vertx.core.json.JsonObject)29 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