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