Example usage for org.springframework.integration.ip.tcp.connection CachingClientConnectionFactory getIdleCount

List of usage examples for org.springframework.integration.ip.tcp.connection CachingClientConnectionFactory getIdleCount

Introduction

In this page you can find the example usage for org.springframework.integration.ip.tcp.connection CachingClientConnectionFactory getIdleCount.

Prototype

public int getIdleCount() 

Source Link

Usage

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

@Test
public void testReducePool() throws Exception {
    AbstractClientConnectionFactory factory = mock(AbstractClientConnectionFactory.class);
    when(factory.isRunning()).thenReturn(true);
    TcpConnectionSupport mockConn1 = makeMockConnection("conn1", true);
    TcpConnectionSupport mockConn2 = makeMockConnection("conn2", true);
    TcpConnectionSupport mockConn3 = makeMockConnection("conn3", true);
    TcpConnectionSupport mockConn4 = makeMockConnection("conn4", true);
    when(factory.getConnection()).thenReturn(mockConn1).thenReturn(mockConn2).thenReturn(mockConn3)
            .thenReturn(mockConn4);/*from   w  w  w.  jav a2  s  .  c o  m*/
    CachingClientConnectionFactory cachingFactory = new CachingClientConnectionFactory(factory, 4);
    cachingFactory.start();
    TcpConnection conn1 = cachingFactory.getConnection();
    TcpConnection conn2 = cachingFactory.getConnection();
    TcpConnection conn3 = cachingFactory.getConnection();
    TcpConnection conn4 = cachingFactory.getConnection();
    Semaphore semaphore = TestUtils.getPropertyValue(TestUtils.getPropertyValue(cachingFactory, "pool"),
            "permits", Semaphore.class);
    assertEquals(0, semaphore.availablePermits());
    conn1.close();
    assertEquals(1, semaphore.availablePermits());
    cachingFactory.setPoolSize(2);
    assertEquals(0, semaphore.availablePermits());
    assertEquals(3, cachingFactory.getActiveCount());
    conn2.close();
    assertEquals(0, semaphore.availablePermits());
    assertEquals(2, cachingFactory.getActiveCount());
    conn3.close();
    assertEquals(1, cachingFactory.getActiveCount());
    assertEquals(1, cachingFactory.getIdleCount());
    conn4.close();
    assertEquals(2, semaphore.availablePermits());
    assertEquals(0, cachingFactory.getActiveCount());
    assertEquals(2, cachingFactory.getIdleCount());
    verify(mockConn1).close();
    verify(mockConn2).close();
}