Search in sources :

Example 1 with ClientTransactionNotActiveException

use of org.apache.qpid.protonj2.client.exceptions.ClientTransactionNotActiveException 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 2 with ClientTransactionNotActiveException

use of org.apache.qpid.protonj2.client.exceptions.ClientTransactionNotActiveException 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

URI (java.net.URI)2 Client (org.apache.qpid.protonj2.client.Client)2 Connection (org.apache.qpid.protonj2.client.Connection)2 Session (org.apache.qpid.protonj2.client.Session)2 ClientTransactionNotActiveException (org.apache.qpid.protonj2.client.exceptions.ClientTransactionNotActiveException)2 ProtonTestServer (org.apache.qpid.protonj2.test.driver.ProtonTestServer)2 Test (org.junit.jupiter.api.Test)2 ClientException (org.apache.qpid.protonj2.client.exceptions.ClientException)1 ClientTransactionDeclarationException (org.apache.qpid.protonj2.client.exceptions.ClientTransactionDeclarationException)1