Search in sources :

Example 1 with ListenerConfig

use of com.hazelcast.config.ListenerConfig in project hazelcast by hazelcast.

the class MembershipListenerTest method initialMemberEvents_whenAddedViaConfig.

@Test
public void initialMemberEvents_whenAddedViaConfig() throws InterruptedException {
    hazelcastFactory.newHazelcastInstance();
    hazelcastFactory.newHazelcastInstance();
    ClientConfig clientConfig = new ClientConfig();
    final InitialMemberShipEventLogger listener = new InitialMemberShipEventLogger();
    clientConfig.addListenerConfig(new ListenerConfig().setImplementation(listener));
    hazelcastFactory.newHazelcastClient(clientConfig);
    EventObject eventObject = listener.events.poll(ASSERT_TRUE_EVENTUALLY_TIMEOUT, TimeUnit.SECONDS);
    assertInstanceOf(InitialMembershipEvent.class, eventObject);
    InitialMembershipEvent event = (InitialMembershipEvent) eventObject;
    assertEquals(2, event.getMembers().size());
    assertEquals(0, listener.events.size());
}
Also used : ListenerConfig(com.hazelcast.config.ListenerConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) EventObject(java.util.EventObject) InitialMembershipEvent(com.hazelcast.core.InitialMembershipEvent) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 2 with ListenerConfig

use of com.hazelcast.config.ListenerConfig in project hazelcast by hazelcast.

the class MembershipListenerTest method testAddInitialMembership_whenListenerAddedViaClientConfig.

/**
     * related to issue #1181
     */
@Test
public void testAddInitialMembership_whenListenerAddedViaClientConfig() throws InterruptedException {
    hazelcastFactory.newHazelcastInstance();
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.addListenerConfig(new ListenerConfig().setImplementation(mock(InitialMembershipListener.class)));
    hazelcastFactory.newHazelcastClient(clientConfig);
}
Also used : ListenerConfig(com.hazelcast.config.ListenerConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 3 with ListenerConfig

use of com.hazelcast.config.ListenerConfig in project hazelcast by hazelcast.

the class MembershipListenerTest method givenMixOfListenerExists_whenConnect_thenCallInitialMembershipListener.

@Test
public void givenMixOfListenerExists_whenConnect_thenCallInitialMembershipListener() throws Exception {
    hazelcastFactory.newHazelcastInstance();
    final ClientConfig config = new ClientConfig();
    // first add bunch of *regular* MembershipListener. They do not implement InitialMembershipListener
    config.addListenerConfig(new ListenerConfig().setImplementation(new MemberShipEventLogger()));
    config.addListenerConfig(new ListenerConfig().setImplementation(new MemberShipEventLogger()));
    config.addListenerConfig(new ListenerConfig().setImplementation(new MemberShipEventLogger()));
    // now add an InitialMembershipListener
    // if there is an exception thrown during event delivery to regular listeners
    // then no event will likely be delivered to InitialMemberShipEventLogger
    final InitialMemberShipEventLogger initialListener = new InitialMemberShipEventLogger();
    config.addListenerConfig(new ListenerConfig().setImplementation(initialListener));
    //connect to a grid
    hazelcastFactory.newHazelcastClient(config);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals("Expecting one event", 1, initialListener.events.size());
            InitialMembershipEvent event = (InitialMembershipEvent) initialListener.events.getLast();
            assertEquals(1, event.getMembers().size());
        }
    });
}
Also used : ListenerConfig(com.hazelcast.config.ListenerConfig) AssertTask(com.hazelcast.test.AssertTask) ClientConfig(com.hazelcast.client.config.ClientConfig) InitialMembershipEvent(com.hazelcast.core.InitialMembershipEvent) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 4 with ListenerConfig

use of com.hazelcast.config.ListenerConfig in project hazelcast by hazelcast.

the class ClientServiceTest method testClientListener_fromConfig.

@Test
public void testClientListener_fromConfig() {
    Config config = new Config();
    final CountDownLatch connectedLatch = new CountDownLatch(1);
    final CountDownLatch disconnectedLatch = new CountDownLatch(1);
    ListenerConfig listenerConfig = new ListenerConfig(new ClientListener() {

        @Override
        public void clientConnected(Client client) {
            connectedLatch.countDown();
        }

        @Override
        public void clientDisconnected(Client client) {
            disconnectedLatch.countDown();
        }
    });
    config.addListenerConfig(listenerConfig);
    HazelcastInstance instance = hazelcastFactory.newHazelcastInstance(config);
    HazelcastInstance client = hazelcastFactory.newHazelcastClient();
    client.shutdown();
    assertOpenEventually(connectedLatch);
    assertOpenEventually(disconnectedLatch);
    instance.shutdown();
}
Also used : ListenerConfig(com.hazelcast.config.ListenerConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ListenerConfig(com.hazelcast.config.ListenerConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) CountDownLatch(java.util.concurrent.CountDownLatch) Client(com.hazelcast.core.Client) ClientListener(com.hazelcast.core.ClientListener) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with ListenerConfig

use of com.hazelcast.config.ListenerConfig in project hazelcast by hazelcast.

the class ClientServiceTest method testConnectedClientsWithReAuth.

@Test(timeout = 120000)
public void testConnectedClientsWithReAuth() throws InterruptedException {
    final ClientConfig clientConfig = new ClientConfig();
    clientConfig.getNetworkConfig().setConnectionAttemptPeriod(1000 * 5);
    clientConfig.getNetworkConfig().setConnectionAttemptLimit(Integer.MAX_VALUE);
    final CountDownLatch countDownLatch = new CountDownLatch(2);
    clientConfig.addListenerConfig(new ListenerConfig(new LifecycleListener() {

        @Override
        public void stateChanged(LifecycleEvent event) {
            if (event.getState() == LifecycleEvent.LifecycleState.CLIENT_CONNECTED) {
                countDownLatch.countDown();
            }
        }
    }));
    HazelcastInstance instance = hazelcastFactory.newHazelcastInstance();
    final HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
    //restart the node
    instance.shutdown();
    final HazelcastInstance restartedInstance = hazelcastFactory.newHazelcastInstance();
    // do any operation
    client.getMap(randomMapName()).size();
    //wait for clients to reconnect & reAuth
    assertOpenEventually(countDownLatch);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(1, restartedInstance.getClientService().getConnectedClients().size());
        }
    });
}
Also used : ListenerConfig(com.hazelcast.config.ListenerConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) LifecycleEvent(com.hazelcast.core.LifecycleEvent) AssertTask(com.hazelcast.test.AssertTask) LifecycleListener(com.hazelcast.core.LifecycleListener) ClientConfig(com.hazelcast.client.config.ClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) UnknownHostException(java.net.UnknownHostException) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

ListenerConfig (com.hazelcast.config.ListenerConfig)46 Test (org.junit.Test)43 QuickTest (com.hazelcast.test.annotation.QuickTest)40 Config (com.hazelcast.config.Config)30 HazelcastInstance (com.hazelcast.core.HazelcastInstance)30 ParallelTest (com.hazelcast.test.annotation.ParallelTest)29 CountDownLatch (java.util.concurrent.CountDownLatch)22 AssertTask (com.hazelcast.test.AssertTask)11 ClientConfig (com.hazelcast.client.config.ClientConfig)10 NightlyTest (com.hazelcast.test.annotation.NightlyTest)9 ReliableTopicConfig (com.hazelcast.config.ReliableTopicConfig)8 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)7 RingbufferConfig (com.hazelcast.config.RingbufferConfig)6 LifecycleListener (com.hazelcast.core.LifecycleListener)6 EntryListenerConfig (com.hazelcast.config.EntryListenerConfig)5 LifecycleEvent (com.hazelcast.core.LifecycleEvent)5 ItemListenerConfig (com.hazelcast.config.ItemListenerConfig)4 MapPartitionLostListenerConfig (com.hazelcast.config.MapPartitionLostListenerConfig)4 ClientListener (com.hazelcast.core.ClientListener)4 InternalPartition (com.hazelcast.internal.partition.InternalPartition)4