Search in sources :

Example 11 with Promise

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

the class WriteHandlerLookupFailureTest method test.

@Test
public void test() {
    Throwable cause = new Throwable();
    VertxOptions options = new VertxOptions();
    options.getEventBusOptions().setHost("localhost").setPort(0);
    NodeSelector nodeSelector = new DefaultNodeSelector() {

        @Override
        public void selectForSend(Message<?> message, Promise<String> promise) {
            promise.fail(cause);
        }

        @Override
        public void selectForPublish(Message<?> message, Promise<Iterable<String>> promise) {
            promise.fail("Not implemented");
        }
    };
    new VertxBuilder(options).init().clusterNodeSelector(nodeSelector).clusteredVertx(onSuccess(node -> {
        vertx = node;
        MessageProducer<String> sender = vertx.eventBus().sender("foo");
        sender.write("the_string", onFailure(err -> {
            assertSame(cause, err);
            testComplete();
        }));
    }));
    await();
}
Also used : NodeSelector(io.vertx.core.spi.cluster.NodeSelector) DefaultNodeSelector(io.vertx.core.spi.cluster.impl.DefaultNodeSelector) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) VertxOptions(io.vertx.core.VertxOptions) VertxBuilder(io.vertx.core.impl.VertxBuilder) Test(org.junit.Test) VertxTestBase(io.vertx.test.core.VertxTestBase) Collections(java.util.Collections) Promise(io.vertx.core.Promise) DefaultNodeSelector(io.vertx.core.spi.cluster.impl.DefaultNodeSelector) NodeSelector(io.vertx.core.spi.cluster.NodeSelector) DefaultNodeSelector(io.vertx.core.spi.cluster.impl.DefaultNodeSelector) VertxBuilder(io.vertx.core.impl.VertxBuilder) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test)

Example 12 with Promise

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

the class TracerTest method testWorkerExecutor.

@Test
public void testWorkerExecutor() {
    WorkerExecutor exec = vertx.createSharedWorkerExecutor("exec");
    ContextInternal ctx = (ContextInternal) vertx.getOrCreateContext();
    ContextInternal duplicate = ctx.duplicate();
    duplicate.runOnContext(v -> {
        exec.executeBlocking(Promise::complete, onSuccess(res -> {
            testComplete();
        }));
    });
    await();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) Promise(io.vertx.core.Promise) ContextInternal(io.vertx.core.impl.ContextInternal) Test(org.junit.Test) FakeTracer(io.vertx.test.faketracer.FakeTracer) Context(io.vertx.core.Context) VertxTestBase(io.vertx.test.core.VertxTestBase) Collections(java.util.Collections) WorkerExecutor(io.vertx.core.WorkerExecutor) Promise(io.vertx.core.Promise) WorkerExecutor(io.vertx.core.WorkerExecutor) ContextInternal(io.vertx.core.impl.ContextInternal) Test(org.junit.Test)

Example 13 with Promise

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

the class AsyncFileImpl method doFlush.

private synchronized void doFlush(Handler<AsyncResult<Void>> handler) {
    checkClosed();
    context.executeBlockingInternal((Promise<Void> fut) -> {
        try {
            ch.force(false);
            fut.complete();
        } catch (IOException e) {
            throw new FileSystemException(e);
        }
    }, handler);
}
Also used : Promise(io.vertx.core.Promise) FileSystemException(io.vertx.core.file.FileSystemException) IOException(java.io.IOException)

Example 14 with Promise

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

the class Http1xServerConnection method netSocket.

void netSocket(Promise<NetSocket> promise) {
    context.execute(() -> {
        // Flush out all pending data
        flush();
        // remove old http handlers and replace the old handler with one that handle plain sockets
        ChannelPipeline pipeline = chctx.pipeline();
        ChannelHandler compressor = pipeline.get(HttpChunkContentCompressor.class);
        if (compressor != null) {
            pipeline.remove(compressor);
        }
        pipeline.remove("httpDecoder");
        if (pipeline.get("chunkedWriter") != null) {
            pipeline.remove("chunkedWriter");
        }
        pipeline.replace("handler", "handler", VertxHandler.create(ctx -> {
            NetSocketImpl socket = new NetSocketImpl(context, ctx, sslHelper, metrics) {

                @Override
                protected void handleClosed() {
                    if (metrics != null) {
                        Http1xServerRequest request = Http1xServerConnection.this.responseInProgress;
                        metrics.responseEnd(request.metric(), request.response(), request.response().bytesWritten());
                    }
                    super.handleClosed();
                }

                @Override
                public synchronized void handleMessage(Object msg) {
                    if (msg instanceof HttpContent) {
                        ReferenceCountUtil.release(msg);
                        return;
                    }
                    super.handleMessage(msg);
                }
            };
            socket.metric(metric());
            return socket;
        }));
        // check if the encoder can be removed yet or not.
        pipeline.remove("httpEncoder");
        // 
        VertxHandler<NetSocketImpl> handler = (VertxHandler<NetSocketImpl>) pipeline.get("handler");
        promise.complete(handler.getConnection());
    });
}
Also used : NetSocketImpl(io.vertx.core.net.impl.NetSocketImpl) HttpServerRequest(io.vertx.core.http.HttpServerRequest) ServerWebSocket(io.vertx.core.http.ServerWebSocket) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) ContextInternal(io.vertx.core.impl.ContextInternal) Supplier(java.util.function.Supplier) Unpooled(io.netty.buffer.Unpooled) BAD_REQUEST(io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST) HttpServerMetrics(io.vertx.core.spi.metrics.HttpServerMetrics) METHOD_NOT_ALLOWED(io.netty.handler.codec.http.HttpResponseStatus.METHOD_NOT_ALLOWED) io.netty.channel(io.netty.channel) HTTP_1_1(io.netty.handler.codec.http.HttpVersion.HTTP_1_1) AsyncResult(io.vertx.core.AsyncResult) TracingPolicy(io.vertx.core.tracing.TracingPolicy) Logger(io.vertx.core.impl.logging.Logger) VertxHandler(io.vertx.core.net.impl.VertxHandler) CONTINUE(io.netty.handler.codec.http.HttpResponseStatus.CONTINUE) PromiseInternal(io.vertx.core.impl.future.PromiseInternal) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) VertxTracer(io.vertx.core.spi.tracing.VertxTracer) Future(io.vertx.core.Future) SSLHelper(io.vertx.core.net.impl.SSLHelper) UPGRADE_REQUIRED(io.netty.handler.codec.http.HttpResponseStatus.UPGRADE_REQUIRED) DecoderResult(io.netty.handler.codec.DecoderResult) io.netty.handler.codec.http(io.netty.handler.codec.http) Buffer(io.vertx.core.buffer.Buffer) io.netty.handler.codec.http.websocketx(io.netty.handler.codec.http.websocketx) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) HttpServerOptions(io.vertx.core.http.HttpServerOptions) METRICS_ENABLED(io.vertx.core.spi.metrics.Metrics.METRICS_ENABLED) Handler(io.vertx.core.Handler) NetSocket(io.vertx.core.net.NetSocket) VertxHandler(io.vertx.core.net.impl.VertxHandler) NetSocketImpl(io.vertx.core.net.impl.NetSocketImpl)

Example 15 with Promise

use of io.vertx.core.Promise 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)

Aggregations

Promise (io.vertx.core.Promise)155 Future (io.vertx.core.Future)122 Handler (io.vertx.core.Handler)95 List (java.util.List)86 Vertx (io.vertx.core.Vertx)85 Buffer (io.vertx.core.buffer.Buffer)83 TimeUnit (java.util.concurrent.TimeUnit)79 HttpURLConnection (java.net.HttpURLConnection)66 Logger (org.slf4j.Logger)63 LoggerFactory (org.slf4j.LoggerFactory)63 Optional (java.util.Optional)62 AsyncResult (io.vertx.core.AsyncResult)61 Truth.assertThat (com.google.common.truth.Truth.assertThat)60 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)60 VertxTestContext (io.vertx.junit5.VertxTestContext)59 Test (org.junit.jupiter.api.Test)58 Map (java.util.Map)54 UUID (java.util.UUID)52 ArrayList (java.util.ArrayList)51 JsonObject (io.vertx.core.json.JsonObject)50