Search in sources :

Example 1 with ProtonTestServer

use of org.apache.qpid.protonj2.test.driver.ProtonTestServer in project qpid-protonj2 by apache.

the class StreamReceiverTest method testStreamDeliveryReceivedWhileTransferIsIncomplete.

@Test
public void testStreamDeliveryReceivedWhileTransferIsIncomplete() throws Exception {
    final byte[] payload = createEncodedMessage(new AmqpValue<>("Hello World"));
    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(true).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 StreamReceiver receiver = connection.openStreamReceiver("test-queue");
        final StreamDelivery delivery = receiver.receive();
        assertNotNull(delivery);
        assertFalse(delivery.completed());
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectDisposition().withSettled(true);
        peer.remoteTransfer().withHandle(0).withDeliveryId(0).withNullDeliveryTag().withMore(false).withMessageFormat(0).withPayload(payload).now();
        Wait.assertTrue("Should eventually be marked as completed", delivery::completed);
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        receiver.closeAsync().get();
        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) 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 2 with ProtonTestServer

use of org.apache.qpid.protonj2.test.driver.ProtonTestServer in project qpid-protonj2 by apache.

the class StreamReceiverTest method doTestStreamReceiverSessionCapacity.

private void doTestStreamReceiverSessionCapacity(int maxFrameSize, int readBufferSize, int expectedSessionWindow) throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().withMaxFrameSize(maxFrameSize).respond();
        peer.expectBegin().withIncomingWindow(expectedSessionWindow).respond();
        peer.expectAttach().ofReceiver().respond();
        peer.expectFlow().withIncomingWindow(expectedSessionWindow);
        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 connectionOptions = new ConnectionOptions().maxFrameSize(maxFrameSize);
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), connectionOptions);
        StreamReceiverOptions streamOptions = new StreamReceiverOptions().readBufferSize(readBufferSize);
        StreamReceiver receiver = connection.openStreamReceiver("test-queue", streamOptions);
        receiver.openFuture().get();
        receiver.closeAsync().get();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) StreamReceiver(org.apache.qpid.protonj2.client.StreamReceiver) StreamReceiverOptions(org.apache.qpid.protonj2.client.StreamReceiverOptions) 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)

Example 3 with ProtonTestServer

use of org.apache.qpid.protonj2.test.driver.ProtonTestServer in project qpid-protonj2 by apache.

the class StreamReceiverTest method testStreamDeliveryRawInputStreamBlockedReadBytesAborted.

@Test
public void testStreamDeliveryRawInputStreamBlockedReadBytesAborted() throws Exception {
    final byte[] payload = createEncodedMessage(new AmqpValue<>("Hello World"));
    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(true).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 StreamReceiver receiver = connection.openStreamReceiver("test-queue");
        final StreamDelivery delivery = receiver.receive();
        assertNotNull(delivery);
        assertFalse(delivery.completed());
        assertFalse(delivery.aborted());
        final InputStream stream = delivery.rawInputStream();
        assertNotNull(stream);
        assertEquals(payload.length, stream.available());
        final byte[] deliveryBytes = new byte[payload.length * 2];
        peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(false).withAborted(true).withMessageFormat(0).later(50);
        try {
            stream.read(deliveryBytes);
            fail("Delivery should have been aborted while waiting for more data.");
        } catch (IOException ioe) {
            assertTrue(ioe.getCause() instanceof ClientDeliveryAbortedException);
        }
        stream.close();
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        receiver.closeAsync().get();
        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) ClientDeliveryAbortedException(org.apache.qpid.protonj2.client.exceptions.ClientDeliveryAbortedException) StreamReceiver(org.apache.qpid.protonj2.client.StreamReceiver) InputStream(java.io.InputStream) Connection(org.apache.qpid.protonj2.client.Connection) IOException(java.io.IOException) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 4 with ProtonTestServer

use of org.apache.qpid.protonj2.test.driver.ProtonTestServer in project qpid-protonj2 by apache.

the class StreamReceiverTest method doTestReceiveFailsWhenLinkRemotelyClose.

private void doTestReceiveFailsWhenLinkRemotelyClose(boolean timed) throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().withRole(Role.RECEIVER.getValue()).respond();
        peer.expectFlow();
        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());
        StreamReceiver receiver = connection.openStreamReceiver("test-queue");
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectDetach();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        peer.remoteDetach().later(50);
        if (timed) {
            assertThrows(ClientLinkRemotelyClosedException.class, () -> receiver.receive(1, TimeUnit.MINUTES));
        } else {
            assertThrows(ClientLinkRemotelyClosedException.class, () -> receiver.receive());
        }
        receiver.closeAsync();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) StreamReceiver(org.apache.qpid.protonj2.client.StreamReceiver) Connection(org.apache.qpid.protonj2.client.Connection) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI)

Example 5 with ProtonTestServer

use of org.apache.qpid.protonj2.test.driver.ProtonTestServer in project qpid-protonj2 by apache.

the class StreamReceiverTest method testReadBytesFromBodyInputStreamWithinTransactedSession.

@Test
public void testReadBytesFromBodyInputStreamWithinTransactedSession() throws Exception {
    final byte[] body = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    final byte[] payload = createEncodedMessage(new Data(body));
    final byte[] txnId = new byte[] { 0, 1, 2, 3 };
    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 StreamReceiver receiver = connection.openStreamReceiver("test-queue");
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectCoordinatorAttach().respond();
        peer.remoteFlow().withLinkCredit(2).queue();
        peer.expectDeclare().accept(txnId);
        peer.expectDisposition().withSettled(true).withState().transactional().withTxnId(txnId).withAccepted();
        peer.expectDischarge().withFail(false).withTxnId(txnId).accept();
        receiver.session().beginTransaction();
        final StreamDelivery delivery = receiver.receive();
        assertNotNull(delivery);
        assertTrue(delivery.completed());
        assertFalse(delivery.aborted());
        StreamReceiverMessage message = delivery.message();
        assertNotNull(message);
        InputStream bodyStream = message.body();
        assertNotNull(bodyStream);
        assertNull(message.header());
        assertNull(message.annotations());
        assertNull(message.properties());
        assertNull(delivery.annotations());
        final byte[] receivedBody = new byte[body.length];
        for (int i = 0; i < body.length; ++i) {
            receivedBody[i] = (byte) bodyStream.read();
        }
        assertArrayEquals(body, receivedBody);
        assertEquals(-1, bodyStream.read());
        receiver.session().commitTransaction();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        receiver.closeAsync().get();
        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) Test(org.junit.jupiter.api.Test)

Aggregations

URI (java.net.URI)353 ProtonTestServer (org.apache.qpid.protonj2.test.driver.ProtonTestServer)353 Client (org.apache.qpid.protonj2.client.Client)352 Connection (org.apache.qpid.protonj2.client.Connection)352 Test (org.junit.jupiter.api.Test)250 Session (org.apache.qpid.protonj2.client.Session)164 ConnectionOptions (org.apache.qpid.protonj2.client.ConnectionOptions)97 Sender (org.apache.qpid.protonj2.client.Sender)80 ExecutionException (java.util.concurrent.ExecutionException)72 Receiver (org.apache.qpid.protonj2.client.Receiver)71 StreamReceiver (org.apache.qpid.protonj2.client.StreamReceiver)63 ClientException (org.apache.qpid.protonj2.client.exceptions.ClientException)60 StreamSender (org.apache.qpid.protonj2.client.StreamSender)47 StreamDelivery (org.apache.qpid.protonj2.client.StreamDelivery)44 TransferPayloadCompositeMatcher (org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher)44 Tracker (org.apache.qpid.protonj2.client.Tracker)40 StreamSenderMessage (org.apache.qpid.protonj2.client.StreamSenderMessage)36 ReceiverOptions (org.apache.qpid.protonj2.client.ReceiverOptions)32 SenderOptions (org.apache.qpid.protonj2.client.SenderOptions)31 ClientIllegalStateException (org.apache.qpid.protonj2.client.exceptions.ClientIllegalStateException)30