Search in sources :

Example 41 with ErrorCondition

use of org.apache.qpid.proton.amqp.transport.ErrorCondition in project vertx-proton by vert-x3.

the class TrackerSubscriberWhiteboxVerificationTckTest method createServer.

private TestServer createServer() throws Exception {
    return new TestServer(vertx, (connection) -> {
        connection.openHandler(res -> {
            LOG.trace("Client connected: " + connection.getRemoteContainer());
            connection.open();
        }).closeHandler(c -> {
            LOG.trace("Client closing amqp connection: " + connection.getRemoteContainer());
            connection.close();
            connection.disconnect();
        }).disconnectHandler(c -> {
            LOG.trace("Client socket disconnected: " + connection.getRemoteContainer());
            connection.disconnect();
        }).sessionOpenHandler(session -> session.open());
        connection.receiverOpenHandler(receiver -> {
            if (!server.getDetachLink()) {
                LOG.trace("Receiving from client to: " + receiver.getRemoteTarget().getAddress());
                // This is rather naive, for example use only, proper
                receiver.setTarget(receiver.getRemoteTarget());
                // servers should ensure that they advertise their own
                // Target settings that actually reflect what is in place.
                // The request may have also been for a dynamic address.
                receiver.handler((delivery, msg) -> {
                    String address = msg.getAddress();
                    if (address == null) {
                        address = receiver.getRemoteTarget().getAddress();
                    }
                    Section body = msg.getBody();
                    String content = "unknown";
                    if (body instanceof AmqpValue) {
                        content = (String) ((AmqpValue) body).getValue();
                    }
                    LOG.trace("message to:" + address + ", body: " + content);
                });
                receiver.closeHandler(s -> {
                    s.result().close();
                });
            }
            receiver.open();
            if (server.getDetachLink()) {
                receiver.setCondition(new ErrorCondition(Symbol.getSymbol("Failed Subscriber Requested"), ""));
                receiver.close();
            }
        });
    });
}
Also used : ProtonConnection(io.vertx.proton.ProtonConnection) SubscriberWhiteboxVerification(org.reactivestreams.tck.SubscriberWhiteboxVerification) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) MockServer(io.vertx.proton.MockServer) ProtonSubscriberImpl(io.vertx.proton.streams.impl.ProtonSubscriberImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AfterMethod(org.testng.annotations.AfterMethod) AtomicReference(java.util.concurrent.atomic.AtomicReference) Symbol(org.apache.qpid.proton.amqp.Symbol) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) Message(org.apache.qpid.proton.message.Message) Subscriber(org.reactivestreams.Subscriber) Logger(io.vertx.core.impl.logging.Logger) Tracker(io.vertx.proton.streams.Tracker) BeforeMethod(org.testng.annotations.BeforeMethod) Vertx(io.vertx.core.Vertx) ProtonClient(io.vertx.proton.ProtonClient) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Proton(org.apache.qpid.proton.Proton) CountDownLatch(java.util.concurrent.CountDownLatch) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Section(org.apache.qpid.proton.amqp.messaging.Section) ProtonSubscriber(io.vertx.proton.streams.ProtonSubscriber) TestEnvironment(org.reactivestreams.tck.TestEnvironment) Subscription(org.reactivestreams.Subscription) ProtonConnectionImpl(io.vertx.proton.impl.ProtonConnectionImpl) Handler(io.vertx.core.Handler) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Section(org.apache.qpid.proton.amqp.messaging.Section) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue)

Example 42 with ErrorCondition

use of org.apache.qpid.proton.amqp.transport.ErrorCondition 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 -> {
            Promise<ProtonSession> sessionPromise = Promise.<ProtonSession>promise();
            // Expect a session to open, when the sender is created by the client
            serverConnection.sessionOpenHandler(serverSession -> {
                LOG.trace("Server session open");
                serverSession.open();
                sessionPromise.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(sessionPromise.future().succeeded(), "Session future not [yet] succeeded");
                LOG.trace("Server session close");
                ProtonSession s = sessionPromise.future().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) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageImpl(org.apache.qpid.proton.message.impl.MessageImpl) LinkedHashMap(java.util.LinkedHashMap) Symbol(org.apache.qpid.proton.amqp.Symbol) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) Modified(org.apache.qpid.proton.amqp.messaging.Modified) 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) WritableBuffer(org.apache.qpid.proton.codec.WritableBuffer) DeliveryState(org.apache.qpid.proton.amqp.transport.DeliveryState) Message(org.apache.qpid.proton.message.Message) AsyncResult(io.vertx.core.AsyncResult) Binary(org.apache.qpid.proton.amqp.Binary) AmqpError(org.apache.qpid.proton.amqp.transport.AmqpError) Logger(io.vertx.core.impl.logging.Logger) Data(org.apache.qpid.proton.amqp.messaging.Data) ProtonServerImpl(io.vertx.proton.impl.ProtonServerImpl) LinkError(org.apache.qpid.proton.amqp.transport.LinkError) Promise(io.vertx.core.Promise) ProtonMetaDataSupportImpl(io.vertx.proton.impl.ProtonMetaDataSupportImpl) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) UUID(java.util.UUID) 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) Mockito(org.mockito.Mockito) 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) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) Collections(java.util.Collections) Promise(io.vertx.core.Promise) Async(io.vertx.ext.unit.Async) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition)

Example 43 with ErrorCondition

use of org.apache.qpid.proton.amqp.transport.ErrorCondition in project vertx-proton by vert-x3.

the class ProtonClientTest method doMaxMessageSizeTestImpl.

private void doMaxMessageSizeTestImpl(TestContext context, final int maxFrameSize, final int maxMessageSize, final int dataPayloadSize) throws Exception {
    server.close();
    Async serverSenderOpenAsync = context.async();
    Async serverSenderCreditCheck = context.async();
    Async clientReceiverOpenAsync = context.async();
    Async serverSenderDetachCheck = context.async();
    Async maxMessageSizeExceededHandlerFired = context.async();
    AtomicInteger messageCount = new AtomicInteger();
    AtomicInteger handlerCount = new AtomicInteger();
    final UnsignedLong receiverMaxMsgSize = UnsignedLong.valueOf(maxMessageSize);
    final UnsignedLong serverMaxMsgSize = UnsignedLong.valueOf(5 * receiverMaxMsgSize.longValue());
    ProtonServer protonServer = null;
    try {
        protonServer = createServer((serverConnection) -> {
            serverConnection.openHandler(result -> {
                serverConnection.open();
            });
            serverConnection.sessionOpenHandler(session -> {
                session.open();
            });
            serverConnection.senderOpenHandler(serverSender -> {
                context.assertEquals(receiverMaxMsgSize, serverSender.getRemoteMaxMessageSize(), "unexpected remote max message size at server");
                context.assertNull(serverSender.getMaxMessageSize(), "Expected no value to be set");
                serverSender.setMaxMessageSize(serverMaxMsgSize);
                context.assertEquals(serverMaxMsgSize, serverSender.getMaxMessageSize(), "Expected value to now be set");
                serverSender.sendQueueDrainHandler(ss -> {
                    if (!serverSenderCreditCheck.isCompleted()) {
                        context.assertTrue(serverSender.getCredit() > 3, "Unexpectedly low credit: " + serverSender.getCredit());
                        serverSenderCreditCheck.complete();
                        sendMessagesForMaxMessageSizeTest(dataPayloadSize, serverSender);
                    }
                });
                serverSender.detachHandler(res -> {
                    ErrorCondition remoteCondition = serverSender.getRemoteCondition();
                    context.assertNotNull(remoteCondition);
                    context.assertEquals(remoteCondition.getCondition(), LinkError.MESSAGE_SIZE_EXCEEDED);
                    context.assertTrue(res.failed());
                    serverSender.detach();
                    serverSenderDetachCheck.complete();
                });
                LOG.trace("Server sender opened");
                serverSender.open();
                serverSenderOpenAsync.complete();
            });
        });
        // ===== Client Handling =====
        ProtonClient client = ProtonClient.create(vertx);
        ProtonClientOptions options = new ProtonClientOptions();
        options.setMaxFrameSize(maxFrameSize);
        client.connect(options, "localhost", protonServer.actualPort(), res -> {
            context.assertTrue(res.succeeded());
            ProtonConnection connection = res.result();
            connection.openHandler(x -> {
                LOG.trace("Client connection opened");
                final ProtonReceiver receiver = connection.createReceiver("some-address");
                context.assertNull(receiver.getMaxMessageSize(), "Expected no value to be set");
                receiver.setMaxMessageSize(receiverMaxMsgSize);
                context.assertEquals(receiverMaxMsgSize, receiver.getMaxMessageSize(), "Expected value to now be set");
                receiver.openHandler(y -> {
                    LOG.trace("Client link opened");
                    context.assertEquals(serverMaxMsgSize, receiver.getRemoteMaxMessageSize(), "unexpected remote max message size at client");
                    clientReceiverOpenAsync.complete();
                    receiver.handler((delivery, message) -> {
                        validateMessage(context, messageCount.incrementAndGet(), "small-first-message", message);
                    });
                    receiver.maxMessageSizeExceededHandler(recv -> {
                        handlerCount.incrementAndGet();
                        maxMessageSizeExceededHandlerFired.complete();
                    });
                });
                receiver.open();
            }).open();
        });
        serverSenderOpenAsync.awaitSuccess();
        clientReceiverOpenAsync.awaitSuccess();
        serverSenderCreditCheck.awaitSuccess();
        maxMessageSizeExceededHandlerFired.awaitSuccess();
        serverSenderDetachCheck.awaitSuccess();
        context.assertEquals(1, handlerCount.get(), "Unexpected number of handler executions");
        context.assertEquals(1, messageCount.get(), "Unexpected number of deliveries");
    } finally {
        if (protonServer != null) {
            protonServer.close();
        }
    }
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Arrays(java.util.Arrays) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageImpl(org.apache.qpid.proton.message.impl.MessageImpl) LinkedHashMap(java.util.LinkedHashMap) Symbol(org.apache.qpid.proton.amqp.Symbol) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) Modified(org.apache.qpid.proton.amqp.messaging.Modified) 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) WritableBuffer(org.apache.qpid.proton.codec.WritableBuffer) DeliveryState(org.apache.qpid.proton.amqp.transport.DeliveryState) Message(org.apache.qpid.proton.message.Message) AsyncResult(io.vertx.core.AsyncResult) Binary(org.apache.qpid.proton.amqp.Binary) AmqpError(org.apache.qpid.proton.amqp.transport.AmqpError) Logger(io.vertx.core.impl.logging.Logger) Data(org.apache.qpid.proton.amqp.messaging.Data) ProtonServerImpl(io.vertx.proton.impl.ProtonServerImpl) LinkError(org.apache.qpid.proton.amqp.transport.LinkError) Promise(io.vertx.core.Promise) ProtonMetaDataSupportImpl(io.vertx.proton.impl.ProtonMetaDataSupportImpl) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) UUID(java.util.UUID) 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) Mockito(org.mockito.Mockito) 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) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) Collections(java.util.Collections) UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Async(io.vertx.ext.unit.Async) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition)

Example 44 with ErrorCondition

use of org.apache.qpid.proton.amqp.transport.ErrorCondition in project vertx-proton by vert-x3.

the class MessagePublisherVerificationTckTest method createServer.

private TestServer createServer() throws Exception {
    return new TestServer(vertx, (connection) -> {
        connection.openHandler(res -> {
            LOG.trace("Client connected: " + connection.getRemoteContainer());
            connection.open();
        }).closeHandler(c -> {
            LOG.trace("Client closing amqp connection: " + connection.getRemoteContainer());
            connection.close();
            connection.disconnect();
        }).disconnectHandler(c -> {
            LOG.trace("Client socket disconnected: " + connection.getRemoteContainer());
            connection.disconnect();
        }).sessionOpenHandler(session -> session.open());
        connection.senderOpenHandler(sender -> {
            if (!server.getDetachLink()) {
                LOG.trace("Sending to client from: " + sender.getRemoteSource().getAddress());
                // This is rather naive, for example use only, proper
                sender.setSource(sender.getRemoteSource());
                // servers should ensure that they advertise their own
                // Source settings that actually reflect what is in place.
                // The request may have also been for a dynamic address.
                AtomicLong count = new AtomicLong();
                AtomicLong outstanding = new AtomicLong();
                sender.sendQueueDrainHandler(s -> {
                    while (!s.sendQueueFull()) {
                        LOG.trace("Sending message to client");
                        Message m = message("Hello World from Server!" + count.incrementAndGet());
                        outstanding.incrementAndGet();
                        s.send(m, delivery -> {
                            LOG.trace("The message was received by the client.");
                        });
                    }
                });
                sender.closeHandler(s -> {
                    s.result().close();
                });
            }
            sender.open();
            if (server.getDetachLink()) {
                sender.setCondition(new ErrorCondition(Symbol.getSymbol("Failed Publisher Requested"), ""));
                sender.close();
            }
        });
    });
}
Also used : ProtonConnection(io.vertx.proton.ProtonConnection) ProtonStreams(io.vertx.proton.streams.ProtonStreams) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) MockServer(io.vertx.proton.MockServer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonPublisherWrapperImpl(io.vertx.proton.streams.impl.ProtonPublisherWrapperImpl) AfterMethod(org.testng.annotations.AfterMethod) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProtonHelper.message(io.vertx.proton.ProtonHelper.message) Symbol(org.apache.qpid.proton.amqp.Symbol) PublisherVerification(org.reactivestreams.tck.PublisherVerification) Message(org.apache.qpid.proton.message.Message) Method(java.lang.reflect.Method) Logger(io.vertx.core.impl.logging.Logger) Publisher(org.reactivestreams.Publisher) BeforeMethod(org.testng.annotations.BeforeMethod) Vertx(io.vertx.core.Vertx) ProtonClient(io.vertx.proton.ProtonClient) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) TestEnvironment(org.reactivestreams.tck.TestEnvironment) ProtonPublisher(io.vertx.proton.streams.ProtonPublisher) Handler(io.vertx.core.Handler) AtomicLong(java.util.concurrent.atomic.AtomicLong) Message(org.apache.qpid.proton.message.Message) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition)

Example 45 with ErrorCondition

use of org.apache.qpid.proton.amqp.transport.ErrorCondition in project vertx-proton by vert-x3.

the class ProtonReceiverImpl method handleMaxMessageSizeExceeded.

private void handleMaxMessageSizeExceeded(final UnsignedLong maxMessageSize, final Receiver receiver) {
    try {
        LOG.debug("delivery received exceeding max-message-size of " + maxMessageSize + " bytes");
        if (maxMessageSizeExceededHandler != null) {
            maxMessageSizeExceededHandler.handle(this);
        }
    } finally {
        // Detach link if handler has not sent a detach frame already
        if (!receiver.detached() && isOpen()) {
            LOG.debug("detaching link with error condition " + LinkError.MESSAGE_SIZE_EXCEEDED);
            setCondition(new ErrorCondition(LinkError.MESSAGE_SIZE_EXCEEDED, "exceeded max-message-size of " + maxMessageSize + " bytes "));
            detach();
        }
    }
}
Also used : ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition)

Aggregations

ErrorCondition (org.apache.qpid.proton.amqp.transport.ErrorCondition)45 Symbol (org.apache.qpid.proton.amqp.Symbol)13 Test (org.junit.Test)11 Handler (io.vertx.core.Handler)10 ProtonConnection (io.vertx.proton.ProtonConnection)10 DeliveryState (org.apache.qpid.proton.amqp.transport.DeliveryState)10 Message (org.apache.qpid.proton.message.Message)10 Vertx (io.vertx.core.Vertx)8 Logger (io.vertx.core.impl.logging.Logger)8 LoggerFactory (io.vertx.core.impl.logging.LoggerFactory)8 ExecutionException (java.util.concurrent.ExecutionException)8 TimeUnit (java.util.concurrent.TimeUnit)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 Rejected (org.apache.qpid.proton.amqp.messaging.Rejected)8 ProtonClient (io.vertx.proton.ProtonClient)7 Map (java.util.Map)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 MockServer (io.vertx.proton.MockServer)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Proton (org.apache.qpid.proton.Proton)6