Search in sources :

Example 41 with Session

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

the class ReconnectTransactionTest method testDeclareTransactionAfterConnectionDropsAndReconnects.

@Test
public void testDeclareTransactionAfterConnectionDropsAndReconnects() throws Exception {
    final byte[] txnId = new byte[] { 0, 1, 2, 3 };
    try (ProtonTestServer firstPeer = new ProtonTestServer();
        ProtonTestServer finalPeer = new ProtonTestServer()) {
        firstPeer.expectSASLAnonymousConnect();
        firstPeer.expectOpen().respond();
        firstPeer.expectBegin().respond();
        firstPeer.dropAfterLastHandler();
        firstPeer.start();
        finalPeer.expectSASLAnonymousConnect();
        finalPeer.expectOpen().respond();
        finalPeer.expectBegin().respond();
        finalPeer.start();
        final URI primaryURI = firstPeer.getServerURI();
        final URI backupURI = finalPeer.getServerURI();
        ConnectionOptions options = new ConnectionOptions();
        options.reconnectOptions().reconnectEnabled(true);
        options.reconnectOptions().addReconnectLocation(backupURI.getHost(), backupURI.getPort());
        Client container = Client.create();
        Connection connection = container.connect(primaryURI.getHost(), primaryURI.getPort(), options);
        Session session = connection.openSession().openFuture().get();
        firstPeer.waitForScriptToComplete();
        finalPeer.waitForScriptToComplete();
        finalPeer.expectCoordinatorAttach().respond();
        finalPeer.remoteFlow().withLinkCredit(2).queue();
        finalPeer.expectDeclare().accept(txnId);
        finalPeer.expectClose().respond();
        try {
            session.beginTransaction();
        } catch (ClientException cliEx) {
            LOG.info("Caught unexpected error from test", cliEx);
            fail("Should not have failed to declare transaction");
        }
        connection.closeAsync().get();
        finalPeer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) 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 42 with Session

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

the class ReconnectSenderTest method testOpenedSenderRecoveredAfterConnectionDropped.

@Test
public void testOpenedSenderRecoveredAfterConnectionDropped() throws Exception {
    try (ProtonTestServer firstPeer = new ProtonTestServer();
        ProtonTestServer finalPeer = new ProtonTestServer()) {
        firstPeer.expectSASLAnonymousConnect();
        firstPeer.expectOpen().respond();
        firstPeer.expectBegin().respond();
        firstPeer.expectAttach().ofSender().withTarget().withAddress("test").and().respond();
        firstPeer.dropAfterLastHandler(5);
        firstPeer.start();
        finalPeer.expectSASLAnonymousConnect();
        finalPeer.expectOpen().respond();
        finalPeer.expectBegin().respond();
        finalPeer.expectAttach().ofSender().withTarget().withAddress("test").and().respond();
        finalPeer.start();
        final URI primaryURI = firstPeer.getServerURI();
        final URI backupURI = finalPeer.getServerURI();
        ConnectionOptions options = new ConnectionOptions();
        options.reconnectOptions().reconnectEnabled(true);
        options.reconnectOptions().addReconnectLocation(backupURI.getHost(), backupURI.getPort());
        Client container = Client.create();
        Connection connection = container.connect(primaryURI.getHost(), primaryURI.getPort(), options);
        Session session = connection.openSession();
        Sender sender = session.openSender("test");
        firstPeer.waitForScriptToComplete();
        finalPeer.waitForScriptToComplete();
        finalPeer.expectDetach().withClosed(true).respond();
        finalPeer.expectEnd().respond();
        finalPeer.expectClose().respond();
        sender.close();
        session.close();
        connection.close();
        finalPeer.waitForScriptToComplete();
    }
}
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) 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 43 with Session

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

the class ReconnectSenderTest method testAwaitSettlementOnSendFiredBeforeConnectionDrops.

@Test
public void testAwaitSettlementOnSendFiredBeforeConnectionDrops() throws Exception {
    try (ProtonTestServer firstPeer = new ProtonTestServer();
        ProtonTestServer finalPeer = new ProtonTestServer()) {
        firstPeer.expectSASLAnonymousConnect();
        firstPeer.expectOpen().respond();
        firstPeer.expectBegin().respond();
        firstPeer.expectAttach().ofSender().withTarget().withAddress("test").and().respond();
        firstPeer.remoteFlow().withLinkCredit(1).queue();
        firstPeer.expectTransfer().withNonNullPayload();
        firstPeer.dropAfterLastHandler(15);
        firstPeer.start();
        finalPeer.expectSASLAnonymousConnect();
        finalPeer.expectOpen().respond();
        finalPeer.expectBegin().respond();
        finalPeer.expectAttach().ofSender().withTarget().withAddress("test").and().respond();
        finalPeer.start();
        final URI primaryURI = firstPeer.getServerURI();
        final URI backupURI = finalPeer.getServerURI();
        ConnectionOptions options = new ConnectionOptions();
        options.reconnectOptions().reconnectEnabled(true);
        options.reconnectOptions().addReconnectLocation(backupURI.getHost(), backupURI.getPort());
        Client container = Client.create();
        Connection connection = container.connect(primaryURI.getHost(), primaryURI.getPort(), options);
        Session session = connection.openSession();
        Sender sender = session.openSender("test");
        Tracker tracker = sender.send(Message.create("Hello"));
        firstPeer.waitForScriptToComplete();
        finalPeer.waitForScriptToComplete();
        finalPeer.expectDetach().withClosed(true).respond();
        finalPeer.expectEnd().respond();
        finalPeer.expectClose().respond();
        try {
            tracker.awaitSettlement(120, TimeUnit.SECONDS);
            fail("Should not be able to successfully await settlement");
        } catch (ClientConnectionRemotelyClosedException crce) {
        }
        assertFalse(tracker.remoteSettled());
        assertNull(tracker.remoteState());
        sender.close();
        session.close();
        connection.close();
        finalPeer.waitForScriptToComplete();
    }
}
Also used : Sender(org.apache.qpid.protonj2.client.Sender) Tracker(org.apache.qpid.protonj2.client.Tracker) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ClientConnectionRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException) Connection(org.apache.qpid.protonj2.client.Connection) 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 44 with Session

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

the class ReconnectSenderTest method testSendBlockedOnCreditGetsSentAfterReconnectAndCreditGranted.

@Test
public void testSendBlockedOnCreditGetsSentAfterReconnectAndCreditGranted() throws Exception {
    try (ProtonTestServer firstPeer = new ProtonTestServer();
        ProtonTestServer finalPeer = new ProtonTestServer()) {
        firstPeer.expectSASLAnonymousConnect();
        firstPeer.expectOpen().respond();
        firstPeer.expectBegin().respond();
        firstPeer.expectAttach().ofSender().withTarget().withAddress("test").and().respond();
        firstPeer.dropAfterLastHandler(15);
        firstPeer.start();
        finalPeer.expectSASLAnonymousConnect();
        finalPeer.expectOpen().respond();
        finalPeer.expectBegin().respond();
        finalPeer.expectAttach().ofSender().withTarget().withAddress("test").and().respond();
        finalPeer.start();
        final URI primaryURI = firstPeer.getServerURI();
        final URI backupURI = finalPeer.getServerURI();
        ConnectionOptions options = new ConnectionOptions();
        options.reconnectOptions().reconnectEnabled(true);
        options.reconnectOptions().addReconnectLocation(backupURI.getHost(), backupURI.getPort());
        Client container = Client.create();
        Connection connection = container.connect(primaryURI.getHost(), primaryURI.getPort(), options);
        Session session = connection.openSession();
        Sender sender = session.openSender("test");
        final AtomicReference<Tracker> tracker = new AtomicReference<>();
        final AtomicReference<Exception> sendError = new AtomicReference<>();
        final CountDownLatch latch = new CountDownLatch(1);
        ForkJoinPool.commonPool().execute(() -> {
            try {
                tracker.set(sender.send(Message.create("Hello")));
            } catch (ClientException e) {
                sendError.set(e);
            } finally {
                latch.countDown();
            }
        });
        firstPeer.waitForScriptToComplete();
        finalPeer.waitForScriptToComplete();
        finalPeer.expectTransfer().withNonNullPayload().respond().withSettled(true).withState().accepted();
        finalPeer.expectDetach().withClosed(true).respond();
        finalPeer.expectEnd().respond();
        finalPeer.expectClose().respond();
        // Grant credit now and await expected message send.
        finalPeer.remoteFlow().withDeliveryCount(0).withLinkCredit(10).withIncomingWindow(10).withOutgoingWindow(10).withNextIncomingId(0).withNextOutgoingId(1).now();
        assertTrue(latch.await(10, TimeUnit.SECONDS), "Should have sent blocked message");
        assertNull(sendError.get());
        assertNotNull(tracker.get());
        Tracker send = tracker.get();
        assertSame(tracker.get(), send.awaitSettlement(10, TimeUnit.SECONDS));
        assertTrue(send.remoteSettled());
        assertEquals(DeliveryState.accepted(), send.remoteState());
        sender.close();
        session.close();
        connection.close();
        finalPeer.waitForScriptToComplete();
    }
}
Also used : Tracker(org.apache.qpid.protonj2.client.Tracker) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) ClientConnectionRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException) Sender(org.apache.qpid.protonj2.client.Sender) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) Client(org.apache.qpid.protonj2.client.Client) Session(org.apache.qpid.protonj2.client.Session) Test(org.junit.jupiter.api.Test)

Example 45 with Session

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

the class ReconnectReceiverTest method testDispositionFromDeliveryReceivedBeforeDisconnectIsNoOp.

@Test
public void testDispositionFromDeliveryReceivedBeforeDisconnectIsNoOp() throws Exception {
    final byte[] payload = createEncodedMessage(new AmqpValue<>("Hello World"));
    try (ProtonTestServer firstPeer = new ProtonTestServer();
        ProtonTestServer finalPeer = new ProtonTestServer()) {
        firstPeer.expectSASLAnonymousConnect();
        firstPeer.expectOpen().respond();
        firstPeer.expectBegin().respond();
        firstPeer.expectAttach().withRole(Role.RECEIVER.getValue()).respond();
        firstPeer.expectFlow().withLinkCredit(10);
        firstPeer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(false).withSettled(true).withMessageFormat(0).withPayload(payload).queue();
        firstPeer.dropAfterLastHandler(100);
        firstPeer.start();
        finalPeer.expectSASLAnonymousConnect();
        finalPeer.expectOpen().respond();
        finalPeer.expectBegin().respond();
        finalPeer.expectAttach().withRole(Role.RECEIVER.getValue()).respond();
        finalPeer.expectFlow().withLinkCredit(9);
        finalPeer.start();
        final URI primaryURI = firstPeer.getServerURI();
        final URI backupURI = finalPeer.getServerURI();
        ConnectionOptions options = new ConnectionOptions();
        options.reconnectOptions().reconnectEnabled(true);
        options.reconnectOptions().addReconnectLocation(backupURI.getHost(), backupURI.getPort());
        Client container = Client.create();
        Connection connection = container.connect(primaryURI.getHost(), primaryURI.getPort(), options);
        Session session = connection.openSession();
        ReceiverOptions rcvOpts = new ReceiverOptions().autoAccept(false);
        Receiver receiver = session.openReceiver("test-queue", rcvOpts);
        Delivery delivery = receiver.receive(10, TimeUnit.SECONDS);
        firstPeer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        finalPeer.waitForScriptToComplete();
        finalPeer.expectDetach().respond();
        finalPeer.expectEnd().respond();
        finalPeer.expectClose().respond();
        assertNotNull(delivery);
        delivery.accept();
        receiver.close();
        session.close();
        connection.close();
        assertNotNull(delivery);
    }
}
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) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) 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)

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