Search in sources :

Example 51 with Connection

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

the class StreamReceiverTest method testStreamDeliveryHandlesInvalidDeliveryAnnotationsEncoding.

@Test
public void testStreamDeliveryHandlesInvalidDeliveryAnnotationsEncoding() throws Exception {
    final byte[] payload = createInvalidDeliveryAnnotationsEncoding();
    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, () -> delivery.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 52 with Connection

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

the class StreamReceiverTest method testSkipPayloadInChunksFromSingleTransferMessage.

@Test
public void testSkipPayloadInChunksFromSingleTransferMessage() throws Exception {
    final byte[] body = new byte[100];
    final Random random = new Random();
    random.setSeed(System.currentTimeMillis());
    random.nextBytes(body);
    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();
        peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(false).withMessageFormat(0).withPayload(payload).queue();
        peer.expectDisposition().withFirst(0).withState().accepted().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();
        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 int skipSize = 10;
        for (int i = 0; i < body.length; i += skipSize) {
            bodyStream.skip(10);
        }
        final byte[] scratchBuffer = new byte[10];
        assertEquals(-1, bodyStream.read(scratchBuffer, 0, scratchBuffer.length));
        assertEquals(-1, bodyStream.read(scratchBuffer));
        assertEquals(-1, bodyStream.read());
        assertNull(message.footer());
        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) Random(java.util.Random) 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)

Example 53 with Connection

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

the class StreamReceiverTest method testReadBytesFromBodyInputStreamUsingReadByteAPI.

@Test
public void testReadBytesFromBodyInputStreamUsingReadByteAPI() 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();
        peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(false).withMessageFormat(0).withPayload(payload).queue();
        peer.expectDisposition().withFirst(0).withState().accepted().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();
        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());
        assertNull(message.footer());
        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)

Example 54 with Connection

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

the class StreamReceiverTest method doTestReceiverCreditReplenishedAfterSyncReceive.

public void doTestReceiverCreditReplenishedAfterSyncReceive(boolean autoAccept) throws Exception {
    byte[] payload = createEncodedMessage(new AmqpValue<String>("Hello World"));
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofReceiver().respond();
        peer.expectFlow().withLinkCredit(10);
        for (int i = 0; i < 10; ++i) {
            peer.remoteTransfer().withDeliveryId(i).withMore(false).withMessageFormat(0).withPayload(payload).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());
        StreamReceiverOptions options = new StreamReceiverOptions();
        options.autoAccept(autoAccept);
        options.creditWindow(10);
        StreamReceiver receiver = connection.openStreamReceiver("test-receiver", options);
        Wait.waitFor(() -> receiver.queuedDeliveries() == 10);
        peer.waitForScriptToComplete();
        if (autoAccept) {
            peer.expectDisposition();
            peer.expectDisposition();
        }
        // Consume messages 1 and 2 which should not provoke credit replenishment
        // as there are still 8 outstanding which is above the 70% mark
        // #1
        assertNotNull(receiver.receive());
        // #2
        assertNotNull(receiver.receive());
        peer.waitForScriptToComplete();
        if (autoAccept) {
            peer.expectDisposition();
        }
        peer.expectFlow().withLinkCredit(3);
        // Now consume message 3 which will trip the replenish barrier and the
        // credit should be updated to reflect that we still have 7 queued
        // #3
        assertNotNull(receiver.receive());
        peer.waitForScriptToComplete();
        if (autoAccept) {
            peer.expectDisposition();
            peer.expectDisposition();
        }
        // Consume messages 4 and 5 which should not provoke credit replenishment
        // as there are still 5 outstanding plus the credit we sent last time
        // which is above the 70% mark
        // #4
        assertNotNull(receiver.receive());
        // #5
        assertNotNull(receiver.receive());
        peer.waitForScriptToComplete();
        if (autoAccept) {
            peer.expectDisposition();
        }
        peer.expectFlow().withLinkCredit(6);
        // Consume number 6 which means we only have 4 outstanding plus the three
        // that we sent last time we flowed which is 70% of possible prefetch so
        // we should flow to top off credit which would be 6 since we have four
        // still pending
        // #6
        assertNotNull(receiver.receive());
        peer.waitForScriptToComplete();
        if (autoAccept) {
            peer.expectDisposition();
            peer.expectDisposition();
        }
        // Consume deliveries 7 and 8 which should not flow as we should be
        // above the threshold of 70% since we would now have 2 outstanding
        // and 6 credits on the link
        // #7
        assertNotNull(receiver.receive());
        // #8
        assertNotNull(receiver.receive());
        peer.waitForScriptToComplete();
        if (autoAccept) {
            peer.expectDisposition();
            peer.expectDisposition();
        }
        // Now consume 9 and 10 but we still shouldn't flow more credit because
        // the link credit is above the 50% mark for overall credit windowing.
        // #9
        assertNotNull(receiver.receive());
        // #10
        assertNotNull(receiver.receive());
        peer.waitForScriptToComplete();
        peer.expectClose().respond();
        connection.close();
        peer.waitForScriptToComplete();
    }
}
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) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI)

Example 55 with Connection

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

the class StreamReceiverTest method testDrainFutureSignalsFailureWhenDrainTimeoutExceeded.

@Test
public void testDrainFutureSignalsFailureWhenDrainTimeoutExceeded() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofReceiver().respond();
        peer.expectFlow();
        peer.expectFlow().withDrain(true);
        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());
        StreamReceiverOptions receiverOptions = new StreamReceiverOptions().drainTimeout(15);
        StreamReceiver receiver = connection.openStreamReceiver("test-queue", receiverOptions).openFuture().get();
        try {
            receiver.drain().get();
            fail("Drain call should fail timeout exceeded.");
        } catch (ExecutionException cliEx) {
            LOG.debug("Receiver threw error on drain call", cliEx);
            assertTrue(cliEx.getCause() instanceof ClientOperationTimedOutException);
        }
        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) ClientOperationTimedOutException(org.apache.qpid.protonj2.client.exceptions.ClientOperationTimedOutException) Client(org.apache.qpid.protonj2.client.Client) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) 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