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));
}
Aggregations