Search in sources :

Example 1 with TcpCrusher

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();
        }
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) TcpCrusher(org.netcrusher.tcp.TcpCrusher) SocketTimeoutException(java.net.SocketTimeoutException) InetSocketAddress(java.net.InetSocketAddress) ExecutorService(java.util.concurrent.ExecutorService) NioReactor(org.netcrusher.core.reactor.NioReactor) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(java.util.concurrent.TimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

ConnectionFactory (com.rabbitmq.client.ConnectionFactory)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketTimeoutException (java.net.SocketTimeoutException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 TimeoutException (java.util.concurrent.TimeoutException)1 Test (org.junit.Test)1 NioReactor (org.netcrusher.core.reactor.NioReactor)1 TcpCrusher (org.netcrusher.tcp.TcpCrusher)1