Search in sources :

Example 1 with AssertTask

use of com.hazelcast.test.AssertTask in project hazelcast by hazelcast.

the class ClientConnectionTest method testAsyncConnectionCreationInAsyncMethods.

@Test
public void testAsyncConnectionCreationInAsyncMethods() throws ExecutionException, InterruptedException {
    hazelcastFactory.newHazelcastInstance();
    CountDownLatch countDownLatch = new CountDownLatch(1);
    ClientConfig config = new ClientConfig();
    WaitingCredentials credentials = new WaitingCredentials("dev", "dev-pass", countDownLatch);
    config.setCredentials(credentials);
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(config);
    final IExecutorService executorService = client.getExecutorService(randomString());
    credentials.waitFlag.set(true);
    final HazelcastInstance secondInstance = hazelcastFactory.newHazelcastInstance();
    final AtomicReference<Future> atomicReference = new AtomicReference<Future>();
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            Member secondMember = secondInstance.getCluster().getLocalMember();
            Future future = executorService.submitToMember(new DummySerializableCallable(), secondMember);
            atomicReference.set(future);
        }
    });
    thread.start();
    try {
        assertTrueEventually(new AssertTask() {

            @Override
            public void run() throws Exception {
                assertNotNull(atomicReference.get());
            }
        }, 30);
    } finally {
        thread.interrupt();
        thread.join();
        countDownLatch.countDown();
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IExecutorService(com.hazelcast.core.IExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Future(java.util.concurrent.Future) AssertTask(com.hazelcast.test.AssertTask) ClientConfig(com.hazelcast.client.config.ClientConfig) Member(com.hazelcast.core.Member) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 2 with AssertTask

use of com.hazelcast.test.AssertTask in project hazelcast by hazelcast.

the class ClientConnectionTest method destroyConnection_whenDestroyedMultipleTimes_thenListenerRemoveCalledOnce.

@Test
public void destroyConnection_whenDestroyedMultipleTimes_thenListenerRemoveCalledOnce() {
    HazelcastInstance server = hazelcastFactory.newHazelcastInstance();
    HazelcastInstance client = hazelcastFactory.newHazelcastClient();
    HazelcastClientInstanceImpl clientImpl = ClientTestUtil.getHazelcastClientInstanceImpl(client);
    ClientConnectionManager connectionManager = clientImpl.getConnectionManager();
    final CountingConnectionRemoveListener listener = new CountingConnectionRemoveListener();
    connectionManager.addConnectionListener(listener);
    final Address serverAddress = new Address(server.getCluster().getLocalMember().getSocketAddress());
    final Connection connectionToServer = connectionManager.getConnection(serverAddress);
    final CountDownLatch isConnected = new CountDownLatch(1);
    clientImpl.getLifecycleService().addLifecycleListener(new LifecycleListener() {

        @Override
        public void stateChanged(LifecycleEvent event) {
            if (LifecycleEvent.LifecycleState.CLIENT_CONNECTED == event.getState()) {
                isConnected.countDown();
            }
        }
    });
    connectionToServer.close(null, null);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertTrue(isConnected.await(5, TimeUnit.SECONDS));
        }
    });
    connectionToServer.close(null, null);
    assertEquals("connection removed should be called only once", 1, listener.count.get());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Address(com.hazelcast.nio.Address) InetSocketAddress(java.net.InetSocketAddress) Connection(com.hazelcast.nio.Connection) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) LifecycleEvent(com.hazelcast.core.LifecycleEvent) AssertTask(com.hazelcast.test.AssertTask) LifecycleListener(com.hazelcast.core.LifecycleListener) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConnectionManager(com.hazelcast.client.connection.ClientConnectionManager) ExecutionException(java.util.concurrent.ExecutionException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 3 with AssertTask

use of com.hazelcast.test.AssertTask in project hazelcast by hazelcast.

the class MembershipListenerTest method whenMemberAdded_thenMemberAddedEvent.

@Test
public void whenMemberAdded_thenMemberAddedEvent() throws Exception {
    final HazelcastInstance server1 = hazelcastFactory.newHazelcastInstance();
    final MemberShipEventLogger listener = new MemberShipEventLogger();
    HazelcastInstance client = hazelcastFactory.newHazelcastClient();
    client.getCluster().addMembershipListener(listener);
    //start a second server and verify that the listener receives it.
    final HazelcastInstance server2 = hazelcastFactory.newHazelcastInstance();
    //verify that the listener receives member added event.
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() {
            assertNotEquals("Expecting one or more events", 0, listener.events.size());
            MembershipEvent event = (MembershipEvent) listener.events.getLast();
            assertEquals("Last event should be member added", MembershipEvent.MEMBER_ADDED, event.getEventType());
            assertEquals(server2.getCluster().getLocalMember(), event.getMember());
            assertEquals(getMembers(server1, server2), event.getMembers());
        }
    });
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) InitialMembershipEvent(com.hazelcast.core.InitialMembershipEvent) MembershipEvent(com.hazelcast.core.MembershipEvent) AssertTask(com.hazelcast.test.AssertTask) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 4 with AssertTask

use of com.hazelcast.test.AssertTask 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 5 with AssertTask

use of com.hazelcast.test.AssertTask 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

AssertTask (com.hazelcast.test.AssertTask)575 Test (org.junit.Test)489 QuickTest (com.hazelcast.test.annotation.QuickTest)428 ParallelTest (com.hazelcast.test.annotation.ParallelTest)347 HazelcastInstance (com.hazelcast.core.HazelcastInstance)263 Config (com.hazelcast.config.Config)113 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)94 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)75 ExecutionException (java.util.concurrent.ExecutionException)57 MapConfig (com.hazelcast.config.MapConfig)49 NightlyTest (com.hazelcast.test.annotation.NightlyTest)48 IOException (java.io.IOException)46 CountDownLatch (java.util.concurrent.CountDownLatch)42 IMap (com.hazelcast.core.IMap)39 NearCacheConfig (com.hazelcast.config.NearCacheConfig)38 TimeoutException (java.util.concurrent.TimeoutException)33 ClientConfig (com.hazelcast.client.config.ClientConfig)32 MapStoreConfig (com.hazelcast.config.MapStoreConfig)29 ExpectedRuntimeException (com.hazelcast.test.ExpectedRuntimeException)23 AtomicReference (java.util.concurrent.atomic.AtomicReference)20