Search in sources :

Example 96 with Session

use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.

the class MessageSendTest method testConvertMessageToAdvancedAndSendAMQPHeader.

@Test
public void testConvertMessageToAdvancedAndSendAMQPHeader() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().respond();
        peer.remoteFlow().withLinkCredit(10).queue();
        // Open a receiver to ensure sender link has processed
        peer.expectAttach().respond();
        // the inbound flow frame we sent previously before send.
        peer.expectFlow();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Sender test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort()).openFuture().get();
        Session session = connection.openSession().openFuture().get();
        SenderOptions options = new SenderOptions().deliveryMode(DeliveryMode.AT_MOST_ONCE);
        Sender sender = session.openSender("test-qos", options);
        // Gates send on remote flow having been sent and received
        session.openReceiver("dummy").openFuture().get();
        HeaderMatcher headerMatcher = new HeaderMatcher(false);
        headerMatcher.withDurable(true);
        headerMatcher.withPriority((byte) 1);
        headerMatcher.withTtl(65535);
        headerMatcher.withFirstAcquirer(true);
        headerMatcher.withDeliveryCount(2);
        TransferPayloadCompositeMatcher payloadMatcher = new TransferPayloadCompositeMatcher();
        payloadMatcher.setHeadersMatcher(headerMatcher);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectTransfer().withPayload(payloadMatcher).accept();
        peer.expectDetach().respond();
        peer.expectClose().respond();
        final Message<String> message = Message.create();
        final AdvancedMessage<String> advanced = message.toAdvancedMessage();
        // Populate all Header values
        Header header = new Header();
        header.setDurable(true);
        header.setPriority((byte) 1);
        header.setTimeToLive(65535);
        header.setFirstAcquirer(true);
        header.setDeliveryCount(2);
        advanced.header(header);
        final Tracker tracker = sender.send(advanced);
        assertNotNull(tracker);
        assertNotNull(tracker.settlementFuture().isDone());
        assertNotNull(tracker.settlementFuture().get().settled());
        sender.closeAsync().get(10, TimeUnit.SECONDS);
        connection.closeAsync().get(10, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : HeaderMatcher(org.apache.qpid.protonj2.test.driver.matchers.messaging.HeaderMatcher) Tracker(org.apache.qpid.protonj2.client.Tracker) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) SenderOptions(org.apache.qpid.protonj2.client.SenderOptions) Connection(org.apache.qpid.protonj2.client.Connection) TransferPayloadCompositeMatcher(org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher) URI(java.net.URI) Sender(org.apache.qpid.protonj2.client.Sender) Header(org.apache.qpid.protonj2.types.messaging.Header) Client(org.apache.qpid.protonj2.client.Client) Session(org.apache.qpid.protonj2.client.Session) Test(org.junit.jupiter.api.Test)

Example 97 with Session

use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.

the class ReceiverTest method doTestReceiveTimedCallFailsWhenReceiverDetachedOrClosed.

private void doTestReceiveTimedCallFailsWhenReceiverDetachedOrClosed(boolean close) throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofReceiver().respond();
        peer.expectFlow().withLinkCredit(10);
        peer.expectDetach().withClosed(close).respond();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
        Session session = connection.openSession();
        Receiver receiver = session.openReceiver("test-queue").openFuture().get();
        if (close) {
            receiver.closeAsync();
        } else {
            receiver.detachAsync();
        }
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        try {
            receiver.receive(60, TimeUnit.SECONDS);
            fail("Receive call should fail when link closed or detached.");
        } catch (ClientIllegalStateException cliEx) {
            LOG.debug("Receiver threw error on receive call", cliEx);
        }
        peer.expectClose().respond();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) ClientIllegalStateException(org.apache.qpid.protonj2.client.exceptions.ClientIllegalStateException) Receiver(org.apache.qpid.protonj2.client.Receiver) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session)

Example 98 with Session

use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.

the class ReceiverTest method testReceiveBlockedForMessageFailsWhenConnectionRemotelyClosed.

@Test
public void testReceiveBlockedForMessageFailsWhenConnectionRemotelyClosed() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofReceiver().respond();
        peer.expectFlow().withLinkCredit(10);
        peer.remoteClose().withErrorCondition(AmqpError.RESOURCE_DELETED.toString(), "Connection was deleted").afterDelay(25).queue();
        peer.expectClose();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Receiver test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
        Session session = connection.openSession();
        Receiver receiver = session.openReceiver("test-queue");
        receiver.openFuture().get();
        try {
            receiver.receive();
            fail("Receive should have failed when Connection remotely closed.");
        } catch (ClientConnectionRemotelyClosedException cliEx) {
        // Expected
        }
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ClientConnectionRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException) Connection(org.apache.qpid.protonj2.client.Connection) Receiver(org.apache.qpid.protonj2.client.Receiver) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session) Test(org.junit.jupiter.api.Test)

Example 99 with Session

use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.

the class ReceiverTest method doTestCreateReceiverWithConfiguredQoS.

private void doTestCreateReceiverWithConfiguredQoS(DeliveryMode qos) throws Exception {
    byte sndMode = qos == DeliveryMode.AT_MOST_ONCE ? SenderSettleMode.SETTLED.byteValue() : SenderSettleMode.UNSETTLED.byteValue();
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().withRole(Role.RECEIVER.getValue()).withSndSettleMode(sndMode).withRcvSettleMode(ReceiverSettleMode.FIRST.byteValue()).respond().withSndSettleMode(sndMode).withRcvSettleMode(ReceiverSettleMode.FIRST.byteValue());
        peer.expectFlow();
        peer.expectDetach().respond();
        peer.expectClose().respond();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
        connection.openFuture().get(10, TimeUnit.SECONDS);
        Session session = connection.openSession();
        session.openFuture().get(10, TimeUnit.SECONDS);
        ReceiverOptions options = new ReceiverOptions().deliveryMode(qos);
        Receiver receiver = session.openReceiver("test-qos", options);
        receiver.openFuture().get(10, TimeUnit.SECONDS);
        assertEquals("test-qos", receiver.address());
        receiver.closeAsync().get(10, TimeUnit.SECONDS);
        connection.closeAsync().get(10, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) ReceiverOptions(org.apache.qpid.protonj2.client.ReceiverOptions) Receiver(org.apache.qpid.protonj2.client.Receiver) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session)

Example 100 with Session

use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.

the class ReceiverTest method doTestOpenReceiverWaitFailsWhenConnectionDrops.

private void doTestOpenReceiverWaitFailsWhenConnectionDrops(boolean timeout) throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofReceiver();
        peer.expectFlow();
        peer.dropAfterLastHandler(10);
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Sender test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
        Session session = connection.openSession();
        Receiver receiver = session.openReceiver("test-queue");
        try {
            if (timeout) {
                receiver.openFuture().get(10, TimeUnit.SECONDS);
            } else {
                receiver.openFuture().get();
            }
            fail("Should not complete the open future without an error");
        } catch (ExecutionException exe) {
            Throwable cause = exe.getCause();
            assertTrue(cause instanceof ClientIOException);
        }
        connection.closeAsync().get(10, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ClientIOException(org.apache.qpid.protonj2.client.exceptions.ClientIOException) Connection(org.apache.qpid.protonj2.client.Connection) Receiver(org.apache.qpid.protonj2.client.Receiver) Client(org.apache.qpid.protonj2.client.Client) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session)

Aggregations

Session (org.apache.qpid.protonj2.client.Session)167 Client (org.apache.qpid.protonj2.client.Client)165 Connection (org.apache.qpid.protonj2.client.Connection)165 URI (java.net.URI)163 ProtonTestServer (org.apache.qpid.protonj2.test.driver.ProtonTestServer)163 Test (org.junit.jupiter.api.Test)109 Sender (org.apache.qpid.protonj2.client.Sender)66 Receiver (org.apache.qpid.protonj2.client.Receiver)64 ExecutionException (java.util.concurrent.ExecutionException)35 ConnectionOptions (org.apache.qpid.protonj2.client.ConnectionOptions)35 Tracker (org.apache.qpid.protonj2.client.Tracker)32 ClientException (org.apache.qpid.protonj2.client.exceptions.ClientException)29 SenderOptions (org.apache.qpid.protonj2.client.SenderOptions)28 ReceiverOptions (org.apache.qpid.protonj2.client.ReceiverOptions)25 TransferPayloadCompositeMatcher (org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher)20 ClientConnectionRemotelyClosedException (org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException)18 HashMap (java.util.HashMap)17 ClientIllegalStateException (org.apache.qpid.protonj2.client.exceptions.ClientIllegalStateException)16 EncodedAmqpValueMatcher (org.apache.qpid.protonj2.test.driver.matchers.types.EncodedAmqpValueMatcher)14 Delivery (org.apache.qpid.protonj2.client.Delivery)13