Search in sources :

Example 1 with ConnectionHeartbeatListener

use of com.hazelcast.client.spi.impl.ConnectionHeartbeatListener in project hazelcast by hazelcast.

the class ClientHeartbeatTest method testHeartbeatStoppedEvent.

@Test
public void testHeartbeatStoppedEvent() throws InterruptedException {
    HazelcastInstance instance = hazelcastFactory.newHazelcastInstance();
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(getClientConfig());
    HazelcastClientInstanceImpl clientImpl = getHazelcastClientInstanceImpl(client);
    ClientConnectionManager connectionManager = clientImpl.getConnectionManager();
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    connectionManager.addConnectionHeartbeatListener(new ConnectionHeartbeatListener() {

        @Override
        public void heartbeatResumed(Connection connection) {
        }

        @Override
        public void heartbeatStopped(Connection connection) {
            countDownLatch.countDown();
        }
    });
    blockMessagesFromInstance(instance, client);
    assertOpenEventually(countDownLatch);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Connection(com.hazelcast.nio.Connection) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) ConnectionHeartbeatListener(com.hazelcast.client.spi.impl.ConnectionHeartbeatListener) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConnectionManager(com.hazelcast.client.connection.ClientConnectionManager) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 2 with ConnectionHeartbeatListener

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

Aggregations

ClientConnectionManager (com.hazelcast.client.connection.ClientConnectionManager)2 HazelcastClientInstanceImpl (com.hazelcast.client.impl.HazelcastClientInstanceImpl)2 ConnectionHeartbeatListener (com.hazelcast.client.spi.impl.ConnectionHeartbeatListener)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 Connection (com.hazelcast.nio.Connection)2 ParallelTest (com.hazelcast.test.annotation.ParallelTest)2 QuickTest (com.hazelcast.test.annotation.QuickTest)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.Test)2 TargetDisconnectedException (com.hazelcast.spi.exception.TargetDisconnectedException)1 AssertTask (com.hazelcast.test.AssertTask)1 ExecutionException (java.util.concurrent.ExecutionException)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 ExpectedException (org.junit.rules.ExpectedException)1