Search in sources :

Example 86 with Handler

use of io.vertx.core.Handler in project vertx-proton by vert-x3.

the class ProtonClientTest method testImmediateInitialCreditWithPrefetchDisabled.

@Test(timeout = 20000)
public void testImmediateInitialCreditWithPrefetchDisabled(TestContext context) {
    Async async = context.async();
    connect(context, connection -> {
        connection.open();
        AtomicInteger counter = new AtomicInteger(0);
        AtomicBoolean creditGranted = new AtomicBoolean();
        ProtonReceiver receiver = connection.createReceiver(MockServer.Addresses.five_messages.toString());
        receiver.handler((d, m) -> {
            int count = counter.incrementAndGet();
            switch(count) {
                // Fall-through
                case 1:
                // Fall-through
                case 2:
                case 3:
                    {
                        validateMessage(context, count, String.valueOf(count), m);
                        break;
                    }
                case 4:
                    {
                        validateMessage(context, count, String.valueOf(count), m);
                        // We only issued 4 credits, so we should not get
                        // more messages until more credit is flowed, use
                        // the callback for this msg to do that
                        vertx.setTimer(1000, x -> {
                            LOG.trace("Flowing more credit");
                            creditGranted.set(true);
                            receiver.flow(1);
                        });
                        // Check that we haven't processed any more messages before then
                        vertx.setTimer(500, x -> {
                            LOG.trace("Checking msg 5 not received yet");
                            context.assertEquals(4, counter.get());
                        });
                        break;
                    }
                case 5:
                    {
                        validateMessage(context, count, String.valueOf(count), m);
                        context.assertTrue(creditGranted.get(), "Additional credit not yet granted, so we" + " should not have received message 5 yet!");
                        // Got the last message, lets finish the test.
                        LOG.trace("Got msg 5, completing async");
                        async.complete();
                        connection.disconnect();
                        break;
                    }
            }
        }).setPrefetch(// Turn off prefetch and related automatic credit handling
        0).flow(// Explicitly grant initial credit of 4. Handler will grant more later.
        4).open();
    });
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Arrays(java.util.Arrays) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LoggerFactory(io.vertx.core.logging.LoggerFactory) LinkedHashMap(java.util.LinkedHashMap) Symbol(org.apache.qpid.proton.amqp.Symbol) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProtonHelper.message(io.vertx.proton.ProtonHelper.message) Target(org.apache.qpid.proton.amqp.transport.Target) Map(java.util.Map) UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) Message(org.apache.qpid.proton.message.Message) AsyncResult(io.vertx.core.AsyncResult) AmqpError(org.apache.qpid.proton.amqp.transport.AmqpError) Logger(io.vertx.core.logging.Logger) ProtonServerImpl(io.vertx.proton.impl.ProtonServerImpl) ProtonMetaDataSupportImpl(io.vertx.proton.impl.ProtonMetaDataSupportImpl) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) Proton(org.apache.qpid.proton.Proton) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Source(org.apache.qpid.proton.amqp.messaging.Source) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Section(org.apache.qpid.proton.amqp.messaging.Section) NetServer(io.vertx.core.net.NetServer) ProtonConnectionImpl(io.vertx.proton.impl.ProtonConnectionImpl) Handler(io.vertx.core.Handler) Collections(java.util.Collections) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 87 with Handler

use of io.vertx.core.Handler in project vertx-proton by vert-x3.

the class ProtonClientTest method testDelayedInitialCreditWithPrefetchDisabled.

@Test(timeout = 20000)
public void testDelayedInitialCreditWithPrefetchDisabled(TestContext context) {
    Async async = context.async();
    connect(context, connection -> {
        connection.open();
        AtomicInteger counter = new AtomicInteger(0);
        AtomicBoolean initialCreditGranted = new AtomicBoolean();
        AtomicBoolean additionalCreditGranted = new AtomicBoolean();
        final int delay = 250;
        final long startTime = System.currentTimeMillis();
        // Create receiver with prefetch disabled
        ProtonReceiver receiver = connection.createReceiver(MockServer.Addresses.two_messages.toString());
        receiver.handler((d, m) -> {
            int count = counter.incrementAndGet();
            switch(count) {
                case 1:
                    {
                        validateMessage(context, count, String.valueOf(count), m);
                        context.assertTrue(initialCreditGranted.get(), "Initial credit not yet granted, so we" + " should not have received message 1 yet!");
                        // Verify lack of initial credit results in delayed receipt of first message.
                        context.assertTrue(System.currentTimeMillis() > startTime + delay, "Message received before expected time delay elapsed!");
                        LOG.trace("Got msg 1");
                        // We only issued 1 credit, so we should not get more
                        // messages until more credit is flowed, use the
                        // callback for this msg to do that after further delay
                        vertx.setTimer(delay, x -> {
                            LOG.trace("Granting additional credit");
                            additionalCreditGranted.set(true);
                            receiver.flow(1);
                        });
                        break;
                    }
                case 2:
                    {
                        validateMessage(context, count, String.valueOf(count), m);
                        context.assertTrue(additionalCreditGranted.get(), "Additional credit not yet granted, so we" + " should not have received message " + count + " yet!");
                        context.assertTrue(System.currentTimeMillis() > startTime + (delay * 2), "Message received before expected time delay elapsed!");
                        // Got the last message, lets finish the test.
                        LOG.trace("Got msg 2, completing async");
                        async.complete();
                        connection.disconnect();
                        break;
                    }
            }
        }).setPrefetch(// Turn off automatic prefetch / credit handling
        0).open();
        // Explicitly grant an initial credit after a delay. Handler will then grant more.
        vertx.setTimer(delay, x -> {
            LOG.trace("Flowing initial credit");
            initialCreditGranted.set(true);
            receiver.flow(1);
        });
    });
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Arrays(java.util.Arrays) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LoggerFactory(io.vertx.core.logging.LoggerFactory) LinkedHashMap(java.util.LinkedHashMap) Symbol(org.apache.qpid.proton.amqp.Symbol) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProtonHelper.message(io.vertx.proton.ProtonHelper.message) Target(org.apache.qpid.proton.amqp.transport.Target) Map(java.util.Map) UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) Message(org.apache.qpid.proton.message.Message) AsyncResult(io.vertx.core.AsyncResult) AmqpError(org.apache.qpid.proton.amqp.transport.AmqpError) Logger(io.vertx.core.logging.Logger) ProtonServerImpl(io.vertx.proton.impl.ProtonServerImpl) ProtonMetaDataSupportImpl(io.vertx.proton.impl.ProtonMetaDataSupportImpl) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) Proton(org.apache.qpid.proton.Proton) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Source(org.apache.qpid.proton.amqp.messaging.Source) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Section(org.apache.qpid.proton.amqp.messaging.Section) NetServer(io.vertx.core.net.NetServer) ProtonConnectionImpl(io.vertx.proton.impl.ProtonConnectionImpl) Handler(io.vertx.core.Handler) Collections(java.util.Collections) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 88 with Handler

use of io.vertx.core.Handler in project vertx-proton by vert-x3.

the class ProtonClientTest method remoteCloseDefaultSessionTestImpl.

private void remoteCloseDefaultSessionTestImpl(TestContext context, boolean sessionError) throws InterruptedException, ExecutionException {
    server.close();
    Async async = context.async();
    ProtonServer protonServer = null;
    try {
        protonServer = createServer(serverConnection -> {
            Future<ProtonSession> sessionFuture = Future.<ProtonSession>future();
            // Expect a session to open, when the sender is created by the client
            serverConnection.sessionOpenHandler(serverSession -> {
                LOG.trace("Server session open");
                serverSession.open();
                sessionFuture.complete(serverSession);
            });
            // Expect a receiver link, then close the session after opening it.
            serverConnection.receiverOpenHandler(serverReceiver -> {
                LOG.trace("Server receiver open");
                serverReceiver.open();
                context.assertTrue(sessionFuture.succeeded(), "Session future not [yet] succeeded");
                LOG.trace("Server session close");
                ProtonSession s = sessionFuture.result();
                if (sessionError) {
                    ErrorCondition error = new ErrorCondition();
                    error.setCondition(AmqpError.INTERNAL_ERROR);
                    error.setDescription("error description");
                    s.setCondition(error);
                }
                s.close();
            });
            serverConnection.openHandler(result -> {
                LOG.trace("Server connection open");
                serverConnection.open();
            });
        });
        // ===== Client Handling =====
        ProtonClient client = ProtonClient.create(vertx);
        client.connect("localhost", protonServer.actualPort(), res -> {
            context.assertTrue(res.succeeded());
            ProtonConnection connection = res.result();
            connection.openHandler(x -> {
                context.assertTrue(x.succeeded(), "Connection open failed");
                LOG.trace("Client connection opened");
                // Create a sender to provoke creation (and subsequent
                // closure of by the server) the connections default session
                connection.createSender(null).open();
            });
            connection.closeHandler(x -> {
                LOG.trace("Connection close handler called (as espected): " + x.cause());
                async.complete();
            });
            connection.open();
        });
        async.awaitSuccess();
    } finally {
        if (protonServer != null) {
            protonServer.close();
        }
    }
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Arrays(java.util.Arrays) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LoggerFactory(io.vertx.core.logging.LoggerFactory) LinkedHashMap(java.util.LinkedHashMap) Symbol(org.apache.qpid.proton.amqp.Symbol) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProtonHelper.message(io.vertx.proton.ProtonHelper.message) Target(org.apache.qpid.proton.amqp.transport.Target) Map(java.util.Map) UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) Message(org.apache.qpid.proton.message.Message) AsyncResult(io.vertx.core.AsyncResult) AmqpError(org.apache.qpid.proton.amqp.transport.AmqpError) Logger(io.vertx.core.logging.Logger) ProtonServerImpl(io.vertx.proton.impl.ProtonServerImpl) ProtonMetaDataSupportImpl(io.vertx.proton.impl.ProtonMetaDataSupportImpl) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) Proton(org.apache.qpid.proton.Proton) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Source(org.apache.qpid.proton.amqp.messaging.Source) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Section(org.apache.qpid.proton.amqp.messaging.Section) NetServer(io.vertx.core.net.NetServer) ProtonConnectionImpl(io.vertx.proton.impl.ProtonConnectionImpl) Handler(io.vertx.core.Handler) Collections(java.util.Collections) Async(io.vertx.ext.unit.Async) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Future(io.vertx.core.Future)

Example 89 with Handler

use of io.vertx.core.Handler in project vertx-proton by vert-x3.

the class ProtonClientTest method doDetachHandlingTestImpl.

public void doDetachHandlingTestImpl(TestContext context, boolean clientSender) throws Exception {
    server.close();
    final Async clientLinkOpenAsync = context.async();
    final Async serverLinkOpenAsync = context.async();
    final Async serverLinkDetachAsync = context.async();
    final Async clientLinkDetachAsync = context.async();
    final AtomicBoolean serverLinkCloseHandlerFired = new AtomicBoolean();
    final AtomicBoolean clientLinkCloseHandlerFired = new AtomicBoolean();
    ProtonServer protonServer = null;
    try {
        protonServer = createServer((serverConnection) -> {
            serverConnection.openHandler(result -> {
                serverConnection.open();
            });
            serverConnection.sessionOpenHandler(session -> session.open());
            if (clientSender) {
                serverConnection.receiverOpenHandler(serverReceiver -> {
                    LOG.trace("Server receiver opened");
                    serverReceiver.open();
                    serverLinkOpenAsync.complete();
                    serverReceiver.closeHandler(res -> {
                        serverLinkCloseHandlerFired.set(true);
                    });
                    serverReceiver.detachHandler(res -> {
                        context.assertTrue(res.succeeded(), "expected non-errored async result");
                        serverReceiver.detach();
                        serverLinkDetachAsync.complete();
                    });
                });
            } else {
                serverConnection.senderOpenHandler(serverSender -> {
                    LOG.trace("Server sender opened");
                    serverSender.open();
                    serverLinkOpenAsync.complete();
                    serverSender.closeHandler(res -> {
                        serverLinkCloseHandlerFired.set(true);
                    });
                    serverSender.detachHandler(res -> {
                        context.assertTrue(res.succeeded(), "expected non-errored async result");
                        serverSender.detach();
                        serverLinkDetachAsync.complete();
                    });
                });
            }
        });
        // ===== Client Handling =====
        ProtonClient client = ProtonClient.create(vertx);
        client.connect("localhost", protonServer.actualPort(), res -> {
            context.assertTrue(res.succeeded());
            ProtonConnection connection = res.result();
            connection.openHandler(x -> {
                LOG.trace("Client connection opened");
                final ProtonLink<?> link;
                if (clientSender) {
                    link = connection.createSender(null);
                } else {
                    link = connection.createReceiver("some-address");
                }
                link.closeHandler(clientLink -> {
                    clientLinkCloseHandlerFired.set(true);
                });
                link.detachHandler(clientLink -> {
                    LOG.trace("Client link detached");
                    clientLinkDetachAsync.complete();
                });
                link.openHandler(y -> {
                    LOG.trace("Client link opened");
                    clientLinkOpenAsync.complete();
                    link.detach();
                });
                link.open();
            }).open();
        });
        serverLinkOpenAsync.awaitSuccess();
        clientLinkOpenAsync.awaitSuccess();
        serverLinkDetachAsync.awaitSuccess();
        clientLinkDetachAsync.awaitSuccess();
    } finally {
        if (protonServer != null) {
            protonServer.close();
        }
    }
    context.assertFalse(serverLinkCloseHandlerFired.get(), "server link close handler should not have fired");
    context.assertFalse(clientLinkCloseHandlerFired.get(), "client link close handler should not have fired");
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Arrays(java.util.Arrays) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LoggerFactory(io.vertx.core.logging.LoggerFactory) LinkedHashMap(java.util.LinkedHashMap) Symbol(org.apache.qpid.proton.amqp.Symbol) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProtonHelper.message(io.vertx.proton.ProtonHelper.message) Target(org.apache.qpid.proton.amqp.transport.Target) Map(java.util.Map) UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) Message(org.apache.qpid.proton.message.Message) AsyncResult(io.vertx.core.AsyncResult) AmqpError(org.apache.qpid.proton.amqp.transport.AmqpError) Logger(io.vertx.core.logging.Logger) ProtonServerImpl(io.vertx.proton.impl.ProtonServerImpl) ProtonMetaDataSupportImpl(io.vertx.proton.impl.ProtonMetaDataSupportImpl) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) Proton(org.apache.qpid.proton.Proton) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Source(org.apache.qpid.proton.amqp.messaging.Source) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Section(org.apache.qpid.proton.amqp.messaging.Section) NetServer(io.vertx.core.net.NetServer) ProtonConnectionImpl(io.vertx.proton.impl.ProtonConnectionImpl) Handler(io.vertx.core.Handler) Collections(java.util.Collections) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Async(io.vertx.ext.unit.Async)

Example 90 with Handler

use of io.vertx.core.Handler in project vertx-proton by vert-x3.

the class ProtonServerImplTest method testCustomAuthenticatorFailsAuthentication.

@Test(timeout = 20000)
public void testCustomAuthenticatorFailsAuthentication(TestContext context) {
    Async connectedAsync = context.async();
    ProtonServer.create(vertx).saslAuthenticatorFactory(new TestPlainAuthenticatorFactory()).connectHandler(protonConnection -> {
        context.fail("Handler should not be called for connection that failed authentication");
    }).listen(server -> ProtonClient.create(vertx).connect("localhost", server.result().actualPort(), BAD_USER, PASSWD, protonConnectionAsyncResult -> {
        context.assertFalse(protonConnectionAsyncResult.succeeded());
        connectedAsync.complete();
    }));
    connectedAsync.awaitSuccess();
}
Also used : TestContext(io.vertx.ext.unit.TestContext) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) ProtonSaslAuthenticator(io.vertx.proton.sasl.ProtonSaslAuthenticator) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonClient(io.vertx.proton.ProtonClient) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Sasl(org.apache.qpid.proton.engine.Sasl) SaslOutcome(org.apache.qpid.proton.engine.Sasl.SaslOutcome) Context(io.vertx.core.Context) ProtonServer(io.vertx.proton.ProtonServer) StandardCharsets(java.nio.charset.StandardCharsets) Transport(org.apache.qpid.proton.engine.Transport) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Handler(io.vertx.core.Handler) ProtonSaslAuthenticatorFactory(io.vertx.proton.sasl.ProtonSaslAuthenticatorFactory) NetSocket(io.vertx.core.net.NetSocket) Before(org.junit.Before) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Aggregations

Handler (io.vertx.core.Handler)119 Test (org.junit.Test)78 Future (io.vertx.core.Future)67 Vertx (io.vertx.core.Vertx)59 AsyncResult (io.vertx.core.AsyncResult)57 Context (io.vertx.core.Context)52 Buffer (io.vertx.core.buffer.Buffer)48 Async (io.vertx.ext.unit.Async)41 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)39 Map (java.util.Map)38 TestContext (io.vertx.ext.unit.TestContext)37 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)36 RunWith (org.junit.runner.RunWith)36 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)34 Collections (java.util.Collections)33 JsonObject (io.vertx.core.json.JsonObject)31 HttpURLConnection (java.net.HttpURLConnection)31 Before (org.junit.Before)31 StandardCharsets (java.nio.charset.StandardCharsets)29 TimeUnit (java.util.concurrent.TimeUnit)29