Search in sources :

Example 11 with Session

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

the class TransactionsTest method testBeginAndCommitTransactions.

@Test
public void testBeginAndCommitTransactions() throws Exception {
    final byte[] txnId1 = new byte[] { 0, 1, 2, 3 };
    final byte[] txnId2 = new byte[] { 1, 1, 2, 3 };
    final byte[] txnId3 = new byte[] { 2, 1, 2, 3 };
    final byte[] txnId4 = new byte[] { 3, 1, 2, 3 };
    final byte[] txnId5 = new byte[] { 4, 1, 2, 3 };
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectCoordinatorAttach().respond();
        peer.remoteFlow().withLinkCredit(10).queue();
        peer.expectDeclare().accept(txnId1);
        peer.expectDischarge().withFail(false).withTxnId(txnId1).accept();
        peer.expectDeclare().accept(txnId2);
        peer.expectDischarge().withFail(false).withTxnId(txnId2).accept();
        peer.expectDeclare().accept(txnId3);
        peer.expectDischarge().withFail(false).withTxnId(txnId3).accept();
        peer.expectDeclare().accept(txnId4);
        peer.expectDischarge().withFail(false).withTxnId(txnId4).accept();
        peer.expectDeclare().accept(txnId5);
        peer.expectDischarge().withFail(false).withTxnId(txnId5).accept();
        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().openFuture().get();
        for (int i = 0; i < 5; ++i) {
            LOG.info("Transaction declare and discharge cycle: {}", i);
            session.beginTransaction();
            session.commitTransaction();
        }
        session.closeAsync();
        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) 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 12 with Session

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

the class TransactionsTest method testTimedOutExceptionOnBeginWithNoResponse.

@Test
public void testTimedOutExceptionOnBeginWithNoResponse() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectCoordinatorAttach().respond();
        peer.remoteFlow().withLinkCredit(2).queue();
        peer.expectDeclare();
        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();
        ConnectionOptions options = new ConnectionOptions().requestTimeout(50);
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), options);
        Session session = connection.openSession().openFuture().get();
        try {
            session.beginTransaction();
            fail("Begin should have timed out after no response.");
        } catch (ClientTransactionDeclarationException expected) {
        // Expect this to time out.
        }
        try {
            session.commitTransaction();
            fail("Commit should have failed due to no active transaction.");
        } catch (ClientIllegalStateException expected) {
        // Expect this to fail since transaction not declared
        }
        try {
            session.rollbackTransaction();
            fail("Rollback should have failed due to no active transaction.");
        } catch (ClientIllegalStateException expected) {
        // Expect this to fail since transaction not declared
        }
        session.closeAsync();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ClientTransactionDeclarationException(org.apache.qpid.protonj2.client.exceptions.ClientTransactionDeclarationException) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) ClientIllegalStateException(org.apache.qpid.protonj2.client.exceptions.ClientIllegalStateException) 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 13 with Session

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

the class TransactionsTest method testExceptionOnBeginWhenCoordinatorLinkRefused.

@Test
public void testExceptionOnBeginWhenCoordinatorLinkRefused() throws Exception {
    final String errorMessage = "CoordinatorLinkRefusal-breadcrumb";
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectCoordinatorAttach().reject(true, AmqpError.NOT_IMPLEMENTED.toString(), errorMessage);
        peer.expectDetach();
        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().openFuture().get();
        try {
            session.beginTransaction();
            fail("Begin should have failed after link closed.");
        } catch (ClientTransactionDeclarationException expected) {
            // Expect this to time out.
            String message = expected.getMessage();
            assertTrue(message.contains(errorMessage));
        }
        try {
            session.commitTransaction();
            fail("Commit should have failed due to no active transaction.");
        } catch (ClientTransactionNotActiveException expected) {
        // Expect this as the begin failed on coordinator rejected
        }
        try {
            session.rollbackTransaction();
            fail("Rollback should have failed due to no active transaction.");
        } catch (ClientTransactionNotActiveException expected) {
        // Expect this as the begin failed on coordinator rejected
        }
        session.closeAsync();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ClientTransactionDeclarationException(org.apache.qpid.protonj2.client.exceptions.ClientTransactionDeclarationException) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ClientTransactionNotActiveException(org.apache.qpid.protonj2.client.exceptions.ClientTransactionNotActiveException) 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)

Example 14 with Session

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

the class TransactionsTest method testCoordinatorLinkSupportedOutcomes.

@Test
public void testCoordinatorLinkSupportedOutcomes() throws Exception {
    final byte[] txnId = new byte[] { 0, 1, 2, 3 };
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectCoordinatorAttach().withSource().withOutcomes(Accepted.DESCRIPTOR_SYMBOL.toString(), Rejected.DESCRIPTOR_SYMBOL.toString(), Released.DESCRIPTOR_SYMBOL.toString(), Modified.DESCRIPTOR_SYMBOL.toString()).and().respond();
        peer.remoteFlow().withLinkCredit(2).queue();
        peer.expectDeclare().accept(txnId);
        peer.expectDischarge().withFail(false).withTxnId(txnId).accept();
        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().openFuture().get();
        session.beginTransaction();
        session.commitTransaction();
        session.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) 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 15 with Session

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

the class TransactionsTest method testExceptionOnBeginWhenCoordinatorLinkClosedAfterDeclare.

@Test
public void testExceptionOnBeginWhenCoordinatorLinkClosedAfterDeclare() throws Exception {
    final String errorMessage = "CoordinatorLinkClosed-breadcrumb";
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectCoordinatorAttach().respond();
        peer.remoteFlow().withLinkCredit(2).queue();
        peer.expectDeclare();
        peer.remoteDetach().withClosed(true).withErrorCondition(AmqpError.NOT_IMPLEMENTED.toString(), errorMessage).queue();
        peer.expectDetach();
        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().openFuture().get();
        try {
            session.beginTransaction();
            fail("Begin should have failed after link closed.");
        } catch (ClientException expected) {
            // Expect this to time out.
            String message = expected.getMessage();
            assertTrue(message.contains(errorMessage));
        }
        try {
            session.commitTransaction();
            fail("Commit should have failed due to no active transaction.");
        } catch (ClientTransactionNotActiveException expected) {
        // Expect this as the begin failed on coordinator close
        }
        try {
            session.rollbackTransaction();
            fail("Rollback should have failed due to no active transaction.");
        } catch (ClientTransactionNotActiveException expected) {
        // Expect this as the begin failed on coordinator close
        }
        session.closeAsync();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ClientTransactionNotActiveException(org.apache.qpid.protonj2.client.exceptions.ClientTransactionNotActiveException) Connection(org.apache.qpid.protonj2.client.Connection) 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)

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