Search in sources :

Example 96 with HazelcastInstance

use of com.hazelcast.core.HazelcastInstance in project hazelcast by hazelcast.

the class ClientHeartbeatTest method testHeartbeatResumedEvent.

@Test
public void testHeartbeatResumedEvent() throws InterruptedException {
    hazelcastFactory.newHazelcastInstance();
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(getClientConfig());
    final HazelcastInstance instance2 = hazelcastFactory.newHazelcastInstance();
    // make sure client is connected to instance2
    String keyOwnedByInstance2 = generateKeyOwnedBy(instance2);
    IMap<String, String> map = client.getMap(randomString());
    map.put(keyOwnedByInstance2, randomString());
    HazelcastClientInstanceImpl clientImpl = getHazelcastClientInstanceImpl(client);
    final ClientConnectionManager connectionManager = clientImpl.getConnectionManager();
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    connectionManager.addConnectionHeartbeatListener(new ConnectionHeartbeatListener() {

        @Override
        public void heartbeatResumed(Connection connection) {
            assertEquals(instance2.getCluster().getLocalMember().getAddress(), connection.getEndPoint());
            countDownLatch.countDown();
        }

        @Override
        public void heartbeatStopped(Connection connection) {
        }
    });
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertNotNull(connectionManager.getConnection(instance2.getCluster().getLocalMember().getAddress()));
        }
    });
    blockMessagesFromInstance(instance2, client);
    sleepMillis(HEARTBEAT_TIMEOUT_MILLIS * 2);
    unblockMessagesFromInstance(instance2, client);
    assertOpenEventually(countDownLatch);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Connection(com.hazelcast.nio.Connection) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) AssertTask(com.hazelcast.test.AssertTask) Matchers.containsString(org.hamcrest.Matchers.containsString) ConnectionHeartbeatListener(com.hazelcast.client.spi.impl.ConnectionHeartbeatListener) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConnectionManager(com.hazelcast.client.connection.ClientConnectionManager) ExpectedException(org.junit.rules.ExpectedException) ExecutionException(java.util.concurrent.ExecutionException) TargetDisconnectedException(com.hazelcast.spi.exception.TargetDisconnectedException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 97 with HazelcastInstance

use of com.hazelcast.core.HazelcastInstance in project hazelcast by hazelcast.

the class ClientHeartbeatTest method testAuthentication_whenHeartbeatResumed.

@Test
public void testAuthentication_whenHeartbeatResumed() throws Exception {
    HazelcastInstance hazelcastInstance = hazelcastFactory.newHazelcastInstance();
    ClientConfig config = new ClientConfig();
    config.setProperty(ClientProperty.SHUFFLE_MEMBER_LIST.getName(), "false");
    final HazelcastInstance client = hazelcastFactory.newHazelcastClient(config);
    HazelcastClientInstanceImpl hazelcastClientInstanceImpl = getHazelcastClientInstanceImpl(client);
    final ClusterListenerSupport clientClusterService = (ClusterListenerSupport) hazelcastClientInstanceImpl.getClientClusterService();
    final CountDownLatch countDownLatch = new CountDownLatch(2);
    client.getLifecycleService().addLifecycleListener(new LifecycleListener() {

        @Override
        public void stateChanged(LifecycleEvent event) {
            countDownLatch.countDown();
        }
    });
    final HazelcastInstance instance2 = hazelcastFactory.newHazelcastInstance();
    blockMessagesFromInstance(instance2, client);
    final HazelcastInstance instance3 = hazelcastFactory.newHazelcastInstance();
    hazelcastInstance.shutdown();
    //wait for disconnect from instance1 since it is shutdown  // CLIENT_DISCONNECTED event
    //and wait for connect to from instance3 // CLIENT_CONNECTED event
    assertOpenEventually(countDownLatch);
    //verify and wait for authentication to 3 is complete
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            String uuid = instance3.getLocalEndpoint().getUuid();
            assertEquals(uuid, getClientEngineImpl(instance3).getOwnerUuid(client.getLocalEndpoint().getUuid()));
            assertEquals(uuid, getClientEngineImpl(instance2).getOwnerUuid(client.getLocalEndpoint().getUuid()));
            assertEquals(uuid, clientClusterService.getPrincipal().getOwnerUuid());
            assertEquals(instance3.getCluster().getLocalMember().getAddress(), clientClusterService.getOwnerConnectionAddress());
        }
    });
    //unblock instance 2 for authentication response.
    unblockMessagesFromInstance(instance2, client);
    //late authentication response from instance2 should not be able to change state in both client and cluster
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            String uuid = instance3.getLocalEndpoint().getUuid();
            assertEquals(uuid, getClientEngineImpl(instance3).getOwnerUuid(client.getLocalEndpoint().getUuid()));
            assertEquals(uuid, getClientEngineImpl(instance2).getOwnerUuid(client.getLocalEndpoint().getUuid()));
            assertEquals(uuid, clientClusterService.getPrincipal().getOwnerUuid());
            assertEquals(instance3.getCluster().getLocalMember().getAddress(), clientClusterService.getOwnerConnectionAddress());
        }
    });
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) LifecycleEvent(com.hazelcast.core.LifecycleEvent) AssertTask(com.hazelcast.test.AssertTask) LifecycleListener(com.hazelcast.core.LifecycleListener) Matchers.containsString(org.hamcrest.Matchers.containsString) ClientConfig(com.hazelcast.client.config.ClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) ClusterListenerSupport(com.hazelcast.client.spi.impl.ClusterListenerSupport) ExpectedException(org.junit.rules.ExpectedException) ExecutionException(java.util.concurrent.ExecutionException) TargetDisconnectedException(com.hazelcast.spi.exception.TargetDisconnectedException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 98 with HazelcastInstance

use of com.hazelcast.core.HazelcastInstance in project hazelcast by hazelcast.

the class ClientICacheManagerTest method getCache_when_clientServiceNotFoundExceptionIsThrown_then_illegalStateExceptionIsThrown.

@Test
public void getCache_when_clientServiceNotFoundExceptionIsThrown_then_illegalStateExceptionIsThrown() {
    // when ClientServiceNotFoundException was thrown by hzInstance.getDistributedObject
    // (i.e. cache support is not available on the client-side)
    HazelcastInstance hzInstance = mock(HazelcastInstance.class);
    when(hzInstance.getDistributedObject(anyString(), anyString())).thenThrow(new ClientServiceNotFoundException("mock exception"));
    ClientICacheManager clientCacheManager = new ClientICacheManager(hzInstance);
    // then an IllegalStateException will be thrown by getCache
    thrown.expect(IllegalStateException.class);
    clientCacheManager.getCache("any-cache");
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ClientServiceNotFoundException(com.hazelcast.client.spi.impl.ClientServiceNotFoundException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 99 with HazelcastInstance

use of com.hazelcast.core.HazelcastInstance in project hazelcast by hazelcast.

the class ClientICacheManagerTest method getCache_when_serviceNotFoundExceptionIsThrown_then_illegalStateExceptionIsThrown.

@Test
public void getCache_when_serviceNotFoundExceptionIsThrown_then_illegalStateExceptionIsThrown() {
    // when HazelcastException with ServiceNotFoundException cause was thrown by hzInstance.getDistributedObject
    // (i.e. cache support is not available server-side)
    HazelcastInstance hzInstance = mock(HazelcastInstance.class);
    HazelcastException hzException = new HazelcastException("mock exception", new ServiceNotFoundException("mock exception"));
    when(hzInstance.getDistributedObject(anyString(), anyString())).thenThrow(hzException);
    ClientICacheManager clientCacheManager = new ClientICacheManager(hzInstance);
    // then an IllegalStateException will be thrown by getCache
    thrown.expect(IllegalStateException.class);
    clientCacheManager.getCache("any-cache");
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastException(com.hazelcast.core.HazelcastException) ClientServiceNotFoundException(com.hazelcast.client.spi.impl.ClientServiceNotFoundException) ServiceNotFoundException(com.hazelcast.spi.exception.ServiceNotFoundException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 100 with HazelcastInstance

use of com.hazelcast.core.HazelcastInstance in project hazelcast by hazelcast.

the class ClientICacheManagerTest method getCache_when_hazelcastExceptionIsThrown_then_isRethrown.

@Test
public void getCache_when_hazelcastExceptionIsThrown_then_isRethrown() {
    // when a HazelcastException occurs whose cause is not a ServiceNotFoundException
    HazelcastInstance hzInstance = mock(HazelcastInstance.class);
    when(hzInstance.getDistributedObject(anyString(), anyString())).thenThrow(new HazelcastException("mock exception"));
    ClientICacheManager clientCacheManager = new ClientICacheManager(hzInstance);
    // then the exception is rethrown
    thrown.expect(HazelcastException.class);
    clientCacheManager.getCache("any-cache");
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastException(com.hazelcast.core.HazelcastException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

HazelcastInstance (com.hazelcast.core.HazelcastInstance)2077 Test (org.junit.Test)1684 QuickTest (com.hazelcast.test.annotation.QuickTest)1466 ParallelTest (com.hazelcast.test.annotation.ParallelTest)1389 Config (com.hazelcast.config.Config)815 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)665 AssertTask (com.hazelcast.test.AssertTask)263 MapConfig (com.hazelcast.config.MapConfig)254 CountDownLatch (java.util.concurrent.CountDownLatch)251 NightlyTest (com.hazelcast.test.annotation.NightlyTest)230 MapStoreConfig (com.hazelcast.config.MapStoreConfig)169 IMap (com.hazelcast.core.IMap)145 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)144 ClientConfig (com.hazelcast.client.config.ClientConfig)137 Before (org.junit.Before)111 NearCacheConfig (com.hazelcast.config.NearCacheConfig)106 Member (com.hazelcast.core.Member)96 Map (java.util.Map)96 SlowTest (com.hazelcast.test.annotation.SlowTest)94 SqlPredicate (com.hazelcast.query.SqlPredicate)83