Search in sources :

Example 1 with Delivery

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

the class DeliveryPublisherVerificationTckTest method createPublisher.

@Override
public Publisher<Delivery> createPublisher(long elements) {
    int actualPort = server.actualPort();
    ProtonClient client = ProtonClient.create(vertx);
    AtomicReference<Publisher<Delivery>> ref = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    client.connect("localhost", actualPort, result -> {
        if (result.succeeded()) {
            ProtonConnection conn = result.result();
            conn.open();
            ProtonPublisher<Delivery> stream = ProtonStreams.createDeliveryConsumer(conn, testName);
            ((ProtonPublisherImpl) 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 publisher", e);
    }
    return ref.get();
}
Also used : ProtonPublisherImpl(io.vertx.proton.streams.impl.ProtonPublisherImpl) AtomicReference(java.util.concurrent.atomic.AtomicReference) Publisher(org.reactivestreams.Publisher) ProtonPublisher(io.vertx.proton.streams.ProtonPublisher) ProtonClient(io.vertx.proton.ProtonClient) CountDownLatch(java.util.concurrent.CountDownLatch) ProtonConnection(io.vertx.proton.ProtonConnection) Delivery(io.vertx.proton.streams.Delivery)

Example 2 with Delivery

use of io.vertx.proton.streams.Delivery 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 3 with Delivery

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

the class ProtonPublisherIntTest method testImmediateInitialRequest.

@Test(timeout = 20000)
public void testImmediateInitialRequest(TestContext context) {
    Async async = context.async();
    connect(context, connection -> {
        connection.open();
        AtomicInteger counter = new AtomicInteger(0);
        AtomicBoolean creditGranted = new AtomicBoolean();
        ProtonPublisher<Delivery> publisher = ProtonStreams.createDeliveryConsumer(connection, "five_messages");
        publisher.subscribe(new Subscriber<Delivery>() {

            Subscription sub = null;

            @Override
            public void onSubscribe(Subscription s) {
                // Explicitly grant initial credit of 4, onNext will grant more later.
                LOG.trace("Flowing initial credit");
                sub = s;
                sub.request(4);
            }

            @Override
            public void onNext(Delivery d) {
                int count = counter.incrementAndGet();
                switch(count) {
                    // Fall-through
                    case 1:
                    // Fall-through
                    case 2:
                    case 3:
                        {
                            validateMessage(context, count, String.valueOf(count), d.message());
                            break;
                        }
                    case 4:
                        {
                            validateMessage(context, count, String.valueOf(count), d.message());
                            // 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);
                                sub.request(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), d.message());
                            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;
                        }
                }
            }

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

            @Override
            public void onComplete() {
                context.fail("onComplete called");
            }
        });
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Async(io.vertx.ext.unit.Async) Delivery(io.vertx.proton.streams.Delivery) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 4 with Delivery

use of io.vertx.proton.streams.Delivery 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 5 with Delivery

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

Aggregations

Delivery (io.vertx.proton.streams.Delivery)15 ProtonClient (io.vertx.proton.ProtonClient)13 ProtonConnection (io.vertx.proton.ProtonConnection)13 ProtonPublisher (io.vertx.proton.streams.ProtonPublisher)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 Handler (io.vertx.core.Handler)12 Logger (io.vertx.core.impl.logging.Logger)12 LoggerFactory (io.vertx.core.impl.logging.LoggerFactory)12 Async (io.vertx.ext.unit.Async)12 ProtonHelper.message (io.vertx.proton.ProtonHelper.message)12 ProtonStreams (io.vertx.proton.streams.ProtonStreams)12 ExecutionException (java.util.concurrent.ExecutionException)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 Symbol (org.apache.qpid.proton.amqp.Symbol)12 Message (org.apache.qpid.proton.message.Message)12 Test (org.junit.Test)12 Subscription (org.reactivestreams.Subscription)12 AsyncResult (io.vertx.core.AsyncResult)11 TestContext (io.vertx.ext.unit.TestContext)11 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)11