Search in sources :

Example 56 with ProtonConnection

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

the class ProtonSubscriberIntTest method testAlternativeDispositionStates.

@Test(timeout = 20000)
public void testAlternativeDispositionStates(TestContext context) throws Exception {
    server.close();
    final Async serverLinkOpenAsync = context.async();
    final Async serverReceivedMessageAsync = context.async();
    final Async serverLinkCloseAsync = context.async();
    int messageCount = 4;
    ProtonServer protonServer = null;
    try {
        protonServer = createServer((serverConnection) -> {
            serverConnection.openHandler(result -> {
                serverConnection.open();
            });
            serverConnection.sessionOpenHandler(session -> session.open());
            AtomicInteger counter = new AtomicInteger(0);
            serverConnection.receiverOpenHandler(serverReceiver -> {
                LOG.trace("Server receiver opened");
                serverReceiver.setAutoAccept(false);
                serverReceiver.handler((delivery, msg) -> {
                    int msgNum = counter.incrementAndGet();
                    // We got the message that was sent, validate it
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Server got msg: " + getMessageBody(context, msg));
                    }
                    validateMessage(context, msgNum, String.valueOf(msgNum), msg);
                    switch(msgNum) {
                        case 1:
                            {
                                delivery.disposition(Accepted.getInstance(), true);
                                break;
                            }
                        case 2:
                            {
                                delivery.disposition(Released.getInstance(), true);
                                break;
                            }
                        case 3:
                            {
                                delivery.disposition(new Rejected(), true);
                                break;
                            }
                        case 4:
                            {
                                delivery.disposition(new Modified(), true);
                                break;
                            }
                    }
                    if (msgNum == messageCount) {
                        // Received all the messages
                        serverReceivedMessageAsync.complete();
                    }
                });
                serverReceiver.closeHandler(x -> {
                    serverReceiver.close();
                    serverLinkCloseAsync.complete();
                });
                // Set the local terminus details [naively]
                serverReceiver.setTarget(serverReceiver.getRemoteTarget().copy());
                serverReceiver.open();
                serverLinkOpenAsync.complete();
            });
        });
        // ===== Client Handling =====
        ProtonClient client = ProtonClient.create(vertx);
        client.connect("localhost", protonServer.actualPort(), res -> {
            context.assertTrue(res.succeeded());
            ProtonConnection connection = res.result();
            connection.open();
            ProtonSubscriber<Tracker> subscriber = ProtonStreams.createTrackerProducer(connection, "myAddress");
            Tracker tracker1 = Tracker.create(message("1"), t -> {
                context.assertTrue(t.isAccepted(), "msg should be accepted");
                context.assertTrue(t.isRemotelySettled(), "msg should be remotely settled");
            });
            Tracker tracker2 = Tracker.create(message("2"), t -> {
                context.assertFalse(t.isAccepted(), "msg should not be accepted");
                context.assertTrue(t.getRemoteState() instanceof Released, "unexpected remote state: " + t.getRemoteState());
                context.assertTrue(t.isRemotelySettled(), "msg should be remotely settled");
            });
            Tracker tracker3 = Tracker.create(message("3"), t -> {
                context.assertFalse(t.isAccepted(), "msg should not be accepted");
                context.assertTrue(t.getRemoteState() instanceof Rejected, "unexpected remote state: " + t.getRemoteState());
                context.assertTrue(t.isRemotelySettled(), "msg should be remotely settled");
            });
            Tracker tracker4 = Tracker.create(message("4"), t -> {
                context.assertFalse(t.isAccepted(), "msg should not be accepted");
                context.assertTrue(t.getRemoteState() instanceof Modified, "unexpected remote state: " + t.getRemoteState());
                context.assertTrue(t.isRemotelySettled(), "msg should be remotely settled");
            });
            Publisher<Tracker> producer = Flowable.just(tracker1, tracker2, tracker3, tracker4);
            producer.subscribe(subscriber);
        });
        serverLinkOpenAsync.awaitSuccess();
        serverReceivedMessageAsync.awaitSuccess();
        serverLinkCloseAsync.awaitSuccess();
    } finally {
        if (protonServer != null) {
            protonServer.close();
        }
    }
}
Also used : TestContext(io.vertx.ext.unit.TestContext) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) Arrays(java.util.Arrays) ProtonStreams(io.vertx.proton.streams.ProtonStreams) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) RunWith(org.junit.runner.RunWith) ProtonServer(io.vertx.proton.ProtonServer) MockServerTestBase(io.vertx.proton.MockServerTestBase) Flowable(io.reactivex.Flowable) ProtonHelper.message(io.vertx.proton.ProtonHelper.message) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Symbol(org.apache.qpid.proton.amqp.Symbol) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) Modified(org.apache.qpid.proton.amqp.messaging.Modified) Message(org.apache.qpid.proton.message.Message) AsyncResult(io.vertx.core.AsyncResult) Logger(io.vertx.core.impl.logging.Logger) Tracker(io.vertx.proton.streams.Tracker) Publisher(org.reactivestreams.Publisher) Test(org.junit.Test) ProtonClient(io.vertx.proton.ProtonClient) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Released(org.apache.qpid.proton.amqp.messaging.Released) ExecutionException(java.util.concurrent.ExecutionException) Section(org.apache.qpid.proton.amqp.messaging.Section) FutureHandler(io.vertx.proton.FutureHandler) ProtonSubscriber(io.vertx.proton.streams.ProtonSubscriber) Handler(io.vertx.core.Handler) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) Tracker(io.vertx.proton.streams.Tracker) Modified(org.apache.qpid.proton.amqp.messaging.Modified) Released(org.apache.qpid.proton.amqp.messaging.Released) ProtonServer(io.vertx.proton.ProtonServer) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) ProtonClient(io.vertx.proton.ProtonClient) ProtonConnection(io.vertx.proton.ProtonConnection) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 57 with ProtonConnection

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

the class TrackerSubscriberBlackboxVerificationTckTest method createSubscriber.

@Override
public Subscriber<Tracker> createSubscriber() {
    int actualPort = server.actualPort();
    ProtonClient client = ProtonClient.create(vertx);
    AtomicReference<Subscriber<Tracker>> ref = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    client.connect("localhost", actualPort, result -> {
        if (result.succeeded()) {
            ProtonConnection conn = result.result();
            conn.open();
            ProtonSubscriber<Tracker> stream = ProtonStreams.createTrackerProducer(conn, "myAddress");
            ((ProtonSubscriberImpl) stream).setEmitOnConnectionEnd(false);
            ref.set(stream);
        } else {
            LOG.error("Connection failed");
        }
        latch.countDown();
    });
    try {
        LOG.trace("Awaiting connection");
        boolean res = latch.await(2, TimeUnit.SECONDS);
        LOG.trace("Client connected: " + res);
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while creating subscriber", e);
    }
    return ref.get();
}
Also used : Tracker(io.vertx.proton.streams.Tracker) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProtonClient(io.vertx.proton.ProtonClient) CountDownLatch(java.util.concurrent.CountDownLatch) ProtonConnection(io.vertx.proton.ProtonConnection) Subscriber(org.reactivestreams.Subscriber) ProtonSubscriber(io.vertx.proton.streams.ProtonSubscriber) ProtonSubscriberImpl(io.vertx.proton.streams.impl.ProtonSubscriberImpl)

Example 58 with ProtonConnection

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

the class ProtonServerImplTest method testAuthenticatorCreatedPerConnection.

@Test(timeout = 20000)
public void testAuthenticatorCreatedPerConnection(TestContext context) {
    Async connectedAsync = context.async();
    Async connectedAsync2 = context.async();
    AtomicInteger port = new AtomicInteger(-1);
    final TestPlainAuthenticatorFactory authenticatorFactory = new TestPlainAuthenticatorFactory();
    ProtonServer.create(vertx).saslAuthenticatorFactory(authenticatorFactory).connectHandler(protonConnection -> {
        // Verify the expected auth detail was recorded in the connection attachments, just using a String here.
        String authValue = protonConnection.attachments().get(AUTH_KEY, String.class);
        context.assertEquals(AUTH_VALUE, authValue);
    }).listen(server -> {
        port.set(server.result().actualPort());
        ProtonClient.create(vertx).connect("localhost", port.intValue(), GOOD_USER, PASSWD, protonConnectionAsyncResult -> {
            context.assertTrue(protonConnectionAsyncResult.succeeded());
            protonConnectionAsyncResult.result().disconnect();
            connectedAsync.complete();
        });
    });
    connectedAsync.awaitSuccess();
    context.assertEquals(1, authenticatorFactory.getCreateCount(), "unexpected authenticator count");
    ProtonClient.create(vertx).connect("localhost", port.intValue(), GOOD_USER, PASSWD, protonConnectionAsyncResult -> {
        context.assertTrue(protonConnectionAsyncResult.succeeded());
        protonConnectionAsyncResult.result().disconnect();
        connectedAsync2.complete();
    });
    connectedAsync2.awaitSuccess();
    context.assertEquals(2, authenticatorFactory.getCreateCount(), "unexpected authenticator count");
}
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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 59 with ProtonConnection

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

the class ProtonServerImplTest method doTestAsyncServerAuthenticatorTestImpl.

private void doTestAsyncServerAuthenticatorTestImpl(TestContext context, boolean passAuthentication) {
    Async connectAsync = context.async();
    AtomicBoolean connectedServer = new AtomicBoolean();
    final long delay = 750;
    TestAsyncAuthenticator testAsyncAuthenticator = new TestAsyncAuthenticator(delay, passAuthentication);
    TestAsyncAuthenticatorFactory authenticatorFactory = new TestAsyncAuthenticatorFactory(testAsyncAuthenticator);
    ProtonServer.create(vertx).saslAuthenticatorFactory(authenticatorFactory).connectHandler(protonConnection -> {
        connectedServer.set(true);
    }).listen(server -> {
        final long startTime = System.currentTimeMillis();
        ProtonClient.create(vertx).connect("localhost", server.result().actualPort(), GOOD_USER, PASSWD, conResult -> {
            // Verify the process took expected time from auth delay.
            long actual = System.currentTimeMillis() - startTime;
            context.assertTrue(actual >= delay, "Connect completed before expected time delay elapsed! " + actual);
            if (passAuthentication) {
                context.assertTrue(conResult.succeeded(), "Expected connect to succeed");
                conResult.result().disconnect();
            } else {
                context.assertFalse(conResult.succeeded(), "Expected connect to fail");
            }
            connectAsync.complete();
        });
    });
    connectAsync.awaitSuccess();
    if (passAuthentication) {
        context.assertTrue(connectedServer.get(), "Server handler should have been called");
    } else {
        context.assertFalse(connectedServer.get(), "Server handler should not have been called");
    }
    context.assertEquals(1, authenticatorFactory.getCreateCount(), "unexpected authenticator creation count");
}
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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Async(io.vertx.ext.unit.Async)

Example 60 with ProtonConnection

use of io.vertx.proton.ProtonConnection 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

ProtonConnection (io.vertx.proton.ProtonConnection)63 ProtonClient (io.vertx.proton.ProtonClient)37 Message (org.apache.qpid.proton.message.Message)36 Handler (io.vertx.core.Handler)35 Test (org.junit.Test)33 Async (io.vertx.ext.unit.Async)27 ProtonServer (io.vertx.proton.ProtonServer)25 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)25 AmqpValue (org.apache.qpid.proton.amqp.messaging.AmqpValue)25 AsyncResult (io.vertx.core.AsyncResult)24 TestContext (io.vertx.ext.unit.TestContext)24 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)24 RunWith (org.junit.runner.RunWith)24 Section (org.apache.qpid.proton.amqp.messaging.Section)23 ProtonHelper.message (io.vertx.proton.ProtonHelper.message)21 Rejected (org.apache.qpid.proton.amqp.messaging.Rejected)21 ProtonStreams (io.vertx.proton.streams.ProtonStreams)20 Vertx (io.vertx.core.Vertx)19 Symbol (org.apache.qpid.proton.amqp.Symbol)19 Accepted (org.apache.qpid.proton.amqp.messaging.Accepted)19