Example usage for org.springframework.integration.ip.util TestingUtilities waitStopListening

List of usage examples for org.springframework.integration.ip.util TestingUtilities waitStopListening

Introduction

In this page you can find the example usage for org.springframework.integration.ip.util TestingUtilities waitStopListening.

Prototype

public static void waitStopListening(AbstractServerConnectionFactory serverConnectionFactory,
        @Nullable Long delayArg) throws IllegalStateException 

Source Link

Document

Wait for a server connection factory to stop listening.

Usage

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

@Test
public void testCachedFailoverRealClose() throws Exception {
    TcpNetServerConnectionFactory server1 = new TcpNetServerConnectionFactory(0);
    server1.setBeanName("server1");
    final CountDownLatch latch1 = new CountDownLatch(3);
    server1.registerListener(message -> {
        latch1.countDown();/*from   w  ww. j  a v a 2 s  . co m*/
        return false;
    });
    server1.start();
    TestingUtilities.waitListening(server1, 10000L);
    int port1 = server1.getPort();
    TcpNetServerConnectionFactory server2 = new TcpNetServerConnectionFactory(0);
    server1.setBeanName("server2");
    final CountDownLatch latch2 = new CountDownLatch(2);
    server2.registerListener(message -> {
        latch2.countDown();
        return false;
    });
    server2.start();
    TestingUtilities.waitListening(server2, 10000L);
    int port2 = server2.getPort();
    // Failover
    AbstractClientConnectionFactory factory1 = new TcpNetClientConnectionFactory("localhost", port1);
    factory1.setBeanName("client1");
    factory1.registerListener(message -> false);
    AbstractClientConnectionFactory factory2 = new TcpNetClientConnectionFactory("localhost", port2);
    factory2.setBeanName("client2");
    factory2.registerListener(message -> false);
    List<AbstractClientConnectionFactory> factories = new ArrayList<AbstractClientConnectionFactory>();
    factories.add(factory1);
    factories.add(factory2);
    FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory(factories);

    // Cache
    CachingClientConnectionFactory cachingFactory = new CachingClientConnectionFactory(failoverFactory, 2);
    cachingFactory.start();
    TcpConnection conn1 = cachingFactory.getConnection();
    GenericMessage<String> message = new GenericMessage<String>("foo");
    conn1.send(message);
    conn1.close();
    TcpConnection conn2 = cachingFactory.getConnection();
    assertSame(((TcpConnectionInterceptorSupport) conn1).getTheConnection(),
            ((TcpConnectionInterceptorSupport) conn2).getTheConnection());
    conn2.send(message);
    conn1 = cachingFactory.getConnection();
    assertNotSame(((TcpConnectionInterceptorSupport) conn1).getTheConnection(),
            ((TcpConnectionInterceptorSupport) conn2).getTheConnection());
    conn1.send(message);
    conn1.close();
    conn2.close();
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    server1.stop();
    TestingUtilities.waitStopListening(server1, 10000L);
    TestingUtilities.waitUntilFactoryHasThisNumberOfConnections(factory1, 0);
    conn1 = cachingFactory.getConnection();
    conn2 = cachingFactory.getConnection();
    conn1.send(message);
    conn2.send(message);
    conn1.close();
    conn2.close();
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    SimplePool<?> pool = TestUtils.getPropertyValue(cachingFactory, "pool", SimplePool.class);
    assertEquals(2, pool.getIdleCount());
    server2.stop();
}