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
Showing
5 changed files
with
56 additions
and
17 deletions
| ... | @@ -107,6 +107,12 @@ | ... | @@ -107,6 +107,12 @@ |
| 107 | <groupId>com.hazelcast</groupId> | 107 | <groupId>com.hazelcast</groupId> |
| 108 | <artifactId>hazelcast</artifactId> | 108 | <artifactId>hazelcast</artifactId> |
| 109 | </dependency> | 109 | </dependency> |
| 110 | + <dependency> | ||
| 111 | + <groupId>com.hazelcast</groupId> | ||
| 112 | + <artifactId>hazelcast</artifactId> | ||
| 113 | + <classifier>tests</classifier> | ||
| 114 | + <scope>test</scope> | ||
| 115 | + </dependency> | ||
| 110 | 116 | ||
| 111 | <!-- for shaded copycat --> | 117 | <!-- for shaded copycat --> |
| 112 | <dependency> | 118 | <dependency> | ... | ... |
| ... | @@ -15,22 +15,28 @@ | ... | @@ -15,22 +15,28 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onosproject.store.hz; | 16 | package org.onosproject.store.hz; |
| 17 | 17 | ||
| 18 | +import static com.google.common.base.Preconditions.checkArgument; | ||
| 19 | +import static com.google.common.base.Preconditions.checkState; | ||
| 20 | + | ||
| 18 | import java.io.FileNotFoundException; | 21 | import java.io.FileNotFoundException; |
| 19 | import java.util.UUID; | 22 | import java.util.UUID; |
| 20 | 23 | ||
| 21 | import com.hazelcast.config.Config; | 24 | import com.hazelcast.config.Config; |
| 22 | import com.hazelcast.config.FileSystemXmlConfig; | 25 | import com.hazelcast.config.FileSystemXmlConfig; |
| 23 | import com.hazelcast.core.HazelcastInstance; | 26 | import com.hazelcast.core.HazelcastInstance; |
| 27 | +import com.hazelcast.test.TestHazelcastInstanceFactory; | ||
| 24 | 28 | ||
| 25 | /** | 29 | /** |
| 26 | * Dummy StoreManager to use specified Hazelcast instance. | 30 | * Dummy StoreManager to use specified Hazelcast instance. |
| 27 | */ | 31 | */ |
| 28 | public class TestStoreManager extends StoreManager { | 32 | public class TestStoreManager extends StoreManager { |
| 29 | 33 | ||
| 34 | + private TestHazelcastInstanceFactory factory; | ||
| 35 | + | ||
| 30 | /** | 36 | /** |
| 31 | * Gets the Hazelcast Config for testing. | 37 | * Gets the Hazelcast Config for testing. |
| 32 | * | 38 | * |
| 33 | - * @return | 39 | + * @return Hazelcast Configuration for testing |
| 34 | */ | 40 | */ |
| 35 | public static Config getTestConfig() { | 41 | public static Config getTestConfig() { |
| 36 | Config config; | 42 | Config config; |
| ... | @@ -53,16 +59,44 @@ public class TestStoreManager extends StoreManager { | ... | @@ -53,16 +59,44 @@ public class TestStoreManager extends StoreManager { |
| 53 | } | 59 | } |
| 54 | 60 | ||
| 55 | /** | 61 | /** |
| 56 | - * Constructor. | 62 | + * Creates an instance of dummy Hazelcast instance for testing. |
| 63 | + * | ||
| 64 | + * @return HazelcastInstance | ||
| 65 | + */ | ||
| 66 | + public HazelcastInstance initSingleInstance() { | ||
| 67 | + return initInstances(1)[0]; | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + /** | ||
| 71 | + * Creates some instances of dummy Hazelcast instances for testing. | ||
| 72 | + * | ||
| 73 | + * @param count number of instances to create | ||
| 74 | + * @return array of HazelcastInstances | ||
| 75 | + */ | ||
| 76 | + public HazelcastInstance[] initInstances(int count) { | ||
| 77 | + checkArgument(count > 0, "Cluster size must be > 0"); | ||
| 78 | + factory = new TestHazelcastInstanceFactory(count); | ||
| 79 | + return factory.newInstances(getTestConfig()); | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + /** | ||
| 83 | + * Sets the Hazelast instance to return on #getHazelcastInstance(). | ||
| 57 | * | 84 | * |
| 58 | * @param instance Hazelast instance to return on #getHazelcastInstance() | 85 | * @param instance Hazelast instance to return on #getHazelcastInstance() |
| 59 | */ | 86 | */ |
| 60 | - public TestStoreManager(HazelcastInstance instance) { | 87 | + public void setHazelcastInstance(HazelcastInstance instance) { |
| 61 | this.instance = instance; | 88 | this.instance = instance; |
| 62 | } | 89 | } |
| 63 | 90 | ||
| 64 | @Override | 91 | @Override |
| 65 | public void activate() { | 92 | public void activate() { |
| 66 | // Hazelcast setup removed from original code. | 93 | // Hazelcast setup removed from original code. |
| 94 | + checkState(this.instance != null, "HazelcastInstance needs to be set"); | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + @Override | ||
| 98 | + public void deactivate() { | ||
| 99 | + // Hazelcast instance shutdown removed from original code. | ||
| 100 | + factory.shutdownAll(); | ||
| 67 | } | 101 | } |
| 68 | } | 102 | } | ... | ... |
| ... | @@ -19,8 +19,6 @@ import static org.junit.Assert.assertEquals; | ... | @@ -19,8 +19,6 @@ import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertNull; | 19 | import static org.junit.Assert.assertNull; |
| 20 | import static org.junit.Assert.assertTrue; | 20 | import static org.junit.Assert.assertTrue; |
| 21 | import static org.onosproject.net.MastershipRole.*; | 21 | import static org.onosproject.net.MastershipRole.*; |
| 22 | -import static org.onosproject.net.intent.TestTools.delay; | ||
| 23 | - | ||
| 24 | import java.util.Map; | 22 | import java.util.Map; |
| 25 | import java.util.Set; | 23 | import java.util.Set; |
| 26 | import java.util.concurrent.CountDownLatch; | 24 | import java.util.concurrent.CountDownLatch; |
| ... | @@ -51,8 +49,6 @@ import org.onosproject.store.serializers.KryoSerializer; | ... | @@ -51,8 +49,6 @@ import org.onosproject.store.serializers.KryoSerializer; |
| 51 | import org.onlab.packet.IpAddress; | 49 | import org.onlab.packet.IpAddress; |
| 52 | 50 | ||
| 53 | import com.google.common.collect.Sets; | 51 | import com.google.common.collect.Sets; |
| 54 | -import com.hazelcast.config.Config; | ||
| 55 | -import com.hazelcast.core.Hazelcast; | ||
| 56 | 52 | ||
| 57 | /** | 53 | /** |
| 58 | * Test of the Hazelcast-based distributed MastershipStore implementation. | 54 | * Test of the Hazelcast-based distributed MastershipStore implementation. |
| ... | @@ -87,9 +83,9 @@ public class DistributedMastershipStoreTest { | ... | @@ -87,9 +83,9 @@ public class DistributedMastershipStoreTest { |
| 87 | @Before | 83 | @Before |
| 88 | public void setUp() throws Exception { | 84 | public void setUp() throws Exception { |
| 89 | // TODO should find a way to clean Hazelcast instance without shutdown. | 85 | // TODO should find a way to clean Hazelcast instance without shutdown. |
| 90 | - Config config = TestStoreManager.getTestConfig(); | 86 | + TestStoreManager testStoreMgr = new TestStoreManager(); |
| 91 | - | 87 | + testStoreMgr.setHazelcastInstance(testStoreMgr.initSingleInstance()); |
| 92 | - storeMgr = new TestStoreManager(Hazelcast.newHazelcastInstance(config)); | 88 | + storeMgr = testStoreMgr; |
| 93 | storeMgr.activate(); | 89 | storeMgr.activate(); |
| 94 | 90 | ||
| 95 | serializationMgr = new KryoSerializer(); | 91 | serializationMgr = new KryoSerializer(); |
| ... | @@ -122,7 +118,6 @@ public class DistributedMastershipStoreTest { | ... | @@ -122,7 +118,6 @@ public class DistributedMastershipStoreTest { |
| 122 | assertTrue("wrong store state:", dms.roleMap.isEmpty()); | 118 | assertTrue("wrong store state:", dms.roleMap.isEmpty()); |
| 123 | 119 | ||
| 124 | testStore.put(DID1, N1, true, false, false); | 120 | testStore.put(DID1, N1, true, false, false); |
| 125 | - delay(10); //TODO there seems to be a race here. | ||
| 126 | assertEquals("wrong master:", N1, dms.getMaster(DID1)); | 121 | assertEquals("wrong master:", N1, dms.getMaster(DID1)); |
| 127 | assertNull("wrong master:", dms.getMaster(DID2)); | 122 | assertNull("wrong master:", dms.getMaster(DID2)); |
| 128 | } | 123 | } | ... | ... |
| ... | @@ -46,9 +46,6 @@ import org.onosproject.store.hz.TestStoreManager; | ... | @@ -46,9 +46,6 @@ import org.onosproject.store.hz.TestStoreManager; |
| 46 | 46 | ||
| 47 | import com.google.common.collect.ImmutableMap; | 47 | import com.google.common.collect.ImmutableMap; |
| 48 | import com.google.common.collect.ImmutableSet; | 48 | import com.google.common.collect.ImmutableSet; |
| 49 | -import com.hazelcast.config.Config; | ||
| 50 | -import com.hazelcast.core.Hazelcast; | ||
| 51 | - | ||
| 52 | import static org.junit.Assert.assertEquals; | 49 | import static org.junit.Assert.assertEquals; |
| 53 | import static org.junit.Assert.assertFalse; | 50 | import static org.junit.Assert.assertFalse; |
| 54 | import static org.junit.Assert.assertNotNull; | 51 | import static org.junit.Assert.assertNotNull; |
| ... | @@ -92,9 +89,9 @@ public class HazelcastLinkResourceStoreTest { | ... | @@ -92,9 +89,9 @@ public class HazelcastLinkResourceStoreTest { |
| 92 | @Before | 89 | @Before |
| 93 | public void setUp() throws Exception { | 90 | public void setUp() throws Exception { |
| 94 | 91 | ||
| 95 | - Config config = TestStoreManager.getTestConfig(); | 92 | + TestStoreManager testStoreMgr = new TestStoreManager(); |
| 96 | - | 93 | + testStoreMgr.setHazelcastInstance(testStoreMgr.initSingleInstance()); |
| 97 | - storeMgr = new TestStoreManager(Hazelcast.newHazelcastInstance(config)); | 94 | + storeMgr = testStoreMgr; |
| 98 | storeMgr.activate(); | 95 | storeMgr.activate(); |
| 99 | 96 | ||
| 100 | 97 | ... | ... |
| ... | @@ -248,6 +248,13 @@ | ... | @@ -248,6 +248,13 @@ |
| 248 | <version>3.4</version> | 248 | <version>3.4</version> |
| 249 | </dependency> | 249 | </dependency> |
| 250 | <dependency> | 250 | <dependency> |
| 251 | + <groupId>com.hazelcast</groupId> | ||
| 252 | + <artifactId>hazelcast</artifactId> | ||
| 253 | + <version>3.4</version> | ||
| 254 | + <classifier>tests</classifier> | ||
| 255 | + <scope>test</scope> | ||
| 256 | + </dependency> | ||
| 257 | + <dependency> | ||
| 251 | <groupId>com.eclipsesource.minimal-json</groupId> | 258 | <groupId>com.eclipsesource.minimal-json</groupId> |
| 252 | <artifactId>minimal-json</artifactId> | 259 | <artifactId>minimal-json</artifactId> |
| 253 | <version>0.9.1</version> | 260 | <version>0.9.1</version> | ... | ... |
-
Please register or login to post a comment