Search in sources :

Example 1 with Wait

use of org.apache.qpid.protonj2.client.test.Wait in project qpid-protonj2 by apache.

the class Respond method main.

public static void main(String[] args) throws Exception {
    final String serverHost = System.getProperty("HOST", "localhost");
    final int serverPort = Integer.getInteger("PORT", 5672);
    final String address = System.getProperty("ADDRESS", "request-respond-example");
    final Client client = Client.create();
    final ConnectionOptions options = new ConnectionOptions();
    options.user(System.getProperty("USER"));
    options.password(System.getProperty("PASSWORD"));
    try (Connection connection = client.connect(serverHost, serverPort, options)) {
        ReceiverOptions receiverOptions = new ReceiverOptions();
        receiverOptions.sourceOptions().capabilities("queue");
        Receiver receiver = connection.openReceiver(address, receiverOptions);
        Delivery request = receiver.receive(60, TimeUnit.SECONDS);
        if (request != null) {
            Message<String> received = request.message();
            System.out.println("Received message with body: " + received.body());
            String replyAddress = received.replyTo();
            if (replyAddress != null) {
                Sender sender = connection.openSender(replyAddress);
                sender.send(Message.create("Response"));
            }
        } else {
            System.out.println("Failed to read a message during the defined wait interval.");
        }
    }
}
Also used : Sender(org.apache.qpid.protonj2.client.Sender) Connection(org.apache.qpid.protonj2.client.Connection) ReceiverOptions(org.apache.qpid.protonj2.client.ReceiverOptions) Receiver(org.apache.qpid.protonj2.client.Receiver) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) Delivery(org.apache.qpid.protonj2.client.Delivery) Client(org.apache.qpid.protonj2.client.Client)

Example 2 with Wait

use of org.apache.qpid.protonj2.client.test.Wait in project qpid-protonj2 by apache.

the class SenderTest method testAwaitSettlementFailedOnConnectionDropped.

@Test
public void testAwaitSettlementFailedOnConnectionDropped() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().withTarget().withAddress("test").and().respond();
        peer.remoteFlow().withLinkCredit(1).queue();
        peer.expectTransfer();
        peer.dropAfterLastHandler(30);
        peer.start();
        URI remoteURI = peer.getServerURI();
        Message<String> message = Message.create("test-message");
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
        Session session = connection.openSession();
        Sender sender = session.openSender("test");
        Tracker tracker = null;
        try {
            tracker = sender.send(message);
        } catch (ClientConnectionRemotelyClosedException cliEx) {
            fail("Send should not fail with remotely closed error after remote drops");
        }
        // the drop completes waiting callers.
        try {
            tracker.awaitSettlement();
            fail("Wait for settlement should fail with remotely closed error after remote drops");
        } catch (ClientConnectionRemotelyClosedException cliRCEx) {
        }
        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) ClientConnectionRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException) 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) Test(org.junit.jupiter.api.Test)

Example 3 with Wait

use of org.apache.qpid.protonj2.client.test.Wait in project qpid-protonj2 by apache.

the class SaslConnectionTest method doMechanismSelectedTestImpl.

private void doMechanismSelectedTestImpl(String username, String password, String clientSelectedMech, String[] serverMechs, boolean wait) throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer(serverOptions())) {
        peer.expectSaslConnectThatAlwaysFailsAuthentication(serverMechs, clientSelectedMech);
        peer.start();
        URI remoteURI = peer.getServerURI();
        ConnectionOptions clientOptions = connectionOptions();
        clientOptions.user(username);
        clientOptions.password(password);
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), clientOptions);
        try {
            connection.openFuture().get(10, TimeUnit.SECONDS);
        } catch (ExecutionException exe) {
            assertTrue(exe.getCause() instanceof ClientConnectionSecuritySaslException);
        }
        if (wait) {
            Thread.sleep(200);
        }
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ClientConnectionSecuritySaslException(org.apache.qpid.protonj2.client.exceptions.ClientConnectionSecuritySaslException) Connection(org.apache.qpid.protonj2.client.Connection) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) Client(org.apache.qpid.protonj2.client.Client) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI)

Example 4 with Wait

use of org.apache.qpid.protonj2.client.test.Wait in project qpid-protonj2 by apache.

the class ClientStreamReceiver method receive.

@Override
public StreamDelivery receive(long timeout, TimeUnit unit) throws ClientException {
    checkClosedOrFailed();
    final ClientFuture<StreamDelivery> receive = session.getFutureFactory().createFuture();
    executor.execute(() -> {
        if (notClosedOrFailed(receive)) {
            IncomingDelivery delivery = null;
            // either it is a complete delivery or the initial stage of the next incoming
            for (IncomingDelivery unsettled : protonReceiver.unsettled()) {
                if (unsettled.getLinkedResource() == null) {
                    delivery = unsettled;
                    break;
                }
            }
            if (delivery == null) {
                if (timeout == 0) {
                    receive.complete(null);
                } else {
                    final ScheduledFuture<?> timeoutFuture;
                    if (timeout > 0) {
                        timeoutFuture = session.getScheduler().schedule(() -> {
                            receiveRequests.remove(receive);
                            // Timed receive returns null on failed wait.
                            receive.complete(null);
                        }, timeout, unit);
                    } else {
                        timeoutFuture = null;
                    }
                    receiveRequests.put(receive, timeoutFuture);
                }
            } else {
                receive.complete(new ClientStreamDelivery(this, delivery));
                asyncReplenishCreditIfNeeded();
            }
        }
    });
    return session.request(this, receive);
}
Also used : StreamDelivery(org.apache.qpid.protonj2.client.StreamDelivery) IncomingDelivery(org.apache.qpid.protonj2.engine.IncomingDelivery)

Example 5 with Wait

use of org.apache.qpid.protonj2.client.test.Wait in project qpid-protonj2 by apache.

the class AMQPTestDriver method waitForScriptToComplete.

/**
 * Waits for the given amount of time for the scripted expectations and actions to be performed.  If
 * the script execution encounters an error this method will throw an {@link AssertionError} that
 * describes the error.
 *
 * @param timeout
 * 		The time to wait for the scripted expectations to complete.
 * @param units
 * 		The {@link TimeUnit} instance that converts the given time value.
 */
public void waitForScriptToComplete(long timeout, TimeUnit units) {
    checkFailed();
    ScriptCompleteAction possibleWait = null;
    synchronized (script) {
        checkFailed();
        if (!script.isEmpty()) {
            possibleWait = new ScriptCompleteAction(this).queue();
        }
    }
    if (possibleWait != null) {
        try {
            possibleWait.await(timeout, units);
        } catch (InterruptedException e) {
            Thread.interrupted();
            signalFailure("Interrupted while waiting for script to complete");
        }
    }
    checkFailed();
}
Also used : ScriptCompleteAction(org.apache.qpid.protonj2.test.driver.actions.ScriptCompleteAction)

Aggregations

Connection (org.apache.qpid.protonj2.engine.Connection)9 Engine (org.apache.qpid.protonj2.engine.Engine)9 ProtonTestConnector (org.apache.qpid.protonj2.test.driver.ProtonTestConnector)9 Client (org.apache.qpid.protonj2.client.Client)6 Connection (org.apache.qpid.protonj2.client.Connection)6 URI (java.net.URI)5 ProtonTestServer (org.apache.qpid.protonj2.test.driver.ProtonTestServer)5 Test (org.junit.jupiter.api.Test)5 Sender (org.apache.qpid.protonj2.client.Sender)4 Session (org.apache.qpid.protonj2.client.Session)4 ExecutionException (java.util.concurrent.ExecutionException)3 Tracker (org.apache.qpid.protonj2.client.Tracker)3 ConnectionOptions (org.apache.qpid.protonj2.client.ConnectionOptions)2 ClientConnectionRemotelyClosedException (org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException)2 Delivery (org.apache.qpid.protonj2.client.Delivery)1 Receiver (org.apache.qpid.protonj2.client.Receiver)1 ReceiverOptions (org.apache.qpid.protonj2.client.ReceiverOptions)1 StreamDelivery (org.apache.qpid.protonj2.client.StreamDelivery)1 StreamSender (org.apache.qpid.protonj2.client.StreamSender)1 ClientConnectionSecuritySaslException (org.apache.qpid.protonj2.client.exceptions.ClientConnectionSecuritySaslException)1