/*
* Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.client;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.core.EntryAdapter;
import com.hazelcast.core.EntryEvent;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.core.LifecycleEvent;
import com.hazelcast.core.LifecycleListener;
import com.hazelcast.test.AssertTask;
import com.hazelcast.test.HazelcastSerialClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@RunWith(HazelcastSerialClassRunner.class)
@Category(QuickTest.class)
public class ClientReconnectTest extends HazelcastTestSupport {
@After
@Before
public void cleanup() throws Exception {
HazelcastClient.shutdownAll();
Hazelcast.shutdownAll();
}
@Test
public void testClientReconnectOnClusterDown() throws Exception {
final HazelcastInstance h1 = Hazelcast.newHazelcastInstance();
final HazelcastInstance client = HazelcastClient.newHazelcastClient();
final CountDownLatch connectedLatch = new CountDownLatch(2);
client.getLifecycleService().addLifecycleListener(new LifecycleListener() {
@Override
public void stateChanged(LifecycleEvent event) {
connectedLatch.countDown();
}
});
IMap<String, String> m = client.getMap("default");
h1.shutdown();
Hazelcast.newHazelcastInstance();
assertOpenEventually(connectedLatch, 10);
assertNull(m.put("test", "test"));
assertEquals("test", m.get("test"));
}
@Test
public void testClientReconnectOnClusterDownWithEntryListeners() throws Exception {
HazelcastInstance h1 = Hazelcast.newHazelcastInstance();
ClientConfig clientConfig = new ClientConfig();
clientConfig.getNetworkConfig().setRedoOperation(true);
final HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
final CountDownLatch connectedLatch = new CountDownLatch(2);
client.getLifecycleService().addLifecycleListener(new LifecycleListener() {
@Override
public void stateChanged(LifecycleEvent event) {
connectedLatch.countDown();
}
});
final IMap<Object, Object> map = client.getMap("default");
final AtomicInteger eventCount = new AtomicInteger();
final EntryAdapter<Object, Object> listener = new EntryAdapter<Object, Object>() {
public void onEntryEvent(EntryEvent<Object, Object> event) {
eventCount.incrementAndGet();
}
};
map.addEntryListener(listener, true);
h1.shutdown();
Hazelcast.newHazelcastInstance();
assertOpenEventually(connectedLatch, 10);
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
map.put("key", "value");
int count = eventCount.get();
assertTrue("No events generated!", count > 0);
}
}, 30);
}
}