use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.
the class TransactionsTest method testSendMessageInsideOfTransaction.
@Test
public void testSendMessageInsideOfTransaction() throws Exception {
final byte[] txnId = new byte[] { 0, 1, 2, 3 };
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectAttach().ofSender().respond();
peer.remoteFlow().withLinkCredit(1).queue();
peer.expectCoordinatorAttach().respond();
peer.remoteFlow().withLinkCredit(2).queue();
peer.expectDeclare().accept(txnId);
peer.expectTransfer().withHandle(0).withNonNullPayload().withState().transactional().withTxnId(txnId).and().respond().withState().transactional().withTxnId(txnId).withAccepted().and().withSettled(true);
peer.expectDischarge().withFail(false).withTxnId(txnId).accept();
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().openFuture().get();
Sender sender = session.openSender("address").openFuture().get();
session.beginTransaction();
final Tracker tracker = sender.send(Message.create("test-message"));
assertNotNull(tracker);
assertNotNull(tracker.settlementFuture().get());
assertEquals(tracker.remoteState().getType(), DeliveryState.Type.TRANSACTIONAL, "Delivery inside transaction should have Transactional state");
assertNotNull(tracker.state());
assertEquals(tracker.state().getType(), DeliveryState.Type.TRANSACTIONAL, "Delivery inside transaction should have Transactional state: " + tracker.state().getType());
Wait.assertTrue("Delivery in transaction should be locally settled after response", () -> tracker.settled());
session.commitTransaction();
session.closeAsync();
connection.closeAsync().get(10, TimeUnit.SECONDS);
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.
the class TransactionsTest method testReceiveMessageInsideOfTransaction.
@Test
public void testReceiveMessageInsideOfTransaction() throws Exception {
final byte[] txnId = new byte[] { 0, 1, 2, 3 };
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectAttach().ofReceiver().respond();
peer.expectFlow();
peer.start();
final URI remoteURI = peer.getServerURI();
final byte[] payload = createEncodedMessage(new AmqpValue<>("Hello World"));
LOG.info("Test started, peer listening on: {}", remoteURI);
Client container = Client.create();
Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
Session session = connection.openSession();
Receiver receiver = session.openReceiver("test-queue").openFuture().get();
peer.expectCoordinatorAttach().respond();
peer.remoteFlow().withLinkCredit(2).queue();
peer.expectDeclare().accept(txnId);
peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(false).withMessageFormat(0).withPayload(payload).queue();
peer.expectDisposition().withSettled(true).withState().transactional().withTxnId(txnId).withAccepted();
peer.expectDischarge().withFail(false).withTxnId(txnId).accept();
peer.expectDetach().respond();
peer.expectClose().respond();
session.beginTransaction();
Delivery delivery = receiver.receive(100, TimeUnit.MILLISECONDS);
assertNotNull(delivery);
Message<?> received = delivery.message();
assertNotNull(received);
assertTrue(received.body() instanceof String);
String value = (String) received.body();
assertEquals("Hello World", value);
session.commitTransaction();
receiver.closeAsync();
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.
the class ReconnectTest method testConnectionNotifiesReconnectionLifecycleEvents.
@Test
public void testConnectionNotifiesReconnectionLifecycleEvents() throws Exception {
try (ProtonTestServer firstPeer = new ProtonTestServer();
ProtonTestServer finalPeer = new ProtonTestServer()) {
firstPeer.expectSASLAnonymousConnect();
firstPeer.expectOpen().withContainerId(any(String.class)).respond();
firstPeer.dropAfterLastHandler(5);
firstPeer.start();
finalPeer.expectSASLAnonymousConnect();
finalPeer.expectOpen().withContainerId(any(String.class)).respond();
finalPeer.start();
final URI primaryURI = firstPeer.getServerURI();
final URI backupURI = finalPeer.getServerURI();
final CountDownLatch connected = new CountDownLatch(1);
final CountDownLatch disconnected = new CountDownLatch(1);
final CountDownLatch reconnected = new CountDownLatch(1);
final CountDownLatch failed = new CountDownLatch(1);
ConnectionOptions options = new ConnectionOptions();
options.reconnectOptions().reconnectEnabled(true);
options.reconnectOptions().maxReconnectAttempts(5);
options.reconnectOptions().reconnectDelay(10);
options.reconnectOptions().useReconnectBackOff(false);
options.reconnectOptions().addReconnectLocation(backupURI.getHost(), backupURI.getPort());
options.connectedHandler((connection, context) -> {
connected.countDown();
});
options.interruptedHandler((connection, context) -> {
disconnected.countDown();
});
options.reconnectedHandler((connection, context) -> {
reconnected.countDown();
});
options.disconnectedHandler((connection, context) -> {
failed.countDown();
});
Client container = Client.create();
Connection connection = container.connect(primaryURI.getHost(), primaryURI.getPort(), options);
firstPeer.waitForScriptToComplete();
connection.openFuture().get();
finalPeer.waitForScriptToComplete();
finalPeer.expectBegin().respond();
finalPeer.expectEnd().respond();
finalPeer.dropAfterLastHandler(10);
Session session = connection.openSession().openFuture().get();
session.close();
finalPeer.waitForScriptToComplete();
assertTrue(connected.await(5, TimeUnit.SECONDS));
assertTrue(disconnected.await(5, TimeUnit.SECONDS));
assertTrue(reconnected.await(5, TimeUnit.SECONDS));
assertTrue(failed.await(5, TimeUnit.SECONDS));
connection.close();
finalPeer.waitForScriptToComplete();
}
}
use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.
the class SenderTest method testRemotelyCloseSenderLinkWithRedirect.
@Test
public void testRemotelyCloseSenderLinkWithRedirect() throws Exception {
final String redirectVhost = "vhost";
final String redirectNetworkHost = "localhost";
final String redirectAddress = "redirect-queue";
final int redirectPort = 5677;
final String redirectScheme = "wss";
final String redirectPath = "/websockets";
// Tell the test peer to close the connection when executing its last handler
final Map<String, Object> errorInfo = new HashMap<>();
errorInfo.put(ClientConstants.OPEN_HOSTNAME.toString(), redirectVhost);
errorInfo.put(ClientConstants.NETWORK_HOST.toString(), redirectNetworkHost);
errorInfo.put(ClientConstants.PORT.toString(), redirectPort);
errorInfo.put(ClientConstants.SCHEME.toString(), redirectScheme);
errorInfo.put(ClientConstants.PATH.toString(), redirectPath);
errorInfo.put(ClientConstants.ADDRESS.toString(), redirectAddress);
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectAttach().ofSender().respond().withNullTarget();
peer.remoteDetach().withClosed(true).withErrorCondition(LinkError.REDIRECT.toString(), "Not accepting links here", errorInfo).queue();
peer.expectDetach();
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();
Sender sender = session.openSender("test-queue");
try {
sender.openFuture().get();
fail("Should not be able to create sender since the remote is redirecting.");
} catch (Exception ex) {
LOG.debug("Received expected exception from sender open: {}", ex.getMessage());
Throwable cause = ex.getCause();
assertTrue(cause instanceof ClientLinkRedirectedException);
ClientLinkRedirectedException linkRedirect = (ClientLinkRedirectedException) ex.getCause();
assertEquals(redirectVhost, linkRedirect.getHostname());
assertEquals(redirectNetworkHost, linkRedirect.getNetworkHost());
assertEquals(redirectPort, linkRedirect.getPort());
assertEquals(redirectScheme, linkRedirect.getScheme());
assertEquals(redirectPath, linkRedirect.getPath());
assertEquals(redirectAddress, linkRedirect.getAddress());
URI redirect = linkRedirect.getRedirectionURI();
assertEquals(redirectNetworkHost, redirect.getHost());
assertEquals(redirectPort, redirect.getPort());
assertEquals(redirectScheme, redirect.getScheme());
assertEquals(redirectPath, redirect.getPath());
}
connection.close();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.
the class SenderTest method doTestCloseOrDetachWithErrorCondition.
public void doTestCloseOrDetachWithErrorCondition(boolean close) throws Exception {
final String condition = "amqp:link:detach-forced";
final String description = "something bad happened.";
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectAttach().ofSender().respond();
peer.expectDetach().withClosed(close).withError(condition, description).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();
Sender sender = session.openSender("test-sender");
sender.openFuture().get();
if (close) {
sender.closeAsync(ErrorCondition.create(condition, description, null));
} else {
sender.detachAsync(ErrorCondition.create(condition, description, null));
}
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
Aggregations