Search in sources :

Example 1 with Accepted

use of org.apache.qpid.protonj2.test.driver.codec.messaging.Accepted in project qpid-protonj2 by apache.

the class ClientLocalTransactionContext method disposition.

@Override
public ClientTransactionContext disposition(IncomingDelivery delivery, DeliveryState outcome, boolean settled) {
    if (isInTransaction()) {
        final DeliveryState txnOutcome;
        if (outcome instanceof Accepted) {
            txnOutcome = cachedReceiverOutcome != null ? cachedReceiverOutcome : (cachedReceiverOutcome = new TransactionalState().setTxnId(currentTxn.getTxnId()).setOutcome(Accepted.getInstance()));
        } else {
            txnOutcome = new TransactionalState().setTxnId(currentTxn.getTxnId()).setOutcome((Outcome) outcome);
        }
        delivery.disposition(txnOutcome, true);
    } else {
        delivery.disposition(outcome, settled);
    }
    return this;
}
Also used : DeliveryState(org.apache.qpid.protonj2.types.transport.DeliveryState) Outcome(org.apache.qpid.protonj2.types.messaging.Outcome) Accepted(org.apache.qpid.protonj2.types.messaging.Accepted) TransactionalState(org.apache.qpid.protonj2.types.transactions.TransactionalState)

Example 2 with Accepted

use of org.apache.qpid.protonj2.test.driver.codec.messaging.Accepted in project qpid-protonj2 by apache.

the class StreamSenderTest method doTestNonBodySectionWrittenWhenNoWritesToStream.

private void doTestNonBodySectionWrittenWhenNoWritesToStream(boolean flushBeforeClose) 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();
        // Populate all Header values
        Header header = new Header();
        header.setDurable(true);
        header.setPriority((byte) 1);
        header.setTimeToLive(65535);
        header.setFirstAcquirer(true);
        header.setDeliveryCount(2);
        message.header(header);
        OutputStreamOptions options = new OutputStreamOptions();
        OutputStream stream = message.body(options);
        HeaderMatcher headerMatcher = new HeaderMatcher(true);
        headerMatcher.withDurable(true);
        headerMatcher.withPriority((byte) 1);
        headerMatcher.withTtl(65535);
        headerMatcher.withFirstAcquirer(true);
        headerMatcher.withDeliveryCount(2);
        TransferPayloadCompositeMatcher payloadMatcher = new TransferPayloadCompositeMatcher();
        payloadMatcher.setHeadersMatcher(headerMatcher);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        if (flushBeforeClose) {
            peer.expectTransfer().withMore(true).withPayload(payloadMatcher);
            peer.expectTransfer().withMore(false).withNullPayload().respond().withSettled(true).withState().accepted();
        } else {
            peer.expectTransfer().withMore(false).withPayload(payloadMatcher).respond().withSettled(true).withState().accepted();
        }
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        // the delivery.
        if (flushBeforeClose) {
            stream.flush();
        }
        stream.close();
        message.tracker().awaitSettlement(10, TimeUnit.SECONDS);
        sender.closeAsync().get();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : HeaderMatcher(org.apache.qpid.protonj2.test.driver.matchers.messaging.HeaderMatcher) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Header(org.apache.qpid.protonj2.types.messaging.Header) OutputStreamOptions(org.apache.qpid.protonj2.client.OutputStreamOptions) 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) TransferPayloadCompositeMatcher(org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher) URI(java.net.URI)

Example 3 with Accepted

use of org.apache.qpid.protonj2.test.driver.codec.messaging.Accepted in project qpid-protonj2 by apache.

the class StreamSenderTest method testConcurrentMessageSendWaitingOnSplitFramedSendToCompleteIsSentAfterCreditUpdated.

@Test
void testConcurrentMessageSendWaitingOnSplitFramedSendToCompleteIsSentAfterCreditUpdated() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().respond();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        ConnectionOptions options = new ConnectionOptions().maxFrameSize(1024);
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), options);
        StreamSender sender = connection.openStreamSender("test-queue");
        final byte[] payload = new byte[1536];
        Arrays.fill(payload, (byte) 1);
        final CountDownLatch send1Started = new CountDownLatch(1);
        final CountDownLatch send2Completed = new CountDownLatch(1);
        final AtomicBoolean sendFailed = new AtomicBoolean();
        ForkJoinPool.commonPool().execute(() -> {
            try {
                LOG.info("Test send 1 is preparing to fire:");
                ForkJoinPool.commonPool().execute(() -> send1Started.countDown());
                sender.send(Message.create(payload));
            } catch (Exception e) {
                LOG.info("Test send 1 failed with error: ", e);
                sendFailed.set(true);
            }
        });
        ForkJoinPool.commonPool().execute(() -> {
            try {
                assertTrue(send1Started.await(10, TimeUnit.SECONDS));
                LOG.info("Test send 2 is preparing to fire:");
                StreamTracker tracker = sender.send(Message.create(payload));
                tracker.awaitSettlement(10, TimeUnit.SECONDS);
                send2Completed.countDown();
            } catch (Exception e) {
                LOG.info("Test send 2 failed with error: ", e);
                sendFailed.set(true);
            }
        });
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.remoteFlow().withIncomingWindow(1).withDeliveryCount(0).withNextIncomingId(1).withLinkCredit(1).now();
        peer.expectTransfer().withNonNullPayload().withMore(true);
        peer.remoteFlow().withIncomingWindow(1).withDeliveryCount(0).withNextIncomingId(2).withLinkCredit(1).queue();
        peer.expectTransfer().withNonNullPayload().withMore(false).respond().withSettled(true).withState().accepted();
        peer.remoteFlow().withIncomingWindow(1).withDeliveryCount(1).withNextIncomingId(3).withLinkCredit(1).queue();
        peer.expectTransfer().withNonNullPayload().withMore(true);
        peer.remoteFlow().withIncomingWindow(1).withDeliveryCount(1).withNextIncomingId(4).withLinkCredit(1).queue();
        peer.expectTransfer().withNonNullPayload().withMore(false).respond().withSettled(true).withState().accepted();
        assertTrue(send2Completed.await(10, TimeUnit.SECONDS));
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        assertFalse(sendFailed.get());
        sender.closeAsync().get();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) StreamSender(org.apache.qpid.protonj2.client.StreamSender) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) StreamTracker(org.apache.qpid.protonj2.client.StreamTracker) Client(org.apache.qpid.protonj2.client.Client) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientUnsupportedOperationException(org.apache.qpid.protonj2.client.exceptions.ClientUnsupportedOperationException) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) ClientIllegalStateException(org.apache.qpid.protonj2.client.exceptions.ClientIllegalStateException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 4 with Accepted

use of org.apache.qpid.protonj2.test.driver.codec.messaging.Accepted in project qpid-protonj2 by apache.

the class StreamReceiverTest method testStreamReadOpensSessionWindowForAdditionalInputAndGrantsCreditOnClose.

@Test
public void testStreamReadOpensSessionWindowForAdditionalInputAndGrantsCreditOnClose() throws Exception {
    final byte[] body1 = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    final byte[] body2 = new byte[] { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
    final byte[] payload1 = createEncodedMessage(new Data(body1));
    final byte[] payload2 = createEncodedMessage(new Data(body2));
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().withMaxFrameSize(1000).respond();
        peer.expectBegin().withIncomingWindow(1).respond();
        peer.expectAttach().ofReceiver().respond();
        peer.expectFlow().withIncomingWindow(1).withLinkCredit(1);
        peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(true).withMessageFormat(0).withPayload(payload1).queue();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        ConnectionOptions connectionOptions = new ConnectionOptions().maxFrameSize(1000);
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), connectionOptions);
        StreamReceiverOptions streamOptions = new StreamReceiverOptions().readBufferSize(2000).creditWindow(1);
        StreamReceiver receiver = connection.openStreamReceiver("test-queue", streamOptions);
        StreamDelivery delivery = receiver.receive();
        assertNotNull(delivery);
        StreamReceiverMessage message = delivery.message();
        assertNotNull(message);
        // Creating the input stream instance should read the first chunk of data from the incoming
        // delivery which should result in a new credit being available to expand the session window.
        // An additional transfer should be placed into the delivery buffer but not yet read since
        // the user hasn't read anything. Since we are in auto settle the completed transfer should
        // trigger settlement and also open the credit window but the session window should not be
        // expanded since we haven't read the data yet.
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectFlow().withDeliveryCount(0).withIncomingWindow(1).withLinkCredit(1);
        peer.remoteTransfer().withHandle(0).withDeliveryId(0).withMore(false).withMessageFormat(0).withPayload(payload2).queue();
        peer.expectDisposition().withSettled(true).withState().accepted();
        peer.expectFlow().withDeliveryCount(1).withIncomingWindow(0).withLinkCredit(1);
        InputStream bodyStream = message.body();
        assertNotNull(bodyStream);
        // Once the read of all data completes the session window should be opened
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectFlow().withDeliveryCount(1).withIncomingWindow(1).withLinkCredit(1);
        byte[] combinedPayloads = new byte[body1.length + body2.length];
        bodyStream.read(combinedPayloads);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        // No frames should be triggered by closing the stream since we already auto settled
        // and updated the session window on the remote.
        assertTrue(Arrays.equals(body1, 0, body1.length, combinedPayloads, 0, body1.length));
        assertTrue(Arrays.equals(body2, 0, body2.length, combinedPayloads, body1.length, body1.length + body2.length));
        bodyStream.close();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        receiver.openFuture().get();
        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) StreamReceiverOptions(org.apache.qpid.protonj2.client.StreamReceiverOptions) 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) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 5 with Accepted

use of org.apache.qpid.protonj2.test.driver.codec.messaging.Accepted in project qpid-protonj2 by apache.

the class StreamReceiverTest method testStreamSupportsMark.

@Test
public void testStreamSupportsMark() throws Exception {
    final byte[] payload = createEncodedMessage(new Data(new byte[] { 0, 1, 2, 3, 4, 5 }));
    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().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());
        final InputStream stream = delivery.rawInputStream();
        assertNotNull(stream);
        assertTrue(stream.markSupported());
        assertEquals(payload.length, stream.available());
        stream.mark(payload.length);
        final byte[] deliveryBytes1 = new byte[payload.length];
        final byte[] deliveryBytes2 = new byte[payload.length];
        stream.read(deliveryBytes1);
        stream.reset();
        stream.read(deliveryBytes2);
        assertNotSame(deliveryBytes1, deliveryBytes2);
        assertArrayEquals(payload, deliveryBytes1);
        assertArrayEquals(payload, deliveryBytes2);
        assertEquals(0, stream.available());
        assertTrue(delivery.completed());
        assertFalse(delivery.aborted());
        stream.close();
        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) 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

Test (org.junit.jupiter.api.Test)72 URI (java.net.URI)48 Client (org.apache.qpid.protonj2.client.Client)48 ProtonTestServer (org.apache.qpid.protonj2.test.driver.ProtonTestServer)48 Connection (org.apache.qpid.protonj2.client.Connection)47 InputStream (java.io.InputStream)29 Engine (org.apache.qpid.protonj2.engine.Engine)29 Connection (org.apache.qpid.protonj2.engine.Connection)28 Session (org.apache.qpid.protonj2.engine.Session)28 ProtonTestConnector (org.apache.qpid.protonj2.test.driver.ProtonTestConnector)28 StreamDelivery (org.apache.qpid.protonj2.client.StreamDelivery)27 StreamReceiver (org.apache.qpid.protonj2.client.StreamReceiver)27 ProtonBuffer (org.apache.qpid.protonj2.buffer.ProtonBuffer)24 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)21 Data (org.apache.qpid.protonj2.types.messaging.Data)16 StreamReceiverMessage (org.apache.qpid.protonj2.client.StreamReceiverMessage)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 Receiver (org.apache.qpid.protonj2.engine.Receiver)13 Sender (org.apache.qpid.protonj2.client.Sender)12 Tracker (org.apache.qpid.protonj2.client.Tracker)12