e5dab97069a9f2a9296a7b9139aaffa92f331b8c
[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.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     @BeforeClass
99     public static void configure() throws Exception {
100         System.setProperty(JAVA_NET_PREFER_IPV4_STACK, "true");
101         final String hazelCastfileLocation = ResourceUtils.getFilePath4Resource(HAZELCAST_XML_FILE);
102         System.setProperty(HAZELCAST_CONFIG, hazelCastfileLocation);
103
104         final TreeSet<String> ipAddressSet = NetworkUtils.getIPv4NonLoopAddresses();
105
106         if (ipAddressSet.size() == 0) {
107             throw new Exception("cound not find real IP address for test");
108         }
109         logger.info("For Infinispan, setting jgroups.tcp.address to: {}", ipAddressSet.first());
110         System.setProperty("jgroups.tcp.address", ipAddressSet.first());
111
112     }
113
114     private void startZookeeperServer() throws Exception {
115         final File zookeeperDirectory = folder.newFolder("zookeeperDirectory");
116
117         zookeeperPort = nextZookeeperPort++;
118         final InetSocketAddress addr = new InetSocketAddress(MessagingUtils.findPort(zookeeperPort));
119         zooKeeperServerServiceProvider = new ZooKeeperServerServiceProvider(zookeeperDirectory, addr);
120         zooKeeperServerServiceProvider.startZookeeperServer();
121     }
122
123     private void stopZookeeperServer() {
124         if (zooKeeperServerServiceProvider != null) {
125             zooKeeperServerServiceProvider.stopZookeeperServer();
126         }
127     }
128
129     @Test
130     public void testConcurrentContextJVMLocalVarSet() throws Exception {
131         logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
132
133         final ContextParameters contextParameters = new ContextParameters();
134         contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
135
136         final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalVarSet",
137                 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
138
139         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
140         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
141
142         assertFalse(result.isEmpty());
143
144         final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
145         final TestContextLongItem actual = result.get(TEST_VALUE);
146         assertNotNull(actual);
147         assertEquals(expected, actual.getLongValue());
148
149         logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
150     }
151
152     @Test
153     public void testConcurrentContextJVMLocalNoVarSet() throws Exception {
154         logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
155
156         new ContextParameters();
157         final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalNoVarSet",
158                 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
159
160         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
161         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
162
163         final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
164         final TestContextLongItem actual = result.get(Constants.TEST_VALUE);
165         assertNotNull(actual);
166         assertEquals(expected, actual.getLongValue());
167
168         logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
169     }
170
171     @Test
172     public void testConcurrentContextMultiJVMNoLock() throws Exception {
173         logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
174
175         final ContextParameters contextParameters = new ContextParameters();
176         contextParameters.getDistributorParameters().setPluginClass(JVMLocalDistributor.class.getCanonicalName());
177         contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
178
179         final ConfigrationProvider configrationProvider = getConfigrationProvider("testConcurrentContextMultiJVMNoLock",
180                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
181
182         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
183         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
184
185         // No concurrent map so result will be zero
186         final TestContextLongItem actual = result.get(TEST_VALUE);
187         assertNotNull(actual);
188         assertEquals(0, actual.getLongValue());
189
190         logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
191     }
192
193     @Test
194     public void testConcurrentContextHazelcastLock() throws Exception {
195         logger.debug("Running testConcurrentContextHazelcastLock test . . .");
196
197         final ContextParameters contextParameters = new ContextParameters();
198         contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
199         contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
200
201         final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastLock",
202                 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
203
204         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
205         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
206
207         final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
208         final TestContextLongItem actual = result.get(TEST_VALUE);
209         assertNotNull(actual);
210         assertEquals(expected, actual.getLongValue());
211
212         logger.debug("Ran testConcurrentContextHazelcastLock test");
213     }
214
215     @Test
216     public void testConcurrentContextCuratorLock() throws Exception {
217         logger.debug("Running testConcurrentContextCuratorLock test . . .");
218         try {
219             startZookeeperServer();
220             final ContextParameters contextParameters = new ContextParameters();
221             final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
222             distributorParameters.setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
223
224             final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
225             curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
226             curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
227             contextParameters.setLockManagerParameters(curatorParameters);
228             ParameterService.register(curatorParameters);
229
230             final ConfigrationProvider configrationProvider = getConfigrationProvider("CuratorLock",
231                     TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
232
233             final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
234             final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
235
236             final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
237             final TestContextLongItem actual = result.get(TEST_VALUE);
238             assertNotNull(actual);
239             assertEquals(expected, actual.getLongValue());
240             logger.debug("Ran testConcurrentContextCuratorLock test");
241         } finally {
242             stopZookeeperServer();
243         }
244     }
245
246     @Test
247     public void testConcurrentContextHazelcastMultiJVMHazelcastLock() throws Exception {
248         logger.debug("Running testConcurrentContextHazelcastMultiJVMHazelcastLock test . . .");
249
250         final ContextParameters contextParameters = new ContextParameters();
251         final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
252         distributorParameters.setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
253         contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
254
255         final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastMultiHazelcastlock",
256                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
257
258         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
259         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
260
261         final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
262         final TestContextLongItem actual = result.get(TEST_VALUE);
263         assertNotNull(actual);
264         assertEquals(expected, actual.getLongValue());
265         logger.debug("Ran testConcurrentContextHazelcastMultiJVMHazelcastLock test");
266     }
267
268     @Test
269     public void testConcurrentContextInfinispanMultiJVMHazelcastlock()
270             throws ApexModelException, IOException, ApexException {
271         logger.debug("Running testConcurrentContextInfinispanMultiJVMHazelcastlock test . . .");
272
273         final ContextParameters contextParameters = new ContextParameters();
274         final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
275         infinispanParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
276         infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
277         contextParameters.setDistributorParameters(infinispanParameters);
278         contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
279
280         final ConfigrationProvider configrationProvider = getConfigrationProvider("InfinispanMultiHazelcastlock",
281                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
282
283         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
284         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
285
286         final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
287         final TestContextLongItem actual = result.get(TEST_VALUE);
288         assertNotNull(actual);
289         assertEquals(expected, actual.getLongValue());
290         logger.debug("Ran testConcurrentContextInfinispanMultiJVMHazelcastlock test");
291     }
292
293     @Test
294     public void testConcurrentContextInfinispanMultiJVMCuratorLock() throws Exception {
295         logger.debug("Running testConcurrentContextInfinispanMultiJVMCuratorLock test . . .");
296
297         try {
298             startZookeeperServer();
299
300             final ContextParameters contextParameters = new ContextParameters();
301             final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
302             infinispanParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
303             infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
304             contextParameters.setDistributorParameters(infinispanParameters);
305
306             final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
307             curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
308             curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
309             contextParameters.setLockManagerParameters(curatorParameters);
310             ParameterService.register(curatorParameters);
311
312             final ConfigrationProvider configrationProvider = getConfigrationProvider("InfinispanMultiCuratorLock",
313                     TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
314
315             final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
316             final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
317
318             final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
319             final TestContextLongItem actual = result.get(TEST_VALUE);
320             assertNotNull(actual);
321             assertEquals(expected, actual.getLongValue());
322         } finally {
323             stopZookeeperServer();
324         }
325
326         logger.debug("Ran testConcurrentContextInfinispanMultiJVMCuratorLock test");
327     }
328
329     @Test
330     public void testConcurrentContextHazelcastMultiJVMCuratorLock() throws Exception {
331         logger.debug("Running testConcurrentContextHazelcastMultiJVMCuratorLock test . . .");
332
333         try {
334             startZookeeperServer();
335
336             final ContextParameters contextParameters = new ContextParameters();
337             contextParameters.getDistributorParameters()
338                     .setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
339
340             final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
341             curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
342             curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
343             contextParameters.setLockManagerParameters(curatorParameters);
344             ParameterService.register(curatorParameters);
345
346             final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastMultiCuratorLock",
347                     TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
348             final Map<String, TestContextLongItem> result =
349                     new ConcurrentContext(configrationProvider).testConcurrentContext();
350
351             final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
352             final TestContextLongItem actual = result.get(TEST_VALUE);
353             assertNotNull(actual);
354             assertEquals(expected, actual.getLongValue());
355         } finally {
356             stopZookeeperServer();
357         }
358         logger.debug("Ran testConcurrentContextHazelcastMultiJVMCuratorLock test");
359     }
360
361     ConfigrationProvider getConfigrationProvider(final String testType, final int jvmCount, final int threadCount,
362             final int threadLoops) {
363         return new ConfigrationProviderImpl(testType, jvmCount, threadCount, threadLoops, 16,
364                 LockType.WRITE_LOCK_SINGLE_VALUE_UPDATE.getValue()) {
365             @Override
366             public Map<String, Object> getContextAlbumInitValues() {
367                 final Map<String, Object> initValues = super.getContextAlbumInitValues();
368                 initValues.put(TEST_VALUE, new TestContextLongItem(0l));
369                 return initValues;
370             }
371
372         };
373     }
374 }