Search in sources :

Example 6 with LifecycleListener

use of com.hazelcast.core.LifecycleListener 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 7 with LifecycleListener

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

the class AbstractListenersOnReconnectTest method testListenersWaitMemberDestroySmartRouting.

//-------------------------- testListenersWaitMemberDestroy --------------------- //
@Test
public void testListenersWaitMemberDestroySmartRouting() {
    Config config = new Config();
    int endpointDelaySeconds = 2;
    config.setProperty(GroupProperty.CLIENT_ENDPOINT_REMOVE_DELAY_SECONDS.getName(), String.valueOf(endpointDelaySeconds));
    factory.newInstances(config, 3);
    client = factory.newHazelcastClient(getSmartClientConfig());
    setupListener();
    Collection<HazelcastInstance> allHazelcastInstances = factory.getAllHazelcastInstances();
    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();
            }
        }
    });
    final HazelcastClientInstanceImpl clientInstanceImpl = getHazelcastClientInstanceImpl(client);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(3, clientInstanceImpl.getConnectionManager().getActiveConnections().size());
        }
    });
    HazelcastInstance ownerMember = getOwnerServer(factory, clientInstanceImpl);
    for (HazelcastInstance member : allHazelcastInstances) {
        blockMessagesFromInstance(member, client);
    }
    ownerMember.getLifecycleService().terminate();
    for (HazelcastInstance member : allHazelcastInstances) {
        unblockMessagesFromInstance(member, client);
    }
    assertOpenEventually(disconnectedLatch);
    assertOpenEventually(connectedLatch);
    sleepAtLeastMillis(endpointDelaySeconds * 1000 + 2000);
    clusterSize = clusterSize - 1;
    validateRegistrationsAndListenerFunctionality();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) LifecycleEvent(com.hazelcast.core.LifecycleEvent) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) AssertTask(com.hazelcast.test.AssertTask) LifecycleListener(com.hazelcast.core.LifecycleListener) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 8 with LifecycleListener

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

the class AbstractListenersOnReconnectTest method testListenersHeartbeatTimeoutToOwner.

private void testListenersHeartbeatTimeoutToOwner() {
    setupListener();
    HazelcastClientInstanceImpl clientInstanceImpl = getHazelcastClientInstanceImpl(client);
    HazelcastInstance server = getOwnerServer(factory, clientInstanceImpl);
    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();
            }
        }
    });
    blockMessagesFromInstance(server, client);
    assertOpenEventually(disconnectedLatch);
    unblockMessagesFromInstance(server, client);
    assertOpenEventually(connectedLatch);
    validateRegistrationsAndListenerFunctionality();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) LifecycleEvent(com.hazelcast.core.LifecycleEvent) LifecycleListener(com.hazelcast.core.LifecycleListener) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 9 with LifecycleListener

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

the class ClientInvocationTest method executionCallback_FailOnShutdown.

@Test
public void executionCallback_FailOnShutdown() {
    HazelcastInstance server = hazelcastFactory.newHazelcastInstance();
    HazelcastInstance client = hazelcastFactory.newHazelcastClient();
    final CountDownLatch disconnectedLatch = new CountDownLatch(1);
    IMap<Object, Object> map = client.getMap(randomName());
    client.getLifecycleService().addLifecycleListener(new LifecycleListener() {

        @Override
        public void stateChanged(LifecycleEvent event) {
            if (event.getState() == LifecycleEvent.LifecycleState.CLIENT_DISCONNECTED) {
                disconnectedLatch.countDown();
            }
        }
    });
    server.shutdown();
    assertOpenEventually(disconnectedLatch);
    int n = 100;
    final CountDownLatch errorLatch = new CountDownLatch(n);
    for (int i = 0; i < n; i++) {
        try {
            map.submitToKey(randomString(), new DummyEntryProcessor(), new ExecutionCallback() {

                @Override
                public void onResponse(Object response) {
                }

                @Override
                public void onFailure(Throwable t) {
                    errorLatch.countDown();
                }
            });
        } catch (Exception e) {
            errorLatch.countDown();
        }
    }
    assertOpenEventually("Not all of the requests failed", errorLatch);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) LifecycleEvent(com.hazelcast.core.LifecycleEvent) LifecycleListener(com.hazelcast.core.LifecycleListener) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionCallback(com.hazelcast.core.ExecutionCallback) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 10 with LifecycleListener

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

the class LifecycleServiceImpl method fireLifecycleEvent.

public void fireLifecycleEvent(LifecycleEvent.LifecycleState lifecycleState) {
    final LifecycleEvent lifecycleEvent = new LifecycleEvent(lifecycleState);
    String revision = buildInfo.getRevision();
    if (isNullOrEmpty(revision)) {
        revision = "";
    } else {
        revision = " - " + revision;
        BuildInfo upstreamInfo = buildInfo.getUpstreamBuildInfo();
        if (upstreamInfo != null) {
            String upstreamRevision = upstreamInfo.getRevision();
            if (!isNullOrEmpty(upstreamRevision)) {
                revision += ", " + upstreamRevision;
            }
        }
    }
    getLogger().info("HazelcastClient " + buildInfo.getVersion() + " (" + buildInfo.getBuild() + revision + ") is " + lifecycleEvent.getState());
    executor.execute(new Runnable() {

        @Override
        public void run() {
            for (LifecycleListener lifecycleListener : lifecycleListeners.values()) {
                lifecycleListener.stateChanged(lifecycleEvent);
            }
        }
    });
}
Also used : BuildInfo(com.hazelcast.instance.BuildInfo) LifecycleEvent(com.hazelcast.core.LifecycleEvent) LifecycleListener(com.hazelcast.core.LifecycleListener)

Aggregations

LifecycleListener (com.hazelcast.core.LifecycleListener)27 LifecycleEvent (com.hazelcast.core.LifecycleEvent)25 HazelcastInstance (com.hazelcast.core.HazelcastInstance)23 CountDownLatch (java.util.concurrent.CountDownLatch)23 Test (org.junit.Test)21 ClientConfig (com.hazelcast.client.config.ClientConfig)14 ParallelTest (com.hazelcast.test.annotation.ParallelTest)13 QuickTest (com.hazelcast.test.annotation.QuickTest)13 NightlyTest (com.hazelcast.test.annotation.NightlyTest)10 Config (com.hazelcast.config.Config)9 ListenerConfig (com.hazelcast.config.ListenerConfig)7 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