use of com.swiftmq.amqp.v100.client.Connection 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.Connection in project qpid-protonj2 by apache.
the class ReconnectTest method testConnectionReportsFailedAfterMaxinitialReconnectAttempts.
@Test
public void testConnectionReportsFailedAfterMaxinitialReconnectAttempts() throws Exception {
try (ProtonTestServer firstPeer = new ProtonTestServer()) {
firstPeer.start();
final URI primaryURI = firstPeer.getServerURI();
firstPeer.close();
ConnectionOptions options = new ConnectionOptions();
options.reconnectOptions().reconnectEnabled(true);
// Try forever if connect succeeds once.
options.reconnectOptions().maxReconnectAttempts(-1);
options.reconnectOptions().maxInitialConnectionAttempts(3);
options.reconnectOptions().warnAfterReconnectAttempts(5);
options.reconnectOptions().reconnectDelay(10);
options.reconnectOptions().useReconnectBackOff(false);
Client container = Client.create();
Connection connection = container.connect(primaryURI.getHost(), primaryURI.getPort(), options);
try {
connection.openFuture().get();
fail("Should not successfully connect.");
} catch (ExecutionException exe) {
assertTrue(exe.getCause() instanceof ClientConnectionRemotelyClosedException);
}
try {
connection.defaultSender();
fail("Connection should be in a failed state now.");
} catch (ClientConnectionRemotelyClosedException cliEx) {
}
connection.close();
try {
connection.defaultSender();
fail("Connection should be in a closed state now.");
} catch (ClientIllegalStateException cliEx) {
}
}
}
use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.
the class ReconnectTest method testConnectHandlesSaslTempFailureAndReconnects.
@Test
public void testConnectHandlesSaslTempFailureAndReconnects() throws Exception {
try (ProtonTestServer firstPeer = new ProtonTestServer();
ProtonTestServer finalPeer = new ProtonTestServer()) {
firstPeer.expectFailingSASLPlainConnect(SaslCode.SYS_TEMP.byteValue());
firstPeer.dropAfterLastHandler();
firstPeer.start();
finalPeer.expectSASLPlainConnect("test", "pass");
finalPeer.expectOpen().respond();
finalPeer.start();
final URI primaryURI = firstPeer.getServerURI();
final URI backupURI = finalPeer.getServerURI();
final CountDownLatch connected = new CountDownLatch(1);
final AtomicReference<String> connectedHost = new AtomicReference<>();
final AtomicReference<Integer> connectedPort = new AtomicReference<>();
ConnectionOptions options = new ConnectionOptions();
options.user("test");
options.password("pass");
options.reconnectOptions().reconnectEnabled(true);
options.reconnectOptions().addReconnectLocation(backupURI.getHost(), backupURI.getPort());
options.connectedHandler((connection, event) -> {
connectedHost.set(event.host());
connectedPort.set(event.port());
connected.countDown();
});
Client container = Client.create();
Connection connection = container.connect(primaryURI.getHost(), primaryURI.getPort(), options);
firstPeer.waitForScriptToComplete();
connection.openFuture().get();
assertTrue(connected.await(5, TimeUnit.SECONDS));
// Should never have connected and exchanged Open performatives with first peer
// so we won't have had a connection established event there.
assertEquals(backupURI.getHost(), connectedHost.get());
assertEquals(backupURI.getPort(), connectedPort.get());
finalPeer.waitForScriptToComplete();
finalPeer.expectClose().respond();
connection.close();
finalPeer.waitForScriptToComplete();
}
}
use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.
the class ReconnectTest method testReconnectHandlesDropThenRejectionCloseAfterConnect.
@Test
public void testReconnectHandlesDropThenRejectionCloseAfterConnect() throws Exception {
try (ProtonTestServer firstPeer = new ProtonTestServer();
ProtonTestServer secondPeer = new ProtonTestServer();
ProtonTestServer thirdPeer = new ProtonTestServer()) {
firstPeer.expectSASLAnonymousConnect();
firstPeer.expectOpen().respond();
firstPeer.start();
secondPeer.expectSASLAnonymousConnect();
secondPeer.expectOpen().reject(AmqpError.INVALID_FIELD.toString(), "Connection configuration has invalid field");
secondPeer.expectClose();
secondPeer.start();
thirdPeer.expectSASLAnonymousConnect();
thirdPeer.expectOpen().respond();
thirdPeer.start();
final CountDownLatch connected = new CountDownLatch(1);
final CountDownLatch disconnected = new CountDownLatch(2);
final CountDownLatch reconnected = new CountDownLatch(2);
final CountDownLatch failed = new CountDownLatch(1);
final URI firstURI = firstPeer.getServerURI();
final URI secondURI = secondPeer.getServerURI();
final URI thirdURI = thirdPeer.getServerURI();
ConnectionOptions options = new ConnectionOptions();
options.reconnectOptions().reconnectEnabled(true);
options.reconnectOptions().addReconnectLocation(secondURI.getHost(), secondURI.getPort()).addReconnectLocation(thirdURI.getHost(), thirdURI.getPort());
options.connectedHandler((connection, context) -> {
connected.countDown();
});
options.interruptedHandler((connection, context) -> {
disconnected.countDown();
});
options.reconnectedHandler((connection, context) -> {
reconnected.countDown();
});
options.disconnectedHandler((connection, context) -> {
// Not expecting any failure in this test case
failed.countDown();
});
Connection connection = Client.create().connect(firstURI.getHost(), firstURI.getPort(), options);
firstPeer.waitForScriptToComplete(5, TimeUnit.SECONDS);
connection.openFuture().get();
firstPeer.close();
secondPeer.waitForScriptToComplete(5, TimeUnit.SECONDS);
// Should connect, then fail and attempt to connect to second and be rejected then reconnect to third.
assertTrue(connected.await(5, TimeUnit.SECONDS));
assertTrue(disconnected.await(5, TimeUnit.SECONDS));
assertTrue(reconnected.await(5, TimeUnit.SECONDS));
assertEquals(1, failed.getCount());
thirdPeer.expectClose().respond();
connection.close();
thirdPeer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.
the class SaslConnectionTest method testSaslAnonymousConnection.
@Test
public void testSaslAnonymousConnection() throws Exception {
try (ProtonTestServer peer = new ProtonTestServer(serverOptions())) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectClose().respond();
peer.start();
URI remoteURI = peer.getServerURI();
Client container = Client.create();
Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
connection.openFuture().get(10, TimeUnit.SECONDS);
connection.close();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
Aggregations