Search in sources :

Example 21 with ProtonServer

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

the class ProtonSubscriberIntTest method testSubCancelledOnConnectionEnd.

@Test(timeout = 20000)
public void testSubCancelledOnConnectionEnd(TestContext context) throws Exception {
    server.close();
    final Async serverLinkOpenAsync = context.async();
    final Async subCancelled = context.async();
    ProtonServer protonServer = null;
    try {
        protonServer = createServer((serverConnection) -> {
            serverConnection.openHandler(result -> {
                serverConnection.open();
            });
            serverConnection.sessionOpenHandler(session -> session.open());
            serverConnection.receiverOpenHandler(serverReceiver -> {
                LOG.trace("Server receiver opened");
                // 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");
            // Create a Publisher that doesn't produce elements, but indicates when its subscription is cancelled.
            Publisher<Tracker> producer = Flowable.<Tracker>never().doOnCancel(() -> {
                LOG.trace("Cancelled!");
                subCancelled.complete();
            });
            producer.subscribe(subscriber);
        });
        serverLinkOpenAsync.awaitSuccess();
        context.assertFalse(subCancelled.isCompleted());
        protonServer.close();
        protonServer = null;
        subCancelled.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) ProtonConnection(io.vertx.proton.ProtonConnection) Tracker(io.vertx.proton.streams.Tracker) ProtonServer(io.vertx.proton.ProtonServer) Async(io.vertx.ext.unit.Async) ProtonClient(io.vertx.proton.ProtonClient) Test(org.junit.Test)

Example 22 with ProtonServer

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

the class ProtonSubscriberIntTest method testSubCancelledOnLinkClose.

@Test(timeout = 20000)
public void testSubCancelledOnLinkClose(TestContext context) throws Exception {
    server.close();
    final Async serverLinkOpenAsync = context.async();
    final Async clientLinkCloseAsync = context.async();
    ProtonServer protonServer = null;
    try {
        protonServer = createServer((serverConnection) -> {
            serverConnection.openHandler(result -> {
                serverConnection.open();
            });
            serverConnection.sessionOpenHandler(session -> session.open());
            serverConnection.receiverOpenHandler(serverReceiver -> {
                LOG.trace("Server receiver opened");
                // Set the local terminus details [naively]
                serverReceiver.setTarget(serverReceiver.getRemoteTarget().copy());
                serverReceiver.open();
                serverLinkOpenAsync.complete();
                serverReceiver.close();
            });
        });
        // ===== 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");
            // Create a Publisher that doesn't produce elements, but indicates when its subscription is cancelled.
            Publisher<Tracker> producer = Flowable.<Tracker>never().doOnCancel(() -> {
                LOG.trace("Cancelled!");
                clientLinkCloseAsync.complete();
            });
            producer.subscribe(subscriber);
        });
        serverLinkOpenAsync.awaitSuccess();
        clientLinkCloseAsync.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) ProtonConnection(io.vertx.proton.ProtonConnection) Tracker(io.vertx.proton.streams.Tracker) ProtonServer(io.vertx.proton.ProtonServer) Async(io.vertx.ext.unit.Async) ProtonClient(io.vertx.proton.ProtonClient) Test(org.junit.Test)

Example 23 with ProtonServer

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

Aggregations

ProtonServer (io.vertx.proton.ProtonServer)23 AsyncResult (io.vertx.core.AsyncResult)21 ProtonConnection (io.vertx.proton.ProtonConnection)19 ProtonHelper.message (io.vertx.proton.ProtonHelper.message)19 AmqpValue (org.apache.qpid.proton.amqp.messaging.AmqpValue)19 Section (org.apache.qpid.proton.amqp.messaging.Section)19 Message (org.apache.qpid.proton.message.Message)19 Handler (io.vertx.core.Handler)18 Logger (io.vertx.core.impl.logging.Logger)18 LoggerFactory (io.vertx.core.impl.logging.LoggerFactory)18 Async (io.vertx.ext.unit.Async)18 TestContext (io.vertx.ext.unit.TestContext)18 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)18 FutureHandler (io.vertx.proton.FutureHandler)18 MockServerTestBase (io.vertx.proton.MockServerTestBase)18 ProtonClient (io.vertx.proton.ProtonClient)18 ProtonStreams (io.vertx.proton.streams.ProtonStreams)18 Arrays (java.util.Arrays)18 ExecutionException (java.util.concurrent.ExecutionException)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18