use of org.netcrusher.tcp.TcpCrusher in project rabbitmq-java-client by rabbitmq.
the class NioTlsUnverifiedConnection method connectionShouldEnforceConnectionTimeout.
@Test
public void connectionShouldEnforceConnectionTimeout() throws Exception {
// assumes RabbitMQ server running on localhost;
int amqpPort = 5671;
int amqpProxyPort = TestUtils.randomNetworkPort();
int connectionTimeout = 3_000;
int handshakeTimeout = 1_000;
try (NioReactor reactor = new NioReactor();
TcpCrusher tcpProxy = TcpCrusherBuilder.builder().withReactor(reactor).withBindAddress(new InetSocketAddress(amqpProxyPort)).withConnectAddress("localhost", amqpPort).build()) {
tcpProxy.open();
tcpProxy.freeze();
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(amqpProxyPort);
factory.useSslProtocol();
factory.useNio();
factory.setConnectionTimeout(connectionTimeout);
factory.setHandshakeTimeout(handshakeTimeout);
ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
CountDownLatch latch = new CountDownLatch(1);
executorService.submit(() -> {
try {
factory.newConnection();
latch.countDown();
} catch (SocketTimeoutException e) {
latch.countDown();
} catch (Exception e) {
// not supposed to happen
}
});
boolean connectionCreatedTimedOut = latch.await(10, TimeUnit.SECONDS);
assertThat(connectionCreatedTimedOut).isTrue();
} finally {
executorService.shutdownNow();
}
}
}
Aggregations