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;
36 import org.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.TemporaryFolder;
41 import org.onap.policy.apex.context.impl.distribution.jvmlocal.JvmLocalDistributor;
42 import org.onap.policy.apex.context.impl.locking.jvmlocal.JvmLocalLockManager;
43 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
44 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
45 import org.onap.policy.apex.context.parameters.ContextParameters;
46 import org.onap.policy.apex.context.parameters.DistributorParameters;
47 import org.onap.policy.apex.context.parameters.SchemaParameters;
48 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
49 import org.onap.policy.apex.core.infrastructure.messaging.util.MessagingUtils;
50 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
51 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
52 import org.onap.policy.apex.plugins.context.distribution.hazelcast.HazelcastContextDistributor;
53 import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanContextDistributor;
54 import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanDistributorParameters;
55 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManager;
56 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManagerParameters;
57 import org.onap.policy.apex.plugins.context.locking.hazelcast.HazelcastLockManager;
58 import org.onap.policy.apex.testsuites.integration.context.lock.modifier.LockType;
59 import org.onap.policy.apex.testsuites.integration.context.locking.ConcurrentContext;
60 import org.onap.policy.apex.testsuites.integration.context.utils.ConfigrationProvider;
61 import org.onap.policy.apex.testsuites.integration.context.utils.ConfigrationProviderImpl;
62 import org.onap.policy.apex.testsuites.integration.context.utils.Constants;
63 import org.onap.policy.apex.testsuites.integration.context.utils.NetworkUtils;
64 import org.onap.policy.apex.testsuites.integration.context.utils.ZooKeeperServerServiceProvider;
65 import org.onap.policy.common.parameters.ParameterService;
66 import org.onap.policy.common.utils.resources.ResourceUtils;
67 import org.slf4j.ext.XLogger;
68 import org.slf4j.ext.XLoggerFactory;
71 * The Class TestConcurrentContext tests concurrent use of context.
73 * @author Liam Fallon (liam.fallon@ericsson.com)
75 public class TestConcurrentContext {
76 private static final String HAZELCAST_CONFIG = "hazelcast.config";
78 private static final String JAVA_NET_PREFER_IPV4_STACK = "java.net.preferIPv4Stack";
79 private static final String HAZELCAST_XML_FILE = "src/test/resources/hazelcast/hazelcast.xml";
81 // Logger for this class
82 private static final XLogger logger = XLoggerFactory.getXLogger(TestConcurrentContext.class);
85 private static final String ZOOKEEPER_ADDRESS = "127.0.0.1";
86 private static final int ZOOKEEPER_START_PORT = 62181;
87 private static final int TEST_JVM_COUNT_SINGLE_JVM = 1;
88 private static final int TEST_JVM_COUNT_MULTI_JVM = 3;
89 private static final int TEST_THREAD_COUNT_SINGLE_JVM = 64;
90 private static final int TEST_THREAD_COUNT_MULTI_JVM = 20;
91 private static final int TEST_THREAD_LOOPS = 100;
93 // We need to increment the Zookeeper port because sometimes the port is not released at the end
94 // of the test for a few seconds.
95 private static int nextZookeeperPort = ZOOKEEPER_START_PORT;
97 private int zookeeperPort;
99 private static SchemaParameters schemaParameters;
102 public final TemporaryFolder folder = new TemporaryFolder();
104 private ZooKeeperServerServiceProvider zooKeeperServerServiceProvider;
109 * @throws Exception the exception
112 public static void configure() throws Exception {
113 System.setProperty(JAVA_NET_PREFER_IPV4_STACK, "true");
114 final String hazelCastfileLocation = ResourceUtils.getFilePath4Resource(HAZELCAST_XML_FILE);
115 System.setProperty(HAZELCAST_CONFIG, hazelCastfileLocation);
117 final SortedSet<String> ipAddressSet = NetworkUtils.getIPv4NonLoopAddresses();
119 if (ipAddressSet.size() == 0) {
120 throw new Exception("cound not find real IP address for test");
122 logger.info("For Infinispan, setting jgroups.tcp.address to: {}", ipAddressSet.first());
123 System.setProperty("jgroups.tcp.address", ipAddressSet.first());
125 schemaParameters = new SchemaParameters();
127 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
128 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
130 ParameterService.register(schemaParameters, true);
134 * Clear configuration.
137 public static void clear() {
138 ParameterService.deregister(schemaParameters);
142 * Start zookeeper server.
144 * @throws Exception the exception
146 private void startZookeeperServer() throws Exception {
147 final File zookeeperDirectory = folder.newFolder("zookeeperDirectory");
149 zookeeperPort = nextZookeeperPort++;
150 final InetSocketAddress addr = new InetSocketAddress(MessagingUtils.findPort(zookeeperPort));
151 zooKeeperServerServiceProvider = new ZooKeeperServerServiceProvider(zookeeperDirectory, addr);
152 zooKeeperServerServiceProvider.startZookeeperServer();
156 * Stop zookeeper server.
158 private void stopZookeeperServer() {
159 if (zooKeeperServerServiceProvider != null) {
160 zooKeeperServerServiceProvider.stopZookeeperServer();
165 * Test concurrent context jvm local var set.
167 * @throws Exception the exception
170 public void testConcurrentContextJvmLocalVarSet() throws Exception {
171 logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
173 final ContextParameters contextParameters = new ContextParameters();
174 contextParameters.getLockManagerParameters().setPluginClass(JvmLocalLockManager.class.getName());
175 setContextParmetersInParameterService(contextParameters);
177 final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalVarSet",
178 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
180 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
181 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
183 assertFalse(result.isEmpty());
185 final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
186 final TestContextLongItem actual = result.get(TEST_VALUE);
187 assertNotNull(actual);
188 assertEquals(expected, actual.getLongValue());
190 clearContextParmetersInParameterService(contextParameters);
192 logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
196 * Test concurrent context jvm local no var set.
198 * @throws Exception the exception
201 public void testConcurrentContextJvmLocalNoVarSet() throws Exception {
202 logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
204 final ContextParameters contextParameters = new ContextParameters();
205 setContextParmetersInParameterService(contextParameters);
207 final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalNoVarSet",
208 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
210 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
211 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
213 final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
214 final TestContextLongItem actual = result.get(Constants.TEST_VALUE);
215 assertNotNull(actual);
216 assertEquals(expected, actual.getLongValue());
218 clearContextParmetersInParameterService(contextParameters);
219 logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
223 * Test concurrent context multi jvm no lock.
225 * @throws Exception the exception
228 public void testConcurrentContextMultiJvmNoLock() throws Exception {
229 logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
231 final ContextParameters contextParameters = new ContextParameters();
232 contextParameters.getDistributorParameters().setPluginClass(JvmLocalDistributor.class.getName());
233 contextParameters.getLockManagerParameters().setPluginClass(JvmLocalLockManager.class.getName());
234 setContextParmetersInParameterService(contextParameters);
236 final ConfigrationProvider configrationProvider = getConfigrationProvider("testConcurrentContextMultiJVMNoLock",
237 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
239 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
240 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
242 // No concurrent map so result will be zero
243 final TestContextLongItem actual = result.get(TEST_VALUE);
244 assertNotNull(actual);
245 assertEquals(0, actual.getLongValue());
247 clearContextParmetersInParameterService(contextParameters);
248 logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
252 * Test concurrent context hazelcast lock.
254 * @throws Exception the exception
257 public void testConcurrentContextHazelcastLock() throws Exception {
258 logger.debug("Running testConcurrentContextHazelcastLock test . . .");
260 final ContextParameters contextParameters = new ContextParameters();
261 contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
262 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getName());
263 setContextParmetersInParameterService(contextParameters);
265 final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastLock",
266 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
268 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
269 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
271 final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
272 final TestContextLongItem actual = result.get(TEST_VALUE);
273 assertNotNull(actual);
274 assertEquals(expected, actual.getLongValue());
276 clearContextParmetersInParameterService(contextParameters);
277 logger.debug("Ran testConcurrentContextHazelcastLock test");
281 * Test concurrent context curator lock.
283 * @throws Exception the exception
286 public void testConcurrentContextCuratorLock() throws Exception {
287 logger.debug("Running testConcurrentContextCuratorLock test . . .");
288 final ContextParameters contextParameters = new ContextParameters();
290 startZookeeperServer();
291 final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
292 distributorParameters.setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
294 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
295 curatorParameters.setPluginClass(CuratorLockManager.class.getName());
296 curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
297 contextParameters.setLockManagerParameters(curatorParameters);
298 setContextParmetersInParameterService(contextParameters);
300 final ConfigrationProvider configrationProvider = getConfigrationProvider("CuratorLock",
301 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
303 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
304 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
306 final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
307 final TestContextLongItem actual = result.get(TEST_VALUE);
308 assertNotNull(actual);
309 assertEquals(expected, actual.getLongValue());
310 logger.debug("Ran testConcurrentContextCuratorLock test");
312 stopZookeeperServer();
313 clearContextParmetersInParameterService(contextParameters);
318 * Test concurrent context hazelcast multi jvm hazelcast lock.
320 * @throws Exception the exception
323 public void testConcurrentContextHazelcastMultiJvmHazelcastLock() throws Exception {
324 logger.debug("Running testConcurrentContextHazelcastMultiJVMHazelcastLock test . . .");
326 final ContextParameters contextParameters = new ContextParameters();
327 final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
328 distributorParameters.setPluginClass(HazelcastContextDistributor.class.getName());
329 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getName());
330 setContextParmetersInParameterService(contextParameters);
332 final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastMultiHazelcastlock",
333 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
335 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
336 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
338 final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
339 final TestContextLongItem actual = result.get(TEST_VALUE);
340 assertNotNull(actual);
341 assertEquals(expected, actual.getLongValue());
343 clearContextParmetersInParameterService(contextParameters);
344 logger.debug("Ran testConcurrentContextHazelcastMultiJVMHazelcastLock test");
348 * Test concurrent context infinispan multi jvm hazelcastlock.
350 * @throws ApexModelException the apex model exception
351 * @throws IOException Signals that an I/O exception has occurred.
352 * @throws ApexException the apex exception
355 public void testConcurrentContextInfinispanMultiJvmHazelcastlock()
356 throws ApexModelException, IOException, ApexException {
357 logger.debug("Running testConcurrentContextInfinispanMultiJVMHazelcastlock test . . .");
359 final ContextParameters contextParameters = new ContextParameters();
360 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
361 infinispanParameters.setPluginClass(InfinispanContextDistributor.class.getName());
362 infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
363 contextParameters.setDistributorParameters(infinispanParameters);
364 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getName());
365 setContextParmetersInParameterService(contextParameters);
367 final ConfigrationProvider configrationProvider = getConfigrationProvider("InfinispanMultiHazelcastlock",
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 clearContextParmetersInParameterService(contextParameters);
379 logger.debug("Ran testConcurrentContextInfinispanMultiJVMHazelcastlock test");
383 * Test concurrent context infinispan multi jvm curator lock.
385 * @throws Exception the exception
388 public void testConcurrentContextInfinispanMultiJvmCuratorLock() throws Exception {
389 logger.debug("Running testConcurrentContextInfinispanMultiJVMCuratorLock test . . .");
391 final ContextParameters contextParameters = new ContextParameters();
393 startZookeeperServer();
395 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
396 infinispanParameters.setPluginClass(InfinispanContextDistributor.class.getName());
397 infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
398 contextParameters.setDistributorParameters(infinispanParameters);
400 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
401 curatorParameters.setPluginClass(CuratorLockManager.class.getName());
402 curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
403 contextParameters.setLockManagerParameters(curatorParameters);
404 setContextParmetersInParameterService(contextParameters);
406 final ConfigrationProvider configrationProvider = getConfigrationProvider("InfinispanMultiCuratorLock",
407 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
409 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
410 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
412 final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
413 final TestContextLongItem actual = result.get(TEST_VALUE);
414 assertNotNull(actual);
415 assertEquals(expected, actual.getLongValue());
417 stopZookeeperServer();
418 clearContextParmetersInParameterService(contextParameters);
421 logger.debug("Ran testConcurrentContextInfinispanMultiJVMCuratorLock test");
425 * Test concurrent context hazelcast multi jvm curator lock.
427 * @throws Exception the exception
430 public void testConcurrentContextHazelcastMultiJvmCuratorLock() throws Exception {
431 logger.debug("Running testConcurrentContextHazelcastMultiJVMCuratorLock test . . .");
433 final ContextParameters contextParameters = new ContextParameters();
435 startZookeeperServer();
437 contextParameters.getDistributorParameters()
438 .setPluginClass(HazelcastContextDistributor.class.getName());
440 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
441 curatorParameters.setPluginClass(CuratorLockManager.class.getName());
442 curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
443 contextParameters.setLockManagerParameters(curatorParameters);
444 setContextParmetersInParameterService(contextParameters);
446 final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastMultiCuratorLock",
447 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
448 final Map<String, TestContextLongItem> result =
449 new ConcurrentContext(configrationProvider).testConcurrentContext();
451 final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
452 final TestContextLongItem actual = result.get(TEST_VALUE);
453 assertNotNull(actual);
454 assertEquals(expected, actual.getLongValue());
456 stopZookeeperServer();
457 clearContextParmetersInParameterService(contextParameters);
459 logger.debug("Ran testConcurrentContextHazelcastMultiJVMCuratorLock test");
463 * Gets the configration provider.
465 * @param testType the test type
466 * @param jvmCount the jvm count
467 * @param threadCount the thread count
468 * @param threadLoops the thread loops
469 * @return the configration provider
471 ConfigrationProvider getConfigrationProvider(final String testType, final int jvmCount, final int threadCount,
472 final int threadLoops) {
473 return new ConfigrationProviderImpl(testType, jvmCount, threadCount, threadLoops, 16,
474 LockType.WRITE_LOCK_SINGLE_VALUE_UPDATE.getValue()) {
476 public Map<String, Object> getContextAlbumInitValues() {
477 final Map<String, Object> initValues = super.getContextAlbumInitValues();
478 initValues.put(TEST_VALUE, new TestContextLongItem(0L));
486 * Set the context parameters in the parameter service.
488 * @param contextParameters The parameters to set.
490 private void setContextParmetersInParameterService(final ContextParameters contextParameters) {
491 ParameterService.register(contextParameters);
492 ParameterService.register(contextParameters.getDistributorParameters());
493 ParameterService.register(contextParameters.getLockManagerParameters());
494 ParameterService.register(contextParameters.getPersistorParameters());
498 * Clear the context parameters in the parameter service.
500 * @param contextParameters The parameters to set.
502 private void clearContextParmetersInParameterService(final ContextParameters contextParameters) {
503 ParameterService.deregister(contextParameters.getPersistorParameters());
504 ParameterService.deregister(contextParameters.getLockManagerParameters());
505 ParameterService.deregister(contextParameters.getDistributorParameters());
506 ParameterService.deregister(contextParameters);