use of com.hazelcast.core.LifecycleEvent in project hazelcast by hazelcast.
the class ClientRegressionWithMockNetworkTest method testClientConnectionEvents.
@Test
public void testClientConnectionEvents() throws InterruptedException {
final LinkedList<LifecycleState> list = new LinkedList<LifecycleState>();
list.offer(LifecycleState.STARTING);
list.offer(LifecycleState.STARTED);
list.offer(LifecycleState.CLIENT_CONNECTED);
list.offer(LifecycleState.CLIENT_DISCONNECTED);
list.offer(LifecycleState.CLIENT_CONNECTED);
list.offer(LifecycleState.CLIENT_DISCONNECTED);
list.offer(LifecycleState.SHUTTING_DOWN);
list.offer(LifecycleState.SHUTDOWN);
hazelcastFactory.newHazelcastInstance();
final CountDownLatch latch = new CountDownLatch(list.size());
final CountDownLatch connectedLatch = new CountDownLatch(2);
final CountDownLatch disconnectedLatch = new CountDownLatch(2);
LifecycleListener listener = new LifecycleListener() {
public void stateChanged(LifecycleEvent event) {
Logger.getLogger(getClass()).info("stateChanged: " + event);
final LifecycleState state = list.poll();
LifecycleState eventState = event.getState();
if (state != null && state.equals(eventState)) {
latch.countDown();
}
if (LifecycleState.CLIENT_CONNECTED.equals(eventState)) {
connectedLatch.countDown();
}
if (LifecycleState.CLIENT_DISCONNECTED.equals(eventState)) {
disconnectedLatch.countDown();
}
}
};
final ListenerConfig listenerConfig = new ListenerConfig(listener);
final ClientConfig clientConfig = new ClientConfig();
clientConfig.addListenerConfig(listenerConfig);
clientConfig.getNetworkConfig().setConnectionAttemptLimit(100);
HazelcastInstance hazelcastClient = hazelcastFactory.newHazelcastClient(clientConfig);
hazelcastFactory.shutdownAllMembers();
hazelcastFactory.newHazelcastInstance();
assertOpenEventually("LifecycleState failed. Expected two CLIENT_CONNECTED events!", connectedLatch);
hazelcastFactory.shutdownAllMembers();
//wait for disconnect then call client.shutdown(). Otherwise shutdown could prevent firing DISCONNECTED event
assertOpenEventually("LifecycleState failed. Expected two CLIENT_DISCONNECTED events!", disconnectedLatch);
hazelcastClient.shutdown();
assertOpenEventually("LifecycleState failed", latch);
}
use of com.hazelcast.core.LifecycleEvent in project hazelcast by hazelcast.
the class ClientServiceTest method testClientShutdownDuration_whenServersDown.
@Test
public void testClientShutdownDuration_whenServersDown() {
HazelcastInstance hazelcastInstance = hazelcastFactory.newHazelcastInstance();
final CountDownLatch countDownLatch = new CountDownLatch(1);
ClientConfig config = new ClientConfig();
config.getNetworkConfig().setConnectionAttemptPeriod(0).setConnectionAttemptLimit(1);
HazelcastInstance client = hazelcastFactory.newHazelcastClient(config);
client.getLifecycleService().addLifecycleListener(new LifecycleListener() {
@Override
public void stateChanged(LifecycleEvent event) {
if (event.getState() == LifecycleEvent.LifecycleState.SHUTDOWN)
countDownLatch.countDown();
}
});
hazelcastInstance.shutdown();
assertOpenEventually(countDownLatch, ClusterListenerSupport.TERMINATE_TIMEOUT_SECONDS);
}
use of com.hazelcast.core.LifecycleEvent in project hazelcast by hazelcast.
the class ClientHeartbeatTest method testClientEndpointsDelaySeconds_whenHeartbeatResumed.
@Test
public void testClientEndpointsDelaySeconds_whenHeartbeatResumed() throws Exception {
int delaySeconds = 2;
Config config = new Config();
config.setProperty(GroupProperty.CLIENT_ENDPOINT_REMOVE_DELAY_SECONDS.getName(), String.valueOf(delaySeconds));
HazelcastInstance hazelcastInstance = hazelcastFactory.newHazelcastInstance(config);
ClientConfig clientConfig = new ClientConfig();
clientConfig.setProperty(ClientProperty.HEARTBEAT_TIMEOUT.getName(), "4000");
clientConfig.setProperty(ClientProperty.HEARTBEAT_INTERVAL.getName(), "1000");
HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
final CountDownLatch disconnectedLatch = new CountDownLatch(1);
LifecycleService lifecycleService = client.getLifecycleService();
lifecycleService.addLifecycleListener(new LifecycleListener() {
@Override
public void stateChanged(LifecycleEvent event) {
if (LifecycleEvent.LifecycleState.CLIENT_DISCONNECTED == event.getState()) {
disconnectedLatch.countDown();
}
}
});
blockMessagesFromInstance(hazelcastInstance, client);
//Wait for client to disconnect because of hearBeat problem.
assertOpenEventually(disconnectedLatch);
final CountDownLatch connectedLatch = new CountDownLatch(1);
final AtomicLong stateChangeCount = new AtomicLong();
lifecycleService.addLifecycleListener(new LifecycleListener() {
@Override
public void stateChanged(LifecycleEvent event) {
stateChangeCount.incrementAndGet();
Logger.getLogger(this.getClass()).info("state event : " + event);
if (LifecycleEvent.LifecycleState.CLIENT_CONNECTED == event.getState()) {
connectedLatch.countDown();
}
}
});
unblockMessagesFromInstance(hazelcastInstance, client);
//Wait for client to connect back after heartbeat issue is resolved
assertOpenEventually(connectedLatch);
//After client connected, there should not be further change in client state
//We are specifically testing for scheduled ClientDisconnectionOperation not to take action when run
assertTrueAllTheTime(new AssertTask() {
@Override
public void run() throws Exception {
assertEquals(1, stateChangeCount.get());
}
}, delaySeconds * 2);
}
use of com.hazelcast.core.LifecycleEvent in project hazelcast by hazelcast.
the class AbstractListenersOnReconnectTest method testListenersWhenClientDisconnectedOperationRuns_whenOwnerConnectionRemoved.
@Test
public void testListenersWhenClientDisconnectedOperationRuns_whenOwnerConnectionRemoved() {
Config config = new Config();
int endpointDelaySeconds = 2;
config.setProperty(GroupProperty.CLIENT_ENDPOINT_REMOVE_DELAY_SECONDS.getName(), String.valueOf(endpointDelaySeconds));
config.setProperty(GroupProperty.CLIENT_HEARTBEAT_TIMEOUT_SECONDS.getName(), "4");
HazelcastInstance ownerServer = factory.newHazelcastInstance(config);
ClientConfig smartClientConfig = getSmartClientConfig();
smartClientConfig.setProperty(ClientProperty.HEARTBEAT_INTERVAL.getName(), "500");
client = factory.newHazelcastClient(smartClientConfig);
factory.newHazelcastInstance(config);
setupListener();
final CountDownLatch disconnectedLatch = new CountDownLatch(1);
final CountDownLatch connectedLatch = new CountDownLatch(1);
client.getLifecycleService().addLifecycleListener(new LifecycleListener() {
@Override
public void stateChanged(LifecycleEvent event) {
if (LifecycleEvent.LifecycleState.CLIENT_DISCONNECTED == event.getState()) {
disconnectedLatch.countDown();
}
if (LifecycleEvent.LifecycleState.CLIENT_CONNECTED == event.getState()) {
connectedLatch.countDown();
}
}
});
blockMessagesToInstance(ownerServer, client);
assertOpenEventually(disconnectedLatch);
sleepAtLeastMillis(TimeUnit.SECONDS.toMillis(endpointDelaySeconds) * 2);
unblockMessagesToInstance(ownerServer, client);
assertOpenEventually(connectedLatch);
validateRegistrationsAndListenerFunctionality();
}
use of com.hazelcast.core.LifecycleEvent in project hazelcast by hazelcast.
the class AbstractListenersOnReconnectTest method testListenersWhenClientDisconnectedOperationRuns_whenOwnerMemberRemoved.
//--------------------------------------------------------------------------------- //
@Test
public void testListenersWhenClientDisconnectedOperationRuns_whenOwnerMemberRemoved() {
Config config = new Config();
int endpointDelaySeconds = 2;
config.setProperty(GroupProperty.CLIENT_ENDPOINT_REMOVE_DELAY_SECONDS.getName(), String.valueOf(endpointDelaySeconds));
HazelcastInstance ownerServer = factory.newHazelcastInstance(config);
client = factory.newHazelcastClient(getSmartClientConfig());
HazelcastInstance server2 = factory.newHazelcastInstance(config);
setupListener();
final CountDownLatch disconnectedLatch = new CountDownLatch(1);
final CountDownLatch connectedLatch = new CountDownLatch(1);
client.getLifecycleService().addLifecycleListener(new LifecycleListener() {
@Override
public void stateChanged(LifecycleEvent event) {
if (LifecycleEvent.LifecycleState.CLIENT_DISCONNECTED == event.getState()) {
disconnectedLatch.countDown();
}
if (LifecycleEvent.LifecycleState.CLIENT_CONNECTED == event.getState()) {
connectedLatch.countDown();
}
}
});
blockMessagesToInstance(server2, client);
ownerServer.shutdown();
sleepAtLeastMillis(TimeUnit.SECONDS.toMillis(endpointDelaySeconds) * 2);
unblockMessagesToInstance(server2, client);
assertOpenEventually(disconnectedLatch);
assertOpenEventually(connectedLatch);
clusterSize = clusterSize - 1;
validateRegistrationsAndListenerFunctionality();
}
Aggregations