2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.testsuites.integration.context;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.onap.policy.apex.context.parameters.DistributorParameters.DEFAULT_DISTRIBUTOR_PLUGIN_CLASS;
28 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.TEST_VALUE;
31 import java.io.IOException;
32 import java.net.InetSocketAddress;
34 import java.util.SortedSet;
35 import org.junit.AfterClass;
36 import org.junit.BeforeClass;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.TemporaryFolder;
40 import org.onap.policy.apex.context.impl.distribution.jvmlocal.JvmLocalDistributor;
41 import org.onap.policy.apex.context.impl.locking.jvmlocal.JvmLocalLockManager;
42 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
43 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
44 import org.onap.policy.apex.context.parameters.ContextParameters;
45 import org.onap.policy.apex.context.parameters.DistributorParameters;
46 import org.onap.policy.apex.context.parameters.SchemaParameters;
47 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
48 import org.onap.policy.apex.core.infrastructure.messaging.util.MessagingUtils;
49 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
50 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
51 import org.onap.policy.apex.plugins.context.distribution.hazelcast.HazelcastContextDistributor;
52 import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanContextDistributor;
53 import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanDistributorParameters;
54 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManager;
55 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManagerParameters;
56 import org.onap.policy.apex.plugins.context.locking.hazelcast.HazelcastLockManager;
57 import org.onap.policy.apex.testsuites.integration.context.lock.modifier.LockType;
58 import org.onap.policy.apex.testsuites.integration.context.locking.ConcurrentContext;
59 import org.onap.policy.apex.testsuites.integration.context.utils.ConfigrationProvider;
60 import org.onap.policy.apex.testsuites.integration.context.utils.ConfigrationProviderImpl;
61 import org.onap.policy.apex.testsuites.integration.context.utils.Constants;
62 import org.onap.policy.apex.testsuites.integration.context.utils.NetworkUtils;
63 import org.onap.policy.apex.testsuites.integration.context.utils.ZooKeeperServerServiceProvider;
64 import org.onap.policy.common.parameters.ParameterService;
65 import org.onap.policy.common.utils.resources.ResourceUtils;
66 import org.slf4j.ext.XLogger;
67 import org.slf4j.ext.XLoggerFactory;
70 * The Class TestConcurrentContext tests concurrent use of context.
72 * @author Liam Fallon (liam.fallon@ericsson.com)
74 public class TestConcurrentContext {
75 private static final String HAZELCAST_CONFIG = "hazelcast.config";
77 private static final String JAVA_NET_PREFER_IPV4_STACK = "java.net.preferIPv4Stack";
78 private static final String HAZELCAST_XML_FILE = "src/test/resources/hazelcast/hazelcast.xml";
80 // Logger for this class
81 private static final XLogger logger = XLoggerFactory.getXLogger(TestConcurrentContext.class);
84 private static final String ZOOKEEPER_ADDRESS = "127.0.0.1";
85 private static final int ZOOKEEPER_START_PORT = 62181;
86 private static final int TEST_JVM_COUNT_SINGLE_JVM = 1;
87 private static final int TEST_JVM_COUNT_MULTI_JVM = 3;
88 private static final int TEST_THREAD_COUNT_SINGLE_JVM = 64;
89 private static final int TEST_THREAD_COUNT_MULTI_JVM = 20;
90 private static final int TEST_THREAD_LOOPS = 100;
92 // We need to increment the Zookeeper port because sometimes the port is not released at the end
93 // of the test for a few seconds.
94 private static int nextZookeeperPort = ZOOKEEPER_START_PORT;
96 private int zookeeperPort;
98 private static SchemaParameters schemaParameters;
101 public final TemporaryFolder folder = new TemporaryFolder();
103 private ZooKeeperServerServiceProvider zooKeeperServerServiceProvider;
108 * @throws Exception the exception
111 public static void configure() throws Exception {
112 System.setProperty(JAVA_NET_PREFER_IPV4_STACK, "true");
113 final String hazelCastfileLocation = ResourceUtils.getFilePath4Resource(HAZELCAST_XML_FILE);
114 System.setProperty(HAZELCAST_CONFIG, hazelCastfileLocation);
116 final SortedSet<String> ipAddressSet = NetworkUtils.getIPv4NonLoopAddresses();
118 if (ipAddressSet.size() == 0) {
119 throw new Exception("cound not find real IP address for test");
121 logger.info("For Infinispan, setting jgroups.tcp.address to: {}", ipAddressSet.first());
122 System.setProperty("jgroups.tcp.address", ipAddressSet.first());
124 schemaParameters = new SchemaParameters();
126 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
127 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
129 ParameterService.register(schemaParameters, true);
133 * Clear configuration.
136 public static void clear() {
137 ParameterService.deregister(schemaParameters);
141 * Start zookeeper server.
143 * @throws Exception the exception
145 private void startZookeeperServer() throws Exception {
146 final File zookeeperDirectory = folder.newFolder("zookeeperDirectory");
148 zookeeperPort = nextZookeeperPort++;
149 final InetSocketAddress addr = new InetSocketAddress(MessagingUtils.findPort(zookeeperPort));
150 zooKeeperServerServiceProvider = new ZooKeeperServerServiceProvider(zookeeperDirectory, addr);
151 zooKeeperServerServiceProvider.startZookeeperServer();
155 * Stop zookeeper server.
157 private void stopZookeeperServer() {
158 if (zooKeeperServerServiceProvider != null) {
159 zooKeeperServerServiceProvider.stopZookeeperServer();
164 * Test concurrent context jvm local var set.
166 * @throws Exception the exception
169 public void testConcurrentContextJvmLocalVarSet() throws Exception {
170 logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
172 final ContextParameters contextParameters = new ContextParameters();
173 contextParameters.getLockManagerParameters().setPluginClass(JvmLocalLockManager.class.getName());
174 setContextParmetersInParameterService(contextParameters);
176 final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalVarSet",
177 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
179 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
180 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
182 assertFalse(result.isEmpty());
184 final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
185 final TestContextLongItem actual = result.get(TEST_VALUE);
186 assertNotNull(actual);
187 assertEquals(expected, actual.getLongValue());
189 clearContextParmetersInParameterService(contextParameters);
191 logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
195 * Test concurrent context jvm local no var set.
197 * @throws Exception the exception
200 public void testConcurrentContextJvmLocalNoVarSet() throws Exception {
201 logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
203 final ContextParameters contextParameters = new ContextParameters();
204 setContextParmetersInParameterService(contextParameters);
206 final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalNoVarSet",
207 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
209 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
210 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
212 final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
213 final TestContextLongItem actual = result.get(Constants.TEST_VALUE);
214 assertNotNull(actual);
215 assertEquals(expected, actual.getLongValue());
217 clearContextParmetersInParameterService(contextParameters);
218 logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
222 * Test concurrent context multi jvm no lock.
224 * @throws Exception the exception
227 public void testConcurrentContextMultiJvmNoLock() throws Exception {
228 logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
230 final ContextParameters contextParameters = new ContextParameters();
231 contextParameters.getDistributorParameters().setPluginClass(JvmLocalDistributor.class.getName());
232 contextParameters.getLockManagerParameters().setPluginClass(JvmLocalLockManager.class.getName());
233 setContextParmetersInParameterService(contextParameters);
235 final ConfigrationProvider configrationProvider = getConfigrationProvider("testConcurrentContextMultiJVMNoLock",
236 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
238 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
239 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
241 // No concurrent map so result will be zero
242 final TestContextLongItem actual = result.get(TEST_VALUE);
243 assertNotNull(actual);
244 assertEquals(0, actual.getLongValue());
246 clearContextParmetersInParameterService(contextParameters);
247 logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
251 * Test concurrent context hazelcast lock.
253 * @throws Exception the exception
256 public void testConcurrentContextHazelcastLock() throws Exception {
257 logger.debug("Running testConcurrentContextHazelcastLock test . . .");
259 final ContextParameters contextParameters = new ContextParameters();
260 contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
261 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getName());
262 setContextParmetersInParameterService(contextParameters);
264 final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastLock",
265 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
267 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
268 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
270 final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
271 final TestContextLongItem actual = result.get(TEST_VALUE);
272 assertNotNull(actual);
273 assertEquals(expected, actual.getLongValue());
275 clearContextParmetersInParameterService(contextParameters);
276 logger.debug("Ran testConcurrentContextHazelcastLock test");
280 * Test concurrent context curator lock.
282 * @throws Exception the exception
285 public void testConcurrentContextCuratorLock() throws Exception {
286 logger.debug("Running testConcurrentContextCuratorLock test . . .");
287 final ContextParameters contextParameters = new ContextParameters();
289 startZookeeperServer();
290 final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
291 distributorParameters.setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
293 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
294 curatorParameters.setPluginClass(CuratorLockManager.class.getName());
295 curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
296 contextParameters.setLockManagerParameters(curatorParameters);
297 setContextParmetersInParameterService(contextParameters);
299 final ConfigrationProvider configrationProvider = getConfigrationProvider("CuratorLock",
300 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
302 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
303 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
305 final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
306 final TestContextLongItem actual = result.get(TEST_VALUE);
307 assertNotNull(actual);
308 assertEquals(expected, actual.getLongValue());
309 logger.debug("Ran testConcurrentContextCuratorLock test");
311 stopZookeeperServer();
312 clearContextParmetersInParameterService(contextParameters);
317 * Test concurrent context hazelcast multi jvm hazelcast lock.
319 * @throws Exception the exception
322 public void testConcurrentContextHazelcastMultiJvmHazelcastLock() throws Exception {
323 logger.debug("Running testConcurrentContextHazelcastMultiJVMHazelcastLock test . . .");
325 final ContextParameters contextParameters = new ContextParameters();
326 final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
327 distributorParameters.setPluginClass(HazelcastContextDistributor.class.getName());
328 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getName());
329 setContextParmetersInParameterService(contextParameters);
331 final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastMultiHazelcastlock",
332 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
334 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
335 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
337 final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
338 final TestContextLongItem actual = result.get(TEST_VALUE);
339 assertNotNull(actual);
340 assertEquals(expected, actual.getLongValue());
342 clearContextParmetersInParameterService(contextParameters);
343 logger.debug("Ran testConcurrentContextHazelcastMultiJVMHazelcastLock test");
347 * Test concurrent context infinispan multi jvm hazelcastlock.
349 * @throws ApexModelException the apex model exception
350 * @throws IOException Signals that an I/O exception has occurred.
351 * @throws ApexException the apex exception
354 public void testConcurrentContextInfinispanMultiJvmHazelcastlock()
355 throws ApexModelException, IOException, ApexException {
356 logger.debug("Running testConcurrentContextInfinispanMultiJVMHazelcastlock test . . .");
358 final ContextParameters contextParameters = new ContextParameters();
359 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
360 infinispanParameters.setPluginClass(InfinispanContextDistributor.class.getName());
361 infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
362 contextParameters.setDistributorParameters(infinispanParameters);
363 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getName());
364 setContextParmetersInParameterService(contextParameters);
366 final ConfigrationProvider configrationProvider = getConfigrationProvider("InfinispanMultiHazelcastlock",
367 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
369 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
370 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
372 final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
373 final TestContextLongItem actual = result.get(TEST_VALUE);
374 assertNotNull(actual);
375 assertEquals(expected, actual.getLongValue());
377 clearContextParmetersInParameterService(contextParameters);
378 logger.debug("Ran testConcurrentContextInfinispanMultiJVMHazelcastlock test");
382 * Test concurrent context infinispan multi jvm curator lock.
384 * @throws Exception the exception
387 public void testConcurrentContextInfinispanMultiJvmCuratorLock() throws Exception {
388 logger.debug("Running testConcurrentContextInfinispanMultiJVMCuratorLock test . . .");
390 final ContextParameters contextParameters = new ContextParameters();
392 startZookeeperServer();
394 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
395 infinispanParameters.setPluginClass(InfinispanContextDistributor.class.getName());
396 infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
397 contextParameters.setDistributorParameters(infinispanParameters);
399 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
400 curatorParameters.setPluginClass(CuratorLockManager.class.getName());
401 curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
402 contextParameters.setLockManagerParameters(curatorParameters);
403 setContextParmetersInParameterService(contextParameters);
405 final ConfigrationProvider configrationProvider = getConfigrationProvider("InfinispanMultiCuratorLock",
406 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
408 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
409 final Map<String, TestContextLongItem> result = concurrentContext.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();
417 clearContextParmetersInParameterService(contextParameters);
420 logger.debug("Ran testConcurrentContextInfinispanMultiJVMCuratorLock test");
424 * Test concurrent context hazelcast multi jvm curator lock.
426 * @throws Exception the exception
429 public void testConcurrentContextHazelcastMultiJvmCuratorLock() throws Exception {
430 logger.debug("Running testConcurrentContextHazelcastMultiJVMCuratorLock test . . .");
432 final ContextParameters contextParameters = new ContextParameters();
434 startZookeeperServer();
436 contextParameters.getDistributorParameters()
437 .setPluginClass(HazelcastContextDistributor.class.getName());
439 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
440 curatorParameters.setPluginClass(CuratorLockManager.class.getName());
441 curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
442 contextParameters.setLockManagerParameters(curatorParameters);
443 setContextParmetersInParameterService(contextParameters);
445 final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastMultiCuratorLock",
446 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
447 final Map<String, TestContextLongItem> result =
448 new ConcurrentContext(configrationProvider).testConcurrentContext();
450 final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
451 final TestContextLongItem actual = result.get(TEST_VALUE);
452 assertNotNull(actual);
453 assertEquals(expected, actual.getLongValue());
455 stopZookeeperServer();
456 clearContextParmetersInParameterService(contextParameters);
458 logger.debug("Ran testConcurrentContextHazelcastMultiJVMCuratorLock test");
462 * Gets the configration provider.
464 * @param testType the test type
465 * @param jvmCount the jvm count
466 * @param threadCount the thread count
467 * @param threadLoops the thread loops
468 * @return the configration provider
470 ConfigrationProvider getConfigrationProvider(final String testType, final int jvmCount, final int threadCount,
471 final int threadLoops) {
472 return new ConfigrationProviderImpl(testType, jvmCount, threadCount, threadLoops, 16,
473 LockType.WRITE_LOCK_SINGLE_VALUE_UPDATE.getValue()) {
475 public Map<String, Object> getContextAlbumInitValues() {
476 final Map<String, Object> initValues = super.getContextAlbumInitValues();
477 initValues.put(TEST_VALUE, new TestContextLongItem(0L));
485 * Set the context parameters in the parameter service.
487 * @param contextParameters The parameters to set.
489 private void setContextParmetersInParameterService(final ContextParameters contextParameters) {
490 ParameterService.register(contextParameters);
491 ParameterService.register(contextParameters.getDistributorParameters());
492 ParameterService.register(contextParameters.getLockManagerParameters());
493 ParameterService.register(contextParameters.getPersistorParameters());
497 * Clear the context parameters in the parameter service.
499 * @param contextParameters The parameters to set.
501 private void clearContextParmetersInParameterService(final ContextParameters contextParameters) {
502 ParameterService.deregister(contextParameters.getPersistorParameters());
503 ParameterService.deregister(contextParameters.getLockManagerParameters());
504 ParameterService.deregister(contextParameters.getDistributorParameters());
505 ParameterService.deregister(contextParameters);