87968e64d33972d2c49cc8ec6217bacfea32eeb8
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.context.test.locking;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.onap.policy.apex.context.parameters.DistributorParameters.DEFAULT_DISTRIBUTOR_PLUGIN_CLASS;
27 import static org.onap.policy.apex.context.test.utils.Constants.TEST_VALUE;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.net.InetSocketAddress;
32 import java.util.Map;
33 import java.util.TreeSet;
34
35 import org.junit.BeforeClass;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.TemporaryFolder;
39 import org.onap.policy.apex.context.impl.distribution.jvmlocal.JVMLocalDistributor;
40 import org.onap.policy.apex.context.impl.locking.jvmlocal.JVMLocalLockManager;
41 import org.onap.policy.apex.context.parameters.ContextParameters;
42 import org.onap.policy.apex.context.parameters.DistributorParameters;
43 import org.onap.policy.apex.context.parameters.LockManagerParameters;
44 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
45 import org.onap.policy.apex.context.test.lock.modifier.LockType;
46 import org.onap.policy.apex.context.test.locking.ConcurrentContext;
47 import org.onap.policy.apex.context.test.utils.ConfigrationProvider;
48 import org.onap.policy.apex.context.test.utils.ConfigrationProviderImpl;
49 import org.onap.policy.apex.context.test.utils.Constants;
50 import org.onap.policy.apex.context.test.utils.NetworkUtils;
51 import org.onap.policy.apex.context.test.utils.ZooKeeperServerServiceProvider;
52 import org.onap.policy.apex.core.infrastructure.messaging.util.MessagingUtils;
53 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
54 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
55 import org.onap.policy.apex.model.basicmodel.service.ParameterService;
56 import org.onap.policy.apex.plugins.context.distribution.hazelcast.HazelcastContextDistributor;
57 import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanContextDistributor;
58 import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanDistributorParameters;
59 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManager;
60 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManagerParameters;
61 import org.onap.policy.apex.plugins.context.locking.hazelcast.HazelcastLockManager;
62 import org.onap.policy.common.utils.resources.ResourceUtils;
63 import org.slf4j.ext.XLogger;
64 import org.slf4j.ext.XLoggerFactory;
65
66 /**
67  * The Class TestConcurrentContext tests concurrent use of context.
68  *
69  * @author Liam Fallon (liam.fallon@ericsson.com)
70  */
71 public class TestConcurrentContext {
72     private static final String HAZELCAST_CONFIG = "hazelcast.config";
73
74     private static final String JAVA_NET_PREFER_IPV4_STACK = "java.net.preferIPv4Stack";
75     private static final String HAZELCAST_XML_FILE = "src/test/resources/hazelcast/hazelcast.xml";
76
77     // Logger for this class
78     private static final XLogger logger = XLoggerFactory.getXLogger(TestConcurrentContext.class);
79
80     // Test parameters
81     private static final String ZOOKEEPER_ADDRESS = "127.0.0.1";
82     private static final int ZOOKEEPER_START_PORT = 62181;
83     private static final int TEST_JVM_COUNT_SINGLE_JVM = 1;
84     private static final int TEST_JVM_COUNT_MULTI_JVM = 3;
85     private static final int TEST_THREAD_COUNT_SINGLE_JVM = 64;
86     private static final int TEST_THREAD_COUNT_MULTI_JVM = 20;
87     private static final int TEST_THREAD_LOOPS = 100;
88
89     // We need to increment the Zookeeper port because sometimes the port is not released at the end
90     // of the test for a few seconds.
91     private static int nextZookeeperPort = ZOOKEEPER_START_PORT;
92     private int zookeeperPort;
93
94     @Rule
95     public final TemporaryFolder folder = new TemporaryFolder();
96
97     private ZooKeeperServerServiceProvider zooKeeperServerServiceProvider;
98
99     @BeforeClass
100     public static void configure() throws Exception {
101         System.setProperty(JAVA_NET_PREFER_IPV4_STACK, "true");
102         final String hazelCastfileLocation = ResourceUtils.getFilePath4Resource(HAZELCAST_XML_FILE);
103         System.setProperty(HAZELCAST_CONFIG, hazelCastfileLocation);
104
105         final TreeSet<String> ipAddressSet = NetworkUtils.getIPv4NonLoopAddresses();
106
107         if (ipAddressSet.size() == 0) {
108             throw new Exception("cound not find real IP address for test");
109         }
110         logger.info("For Infinispan, setting jgroups.tcp.address to: {}", ipAddressSet.first());
111         System.setProperty("jgroups.tcp.address", ipAddressSet.first());
112
113     }
114
115     private void startZookeeperServer() throws Exception {
116         final File zookeeperDirectory = folder.newFolder("zookeeperDirectory");
117
118         zookeeperPort = nextZookeeperPort++;
119         final InetSocketAddress addr = new InetSocketAddress(MessagingUtils.findPort(zookeeperPort));
120         zooKeeperServerServiceProvider = new ZooKeeperServerServiceProvider(zookeeperDirectory, addr);
121         zooKeeperServerServiceProvider.startZookeeperServer();
122     }
123
124     private void stopZookeeperServer() {
125         if (zooKeeperServerServiceProvider != null) {
126             zooKeeperServerServiceProvider.stopZookeeperServer();
127         }
128     }
129
130     @Test
131     public void testConcurrentContextJVMLocalVarSet() throws Exception {
132         logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
133
134         final ContextParameters contextParameters = new ContextParameters();
135         contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
136
137         final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalVarSet",
138                 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
139
140         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
141         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
142
143         assertFalse(result.isEmpty());
144
145         final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
146         final TestContextLongItem actual = result.get(TEST_VALUE);
147         assertNotNull(actual);
148         assertEquals(expected, actual.getLongValue());
149
150         logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
151     }
152
153     @Test
154     public void testConcurrentContextJVMLocalNoVarSet() throws Exception {
155         logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
156
157         new ContextParameters();
158         final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalNoVarSet",
159                 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
160
161         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
162         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
163
164         final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
165         final TestContextLongItem actual = result.get(Constants.TEST_VALUE);
166         assertNotNull(actual);
167         assertEquals(expected, actual.getLongValue());
168
169         logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
170     }
171
172     @Test
173     public void testConcurrentContextMultiJVMNoLock() throws Exception {
174         logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
175
176         final ContextParameters contextParameters = new ContextParameters();
177         contextParameters.getDistributorParameters().setPluginClass(JVMLocalDistributor.class.getCanonicalName());
178         contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
179
180         final ConfigrationProvider configrationProvider = getConfigrationProvider("testConcurrentContextMultiJVMNoLock",
181                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
182
183         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
184         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
185
186         // No concurrent map so result will be zero
187         final TestContextLongItem actual = result.get(TEST_VALUE);
188         assertNotNull(actual);
189         assertEquals(0, actual.getLongValue());
190
191         logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
192     }
193
194     @Test
195     public void testConcurrentContextHazelcastLock() throws Exception {
196         logger.debug("Running testConcurrentContextHazelcastLock test . . .");
197
198         final ContextParameters contextParameters = new ContextParameters();
199         contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
200         contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
201
202         final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastLock",
203                 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
204
205         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
206         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
207
208         final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
209         final TestContextLongItem actual = result.get(TEST_VALUE);
210         assertNotNull(actual);
211         assertEquals(expected, actual.getLongValue());
212
213         logger.debug("Ran testConcurrentContextHazelcastLock test");
214     }
215
216     @Test
217     public void testConcurrentContextCuratorLock() throws Exception {
218         logger.debug("Running testConcurrentContextCuratorLock test . . .");
219         try {
220             startZookeeperServer();
221             final ContextParameters contextParameters = new ContextParameters();
222             final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
223             distributorParameters.setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
224
225             final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
226             curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
227             curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
228             contextParameters.setLockManagerParameters(curatorParameters);
229             ParameterService.registerParameters(LockManagerParameters.class, curatorParameters);
230
231             final ConfigrationProvider configrationProvider = getConfigrationProvider("CuratorLock",
232                     TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
233
234             final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
235             final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
236
237             final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
238             final TestContextLongItem actual = result.get(TEST_VALUE);
239             assertNotNull(actual);
240             assertEquals(expected, actual.getLongValue());
241             logger.debug("Ran testConcurrentContextCuratorLock test");
242         } finally {
243             stopZookeeperServer();
244         }
245     }
246
247     @Test
248     public void testConcurrentContextHazelcastMultiJVMHazelcastLock() throws Exception {
249         logger.debug("Running testConcurrentContextHazelcastMultiJVMHazelcastLock test . . .");
250
251         final ContextParameters contextParameters = new ContextParameters();
252         final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
253         distributorParameters.setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
254         contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
255
256         final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastMultiHazelcastlock",
257                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
258
259         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
260         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
261
262         final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
263         final TestContextLongItem actual = result.get(TEST_VALUE);
264         assertNotNull(actual);
265         assertEquals(expected, actual.getLongValue());
266         logger.debug("Ran testConcurrentContextHazelcastMultiJVMHazelcastLock test");
267     }
268
269     @Test
270     public void testConcurrentContextInfinispanMultiJVMHazelcastlock()
271             throws ApexModelException, IOException, ApexException {
272         logger.debug("Running testConcurrentContextInfinispanMultiJVMHazelcastlock test . . .");
273
274         final ContextParameters contextParameters = new ContextParameters();
275         final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
276         infinispanParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
277         infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
278         contextParameters.setDistributorParameters(infinispanParameters);
279         contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
280
281         final ConfigrationProvider configrationProvider = getConfigrationProvider("InfinispanMultiHazelcastlock",
282                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
283
284         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
285         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
286
287         final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
288         final TestContextLongItem actual = result.get(TEST_VALUE);
289         assertNotNull(actual);
290         assertEquals(expected, actual.getLongValue());
291         logger.debug("Ran testConcurrentContextInfinispanMultiJVMHazelcastlock test");
292     }
293
294     @Test
295     public void testConcurrentContextInfinispanMultiJVMCuratorLock() throws Exception {
296         logger.debug("Running testConcurrentContextInfinispanMultiJVMCuratorLock test . . .");
297
298         try {
299             startZookeeperServer();
300
301             final ContextParameters contextParameters = new ContextParameters();
302             final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
303             infinispanParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
304             infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
305             contextParameters.setDistributorParameters(infinispanParameters);
306
307             final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
308             curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
309             curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
310             contextParameters.setLockManagerParameters(curatorParameters);
311             ParameterService.registerParameters(LockManagerParameters.class, curatorParameters);
312
313             final ConfigrationProvider configrationProvider = getConfigrationProvider("InfinispanMultiCuratorLock",
314                     TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
315
316             final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
317             final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
318
319             final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
320             final TestContextLongItem actual = result.get(TEST_VALUE);
321             assertNotNull(actual);
322             assertEquals(expected, actual.getLongValue());
323         } finally {
324             stopZookeeperServer();
325         }
326
327         logger.debug("Ran testConcurrentContextInfinispanMultiJVMCuratorLock test");
328     }
329
330     @Test
331     public void testConcurrentContextHazelcastMultiJVMCuratorLock() throws Exception {
332         logger.debug("Running testConcurrentContextHazelcastMultiJVMCuratorLock test . . .");
333
334         try {
335             startZookeeperServer();
336
337             final ContextParameters contextParameters = new ContextParameters();
338             contextParameters.getDistributorParameters()
339                     .setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
340
341             final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
342             curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
343             curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
344             contextParameters.setLockManagerParameters(curatorParameters);
345             ParameterService.registerParameters(LockManagerParameters.class, curatorParameters);
346
347             final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastMultiCuratorLock",
348                     TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
349             final Map<String, TestContextLongItem> result =
350                     new ConcurrentContext(configrationProvider).testConcurrentContext();
351
352             final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
353             final TestContextLongItem actual = result.get(TEST_VALUE);
354             assertNotNull(actual);
355             assertEquals(expected, actual.getLongValue());
356         } finally {
357             stopZookeeperServer();
358         }
359         logger.debug("Ran testConcurrentContextHazelcastMultiJVMCuratorLock test");
360     }
361
362     ConfigrationProvider getConfigrationProvider(final String testType, final int jvmCount, final int threadCount,
363             final int threadLoops) {
364         return new ConfigrationProviderImpl(testType, jvmCount, threadCount, threadLoops, 16,
365                 LockType.WRITE_LOCK_SINGLE_VALUE_UPDATE.getValue()) {
366             @Override
367             public Map<String, Object> getContextAlbumInitValues() {
368                 final Map<String, Object> initValues = super.getContextAlbumInitValues();
369                 initValues.put(TEST_VALUE, new TestContextLongItem(0l));
370                 return initValues;
371             }
372
373         };
374     }
375 }