Search in sources :

Example 51 with Session

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

the class ReceiverTest method tryReadDynamicReceiverAddress.

private void tryReadDynamicReceiverAddress(boolean attachResponse) throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().withRole(Role.RECEIVER.getValue()).withSource().withDynamic(true).withAddress((String) null);
        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.openDynamicReceiver();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        if (attachResponse) {
            peer.expectDetach().respond();
            peer.respondToLastAttach().withSource().withAddress("test-dynamic-node").and().later(10);
        } else {
            peer.expectDetach();
        }
        if (attachResponse) {
            assertNotNull(receiver.address(), "Remote should have assigned the address for the dynamic receiver");
            assertEquals("test-dynamic-node", receiver.address());
        } else {
            try {
                receiver.address();
                fail("Should failed to get address due to no attach response");
            } catch (ClientException ex) {
                LOG.debug("Caught expected exception from address 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 52 with Session

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

the class ReceiverTest method testReceiverAddCreditOnAbortedTransferWhenNeeded.

@Test
public void testReceiverAddCreditOnAbortedTransferWhenNeeded() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().withRole(Role.RECEIVER.getValue()).respond();
        peer.expectFlow();
        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);
        receiver.openFuture().get();
        final byte[] payload = createEncodedMessage(new AmqpValue<>("Hello World"));
        peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(true).withMessageFormat(0).withPayload(payload).now();
        assertNull(receiver.tryReceive());
        peer.expectFlow();
        peer.remoteTransfer().withHandle(0).withDeliveryId(1).withMessageFormat(0).withMore(false).withPayload(payload).queue();
        peer.expectDisposition().withSettled(true).withState().accepted();
        peer.expectFlow();
        peer.expectDetach().respond();
        peer.expectClose().respond();
        // Send final aborted transfer to complete first transfer and allow next to commence.
        peer.remoteTransfer().withHandle(0).withDeliveryId(0).withMore(false).withAborted(true).withMessageFormat(0).withPayload(payload).now();
        Delivery delivery = receiver.receive();
        assertNotNull(delivery);
        Message<?> received = delivery.message();
        assertNotNull(received);
        assertTrue(received.body() instanceof String);
        String value = (String) received.body();
        assertEquals("Hello World", value);
        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) Delivery(org.apache.qpid.protonj2.client.Delivery) 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 53 with Session

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

the class ReceiverTest method testDrainFutureSignalsFailureWhenDrainTimeoutExceeded.

@Test
public void testDrainFutureSignalsFailureWhenDrainTimeoutExceeded() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofReceiver().respond();
        peer.expectFlow();
        peer.expectFlow().withDrain(true);
        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();
        ReceiverOptions receiverOptions = new ReceiverOptions().drainTimeout(15);
        Receiver receiver = session.openReceiver("test-queue", receiverOptions).openFuture().get();
        try {
            receiver.drain().get();
            fail("Drain call should fail timeout exceeded.");
        } catch (ExecutionException cliEx) {
            LOG.debug("Receiver threw error on drain call", cliEx);
            assertTrue(cliEx.getCause() instanceof ClientOperationTimedOutException);
        }
        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) 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) Test(org.junit.jupiter.api.Test)

Example 54 with Session

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

the class ReceiverTest method testReceiverOpenRejectedByRemote.

@Test
public void testReceiverOpenRejectedByRemote() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().respond().withNullSource();
        peer.expectFlow();
        peer.remoteDetach().withErrorCondition(AmqpError.UNAUTHORIZED_ACCESS.toString(), "Cannot read from this address").queue();
        peer.expectDetach();
        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();
        Session session = connection.openSession();
        session.openFuture().get();
        Receiver receiver = session.openReceiver("test-queue");
        try {
            receiver.openFuture().get();
            fail("Open of receiver should fail due to remote indicating pending close.");
        } catch (ExecutionException exe) {
            assertNotNull(exe.getCause());
            assertTrue(exe.getCause() instanceof ClientLinkRemotelyClosedException);
            ClientLinkRemotelyClosedException linkClosed = (ClientLinkRemotelyClosedException) exe.getCause();
            assertNotNull(linkClosed.getErrorCondition());
            assertEquals(AmqpError.UNAUTHORIZED_ACCESS.toString(), linkClosed.getErrorCondition().condition());
        }
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        // Should not result in any close being sent now, already closed.
        receiver.closeAsync().get();
        peer.expectClose().respond();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(1, TimeUnit.SECONDS);
    }
}
Also used : ClientLinkRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientLinkRemotelyClosedException) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) 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) Test(org.junit.jupiter.api.Test)

Example 55 with Session

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

the class ReceiverTest method testDrainCompletesWhenReceiverHasNoCredit.

@Test
public void testDrainCompletesWhenReceiverHasNoCredit() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().withRole(Role.RECEIVER.getValue()).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", new ReceiverOptions().creditWindow(0));
        receiver.openFuture().get(5, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        Future<? extends Receiver> draining = receiver.drain();
        draining.get(5, TimeUnit.SECONDS);
        // Close things down
        peer.expectClose().respond();
        connection.closeAsync().get(5, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(1, 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) 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