Search in sources :

Example 1 with ConnectionListener

use of org.redisson.connection.ConnectionListener in project redisson by redisson.

the class RedissonTest method testConnectionListener.

@Test
public void testConnectionListener() throws IOException, InterruptedException, TimeoutException {
    final RedisProcess p = redisTestConnection();
    final AtomicInteger connectCounter = new AtomicInteger();
    final AtomicInteger disconnectCounter = new AtomicInteger();
    Config config = new Config();
    config.useSingleServer().setAddress(p.getRedisServerAddressAndPort());
    RedissonClient r = Redisson.create(config);
    int id = r.getNodesGroup().addConnectionListener(new ConnectionListener() {

        @Override
        public void onDisconnect(InetSocketAddress addr) {
            assertThat(addr).isEqualTo(new InetSocketAddress(p.getRedisServerBindAddress(), p.getRedisServerPort()));
            disconnectCounter.incrementAndGet();
        }

        @Override
        public void onConnect(InetSocketAddress addr) {
            assertThat(addr).isEqualTo(new InetSocketAddress(p.getRedisServerBindAddress(), p.getRedisServerPort()));
            connectCounter.incrementAndGet();
        }
    });
    assertThat(id).isNotZero();
    r.getBucket("1").get();
    Assert.assertEquals(0, p.stop());
    try {
        r.getBucket("1").get();
    } catch (Exception e) {
    }
    RedisProcess pp = new RedisRunner().nosave().port(p.getRedisServerPort()).randomDir().run();
    r.getBucket("1").get();
    r.shutdown();
    Assert.assertEquals(0, pp.stop());
    await().atMost(1, TimeUnit.SECONDS).until(() -> assertThat(connectCounter.get()).isEqualTo(1));
    await().until(() -> assertThat(disconnectCounter.get()).isEqualTo(1));
}
Also used : RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Config(org.redisson.config.Config) InetSocketAddress(java.net.InetSocketAddress) ConnectionListener(org.redisson.connection.ConnectionListener) RedisException(org.redisson.client.RedisException) TimeoutException(java.util.concurrent.TimeoutException) RedisConnectionException(org.redisson.client.RedisConnectionException) RedisOutOfMemoryException(org.redisson.client.RedisOutOfMemoryException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1 RedisProcess (org.redisson.RedisRunner.RedisProcess)1 RedissonClient (org.redisson.api.RedissonClient)1 RedisConnectionException (org.redisson.client.RedisConnectionException)1 RedisException (org.redisson.client.RedisException)1 RedisOutOfMemoryException (org.redisson.client.RedisOutOfMemoryException)1 Config (org.redisson.config.Config)1 ConnectionListener (org.redisson.connection.ConnectionListener)1