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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.testsuites.integration.context;
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;
30 import java.io.IOException;
31 import java.net.InetSocketAddress;
33 import java.util.SortedSet;
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;
66 * The Class TestConcurrentContext tests concurrent use of context.
68 * @author Liam Fallon (liam.fallon@ericsson.com)
70 public class TestConcurrentContext {
71 private static final String HAZELCAST_CONFIG = "hazelcast.config";
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";
76 // Logger for this class
77 private static final XLogger logger = XLoggerFactory.getXLogger(TestConcurrentContext.class);
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;
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;
94 public final TemporaryFolder folder = new TemporaryFolder();
96 private ZooKeeperServerServiceProvider zooKeeperServerServiceProvider;
101 * @throws Exception the exception
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);
109 final SortedSet<String> ipAddressSet = NetworkUtils.getIPv4NonLoopAddresses();
111 if (ipAddressSet.size() == 0) {
112 throw new Exception("cound not find real IP address for test");
114 logger.info("For Infinispan, setting jgroups.tcp.address to: {}", ipAddressSet.first());
115 System.setProperty("jgroups.tcp.address", ipAddressSet.first());
120 * Start zookeeper server.
122 * @throws Exception the exception
124 private void startZookeeperServer() throws Exception {
125 final File zookeeperDirectory = folder.newFolder("zookeeperDirectory");
127 zookeeperPort = nextZookeeperPort++;
128 final InetSocketAddress addr = new InetSocketAddress(MessagingUtils.findPort(zookeeperPort));
129 zooKeeperServerServiceProvider = new ZooKeeperServerServiceProvider(zookeeperDirectory, addr);
130 zooKeeperServerServiceProvider.startZookeeperServer();
134 * Stop zookeeper server.
136 private void stopZookeeperServer() {
137 if (zooKeeperServerServiceProvider != null) {
138 zooKeeperServerServiceProvider.stopZookeeperServer();
143 * Test concurrent context jvm local var set.
145 * @throws Exception the exception
148 public void testConcurrentContextJvmLocalVarSet() throws Exception {
149 logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
151 final ContextParameters contextParameters = new ContextParameters();
152 contextParameters.getLockManagerParameters().setPluginClass(JvmLocalLockManager.class.getCanonicalName());
154 final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalVarSet",
155 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
157 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
158 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
160 assertFalse(result.isEmpty());
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());
167 logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
171 * Test concurrent context jvm local no var set.
173 * @throws Exception the exception
176 public void testConcurrentContextJvmLocalNoVarSet() throws Exception {
177 logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
179 new ContextParameters();
180 final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalNoVarSet",
181 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
183 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
184 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
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());
191 logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
195 * Test concurrent context multi jvm no lock.
197 * @throws Exception the exception
200 public void testConcurrentContextMultiJvmNoLock() throws Exception {
201 logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
203 final ContextParameters contextParameters = new ContextParameters();
204 contextParameters.getDistributorParameters().setPluginClass(JvmLocalDistributor.class.getCanonicalName());
205 contextParameters.getLockManagerParameters().setPluginClass(JvmLocalLockManager.class.getCanonicalName());
207 final ConfigrationProvider configrationProvider = getConfigrationProvider("testConcurrentContextMultiJVMNoLock",
208 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
210 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
211 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
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());
218 logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
222 * Test concurrent context hazelcast lock.
224 * @throws Exception the exception
227 public void testConcurrentContextHazelcastLock() throws Exception {
228 logger.debug("Running testConcurrentContextHazelcastLock test . . .");
230 final ContextParameters contextParameters = new ContextParameters();
231 contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
232 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
234 final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastLock",
235 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
237 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
238 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
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());
245 logger.debug("Ran testConcurrentContextHazelcastLock test");
249 * Test concurrent context curator lock.
251 * @throws Exception the exception
254 public void testConcurrentContextCuratorLock() throws Exception {
255 logger.debug("Running testConcurrentContextCuratorLock test . . .");
257 startZookeeperServer();
258 final ContextParameters contextParameters = new ContextParameters();
259 final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
260 distributorParameters.setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
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);
268 final ConfigrationProvider configrationProvider = getConfigrationProvider("CuratorLock",
269 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
271 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
272 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
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");
280 stopZookeeperServer();
285 * Test concurrent context hazelcast multi jvm hazelcast lock.
287 * @throws Exception the exception
290 public void testConcurrentContextHazelcastMultiJvmHazelcastLock() throws Exception {
291 logger.debug("Running testConcurrentContextHazelcastMultiJVMHazelcastLock test . . .");
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());
298 final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastMultiHazelcastlock",
299 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
301 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
302 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
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");
312 * Test concurrent context infinispan multi jvm hazelcastlock.
314 * @throws ApexModelException the apex model exception
315 * @throws IOException Signals that an I/O exception has occurred.
316 * @throws ApexException the apex exception
319 public void testConcurrentContextInfinispanMultiJvmHazelcastlock()
320 throws ApexModelException, IOException, ApexException {
321 logger.debug("Running testConcurrentContextInfinispanMultiJVMHazelcastlock test . . .");
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());
330 final ConfigrationProvider configrationProvider = getConfigrationProvider("InfinispanMultiHazelcastlock",
331 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
333 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
334 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
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");
344 * Test concurrent context infinispan multi jvm curator lock.
346 * @throws Exception the exception
349 public void testConcurrentContextInfinispanMultiJvmCuratorLock() throws Exception {
350 logger.debug("Running testConcurrentContextInfinispanMultiJVMCuratorLock test . . .");
353 startZookeeperServer();
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);
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);
367 final ConfigrationProvider configrationProvider = getConfigrationProvider("InfinispanMultiCuratorLock",
368 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
370 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
371 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
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());
378 stopZookeeperServer();
381 logger.debug("Ran testConcurrentContextInfinispanMultiJVMCuratorLock test");
385 * Test concurrent context hazelcast multi jvm curator lock.
387 * @throws Exception the exception
390 public void testConcurrentContextHazelcastMultiJvmCuratorLock() throws Exception {
391 logger.debug("Running testConcurrentContextHazelcastMultiJVMCuratorLock test . . .");
394 startZookeeperServer();
396 final ContextParameters contextParameters = new ContextParameters();
397 contextParameters.getDistributorParameters()
398 .setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
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);
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();
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());
416 stopZookeeperServer();
418 logger.debug("Ran testConcurrentContextHazelcastMultiJVMCuratorLock test");
422 * Gets the configration provider.
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
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()) {
435 public Map<String, Object> getContextAlbumInitValues() {
436 final Map<String, Object> initValues = super.getContextAlbumInitValues();
437 initValues.put(TEST_VALUE, new TestContextLongItem(0L));