Search in sources :

Example 1 with LifecycleEvent

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());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Address(com.hazelcast.nio.Address) InetSocketAddress(java.net.InetSocketAddress) Connection(com.hazelcast.nio.Connection) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) LifecycleEvent(com.hazelcast.core.LifecycleEvent) AssertTask(com.hazelcast.test.AssertTask) LifecycleListener(com.hazelcast.core.LifecycleListener) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConnectionManager(com.hazelcast.client.connection.ClientConnectionManager) ExecutionException(java.util.concurrent.ExecutionException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 2 with LifecycleEvent

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());
        }
    });
}
Also used : ListenerConfig(com.hazelcast.config.ListenerConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) LifecycleEvent(com.hazelcast.core.LifecycleEvent) AssertTask(com.hazelcast.test.AssertTask) LifecycleListener(com.hazelcast.core.LifecycleListener) ClientConfig(com.hazelcast.client.config.ClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) UnknownHostException(java.net.UnknownHostException) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 3 with LifecycleEvent

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);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) LifecycleEvent(com.hazelcast.core.LifecycleEvent) DistributedObject(com.hazelcast.core.DistributedObject) LifecycleListener(com.hazelcast.core.LifecycleListener) ClientConfig(com.hazelcast.client.config.ClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 4 with LifecycleEvent

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"));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) LifecycleEvent(com.hazelcast.core.LifecycleEvent) LifecycleListener(com.hazelcast.core.LifecycleListener) ClientConfig(com.hazelcast.client.config.ClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with LifecycleEvent

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);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) EvictionConfig(com.hazelcast.config.EvictionConfig) NearCacheConfig(com.hazelcast.config.NearCacheConfig) LifecycleEvent(com.hazelcast.core.LifecycleEvent) DistributedObject(com.hazelcast.core.DistributedObject) LifecycleListener(com.hazelcast.core.LifecycleListener) ClientConfig(com.hazelcast.client.config.ClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

LifecycleEvent (com.hazelcast.core.LifecycleEvent)25 LifecycleListener (com.hazelcast.core.LifecycleListener)25 HazelcastInstance (com.hazelcast.core.HazelcastInstance)22 CountDownLatch (java.util.concurrent.CountDownLatch)22 Test (org.junit.Test)20 ClientConfig (com.hazelcast.client.config.ClientConfig)13 ParallelTest (com.hazelcast.test.annotation.ParallelTest)13 QuickTest (com.hazelcast.test.annotation.QuickTest)13 NightlyTest (com.hazelcast.test.annotation.NightlyTest)9 Config (com.hazelcast.config.Config)8 ListenerConfig (com.hazelcast.config.ListenerConfig)6 HazelcastClientInstanceImpl (com.hazelcast.client.impl.HazelcastClientInstanceImpl)5 AssertTask (com.hazelcast.test.AssertTask)5 JoinConfig (com.hazelcast.config.JoinConfig)3 NetworkConfig (com.hazelcast.config.NetworkConfig)3 DistributedObject (com.hazelcast.core.DistributedObject)3 ExecutionException (java.util.concurrent.ExecutionException)3 LifecycleService (com.hazelcast.core.LifecycleService)2 HazelcastInstanceFactory.newHazelcastInstance (com.hazelcast.instance.HazelcastInstanceFactory.newHazelcastInstance)2 Node (com.hazelcast.instance.Node)2