Search in sources :

Example 66 with Session

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

the class ReceiverTest method testReceiverHandlesAbortedSplitFrameTransferAndReplenishesCredit.

@Test
public void testReceiverHandlesAbortedSplitFrameTransferAndReplenishesCredit() throws Exception {
    final byte[] payload = createEncodedMessage(new AmqpValue<>("Hello World"));
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().withRole(Role.RECEIVER.getValue()).respond();
        peer.expectFlow().withLinkCredit(1);
        peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(true).withMessageFormat(0).withPayload(payload).queue();
        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();
        ReceiverOptions options = new ReceiverOptions();
        options.creditWindow(1);
        final Receiver receiver = session.openReceiver("test-queue", options);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        assertNull(receiver.receive(15, TimeUnit.MILLISECONDS));
        // Credit window is one and next transfer signals aborted so receiver should
        // top-up the credit window to allow more transfers to arrive.
        peer.expectFlow().withLinkCredit(1);
        peer.expectDetach().respond();
        peer.expectClose().respond();
        // Abort the delivery which should result in a credit top-up.
        peer.remoteTransfer().withHandle(0).withMore(false).withAborted(true).withMessageFormat(0).withPayload(payload).now();
        assertNull(receiver.receive(15, TimeUnit.MILLISECONDS));
        receiver.closeAsync();
        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) 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) Test(org.junit.jupiter.api.Test)

Example 67 with Session

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

the class ReceiverTest method doTestDrainFutureSignalsFailureWhenReceiverClosedOrDetached.

private void doTestDrainFutureSignalsFailureWhenReceiverClosedOrDetached(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.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();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectFlow().withDrain(true).withLinkCredit(10);
        peer.execute(() -> {
            if (close) {
                receiver.closeAsync();
            } else {
                receiver.detachAsync();
            }
        }).queue();
        peer.expectDetach().withClosed(close).respond();
        peer.expectClose().respond();
        try {
            receiver.drain().get(10, TimeUnit.SECONDS);
            fail("Drain call should fail when link closed or detached.");
        } catch (ExecutionException cliEx) {
            LOG.debug("Receiver threw error on drain call", cliEx);
            assertTrue(cliEx.getCause() instanceof ClientException);
        }
        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) Receiver(org.apache.qpid.protonj2.client.Receiver) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) Client(org.apache.qpid.protonj2.client.Client) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session)

Example 68 with Session

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

the class ReceiverTest method tryReadReceiverSource.

private void tryReadReceiverSource(boolean attachResponse) throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().withRole(Role.RECEIVER.getValue());
        peer.expectFlow();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        ConnectionOptions options = new ConnectionOptions().openTimeout(100);
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), options);
        connection.openFuture().get();
        Session session = connection.openSession();
        session.openFuture().get();
        Receiver receiver = session.openReceiver("test-receiver");
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        if (attachResponse) {
            peer.expectDetach().respond();
            peer.respondToLastAttach().later(10);
        } else {
            peer.expectDetach();
        }
        if (attachResponse) {
            assertNotNull(receiver.source(), "Remote should have responded with a Source value");
            assertEquals("test-receiver", receiver.source().address());
        } else {
            try {
                receiver.source();
                fail("Should failed to get remote source due to no attach response");
            } catch (ClientException ex) {
                LOG.debug("Caught expected exception from blocking call", ex);
            }
        }
        try {
            receiver.closeAsync().get();
        } catch (ExecutionException ex) {
            LOG.debug("Caught unexpected exception from close call", ex);
            fail("Should not fail to close when connection not closed and detach sent");
        }
        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) Receiver(org.apache.qpid.protonj2.client.Receiver) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) Client(org.apache.qpid.protonj2.client.Client) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session)

Example 69 with Session

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

the class ReceiverTest method tryReadReceiverRemoteOfferedCapabilities.

private void tryReadReceiverRemoteOfferedCapabilities(boolean attachResponse) throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().withRole(Role.RECEIVER.getValue());
        peer.expectFlow();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        ConnectionOptions options = new ConnectionOptions().openTimeout(100);
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), options);
        connection.openFuture().get();
        Session session = connection.openSession();
        session.openFuture().get();
        Receiver receiver = session.openReceiver("test-receiver");
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        if (attachResponse) {
            peer.expectDetach().respond();
            peer.respondToLastAttach().withOfferedCapabilities("QUEUE").later(10);
        } else {
            peer.expectDetach();
        }
        if (attachResponse) {
            assertNotNull(receiver.offeredCapabilities(), "Remote should have responded with a remote offered Capabilities value");
            assertEquals(1, receiver.offeredCapabilities().length);
            assertEquals("QUEUE", receiver.offeredCapabilities()[0]);
        } else {
            try {
                receiver.offeredCapabilities();
                fail("Should failed to get remote state due to no attach response");
            } catch (ClientException ex) {
                LOG.debug("Caught expected exception from blocking call", ex);
            }
        }
        try {
            receiver.closeAsync().get();
        } catch (ExecutionException ex) {
            LOG.debug("Caught unexpected exception from close call", ex);
            fail("Should not fail to close when connection not closed and detach sent");
        }
        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) Receiver(org.apache.qpid.protonj2.client.Receiver) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) Client(org.apache.qpid.protonj2.client.Client) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session)

Example 70 with Session

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

the class ReceiverTest method testAddCreditFailsWhileDrainPending.

@Test
public void testAddCreditFailsWhileDrainPending() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().withRole(Role.RECEIVER.getValue()).respond().withInitialDeliveryCount(20);
        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", new ReceiverOptions().creditWindow(0));
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        // Add some credit, verify not draining
        int credit = 7;
        Matcher<Boolean> drainMatcher = anyOf(equalTo(false), nullValue());
        peer.expectFlow().withDrain(drainMatcher).withLinkCredit(credit);
        // Ensure we get the attach response with the initial delivery count.
        receiver.openFuture().get().addCredit(credit);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        // Drain all the credit
        peer.expectFlow().withDrain(true).withLinkCredit(credit).withDeliveryCount(20);
        peer.expectClose().respond();
        Future<? extends Receiver> draining = receiver.drain();
        assertFalse(draining.isDone());
        try {
            receiver.addCredit(1);
            fail("Should not allow add credit when drain is pending");
        } catch (ClientIllegalStateException ise) {
        // Expected
        }
        connection.closeAsync().get();
        peer.waitForScriptToComplete(1, 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) ReceiverOptions(org.apache.qpid.protonj2.client.ReceiverOptions) 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