Yuta HIGUCHI
Committed by Gerrit Code Review

Use TestHazelcastFactory

- Modified Hazelcast related tests to use TestHazelcastFactory.
  Hazelcast instances generated by it uses mocked network, which might resolve timing issue (ONOS-368).

  See: https://github.com/hazelcast/hazelcast/blob/master/hazelcast/src/test/java/com/hazelcast/test/TestHazelcastInstanceFactory.java

Change-Id: I18f1c2d855eebf679a4be97a53cea2c808acfd04
......@@ -107,6 +107,12 @@
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<!-- for shaded copycat -->
<dependency>
......
......@@ -15,22 +15,28 @@
*/
package org.onosproject.store.hz;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import java.io.FileNotFoundException;
import java.util.UUID;
import com.hazelcast.config.Config;
import com.hazelcast.config.FileSystemXmlConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.test.TestHazelcastInstanceFactory;
/**
* Dummy StoreManager to use specified Hazelcast instance.
*/
public class TestStoreManager extends StoreManager {
private TestHazelcastInstanceFactory factory;
/**
* Gets the Hazelcast Config for testing.
*
* @return
* @return Hazelcast Configuration for testing
*/
public static Config getTestConfig() {
Config config;
......@@ -53,16 +59,44 @@ public class TestStoreManager extends StoreManager {
}
/**
* Constructor.
* Creates an instance of dummy Hazelcast instance for testing.
*
* @return HazelcastInstance
*/
public HazelcastInstance initSingleInstance() {
return initInstances(1)[0];
}
/**
* Creates some instances of dummy Hazelcast instances for testing.
*
* @param count number of instances to create
* @return array of HazelcastInstances
*/
public HazelcastInstance[] initInstances(int count) {
checkArgument(count > 0, "Cluster size must be > 0");
factory = new TestHazelcastInstanceFactory(count);
return factory.newInstances(getTestConfig());
}
/**
* Sets the Hazelast instance to return on #getHazelcastInstance().
*
* @param instance Hazelast instance to return on #getHazelcastInstance()
*/
public TestStoreManager(HazelcastInstance instance) {
public void setHazelcastInstance(HazelcastInstance instance) {
this.instance = instance;
}
@Override
public void activate() {
// Hazelcast setup removed from original code.
checkState(this.instance != null, "HazelcastInstance needs to be set");
}
@Override
public void deactivate() {
// Hazelcast instance shutdown removed from original code.
factory.shutdownAll();
}
}
......
......@@ -19,8 +19,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.onosproject.net.MastershipRole.*;
import static org.onosproject.net.intent.TestTools.delay;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
......@@ -51,8 +49,6 @@ import org.onosproject.store.serializers.KryoSerializer;
import org.onlab.packet.IpAddress;
import com.google.common.collect.Sets;
import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
/**
* Test of the Hazelcast-based distributed MastershipStore implementation.
......@@ -87,9 +83,9 @@ public class DistributedMastershipStoreTest {
@Before
public void setUp() throws Exception {
// TODO should find a way to clean Hazelcast instance without shutdown.
Config config = TestStoreManager.getTestConfig();
storeMgr = new TestStoreManager(Hazelcast.newHazelcastInstance(config));
TestStoreManager testStoreMgr = new TestStoreManager();
testStoreMgr.setHazelcastInstance(testStoreMgr.initSingleInstance());
storeMgr = testStoreMgr;
storeMgr.activate();
serializationMgr = new KryoSerializer();
......@@ -122,7 +118,6 @@ public class DistributedMastershipStoreTest {
assertTrue("wrong store state:", dms.roleMap.isEmpty());
testStore.put(DID1, N1, true, false, false);
delay(10); //TODO there seems to be a race here.
assertEquals("wrong master:", N1, dms.getMaster(DID1));
assertNull("wrong master:", dms.getMaster(DID2));
}
......
......@@ -46,9 +46,6 @@ import org.onosproject.store.hz.TestStoreManager;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
......@@ -92,9 +89,9 @@ public class HazelcastLinkResourceStoreTest {
@Before
public void setUp() throws Exception {
Config config = TestStoreManager.getTestConfig();
storeMgr = new TestStoreManager(Hazelcast.newHazelcastInstance(config));
TestStoreManager testStoreMgr = new TestStoreManager();
testStoreMgr.setHazelcastInstance(testStoreMgr.initSingleInstance());
storeMgr = testStoreMgr;
storeMgr.activate();
......
......@@ -248,6 +248,13 @@
<version>3.4</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>3.4</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.eclipsesource.minimal-json</groupId>
<artifactId>minimal-json</artifactId>
<version>0.9.1</version>
......