Search in sources :

Example 61 with Connection

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

the class StreamReceiverTest method testReadBytesFromBodyInputStreamWithSplitSingleByteTransfers.

private void testReadBytesFromBodyInputStreamWithSplitSingleByteTransfers(int option) throws Exception {
    final byte[] body = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    final byte[] payload = createEncodedMessage(new Data(body));
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().withRole(Role.RECEIVER.getValue()).respond();
        peer.expectFlow();
        for (int i = 0; i < payload.length; ++i) {
            peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(true).withMessageFormat(0).withPayload(new byte[] { payload[i] }).afterDelay(3).queue();
        }
        peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(false).withMessageFormat(0).afterDelay(5).queue();
        peer.expectDisposition().withFirst(0).withSettled(true);
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Test started, peer listening on: {}", remoteURI);
        final Client container = Client.create();
        final Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
        final StreamReceiver receiver = connection.openStreamReceiver("test-queue");
        final StreamDelivery delivery = receiver.receive();
        final StreamReceiverMessage message = delivery.message();
        final InputStream bodyStream = message.body();
        final byte[] receivedBody = new byte[body.length];
        if (option == 1) {
            for (int i = 0; i < body.length; ++i) {
                receivedBody[i] = (byte) bodyStream.read();
            }
            assertArrayEquals(body, receivedBody);
        } else if (option == 2) {
            assertEquals(body.length, bodyStream.read(receivedBody));
            assertArrayEquals(body, receivedBody);
        } else if (option == 3) {
            assertEquals(body.length, bodyStream.skip(body.length));
        } else {
            fail("Unknown test option");
        }
        bodyStream.close();
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        receiver.close();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : StreamDelivery(org.apache.qpid.protonj2.client.StreamDelivery) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) StreamReceiver(org.apache.qpid.protonj2.client.StreamReceiver) StreamReceiverMessage(org.apache.qpid.protonj2.client.StreamReceiverMessage) InputStream(java.io.InputStream) Connection(org.apache.qpid.protonj2.client.Connection) Data(org.apache.qpid.protonj2.types.messaging.Data) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI)

Example 62 with Connection

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

the class StreamReceiverTest method testStreamDeliveryHandlesInvalidMessageAnnotationsEncoding.

@Test
public void testStreamDeliveryHandlesInvalidMessageAnnotationsEncoding() throws Exception {
    final byte[] payload = createInvalidMessageAnnotationsEncoding();
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().withRole(Role.RECEIVER.getValue()).respond();
        peer.expectFlow();
        peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(false).withMessageFormat(0).withPayload(payload).queue();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Test started, peer listening on: {}", remoteURI);
        final Client container = Client.create();
        final Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
        final StreamReceiverOptions options = new StreamReceiverOptions().autoAccept(false);
        final StreamReceiver receiver = connection.openStreamReceiver("test-queue", options);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectDisposition().withState().rejected("decode-error", "failed reading message header");
        final StreamDelivery delivery = receiver.receive();
        final StreamReceiverMessage message = delivery.message();
        assertThrows(ClientException.class, () -> message.annotations());
        assertThrows(ClientException.class, () -> message.body());
        delivery.reject("decode-error", "failed reading message header");
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        receiver.close();
        connection.close();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : StreamDelivery(org.apache.qpid.protonj2.client.StreamDelivery) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) StreamReceiver(org.apache.qpid.protonj2.client.StreamReceiver) StreamReceiverOptions(org.apache.qpid.protonj2.client.StreamReceiverOptions) StreamReceiverMessage(org.apache.qpid.protonj2.client.StreamReceiverMessage) Connection(org.apache.qpid.protonj2.client.Connection) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 63 with Connection

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

the class ReconnectStreamSenderTest method testStreamMessageWriteThatFlushesFailsAfterConnectionDropped.

@Test
void testStreamMessageWriteThatFlushesFailsAfterConnectionDropped() throws Exception {
    try (ProtonTestServer firstPeer = new ProtonTestServer();
        ProtonTestServer finalPeer = new ProtonTestServer()) {
        firstPeer.expectSASLAnonymousConnect();
        firstPeer.expectOpen().respond();
        firstPeer.expectBegin().respond();
        firstPeer.expectAttach().ofSender().respond();
        firstPeer.remoteFlow().withLinkCredit(1).queue();
        firstPeer.dropAfterLastHandler();
        firstPeer.start();
        finalPeer.expectSASLAnonymousConnect();
        finalPeer.expectOpen().respond();
        finalPeer.expectBegin().respond();
        finalPeer.expectAttach().ofSender().respond();
        finalPeer.remoteFlow().withLinkCredit(1).queue();
        finalPeer.start();
        final URI primaryURI = firstPeer.getServerURI();
        final URI backupURI = finalPeer.getServerURI();
        ConnectionOptions options = new ConnectionOptions();
        options.maxFrameSize(32768);
        options.idleTimeout(5, TimeUnit.SECONDS);
        options.reconnectOptions().reconnectEnabled(true);
        options.reconnectOptions().addReconnectLocation(backupURI.getHost(), backupURI.getPort());
        Client container = Client.create();
        Connection connection = container.connect(primaryURI.getHost(), primaryURI.getPort(), options);
        StreamSender sender = connection.openStreamSender("test-queue");
        StreamSenderMessage message = sender.beginMessage();
        byte[] payload = new byte[65536];
        Arrays.fill(payload, (byte) 65);
        OutputStreamOptions streamOptions = new OutputStreamOptions().bodyLength(payload.length);
        OutputStream stream = message.body(streamOptions);
        firstPeer.waitForScriptToComplete();
        // Reconnection should have occurred now and we should not be able to flush data
        // from
        // the stream as its initial sender instance was closed on disconnect.
        finalPeer.waitForScriptToComplete();
        finalPeer.expectClose().respond();
        try {
            stream.write(payload);
            fail("Should not be able to write section after connection drop");
        } catch (IOException ioe) {
            assertTrue(ioe.getCause() instanceof ClientException);
        }
        connection.closeAsync().get();
        finalPeer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) OutputStreamOptions(org.apache.qpid.protonj2.client.OutputStreamOptions) 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) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) IOException(java.io.IOException) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 64 with Connection

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

the class ReconnectStreamSenderTest method testStreamMessageFlushFailsAfterConnectionDropped.

@Test
void testStreamMessageFlushFailsAfterConnectionDropped() throws Exception {
    try (ProtonTestServer firstPeer = new ProtonTestServer();
        ProtonTestServer finalPeer = new ProtonTestServer()) {
        firstPeer.expectSASLAnonymousConnect();
        firstPeer.expectOpen().respond();
        firstPeer.expectBegin().respond();
        firstPeer.expectAttach().ofSender().respond();
        firstPeer.remoteFlow().withLinkCredit(1).queue();
        firstPeer.start();
        finalPeer.expectSASLAnonymousConnect();
        finalPeer.expectOpen().respond();
        finalPeer.expectBegin().respond();
        finalPeer.expectAttach().ofSender().respond();
        finalPeer.remoteFlow().withLinkCredit(1).queue();
        finalPeer.start();
        final URI primaryURI = firstPeer.getServerURI();
        final URI backupURI = finalPeer.getServerURI();
        ConnectionOptions options = new ConnectionOptions();
        options.idleTimeout(5, TimeUnit.SECONDS);
        options.reconnectOptions().reconnectEnabled(true);
        options.reconnectOptions().addReconnectLocation(backupURI.getHost(), backupURI.getPort());
        Client container = Client.create();
        Connection connection = container.connect(primaryURI.getHost(), primaryURI.getPort(), options);
        StreamSender sender = connection.openStreamSender("test-queue");
        StreamSenderMessage message = sender.beginMessage();
        OutputStream stream = message.body();
        EncodedDataMatcher dataMatcher1 = new EncodedDataMatcher(new byte[] { 0, 1, 2, 3 });
        TransferPayloadCompositeMatcher payloadMatcher1 = new TransferPayloadCompositeMatcher();
        payloadMatcher1.setMessageContentMatcher(dataMatcher1);
        EncodedDataMatcher dataMatcher2 = new EncodedDataMatcher(new byte[] { 4, 5, 6, 7 });
        TransferPayloadCompositeMatcher payloadMatcher2 = new TransferPayloadCompositeMatcher();
        payloadMatcher2.setMessageContentMatcher(dataMatcher2);
        firstPeer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        firstPeer.expectTransfer().withPayload(payloadMatcher1).withMore(true);
        firstPeer.expectTransfer().withPayload(payloadMatcher2).withMore(true);
        firstPeer.dropAfterLastHandler();
        // Write two then after connection drops the message should fail on future
        // writes
        stream.write(new byte[] { 0, 1, 2, 3 });
        stream.flush();
        stream.write(new byte[] { 4, 5, 6, 7 });
        stream.flush();
        firstPeer.waitForScriptToComplete();
        // Reconnection should have occurred now and we should not be able to flush data
        // from
        // the stream as its initial sender instance was closed on disconnect.
        finalPeer.waitForScriptToComplete();
        finalPeer.expectClose().respond();
        // Next write should fail as connection should have dropped.
        stream.write(new byte[] { 8, 9, 10, 11 });
        try {
            stream.flush();
            fail("Should not be able to flush after connection drop");
        } catch (IOException ioe) {
            assertTrue(ioe.getCause() instanceof ClientException);
        }
        connection.closeAsync().get();
        finalPeer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) OutputStream(java.io.OutputStream) Connection(org.apache.qpid.protonj2.client.Connection) EncodedDataMatcher(org.apache.qpid.protonj2.test.driver.matchers.types.EncodedDataMatcher) StreamSender(org.apache.qpid.protonj2.client.StreamSender) StreamSenderMessage(org.apache.qpid.protonj2.client.StreamSenderMessage) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) IOException(java.io.IOException) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) Client(org.apache.qpid.protonj2.client.Client) TransferPayloadCompositeMatcher(org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 65 with Connection

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

the class ReconnectStreamSenderTest method testStreamMessageWriteThatFlushesFailsAfterConnectionDroppedAndReconnected.

@Test
public void testStreamMessageWriteThatFlushesFailsAfterConnectionDroppedAndReconnected() throws Exception {
    try (ProtonTestServer firstPeer = new ProtonTestServer();
        ProtonTestServer finalPeer = new ProtonTestServer()) {
        EncodedDataMatcher dataMatcher = new EncodedDataMatcher(new byte[] { 0, 1, 2, 3 });
        TransferPayloadCompositeMatcher payloadMatcher = new TransferPayloadCompositeMatcher();
        payloadMatcher.setMessageContentMatcher(dataMatcher);
        firstPeer.expectSASLAnonymousConnect();
        firstPeer.expectOpen().respond();
        firstPeer.expectBegin().respond();
        firstPeer.expectAttach().ofSender().respond();
        firstPeer.remoteFlow().withLinkCredit(1).queue();
        firstPeer.expectTransfer().withPayload(payloadMatcher).withMore(true);
        firstPeer.dropAfterLastHandler();
        firstPeer.start();
        finalPeer.expectSASLAnonymousConnect();
        finalPeer.expectOpen().respond();
        finalPeer.expectBegin().respond();
        finalPeer.expectAttach().ofSender().respond();
        finalPeer.start();
        final URI primaryURI = firstPeer.getServerURI();
        final URI backupURI = finalPeer.getServerURI();
        ConnectionOptions options = new ConnectionOptions();
        options.maxFrameSize(32768);
        options.idleTimeout(5, TimeUnit.SECONDS);
        options.reconnectOptions().reconnectEnabled(true);
        options.reconnectOptions().addReconnectLocation(backupURI.getHost(), backupURI.getPort());
        Client container = Client.create();
        Connection connection = container.connect(primaryURI.getHost(), primaryURI.getPort(), options);
        StreamSenderOptions senderOptions = new StreamSenderOptions();
        senderOptions.sendTimeout(1000);
        StreamSender sender = connection.openStreamSender("test-queue", senderOptions);
        StreamSenderMessage message = sender.beginMessage();
        OutputStream stream = message.body();
        stream.write(new byte[] { 0, 1, 2, 3 });
        stream.flush();
        firstPeer.waitForScriptToComplete();
        // Reconnection should have occurred now and we should not be able to flush data
        // from the stream as its initial sender instance was closed on disconnect.
        finalPeer.waitForScriptToComplete();
        // Ensure that idle processing happens in case send blocks so we can see the
        // send timed out exception
        finalPeer.remoteEmptyFrame().later(5000);
        finalPeer.remoteEmptyFrame().later(10000);
        finalPeer.remoteEmptyFrame().later(15000);
        // Test timeout kicks in now
        finalPeer.remoteEmptyFrame().later(20000);
        finalPeer.expectClose().respond();
        byte[] payload = new byte[1024];
        Arrays.fill(payload, (byte) 65);
        try {
            stream.write(payload);
            stream.flush();
            fail("Should not be able to write section after connection drop");
        } catch (IOException ioe) {
            assertFalse(ioe.getCause() instanceof ClientSendTimedOutException);
            assertTrue(ioe.getCause() instanceof ClientConnectionRemotelyClosedException);
        }
        connection.closeAsync().get();
        finalPeer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) OutputStream(java.io.OutputStream) EncodedDataMatcher(org.apache.qpid.protonj2.test.driver.matchers.types.EncodedDataMatcher) Connection(org.apache.qpid.protonj2.client.Connection) ClientSendTimedOutException(org.apache.qpid.protonj2.client.exceptions.ClientSendTimedOutException) IOException(java.io.IOException) TransferPayloadCompositeMatcher(org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher) URI(java.net.URI) StreamSenderOptions(org.apache.qpid.protonj2.client.StreamSenderOptions) ClientConnectionRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException) StreamSender(org.apache.qpid.protonj2.client.StreamSender) StreamSenderMessage(org.apache.qpid.protonj2.client.StreamSenderMessage) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) Client(org.apache.qpid.protonj2.client.Client) 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