Search in sources :

Example 21 with Session

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

the class SenderTest method testSendCompletesWhenCreditEventuallyOffered.

@Test
public void testSendCompletesWhenCreditEventuallyOffered() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().respond();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Sender test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        ConnectionOptions options = new ConnectionOptions();
        options.sendTimeout(200);
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), options);
        Session session = connection.openSession();
        Sender sender = session.openSender("test-queue");
        sender.openFuture().get(10, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        // Expect a transfer but only after the flow which is delayed to allow the
        // client time to block on credit.
        peer.expectTransfer().withNonNullPayload();
        peer.remoteFlow().withDeliveryCount(0).withLinkCredit(1).withIncomingWindow(1024).withOutgoingWindow(10).withNextIncomingId(0).withNextOutgoingId(1).later(30);
        peer.expectDetach().respond();
        peer.expectClose().respond();
        Message<String> message = Message.create("Hello World");
        try {
            LOG.debug("Attempting send with sender: {}", sender);
            sender.send(message);
        } catch (ClientSendTimedOutException ex) {
            fail("Should not throw a send timed out exception");
        }
        sender.closeAsync().get(10, TimeUnit.SECONDS);
        connection.closeAsync().get(10, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : Sender(org.apache.qpid.protonj2.client.Sender) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) ClientSendTimedOutException(org.apache.qpid.protonj2.client.exceptions.ClientSendTimedOutException) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) 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 22 with Session

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

the class SenderTest method doTestCloseOrDetachSenderTimesOutWhenNoCloseResponseReceived.

private void doTestCloseOrDetachSenderTimesOutWhenNoCloseResponseReceived(boolean close, boolean timeout) throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().respond();
        peer.expectDetach();
        peer.expectClose().respond();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Sender test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        ConnectionOptions options = new ConnectionOptions();
        options.closeTimeout(10);
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), options);
        Session session = connection.openSession();
        Sender sender = session.openSender("test-queue");
        sender.openFuture().get(10, TimeUnit.SECONDS);
        try {
            if (close) {
                if (timeout) {
                    sender.closeAsync().get(10, TimeUnit.SECONDS);
                } else {
                    sender.closeAsync().get();
                }
            } else {
                if (timeout) {
                    sender.detachAsync().get(10, TimeUnit.SECONDS);
                } else {
                    sender.detachAsync().get();
                }
            }
            fail("Should not complete the close or detach future without an error");
        } catch (ExecutionException exe) {
            Throwable cause = exe.getCause();
            assertTrue(cause instanceof ClientOperationTimedOutException);
        }
        connection.closeAsync().get(10, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : Sender(org.apache.qpid.protonj2.client.Sender) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) ClientOperationTimedOutException(org.apache.qpid.protonj2.client.exceptions.ClientOperationTimedOutException) Client(org.apache.qpid.protonj2.client.Client) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session)

Example 23 with Session

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

the class SenderTest method testCreateSenderWithUserConfiguredSourceAndTargetOptions.

@Test
public void testCreateSenderWithUserConfiguredSourceAndTargetOptions() throws Exception {
    final Map<String, Object> filtersToObject = new HashMap<>();
    filtersToObject.put("x-opt-filter", "a = b");
    final Map<String, String> filters = new HashMap<>();
    filters.put("x-opt-filter", "a = b");
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().withSource().withAddress(notNullValue()).withDistributionMode("copy").withTimeout(128).withDurable(TerminusDurability.UNSETTLED_STATE).withExpiryPolicy(TerminusExpiryPolicy.CONNECTION_CLOSE).withDefaultOutcome(new Released()).withCapabilities("QUEUE").withFilter(filtersToObject).withOutcomes("amqp:accepted:list", "amqp:rejected:list").also().withTarget().withAddress("test-queue").withCapabilities("QUEUE").withDurable(TerminusDurability.CONFIGURATION).withExpiryPolicy(TerminusExpiryPolicy.SESSION_END).withTimeout(42).withDynamic(anyOf(nullValue(), equalTo(false))).withDynamicNodeProperties(nullValue()).and().respond();
        peer.expectDetach().respond();
        peer.expectEnd().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());
        Session session = connection.openSession();
        SenderOptions senderOptions = new SenderOptions();
        senderOptions.sourceOptions().capabilities("QUEUE");
        senderOptions.sourceOptions().distributionMode(DistributionMode.COPY);
        senderOptions.sourceOptions().timeout(128);
        senderOptions.sourceOptions().durabilityMode(DurabilityMode.UNSETTLED_STATE);
        senderOptions.sourceOptions().expiryPolicy(ExpiryPolicy.CONNECTION_CLOSE);
        senderOptions.sourceOptions().defaultOutcome(DeliveryState.released());
        senderOptions.sourceOptions().filters(filters);
        senderOptions.sourceOptions().outcomes(DeliveryState.Type.ACCEPTED, DeliveryState.Type.REJECTED);
        senderOptions.targetOptions().capabilities("QUEUE");
        senderOptions.targetOptions().durabilityMode(DurabilityMode.CONFIGURATION);
        senderOptions.targetOptions().expiryPolicy(ExpiryPolicy.SESSION_CLOSE);
        senderOptions.targetOptions().timeout(42);
        Sender sender = session.openSender("test-queue", senderOptions).openFuture().get();
        sender.close();
        session.close();
        connection.close();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) SenderOptions(org.apache.qpid.protonj2.client.SenderOptions) Released(org.apache.qpid.protonj2.test.driver.codec.messaging.Released) HashMap(java.util.HashMap) Connection(org.apache.qpid.protonj2.client.Connection) URI(java.net.URI) Sender(org.apache.qpid.protonj2.client.Sender) Client(org.apache.qpid.protonj2.client.Client) Session(org.apache.qpid.protonj2.client.Session) Test(org.junit.jupiter.api.Test)

Example 24 with Session

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

the class SenderTest method doTestSenderSendingSettledCompletesTrackerAcknowledgeFuture.

private void doTestSenderSendingSettledCompletesTrackerAcknowledgeFuture(boolean trySend) throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().withSenderSettleModeSettled().withReceiverSettlesFirst().respond().withSenderSettleModeSettled().withReceivervSettlesFirst();
        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);
        assertEquals("test-qos", sender.address());
        session.openReceiver("dummy").openFuture().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectTransfer().withNonNullPayload();
        peer.expectDetach().respond();
        peer.expectClose().respond();
        final Message<String> message = Message.create("Hello World");
        final Tracker tracker;
        if (trySend) {
            tracker = sender.trySend(message);
        } else {
            tracker = sender.send(message);
        }
        assertNotNull(tracker);
        assertTrue(tracker.settlementFuture().isDone());
        assertTrue(tracker.settlementFuture().get().settled());
        assertFalse(tracker.settlementFuture().get().remoteSettled());
        sender.closeAsync().get(10, TimeUnit.SECONDS);
        connection.closeAsync().get(10, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : Sender(org.apache.qpid.protonj2.client.Sender) 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) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session)

Example 25 with Session

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

the class SenderTest method testSendBlockedForCreditFailsWhenConnectionDrops.

@Test
public void testSendBlockedForCreditFailsWhenConnectionDrops() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().respond();
        peer.dropAfterLastHandler(25);
        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();
        Sender sender = session.openSender("test-queue");
        sender.openFuture().get();
        Message<String> message = Message.create("Hello World");
        try {
            sender.send(message);
            fail("Send should have timed out.");
        } catch (ClientConnectionRemotelyClosedException cliEx) {
        // Expected send to throw indicating that the remote closed unexpectedly
        }
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : Sender(org.apache.qpid.protonj2.client.Sender) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ClientConnectionRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException) Connection(org.apache.qpid.protonj2.client.Connection) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session) Test(org.junit.jupiter.api.Test)

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