Search in sources :

Example 11 with HazelcastClientInstanceImpl

use of com.hazelcast.client.impl.HazelcastClientInstanceImpl 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 12 with HazelcastClientInstanceImpl

use of com.hazelcast.client.impl.HazelcastClientInstanceImpl 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 13 with HazelcastClientInstanceImpl

use of com.hazelcast.client.impl.HazelcastClientInstanceImpl in project hazelcast by hazelcast.

the class ListenerLeakTestSupport method getClientEventRegistrations.

protected Collection<ClientEventRegistration> getClientEventRegistrations(HazelcastInstance client, String id) {
    HazelcastClientInstanceImpl clientImpl = ClientTestUtil.getHazelcastClientInstanceImpl(client);
    ClientListenerServiceImpl listenerService = (ClientListenerServiceImpl) clientImpl.getListenerService();
    return listenerService.getActiveRegistrations(id);
}
Also used : ClientListenerServiceImpl(com.hazelcast.client.spi.impl.listener.ClientListenerServiceImpl) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl)

Example 14 with HazelcastClientInstanceImpl

use of com.hazelcast.client.impl.HazelcastClientInstanceImpl in project hazelcast by hazelcast.

the class AbstractListenersOnReconnectTest method testListenersTemporaryNetworkBlockage.

private void testListenersTemporaryNetworkBlockage() {
    setupListener();
    HazelcastClientInstanceImpl clientInstanceImpl = getHazelcastClientInstanceImpl(client);
    HazelcastInstance server = getOwnerServer(factory, clientInstanceImpl);
    long timeout = clientInstanceImpl.getProperties().getMillis(HEARTBEAT_TIMEOUT);
    long heartbeatTimeout = timeout > 0 ? timeout : Integer.parseInt(HEARTBEAT_TIMEOUT.getDefaultValue());
    long waitTime = heartbeatTimeout / 2;
    long endTime = System.currentTimeMillis() + waitTime;
    blockMessagesFromInstance(server, client);
    long sleepTime = endTime - System.currentTimeMillis();
    if (sleepTime > 0) {
        sleepMillis((int) sleepTime);
    }
    unblockMessagesFromInstance(server, client);
    validateRegistrationsAndListenerFunctionality();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl)

Example 15 with HazelcastClientInstanceImpl

use of com.hazelcast.client.impl.HazelcastClientInstanceImpl 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)

Aggregations

HazelcastClientInstanceImpl (com.hazelcast.client.impl.HazelcastClientInstanceImpl)40 HazelcastInstance (com.hazelcast.core.HazelcastInstance)15 Address (com.hazelcast.nio.Address)14 Test (org.junit.Test)12 ParallelTest (com.hazelcast.test.annotation.ParallelTest)11 QuickTest (com.hazelcast.test.annotation.QuickTest)11 ClientConnectionManager (com.hazelcast.client.connection.ClientConnectionManager)10 ClientInvocation (com.hazelcast.client.spi.impl.ClientInvocation)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)7 ClientInvocationFuture (com.hazelcast.client.spi.impl.ClientInvocationFuture)7 HazelcastClientProxy (com.hazelcast.client.impl.HazelcastClientProxy)6 Connection (com.hazelcast.nio.Connection)6 ClientConfig (com.hazelcast.client.config.ClientConfig)5 ClientTestUtil.getHazelcastClientInstanceImpl (com.hazelcast.client.impl.ClientTestUtil.getHazelcastClientInstanceImpl)5 LifecycleEvent (com.hazelcast.core.LifecycleEvent)5 LifecycleListener (com.hazelcast.core.LifecycleListener)5 ClientConnection (com.hazelcast.client.connection.nio.ClientConnection)4 AssertTask (com.hazelcast.test.AssertTask)4 CacheException (javax.cache.CacheException)4