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.");
}
}
}
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);
}
}
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);
}
}
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);
}
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();
}
Aggregations