use of com.hazelcast.core.LifecycleEvent in project hazelcast by hazelcast.
the class ClientConnectionTest method destroyConnection_whenDestroyedMultipleTimes_thenListenerRemoveCalledOnce.
@Test
public void destroyConnection_whenDestroyedMultipleTimes_thenListenerRemoveCalledOnce() {
HazelcastInstance server = hazelcastFactory.newHazelcastInstance();
HazelcastInstance client = hazelcastFactory.newHazelcastClient();
HazelcastClientInstanceImpl clientImpl = ClientTestUtil.getHazelcastClientInstanceImpl(client);
ClientConnectionManager connectionManager = clientImpl.getConnectionManager();
final CountingConnectionRemoveListener listener = new CountingConnectionRemoveListener();
connectionManager.addConnectionListener(listener);
final Address serverAddress = new Address(server.getCluster().getLocalMember().getSocketAddress());
final Connection connectionToServer = connectionManager.getConnection(serverAddress);
final CountDownLatch isConnected = new CountDownLatch(1);
clientImpl.getLifecycleService().addLifecycleListener(new LifecycleListener() {
@Override
public void stateChanged(LifecycleEvent event) {
if (LifecycleEvent.LifecycleState.CLIENT_CONNECTED == event.getState()) {
isConnected.countDown();
}
}
});
connectionToServer.close(null, null);
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
assertTrue(isConnected.await(5, TimeUnit.SECONDS));
}
});
connectionToServer.close(null, null);
assertEquals("connection removed should be called only once", 1, listener.count.get());
}
use of com.hazelcast.core.LifecycleEvent in project hazelcast by hazelcast.
the class ClientServiceTest method testConnectedClientsWithReAuth.
@Test(timeout = 120000)
public void testConnectedClientsWithReAuth() throws InterruptedException {
final ClientConfig clientConfig = new ClientConfig();
clientConfig.getNetworkConfig().setConnectionAttemptPeriod(1000 * 5);
clientConfig.getNetworkConfig().setConnectionAttemptLimit(Integer.MAX_VALUE);
final CountDownLatch countDownLatch = new CountDownLatch(2);
clientConfig.addListenerConfig(new ListenerConfig(new LifecycleListener() {
@Override
public void stateChanged(LifecycleEvent event) {
if (event.getState() == LifecycleEvent.LifecycleState.CLIENT_CONNECTED) {
countDownLatch.countDown();
}
}
}));
HazelcastInstance instance = hazelcastFactory.newHazelcastInstance();
final HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
//restart the node
instance.shutdown();
final HazelcastInstance restartedInstance = hazelcastFactory.newHazelcastInstance();
// do any operation
client.getMap(randomMapName()).size();
//wait for clients to reconnect & reAuth
assertOpenEventually(countDownLatch);
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
assertEquals(1, restartedInstance.getClientService().getConnectedClients().size());
}
});
}
use of com.hazelcast.core.LifecycleEvent in project hazelcast by hazelcast.
the class ClientRegressionWithMockNetworkTest method testDeadlock_WhenDoingOperationFromLifecycleListenerWithInitialPartitionTable.
@Test
public void testDeadlock_WhenDoingOperationFromLifecycleListenerWithInitialPartitionTable() {
HazelcastInstance instance = hazelcastFactory.newHazelcastInstance();
final ClientConfig clientConfig = new ClientConfig();
final HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig.setExecutorPoolSize(1));
hazelcastFactory.newHazelcastInstance();
final CountDownLatch latch = new CountDownLatch(1);
final IMap<Object, Object> map = client.getMap(randomMapName());
// Let the partition table retrieved the first time
map.get(1);
client.getLifecycleService().addLifecycleListener(new LifecycleListener() {
@Override
public void stateChanged(LifecycleEvent event) {
if (event.getState() == LifecycleState.CLIENT_DISCONNECTED) {
for (int i = 0; i < 1000; i++) {
map.get(i);
}
latch.countDown();
}
}
});
instance.shutdown();
assertOpenEventually(latch);
}
use of com.hazelcast.core.LifecycleEvent in project hazelcast by hazelcast.
the class ClientReconnectTest method testClientReconnectOnClusterDown.
@Test
public void testClientReconnectOnClusterDown() throws Exception {
final HazelcastInstance h1 = hazelcastFactory.newHazelcastInstance();
ClientConfig clientConfig = new ClientConfig();
clientConfig.getNetworkConfig().setConnectionAttemptLimit(Integer.MAX_VALUE);
final HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
final CountDownLatch connectedLatch = new CountDownLatch(2);
client.getLifecycleService().addLifecycleListener(new LifecycleListener() {
@Override
public void stateChanged(LifecycleEvent event) {
connectedLatch.countDown();
}
});
IMap<String, String> m = client.getMap("default");
h1.shutdown();
hazelcastFactory.newHazelcastInstance();
assertOpenEventually(connectedLatch);
assertNull(m.put("test", "test"));
assertEquals("test", m.get("test"));
}
use of com.hazelcast.core.LifecycleEvent in project hazelcast by hazelcast.
the class ClientRegressionWithMockNetworkTest method testDeadlock_whenDoingOperationFromLifecycleListener_withNearCache.
@Test
public void testDeadlock_whenDoingOperationFromLifecycleListener_withNearCache() {
String mapName = randomMapName();
EvictionConfig evictionConfig = new EvictionConfig().setMaximumSizePolicy(ENTRY_COUNT).setSize(1);
NearCacheConfig nearCacheConfig = new NearCacheConfig().setName(mapName).setEvictionConfig(evictionConfig);
ClientConfig clientConfig = new ClientConfig().addNearCacheConfig(nearCacheConfig).setExecutorPoolSize(1);
HazelcastInstance instance = hazelcastFactory.newHazelcastInstance();
HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
hazelcastFactory.newHazelcastInstance();
final CountDownLatch latch = new CountDownLatch(1);
final IMap<Object, Object> map = client.getMap(mapName);
client.getLifecycleService().addLifecycleListener(new LifecycleListener() {
@Override
public void stateChanged(LifecycleEvent event) {
if (event.getState() == LifecycleState.CLIENT_DISCONNECTED) {
map.get(1);
map.get(2);
latch.countDown();
}
}
});
instance.shutdown();
assertOpenEventually(latch);
}
Aggregations