Search in sources :

Example 6 with ProtonServer

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

the class ProtonPublisherIntTest method testCreateUsingCustomSource.

@Test(timeout = 20000)
public void testCreateUsingCustomSource(TestContext context) throws Exception {
    server.close();
    final Async clientLinkOpenAsync = context.async();
    final Async serverLinkOpenAsync = context.async();
    final Async clientLinkCloseAsync = context.async();
    final String dynamicAddress = "testCreateUsingCustomSource:" + UUID.randomUUID();
    ProtonServer protonServer = null;
    try {
        protonServer = createServer((serverConnection) -> {
            serverConnection.openHandler(result -> {
                serverConnection.open();
            });
            serverConnection.sessionOpenHandler(session -> session.open());
            serverConnection.senderOpenHandler(serverSender -> {
                serverSender.closeHandler(res -> {
                    serverSender.close();
                });
                // Verify the remote terminus details used were as expected
                context.assertNotNull(serverSender.getRemoteSource(), "source should not be null");
                org.apache.qpid.proton.amqp.messaging.Source remoteSource = (org.apache.qpid.proton.amqp.messaging.Source) serverSender.getRemoteSource();
                context.assertTrue(remoteSource.getDynamic(), "expected dynamic source");
                context.assertNull(remoteSource.getAddress(), "expected no source address");
                // Set the local terminus details
                org.apache.qpid.proton.amqp.messaging.Source source = (org.apache.qpid.proton.amqp.messaging.Source) remoteSource.copy();
                source.setAddress(dynamicAddress);
                serverSender.setSource(source);
                LOG.trace("Server sender opened");
                serverSender.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();
            // Create publisher with source configured to be dynamic
            ProtonPublisher<Delivery> publisher = ProtonStreams.createDeliveryConsumer(connection, null);
            org.apache.qpid.proton.amqp.messaging.Source source = (org.apache.qpid.proton.amqp.messaging.Source) publisher.getSource();
            source.setDynamic(true);
            publisher.subscribe(new Subscriber<Delivery>() {

                Subscription sub = null;

                @Override
                public void onSubscribe(Subscription s) {
                    // Grab and verify the dynamic address details
                    org.apache.qpid.proton.amqp.messaging.Source remoteSource = (org.apache.qpid.proton.amqp.messaging.Source) publisher.getRemoteSource();
                    context.assertTrue(remoteSource.getDynamic(), "expected dynamic source");
                    context.assertEquals(dynamicAddress, remoteSource.getAddress(), "unexpected source address");
                    clientLinkOpenAsync.complete();
                    sub = s;
                    s.request(1);
                    sub.cancel();
                }

                @Override
                public void onNext(Delivery e) {
                }

                @Override
                public void onError(Throwable t) {
                    if (!clientLinkCloseAsync.isCompleted()) {
                        context.fail("onError called");
                    }
                }

                @Override
                public void onComplete() {
                    clientLinkCloseAsync.complete();
                }
            });
        });
        serverLinkOpenAsync.awaitSuccess();
        clientLinkOpenAsync.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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonServer(io.vertx.proton.ProtonServer) ArrayList(java.util.ArrayList) MockServerTestBase(io.vertx.proton.MockServerTestBase) Delivery(io.vertx.proton.streams.Delivery) 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) DeliveryState(org.apache.qpid.proton.amqp.transport.DeliveryState) Message(org.apache.qpid.proton.message.Message) AsyncResult(io.vertx.core.AsyncResult) Subscriber(org.reactivestreams.Subscriber) Logger(io.vertx.core.impl.logging.Logger) Test(org.junit.Test) ProtonClient(io.vertx.proton.ProtonClient) UUID(java.util.UUID) TerminusExpiryPolicy(org.apache.qpid.proton.amqp.messaging.TerminusExpiryPolicy) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Released(org.apache.qpid.proton.amqp.messaging.Released) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Section(org.apache.qpid.proton.amqp.messaging.Section) FutureHandler(io.vertx.proton.FutureHandler) Subscription(org.reactivestreams.Subscription) TerminusDurability(org.apache.qpid.proton.amqp.messaging.TerminusDurability) ProtonPublisher(io.vertx.proton.streams.ProtonPublisher) Handler(io.vertx.core.Handler) Collections(java.util.Collections) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) ProtonServer(io.vertx.proton.ProtonServer) ProtonClient(io.vertx.proton.ProtonClient) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) Delivery(io.vertx.proton.streams.Delivery) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 7 with ProtonServer

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

the class ProtonPublisherIntTest method createServer.

private ProtonServer createServer(Handler<ProtonConnection> serverConnHandler) throws InterruptedException, ExecutionException {
    ProtonServer server = ProtonServer.create(vertx);
    server.connectHandler(serverConnHandler);
    FutureHandler<ProtonServer, AsyncResult<ProtonServer>> handler = FutureHandler.asyncResult();
    server.listen(0, handler);
    handler.get();
    return server;
}
Also used : ProtonServer(io.vertx.proton.ProtonServer) AsyncResult(io.vertx.core.AsyncResult)

Example 8 with ProtonServer

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

the class ProtonPublisherIntTest method testCreateCancelSubscription.

@Test(timeout = 20000)
public void testCreateCancelSubscription(TestContext context) throws Exception {
    server.close();
    final Async clientLinkOpenAsync = context.async();
    final Async serverLinkOpenAsync = context.async();
    final Async serverLinkCloseAsync = 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.senderOpenHandler(serverSender -> {
                serverSender.closeHandler(res -> {
                    serverLinkCloseAsync.complete();
                    serverSender.close();
                });
                // Buffer a single message to send when credit arrives
                serverSender.send(message(String.valueOf(1)));
                LOG.trace("Server sender opened");
                serverSender.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();
            ProtonPublisher<Delivery> publisher = ProtonStreams.createDeliveryConsumer(connection, "myAddress");
            publisher.subscribe(new Subscriber<Delivery>() {

                Subscription sub = null;

                @Override
                public void onSubscribe(Subscription s) {
                    clientLinkOpenAsync.complete();
                    sub = s;
                    s.request(5);
                }

                @Override
                public void onNext(Delivery d) {
                    validateMessage(context, 1, String.valueOf(1), d.message());
                    sub.cancel();
                }

                @Override
                public void onError(Throwable t) {
                    if (!clientLinkCloseAsync.isCompleted()) {
                        context.fail("onError called");
                    }
                }

                @Override
                public void onComplete() {
                    clientLinkCloseAsync.complete();
                }
            });
        });
        serverLinkOpenAsync.awaitSuccess();
        clientLinkOpenAsync.awaitSuccess();
        serverLinkCloseAsync.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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonServer(io.vertx.proton.ProtonServer) ArrayList(java.util.ArrayList) MockServerTestBase(io.vertx.proton.MockServerTestBase) Delivery(io.vertx.proton.streams.Delivery) 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) DeliveryState(org.apache.qpid.proton.amqp.transport.DeliveryState) Message(org.apache.qpid.proton.message.Message) AsyncResult(io.vertx.core.AsyncResult) Subscriber(org.reactivestreams.Subscriber) Logger(io.vertx.core.impl.logging.Logger) Test(org.junit.Test) ProtonClient(io.vertx.proton.ProtonClient) UUID(java.util.UUID) TerminusExpiryPolicy(org.apache.qpid.proton.amqp.messaging.TerminusExpiryPolicy) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Released(org.apache.qpid.proton.amqp.messaging.Released) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Section(org.apache.qpid.proton.amqp.messaging.Section) FutureHandler(io.vertx.proton.FutureHandler) Subscription(org.reactivestreams.Subscription) TerminusDurability(org.apache.qpid.proton.amqp.messaging.TerminusDurability) ProtonPublisher(io.vertx.proton.streams.ProtonPublisher) Handler(io.vertx.core.Handler) Collections(java.util.Collections) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonServer(io.vertx.proton.ProtonServer) Async(io.vertx.ext.unit.Async) Delivery(io.vertx.proton.streams.Delivery) ProtonClient(io.vertx.proton.ProtonClient) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 9 with ProtonServer

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

the class ProtonPublisherIntTest method testSubscriberErrorOnLinkCLose.

@Test(timeout = 20000)
public void testSubscriberErrorOnLinkCLose(TestContext context) throws Exception {
    server.close();
    final Async clientLinkOpenAsync = context.async();
    final Async serverLinkOpenAsync = context.async();
    final Async serverLinkCloseAsync = 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.senderOpenHandler(serverSender -> {
                serverSender.closeHandler(res -> {
                    serverLinkCloseAsync.complete();
                });
                LOG.trace("Server sender opened");
                serverSender.open();
                serverLinkOpenAsync.complete();
                serverSender.close();
            });
        });
        // ===== Client Handling =====
        ProtonClient client = ProtonClient.create(vertx);
        client.connect("localhost", protonServer.actualPort(), res -> {
            context.assertTrue(res.succeeded());
            ProtonConnection connection = res.result();
            connection.open();
            ProtonPublisher<Delivery> publisher = ProtonStreams.createDeliveryConsumer(connection, "myAddress");
            publisher.subscribe(new Subscriber<Delivery>() {

                @Override
                public void onSubscribe(Subscription s) {
                    clientLinkOpenAsync.complete();
                }

                @Override
                public void onNext(Delivery e) {
                    context.fail("onNext called");
                }

                @Override
                public void onError(Throwable t) {
                    clientLinkCloseAsync.complete();
                }

                @Override
                public void onComplete() {
                    context.fail("onComplete called");
                }
            });
        });
        serverLinkOpenAsync.awaitSuccess();
        clientLinkOpenAsync.awaitSuccess();
        clientLinkCloseAsync.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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonServer(io.vertx.proton.ProtonServer) ArrayList(java.util.ArrayList) MockServerTestBase(io.vertx.proton.MockServerTestBase) Delivery(io.vertx.proton.streams.Delivery) 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) DeliveryState(org.apache.qpid.proton.amqp.transport.DeliveryState) Message(org.apache.qpid.proton.message.Message) AsyncResult(io.vertx.core.AsyncResult) Subscriber(org.reactivestreams.Subscriber) Logger(io.vertx.core.impl.logging.Logger) Test(org.junit.Test) ProtonClient(io.vertx.proton.ProtonClient) UUID(java.util.UUID) TerminusExpiryPolicy(org.apache.qpid.proton.amqp.messaging.TerminusExpiryPolicy) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Released(org.apache.qpid.proton.amqp.messaging.Released) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Section(org.apache.qpid.proton.amqp.messaging.Section) FutureHandler(io.vertx.proton.FutureHandler) Subscription(org.reactivestreams.Subscription) TerminusDurability(org.apache.qpid.proton.amqp.messaging.TerminusDurability) ProtonPublisher(io.vertx.proton.streams.ProtonPublisher) Handler(io.vertx.core.Handler) Collections(java.util.Collections) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonServer(io.vertx.proton.ProtonServer) Async(io.vertx.ext.unit.Async) Delivery(io.vertx.proton.streams.Delivery) ProtonClient(io.vertx.proton.ProtonClient) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 10 with ProtonServer

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

the class ProtonPublisherIntTest method testAutoAccept.

@Test(timeout = 20000)
public void testAutoAccept(TestContext context) throws Exception {
    server.close();
    final Async receivedMessagesAsync = context.async();
    final Async ackedMessagesAsync = context.async();
    int messageCount = 5;
    List<Integer> accepted = Collections.synchronizedList(new ArrayList<>());
    AtomicInteger msgNum = new AtomicInteger(1);
    ProtonServer protonServer = null;
    try {
        protonServer = createServer((serverConnection) -> {
            serverConnection.openHandler(result -> {
                serverConnection.open();
            });
            serverConnection.sessionOpenHandler(session -> session.open());
            serverConnection.senderOpenHandler(serverSender -> {
                LOG.trace("Server sender opened");
                serverSender.open();
                serverSender.sendQueueDrainHandler(s -> {
                    for (int i = msgNum.get(); i <= messageCount; i = msgNum.incrementAndGet()) {
                        int j = i;
                        serverSender.send(message(String.valueOf(i)), del -> {
                            LOG.trace("Server received disposition for msg: " + j);
                            if (del.getRemoteState() instanceof Accepted) {
                                accepted.add(j);
                                if (accepted.size() == messageCount) {
                                    ackedMessagesAsync.complete();
                                }
                            } else {
                                context.fail("Expected message to be accepted");
                            }
                        });
                    }
                });
            });
        });
        // ===== Client Handling =====
        AtomicInteger counter = new AtomicInteger(0);
        ProtonClient client = ProtonClient.create(vertx);
        client.connect("localhost", protonServer.actualPort(), res -> {
            context.assertTrue(res.succeeded());
            ProtonConnection connection = res.result();
            // Create consumer stream of Message, which auto-accepts after onNext
            ProtonPublisher<Message> publisher = ProtonStreams.createConsumer(connection, "myAddress");
            publisher.subscribe(new Subscriber<Message>() {

                Subscription sub = null;

                @Override
                public void onSubscribe(Subscription s) {
                    sub = s;
                    LOG.trace("Flowing initial credit");
                    sub.request(messageCount);
                }

                @Override
                public void onNext(Message m) {
                    int count = counter.incrementAndGet();
                    validateMessage(context, count, String.valueOf(count), m);
                    if (count == messageCount) {
                        LOG.trace("Got all messages, completing async");
                        receivedMessagesAsync.complete();
                    }
                }

                @Override
                public void onError(Throwable t) {
                    if (!receivedMessagesAsync.isCompleted()) {
                        context.fail("onError called");
                    }
                }

                @Override
                public void onComplete() {
                    context.fail("onComplete called");
                }
            });
            connection.open();
        });
        receivedMessagesAsync.awaitSuccess();
        ackedMessagesAsync.awaitSuccess();
    } finally {
        if (protonServer != null) {
            protonServer.close();
        }
    }
    // Verify the messages were all accepted
    context.assertEquals(accepted.size(), messageCount, "Unexpected accepted count");
    for (int i = 1; i <= messageCount; i++) {
        // Verify each accepted number, establish correct order etc.
        context.assertEquals(accepted.remove(0), i, "Unexpected msgNum");
    }
}
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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonServer(io.vertx.proton.ProtonServer) ArrayList(java.util.ArrayList) MockServerTestBase(io.vertx.proton.MockServerTestBase) Delivery(io.vertx.proton.streams.Delivery) 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) DeliveryState(org.apache.qpid.proton.amqp.transport.DeliveryState) Message(org.apache.qpid.proton.message.Message) AsyncResult(io.vertx.core.AsyncResult) Subscriber(org.reactivestreams.Subscriber) Logger(io.vertx.core.impl.logging.Logger) Test(org.junit.Test) ProtonClient(io.vertx.proton.ProtonClient) UUID(java.util.UUID) TerminusExpiryPolicy(org.apache.qpid.proton.amqp.messaging.TerminusExpiryPolicy) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Released(org.apache.qpid.proton.amqp.messaging.Released) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Section(org.apache.qpid.proton.amqp.messaging.Section) FutureHandler(io.vertx.proton.FutureHandler) Subscription(org.reactivestreams.Subscription) TerminusDurability(org.apache.qpid.proton.amqp.messaging.TerminusDurability) ProtonPublisher(io.vertx.proton.streams.ProtonPublisher) Handler(io.vertx.core.Handler) Collections(java.util.Collections) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) Message(org.apache.qpid.proton.message.Message) ProtonServer(io.vertx.proton.ProtonServer) ProtonClient(io.vertx.proton.ProtonClient) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProtonConnection(io.vertx.proton.ProtonConnection) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Async(io.vertx.ext.unit.Async) Subscription(org.reactivestreams.Subscription) 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