Search in sources :

Example 76 with Connection

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

the class SenderTest method testConcurrentSendWaitingOnSplitFramedSendToCompleteIsSentAfterCreditUpdated.

@Test
void testConcurrentSendWaitingOnSplitFramedSendToCompleteIsSentAfterCreditUpdated() 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);
        Sender sender = connection.openSender("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:");
                Tracker 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.expectClose().respond();
        assertFalse(sendFailed.get());
        sender.closeAsync().get();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : Sender(org.apache.qpid.protonj2.client.Sender) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tracker(org.apache.qpid.protonj2.client.Tracker) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) Client(org.apache.qpid.protonj2.client.Client) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientDeliveryStateException(org.apache.qpid.protonj2.client.exceptions.ClientDeliveryStateException) ClientResourceRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientResourceRemotelyClosedException) ClientLinkRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientLinkRemotelyClosedException) ClientOperationTimedOutException(org.apache.qpid.protonj2.client.exceptions.ClientOperationTimedOutException) ClientUnsupportedOperationException(org.apache.qpid.protonj2.client.exceptions.ClientUnsupportedOperationException) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) ClientConnectionRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException) ClientSendTimedOutException(org.apache.qpid.protonj2.client.exceptions.ClientSendTimedOutException) ClientLinkRedirectedException(org.apache.qpid.protonj2.client.exceptions.ClientLinkRedirectedException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 77 with Connection

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

the class SenderTest method testSendCompletesWhenCreditEventuallyOffered.

@Test
public void testSendCompletesWhenCreditEventuallyOffered() 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("Sender test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        ConnectionOptions options = new ConnectionOptions();
        options.sendTimeout(200);
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), options);
        Session session = connection.openSession();
        Sender sender = session.openSender("test-queue");
        sender.openFuture().get(10, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        // Expect a transfer but only after the flow which is delayed to allow the
        // client time to block on credit.
        peer.expectTransfer().withNonNullPayload();
        peer.remoteFlow().withDeliveryCount(0).withLinkCredit(1).withIncomingWindow(1024).withOutgoingWindow(10).withNextIncomingId(0).withNextOutgoingId(1).later(30);
        peer.expectDetach().respond();
        peer.expectClose().respond();
        Message<String> message = Message.create("Hello World");
        try {
            LOG.debug("Attempting send with sender: {}", sender);
            sender.send(message);
        } catch (ClientSendTimedOutException ex) {
            fail("Should not throw a send timed out exception");
        }
        sender.closeAsync().get(10, TimeUnit.SECONDS);
        connection.closeAsync().get(10, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : Sender(org.apache.qpid.protonj2.client.Sender) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) ClientSendTimedOutException(org.apache.qpid.protonj2.client.exceptions.ClientSendTimedOutException) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) 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 78 with Connection

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

the class SenderTest method doTestCloseOrDetachSenderTimesOutWhenNoCloseResponseReceived.

private void doTestCloseOrDetachSenderTimesOutWhenNoCloseResponseReceived(boolean close, boolean timeout) throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().respond();
        peer.expectDetach();
        peer.expectClose().respond();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Sender test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        ConnectionOptions options = new ConnectionOptions();
        options.closeTimeout(10);
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), options);
        Session session = connection.openSession();
        Sender sender = session.openSender("test-queue");
        sender.openFuture().get(10, TimeUnit.SECONDS);
        try {
            if (close) {
                if (timeout) {
                    sender.closeAsync().get(10, TimeUnit.SECONDS);
                } else {
                    sender.closeAsync().get();
                }
            } else {
                if (timeout) {
                    sender.detachAsync().get(10, TimeUnit.SECONDS);
                } else {
                    sender.detachAsync().get();
                }
            }
            fail("Should not complete the close or detach future without an error");
        } catch (ExecutionException exe) {
            Throwable cause = exe.getCause();
            assertTrue(cause instanceof ClientOperationTimedOutException);
        }
        connection.closeAsync().get(10, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : Sender(org.apache.qpid.protonj2.client.Sender) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) ClientOperationTimedOutException(org.apache.qpid.protonj2.client.exceptions.ClientOperationTimedOutException) Client(org.apache.qpid.protonj2.client.Client) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session)

Example 79 with Connection

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

the class SenderTest method testCreateSenderWithUserConfiguredSourceAndTargetOptions.

@Test
public void testCreateSenderWithUserConfiguredSourceAndTargetOptions() throws Exception {
    final Map<String, Object> filtersToObject = new HashMap<>();
    filtersToObject.put("x-opt-filter", "a = b");
    final Map<String, String> filters = new HashMap<>();
    filters.put("x-opt-filter", "a = b");
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().withSource().withAddress(notNullValue()).withDistributionMode("copy").withTimeout(128).withDurable(TerminusDurability.UNSETTLED_STATE).withExpiryPolicy(TerminusExpiryPolicy.CONNECTION_CLOSE).withDefaultOutcome(new Released()).withCapabilities("QUEUE").withFilter(filtersToObject).withOutcomes("amqp:accepted:list", "amqp:rejected:list").also().withTarget().withAddress("test-queue").withCapabilities("QUEUE").withDurable(TerminusDurability.CONFIGURATION).withExpiryPolicy(TerminusExpiryPolicy.SESSION_END).withTimeout(42).withDynamic(anyOf(nullValue(), equalTo(false))).withDynamicNodeProperties(nullValue()).and().respond();
        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();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
        Session session = connection.openSession();
        SenderOptions senderOptions = new SenderOptions();
        senderOptions.sourceOptions().capabilities("QUEUE");
        senderOptions.sourceOptions().distributionMode(DistributionMode.COPY);
        senderOptions.sourceOptions().timeout(128);
        senderOptions.sourceOptions().durabilityMode(DurabilityMode.UNSETTLED_STATE);
        senderOptions.sourceOptions().expiryPolicy(ExpiryPolicy.CONNECTION_CLOSE);
        senderOptions.sourceOptions().defaultOutcome(DeliveryState.released());
        senderOptions.sourceOptions().filters(filters);
        senderOptions.sourceOptions().outcomes(DeliveryState.Type.ACCEPTED, DeliveryState.Type.REJECTED);
        senderOptions.targetOptions().capabilities("QUEUE");
        senderOptions.targetOptions().durabilityMode(DurabilityMode.CONFIGURATION);
        senderOptions.targetOptions().expiryPolicy(ExpiryPolicy.SESSION_CLOSE);
        senderOptions.targetOptions().timeout(42);
        Sender sender = session.openSender("test-queue", senderOptions).openFuture().get();
        sender.close();
        session.close();
        connection.close();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) SenderOptions(org.apache.qpid.protonj2.client.SenderOptions) Released(org.apache.qpid.protonj2.test.driver.codec.messaging.Released) HashMap(java.util.HashMap) Connection(org.apache.qpid.protonj2.client.Connection) URI(java.net.URI) Sender(org.apache.qpid.protonj2.client.Sender) Client(org.apache.qpid.protonj2.client.Client) Session(org.apache.qpid.protonj2.client.Session) Test(org.junit.jupiter.api.Test)

Example 80 with Connection

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

the class SenderTest method doTestSenderSendingSettledCompletesTrackerAcknowledgeFuture.

private void doTestSenderSendingSettledCompletesTrackerAcknowledgeFuture(boolean trySend) throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().withSenderSettleModeSettled().withReceiverSettlesFirst().respond().withSenderSettleModeSettled().withReceivervSettlesFirst();
        peer.remoteFlow().withLinkCredit(10).queue();
        // Open a receiver to ensure sender link has processed
        peer.expectAttach().respond();
        // the inbound flow frame we sent previously before send.
        peer.expectFlow();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Sender test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort()).openFuture().get();
        Session session = connection.openSession().openFuture().get();
        SenderOptions options = new SenderOptions().deliveryMode(DeliveryMode.AT_MOST_ONCE);
        Sender sender = session.openSender("test-qos", options);
        assertEquals("test-qos", sender.address());
        session.openReceiver("dummy").openFuture().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectTransfer().withNonNullPayload();
        peer.expectDetach().respond();
        peer.expectClose().respond();
        final Message<String> message = Message.create("Hello World");
        final Tracker tracker;
        if (trySend) {
            tracker = sender.trySend(message);
        } else {
            tracker = sender.send(message);
        }
        assertNotNull(tracker);
        assertTrue(tracker.settlementFuture().isDone());
        assertTrue(tracker.settlementFuture().get().settled());
        assertFalse(tracker.settlementFuture().get().remoteSettled());
        sender.closeAsync().get(10, TimeUnit.SECONDS);
        connection.closeAsync().get(10, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : Sender(org.apache.qpid.protonj2.client.Sender) Tracker(org.apache.qpid.protonj2.client.Tracker) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) SenderOptions(org.apache.qpid.protonj2.client.SenderOptions) 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)

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