a62414ac6d1ad963103acb932078e5f1c3cdbdbc
[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.testsuites.integration.context;
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.SortedSet;
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.test.concepts.TestContextLongItem;
44 import org.onap.policy.apex.context.test.lock.modifier.LockType;
45 import org.onap.policy.apex.context.test.locking.ConcurrentContext;
46 import org.onap.policy.apex.context.test.utils.ConfigrationProvider;
47 import org.onap.policy.apex.context.test.utils.ConfigrationProviderImpl;
48 import org.onap.policy.apex.context.test.utils.Constants;
49 import org.onap.policy.apex.context.test.utils.NetworkUtils;
50 import org.onap.policy.apex.context.test.utils.ZooKeeperServerServiceProvider;
51 import org.onap.policy.apex.core.infrastructure.messaging.util.MessagingUtils;
52 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
53 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
54 import org.onap.policy.apex.plugins.context.distribution.hazelcast.HazelcastContextDistributor;
55 import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanContextDistributor;
56 import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanDistributorParameters;
57 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManager;
58 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManagerParameters;
59 import org.onap.policy.apex.plugins.context.locking.hazelcast.HazelcastLockManager;
60 import org.onap.policy.common.parameters.ParameterService;
61 import org.onap.policy.common.utils.resources.ResourceUtils;
62 import org.slf4j.ext.XLogger;
63 import org.slf4j.ext.XLoggerFactory;
64
65 /**
66  * The Class TestConcurrentContext tests concurrent use of context.
67  *
68  * @author Liam Fallon (liam.fallon@ericsson.com)
69  */
70 public class TestConcurrentContext {
71     private static final String HAZELCAST_CONFIG = "hazelcast.config";
72
73     private static final String JAVA_NET_PREFER_IPV4_STACK = "java.net.preferIPv4Stack";
74     private static final String HAZELCAST_XML_FILE = "src/test/resources/hazelcast/hazelcast.xml";
75
76     // Logger for this class
77     private static final XLogger logger = XLoggerFactory.getXLogger(TestConcurrentContext.class);
78
79     // Test parameters
80     private static final String ZOOKEEPER_ADDRESS = "127.0.0.1";
81     private static final int ZOOKEEPER_START_PORT = 62181;
82     private static final int TEST_JVM_COUNT_SINGLE_JVM = 1;
83     private static final int TEST_JVM_COUNT_MULTI_JVM = 3;
84     private static final int TEST_THREAD_COUNT_SINGLE_JVM = 64;
85     private static final int TEST_THREAD_COUNT_MULTI_JVM = 20;
86     private static final int TEST_THREAD_LOOPS = 100;
87
88     // We need to increment the Zookeeper port because sometimes the port is not released at the end
89     // of the test for a few seconds.
90     private static int nextZookeeperPort = ZOOKEEPER_START_PORT;
91     private int zookeeperPort;
92
93     @Rule
94     public final TemporaryFolder folder = new TemporaryFolder();
95
96     private ZooKeeperServerServiceProvider zooKeeperServerServiceProvider;
97
98     /**
99      * Configure.
100      *
101      * @throws Exception the exception
102      */
103     @BeforeClass
104     public static void configure() throws Exception {
105         System.setProperty(JAVA_NET_PREFER_IPV4_STACK, "true");
106         final String hazelCastfileLocation = ResourceUtils.getFilePath4Resource(HAZELCAST_XML_FILE);
107         System.setProperty(HAZELCAST_CONFIG, hazelCastfileLocation);
108
109         final SortedSet<String> ipAddressSet = NetworkUtils.getIPv4NonLoopAddresses();
110
111         if (ipAddressSet.size() == 0) {
112             throw new Exception("cound not find real IP address for test");
113         }
114         logger.info("For Infinispan, setting jgroups.tcp.address to: {}", ipAddressSet.first());
115         System.setProperty("jgroups.tcp.address", ipAddressSet.first());
116
117     }
118
119     /**
120      * Start zookeeper server.
121      *
122      * @throws Exception the exception
123      */
124     private void startZookeeperServer() throws Exception {
125         final File zookeeperDirectory = folder.newFolder("zookeeperDirectory");
126
127         zookeeperPort = nextZookeeperPort++;
128         final InetSocketAddress addr = new InetSocketAddress(MessagingUtils.findPort(zookeeperPort));
129         zooKeeperServerServiceProvider = new ZooKeeperServerServiceProvider(zookeeperDirectory, addr);
130         zooKeeperServerServiceProvider.startZookeeperServer();
131     }
132
133     /**
134      * Stop zookeeper server.
135      */
136     private void stopZookeeperServer() {
137         if (zooKeeperServerServiceProvider != null) {
138             zooKeeperServerServiceProvider.stopZookeeperServer();
139         }
140     }
141
142     /**
143      * Test concurrent context jvm local var set.
144      *
145      * @throws Exception the exception
146      */
147     @Test
148     public void testConcurrentContextJvmLocalVarSet() throws Exception {
149         logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
150
151         final ContextParameters contextParameters = new ContextParameters();
152         contextParameters.getLockManagerParameters().setPluginClass(JvmLocalLockManager.class.getCanonicalName());
153
154         final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalVarSet",
155                 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
156
157         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
158         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
159
160         assertFalse(result.isEmpty());
161
162         final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
163         final TestContextLongItem actual = result.get(TEST_VALUE);
164         assertNotNull(actual);
165         assertEquals(expected, actual.getLongValue());
166
167         logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
168     }
169
170     /**
171      * Test concurrent context jvm local no var set.
172      *
173      * @throws Exception the exception
174      */
175     @Test
176     public void testConcurrentContextJvmLocalNoVarSet() throws Exception {
177         logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
178
179         new ContextParameters();
180         final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalNoVarSet",
181                 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
182
183         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
184         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
185
186         final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
187         final TestContextLongItem actual = result.get(Constants.TEST_VALUE);
188         assertNotNull(actual);
189         assertEquals(expected, actual.getLongValue());
190
191         logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
192     }
193
194     /**
195      * Test concurrent context multi jvm no lock.
196      *
197      * @throws Exception the exception
198      */
199     @Test
200     public void testConcurrentContextMultiJvmNoLock() throws Exception {
201         logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
202
203         final ContextParameters contextParameters = new ContextParameters();
204         contextParameters.getDistributorParameters().setPluginClass(JvmLocalDistributor.class.getCanonicalName());
205         contextParameters.getLockManagerParameters().setPluginClass(JvmLocalLockManager.class.getCanonicalName());
206
207         final ConfigrationProvider configrationProvider = getConfigrationProvider("testConcurrentContextMultiJVMNoLock",
208                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
209
210         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
211         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
212
213         // No concurrent map so result will be zero
214         final TestContextLongItem actual = result.get(TEST_VALUE);
215         assertNotNull(actual);
216         assertEquals(0, actual.getLongValue());
217
218         logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
219     }
220
221     /**
222      * Test concurrent context hazelcast lock.
223      *
224      * @throws Exception the exception
225      */
226     @Test
227     public void testConcurrentContextHazelcastLock() throws Exception {
228         logger.debug("Running testConcurrentContextHazelcastLock test . . .");
229
230         final ContextParameters contextParameters = new ContextParameters();
231         contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
232         contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
233
234         final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastLock",
235                 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
236
237         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
238         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
239
240         final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
241         final TestContextLongItem actual = result.get(TEST_VALUE);
242         assertNotNull(actual);
243         assertEquals(expected, actual.getLongValue());
244
245         logger.debug("Ran testConcurrentContextHazelcastLock test");
246     }
247
248     /**
249      * Test concurrent context curator lock.
250      *
251      * @throws Exception the exception
252      */
253     @Test
254     public void testConcurrentContextCuratorLock() throws Exception {
255         logger.debug("Running testConcurrentContextCuratorLock test . . .");
256         try {
257             startZookeeperServer();
258             final ContextParameters contextParameters = new ContextParameters();
259             final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
260             distributorParameters.setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
261
262             final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
263             curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
264             curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
265             contextParameters.setLockManagerParameters(curatorParameters);
266             ParameterService.register(curatorParameters);
267
268             final ConfigrationProvider configrationProvider = getConfigrationProvider("CuratorLock",
269                     TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
270
271             final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
272             final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
273
274             final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
275             final TestContextLongItem actual = result.get(TEST_VALUE);
276             assertNotNull(actual);
277             assertEquals(expected, actual.getLongValue());
278             logger.debug("Ran testConcurrentContextCuratorLock test");
279         } finally {
280             stopZookeeperServer();
281         }
282     }
283
284     /**
285      * Test concurrent context hazelcast multi jvm hazelcast lock.
286      *
287      * @throws Exception the exception
288      */
289     @Test
290     public void testConcurrentContextHazelcastMultiJvmHazelcastLock() throws Exception {
291         logger.debug("Running testConcurrentContextHazelcastMultiJVMHazelcastLock test . . .");
292
293         final ContextParameters contextParameters = new ContextParameters();
294         final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
295         distributorParameters.setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
296         contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
297
298         final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastMultiHazelcastlock",
299                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
300
301         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
302         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
303
304         final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
305         final TestContextLongItem actual = result.get(TEST_VALUE);
306         assertNotNull(actual);
307         assertEquals(expected, actual.getLongValue());
308         logger.debug("Ran testConcurrentContextHazelcastMultiJVMHazelcastLock test");
309     }
310
311     /**
312      * Test concurrent context infinispan multi jvm hazelcastlock.
313      *
314      * @throws ApexModelException the apex model exception
315      * @throws IOException Signals that an I/O exception has occurred.
316      * @throws ApexException the apex exception
317      */
318     @Test
319     public void testConcurrentContextInfinispanMultiJvmHazelcastlock()
320             throws ApexModelException, IOException, ApexException {
321         logger.debug("Running testConcurrentContextInfinispanMultiJVMHazelcastlock test . . .");
322
323         final ContextParameters contextParameters = new ContextParameters();
324         final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
325         infinispanParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
326         infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
327         contextParameters.setDistributorParameters(infinispanParameters);
328         contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
329
330         final ConfigrationProvider configrationProvider = getConfigrationProvider("InfinispanMultiHazelcastlock",
331                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
332
333         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
334         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
335
336         final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
337         final TestContextLongItem actual = result.get(TEST_VALUE);
338         assertNotNull(actual);
339         assertEquals(expected, actual.getLongValue());
340         logger.debug("Ran testConcurrentContextInfinispanMultiJVMHazelcastlock test");
341     }
342
343     /**
344      * Test concurrent context infinispan multi jvm curator lock.
345      *
346      * @throws Exception the exception
347      */
348     @Test
349     public void testConcurrentContextInfinispanMultiJvmCuratorLock() throws Exception {
350         logger.debug("Running testConcurrentContextInfinispanMultiJVMCuratorLock test . . .");
351
352         try {
353             startZookeeperServer();
354
355             final ContextParameters contextParameters = new ContextParameters();
356             final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
357             infinispanParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
358             infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
359             contextParameters.setDistributorParameters(infinispanParameters);
360
361             final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
362             curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
363             curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
364             contextParameters.setLockManagerParameters(curatorParameters);
365             ParameterService.register(curatorParameters);
366
367             final ConfigrationProvider configrationProvider = getConfigrationProvider("InfinispanMultiCuratorLock",
368                     TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
369
370             final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
371             final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
372
373             final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
374             final TestContextLongItem actual = result.get(TEST_VALUE);
375             assertNotNull(actual);
376             assertEquals(expected, actual.getLongValue());
377         } finally {
378             stopZookeeperServer();
379         }
380
381         logger.debug("Ran testConcurrentContextInfinispanMultiJVMCuratorLock test");
382     }
383
384     /**
385      * Test concurrent context hazelcast multi jvm curator lock.
386      *
387      * @throws Exception the exception
388      */
389     @Test
390     public void testConcurrentContextHazelcastMultiJvmCuratorLock() throws Exception {
391         logger.debug("Running testConcurrentContextHazelcastMultiJVMCuratorLock test . . .");
392
393         try {
394             startZookeeperServer();
395
396             final ContextParameters contextParameters = new ContextParameters();
397             contextParameters.getDistributorParameters()
398                     .setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
399
400             final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
401             curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
402             curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
403             contextParameters.setLockManagerParameters(curatorParameters);
404             ParameterService.register(curatorParameters);
405
406             final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastMultiCuratorLock",
407                     TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
408             final Map<String, TestContextLongItem> result =
409                     new ConcurrentContext(configrationProvider).testConcurrentContext();
410
411             final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
412             final TestContextLongItem actual = result.get(TEST_VALUE);
413             assertNotNull(actual);
414             assertEquals(expected, actual.getLongValue());
415         } finally {
416             stopZookeeperServer();
417         }
418         logger.debug("Ran testConcurrentContextHazelcastMultiJVMCuratorLock test");
419     }
420
421     /**
422      * Gets the configration provider.
423      *
424      * @param testType the test type
425      * @param jvmCount the jvm count
426      * @param threadCount the thread count
427      * @param threadLoops the thread loops
428      * @return the configration provider
429      */
430     ConfigrationProvider getConfigrationProvider(final String testType, final int jvmCount, final int threadCount,
431             final int threadLoops) {
432         return new ConfigrationProviderImpl(testType, jvmCount, threadCount, threadLoops, 16,
433                 LockType.WRITE_LOCK_SINGLE_VALUE_UPDATE.getValue()) {
434             @Override
435             public Map<String, Object> getContextAlbumInitValues() {
436                 final Map<String, Object> initValues = super.getContextAlbumInitValues();
437                 initValues.put(TEST_VALUE, new TestContextLongItem(0L));
438                 return initValues;
439             }
440
441         };
442     }
443 }