Search in sources :

Example 31 with Connection

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

the class TransactionsTest method testExceptionOnBeginWhenCoordinatorLinkClosedAfterDeclareAllowsNewTransactionDeclaration.

@Test
public void testExceptionOnBeginWhenCoordinatorLinkClosedAfterDeclareAllowsNewTransactionDeclaration() 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.expectCoordinatorAttach().respond();
        peer.remoteFlow().withLinkCredit(2).queue();
        peer.expectDeclare().accept();
        peer.expectDischarge().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();
        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 again and expect to return to normal state now.
        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) 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 32 with Connection

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

the class WsConnectionTest method testWSConnectFailsDueToServerListeningOverTCP.

@Test
public void testWSConnectFailsDueToServerListeningOverTCP() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer(testServerOptions())) {
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("WebSocket Connect test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        ConnectionOptions options = connectionOptions();
        try {
            Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), options);
            connection.openFuture().get();
            fail("Should fail to connect");
        } catch (ExecutionException ex) {
            LOG.info("Connection create failed due to: ", ex);
            assertTrue(ex.getCause() instanceof ClientException);
        }
        peer.waitForScriptToCompleteIgnoreErrors();
    }
}
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) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 33 with Connection

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

the class StreamSenderTest method testRawOutputStreamFromMessageWritesUnmodifiedBytes.

@Test
void testRawOutputStreamFromMessageWritesUnmodifiedBytes() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().respond();
        peer.remoteFlow().withLinkCredit(1).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());
        StreamSender sender = connection.openStreamSender("test-queue");
        StreamSenderMessage message = sender.beginMessage();
        OutputStream stream = message.rawOutputStream();
        // Only one writer at a time can exist
        assertThrows(ClientIllegalStateException.class, () -> message.rawOutputStream());
        assertThrows(ClientIllegalStateException.class, () -> message.body());
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectTransfer().withMore(true).withPayload(new byte[] { 0, 1, 2, 3 });
        peer.expectTransfer().withMore(false).withNullPayload();
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        stream.write(new byte[] { 0, 1, 2, 3 });
        stream.flush();
        stream.close();
        sender.closeAsync().get();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Connection(org.apache.qpid.protonj2.client.Connection) StreamSender(org.apache.qpid.protonj2.client.StreamSender) StreamSenderMessage(org.apache.qpid.protonj2.client.StreamSenderMessage) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 34 with Connection

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

the class TransactionsTest method testExceptionOnRollbackWhenCoordinatorRejectsDischarge.

@Test
public void testExceptionOnRollbackWhenCoordinatorRejectsDischarge() throws Exception {
    final String errorMessage = "Transaction aborted due to timeout";
    final byte[] txnId1 = new byte[] { 0, 1, 2, 3 };
    final byte[] txnId2 = new byte[] { 1, 1, 2, 3 };
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectCoordinatorAttach().respond();
        peer.remoteFlow().withLinkCredit(4).queue();
        peer.expectDeclare().accept(txnId1);
        peer.expectDischarge().withFail(true).withTxnId(txnId1).reject(TransactionErrors.TRANSACTION_TIMEOUT.toString(), "Transaction aborted due to timeout");
        peer.expectDeclare().accept(txnId2);
        peer.expectDischarge().withFail(false).withTxnId(txnId2).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();
        try {
            session.rollbackTransaction();
            fail("Commit should have failed after link closed.");
        } catch (ClientTransactionRolledBackException expected) {
            // Expect this to time out.
            String message = expected.getMessage();
            assertTrue(message.contains(errorMessage));
        }
        session.beginTransaction();
        session.commitTransaction();
        session.closeAsync();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ClientTransactionRolledBackException(org.apache.qpid.protonj2.client.exceptions.ClientTransactionRolledBackException) 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 35 with Connection

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

the class TransactionsTest method testCannotBeginSecondTransactionWhileFirstIsActive.

@Test
public void testCannotBeginSecondTransactionWhileFirstIsActive() 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().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();
        try {
            session.beginTransaction();
            fail("Should not be allowed to begin another transaction");
        } catch (ClientIllegalStateException cliEx) {
        // Expected
        }
        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) ClientIllegalStateException(org.apache.qpid.protonj2.client.exceptions.ClientIllegalStateException) 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

Connection (org.apache.qpid.protonj2.client.Connection)368 Client (org.apache.qpid.protonj2.client.Client)367 URI (java.net.URI)354 ProtonTestServer (org.apache.qpid.protonj2.test.driver.ProtonTestServer)352 Test (org.junit.jupiter.api.Test)250 Session (org.apache.qpid.protonj2.client.Session)166 ConnectionOptions (org.apache.qpid.protonj2.client.ConnectionOptions)112 Sender (org.apache.qpid.protonj2.client.Sender)89 Receiver (org.apache.qpid.protonj2.client.Receiver)79 ExecutionException (java.util.concurrent.ExecutionException)72 StreamReceiver (org.apache.qpid.protonj2.client.StreamReceiver)65 ClientException (org.apache.qpid.protonj2.client.exceptions.ClientException)60 StreamSender (org.apache.qpid.protonj2.client.StreamSender)49 StreamDelivery (org.apache.qpid.protonj2.client.StreamDelivery)46 TransferPayloadCompositeMatcher (org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher)44 Tracker (org.apache.qpid.protonj2.client.Tracker)41 StreamSenderMessage (org.apache.qpid.protonj2.client.StreamSenderMessage)38 ReceiverOptions (org.apache.qpid.protonj2.client.ReceiverOptions)34 SenderOptions (org.apache.qpid.protonj2.client.SenderOptions)33 OutputStream (java.io.OutputStream)31