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.model.basicmodel.concepts.ApexException;
49 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
50 import org.onap.policy.apex.plugins.context.distribution.hazelcast.HazelcastContextDistributor;
51 import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanContextDistributor;
52 import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanDistributorParameters;
53 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManager;
54 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManagerParameters;
55 import org.onap.policy.apex.plugins.context.locking.hazelcast.HazelcastLockManager;
56 import org.onap.policy.apex.testsuites.integration.context.lock.modifier.LockType;
57 import org.onap.policy.apex.testsuites.integration.context.locking.ConcurrentContext;
58 import org.onap.policy.apex.testsuites.integration.context.utils.ConfigrationProvider;
59 import org.onap.policy.apex.testsuites.integration.context.utils.ConfigrationProviderImpl;
60 import org.onap.policy.apex.testsuites.integration.context.utils.Constants;
61 import org.onap.policy.apex.testsuites.integration.context.utils.NetworkUtils;
62 import org.onap.policy.apex.testsuites.integration.context.utils.ZooKeeperServerServiceProvider;
63 import org.onap.policy.common.parameters.ParameterService;
64 import org.onap.policy.common.utils.resources.ResourceUtils;
65 import org.slf4j.ext.XLogger;
66 import org.slf4j.ext.XLoggerFactory;
69 * The Class TestConcurrentContext tests concurrent use of context.
71 * @author Liam Fallon (liam.fallon@ericsson.com)
73 public class TestConcurrentContext {
74 private static final String HAZELCAST_CONFIG = "hazelcast.config";
76 private static final String JAVA_NET_PREFER_IPV4_STACK = "java.net.preferIPv4Stack";
77 private static final String HAZELCAST_XML_FILE = "src/test/resources/hazelcast/hazelcast.xml";
79 // Logger for this class
80 private static final XLogger logger = XLoggerFactory.getXLogger(TestConcurrentContext.class);
83 private static final String ZOOKEEPER_ADDRESS = "127.0.0.1";
84 private static final int ZOOKEEPER_START_PORT = 62181;
85 private static final int TEST_JVM_COUNT_SINGLE_JVM = 1;
86 private static final int TEST_JVM_COUNT_MULTI_JVM = 3;
87 private static final int TEST_THREAD_COUNT_SINGLE_JVM = 64;
88 private static final int TEST_THREAD_COUNT_MULTI_JVM = 20;
89 private static final int TEST_THREAD_LOOPS = 100;
91 // We need to increment the Zookeeper port because sometimes the port is not released at the end
92 // of the test for a few seconds.
93 private static int nextZookeeperPort = ZOOKEEPER_START_PORT;
95 private int zookeeperPort;
97 private static SchemaParameters schemaParameters;
100 public final TemporaryFolder folder = new TemporaryFolder();
102 private ZooKeeperServerServiceProvider zooKeeperServerServiceProvider;
107 * @throws Exception the exception
110 public static void configure() throws Exception {
111 System.setProperty(JAVA_NET_PREFER_IPV4_STACK, "true");
112 final String hazelCastfileLocation = ResourceUtils.getFilePath4Resource(HAZELCAST_XML_FILE);
113 System.setProperty(HAZELCAST_CONFIG, hazelCastfileLocation);
115 final SortedSet<String> ipAddressSet = NetworkUtils.getIPv4NonLoopAddresses();
117 if (ipAddressSet.size() == 0) {
118 throw new Exception("cound not find real IP address for test");
120 logger.info("For Infinispan, setting jgroups.tcp.address to: {}", ipAddressSet.first());
121 System.setProperty("jgroups.tcp.address", ipAddressSet.first());
123 schemaParameters = new SchemaParameters();
125 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
126 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
128 ParameterService.register(schemaParameters, true);
132 * Clear configuration.
135 public static void clear() {
136 ParameterService.deregister(schemaParameters);
140 * Start zookeeper server.
142 * @throws Exception the exception
144 private void startZookeeperServer() throws Exception {
145 final File zookeeperDirectory = folder.newFolder("zookeeperDirectory");
147 zookeeperPort = nextZookeeperPort++;
148 final InetSocketAddress addr = new InetSocketAddress(zookeeperPort);
149 zooKeeperServerServiceProvider = new ZooKeeperServerServiceProvider(zookeeperDirectory, addr);
150 zooKeeperServerServiceProvider.startZookeeperServer();
154 * Stop zookeeper server.
156 private void stopZookeeperServer() {
157 if (zooKeeperServerServiceProvider != null) {
158 zooKeeperServerServiceProvider.stopZookeeperServer();
163 * Test concurrent context jvm local var set.
165 * @throws Exception the exception
168 public void testConcurrentContextJvmLocalVarSet() throws Exception {
169 logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
171 final ContextParameters contextParameters = new ContextParameters();
172 contextParameters.getLockManagerParameters().setPluginClass(JvmLocalLockManager.class.getName());
173 setContextParmetersInParameterService(contextParameters);
175 final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalVarSet",
176 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
178 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
179 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
181 assertFalse(result.isEmpty());
183 final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
184 final TestContextLongItem actual = result.get(TEST_VALUE);
185 assertNotNull(actual);
186 assertEquals(expected, actual.getLongValue());
188 clearContextParmetersInParameterService(contextParameters);
190 logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
194 * Test concurrent context jvm local no var set.
196 * @throws Exception the exception
199 public void testConcurrentContextJvmLocalNoVarSet() throws Exception {
200 logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
202 final ContextParameters contextParameters = new ContextParameters();
203 setContextParmetersInParameterService(contextParameters);
205 final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalNoVarSet",
206 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
208 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
209 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
211 final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
212 final TestContextLongItem actual = result.get(Constants.TEST_VALUE);
213 assertNotNull(actual);
214 assertEquals(expected, actual.getLongValue());
216 clearContextParmetersInParameterService(contextParameters);
217 logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
221 * Test concurrent context multi jvm no lock.
223 * @throws Exception the exception
226 public void testConcurrentContextMultiJvmNoLock() throws Exception {
227 logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
229 final ContextParameters contextParameters = new ContextParameters();
230 contextParameters.getDistributorParameters().setPluginClass(JvmLocalDistributor.class.getName());
231 contextParameters.getLockManagerParameters().setPluginClass(JvmLocalLockManager.class.getName());
232 setContextParmetersInParameterService(contextParameters);
234 final ConfigrationProvider configrationProvider = getConfigrationProvider("testConcurrentContextMultiJVMNoLock",
235 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
237 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
238 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
240 // No concurrent map so result will be zero
241 final TestContextLongItem actual = result.get(TEST_VALUE);
242 assertNotNull(actual);
243 assertEquals(0, actual.getLongValue());
245 clearContextParmetersInParameterService(contextParameters);
246 logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
250 * Test concurrent context hazelcast lock.
252 * @throws Exception the exception
255 public void testConcurrentContextHazelcastLock() throws Exception {
256 logger.debug("Running testConcurrentContextHazelcastLock test . . .");
258 final ContextParameters contextParameters = new ContextParameters();
259 contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
260 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getName());
261 setContextParmetersInParameterService(contextParameters);
263 final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastLock",
264 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
266 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
267 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
269 final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
270 final TestContextLongItem actual = result.get(TEST_VALUE);
271 assertNotNull(actual);
272 assertEquals(expected, actual.getLongValue());
274 clearContextParmetersInParameterService(contextParameters);
275 logger.debug("Ran testConcurrentContextHazelcastLock test");
279 * Test concurrent context curator lock.
281 * @throws Exception the exception
284 public void testConcurrentContextCuratorLock() throws Exception {
285 logger.debug("Running testConcurrentContextCuratorLock test . . .");
286 final ContextParameters contextParameters = new ContextParameters();
288 startZookeeperServer();
289 final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
290 distributorParameters.setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
292 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
293 curatorParameters.setPluginClass(CuratorLockManager.class.getName());
294 curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
295 contextParameters.setLockManagerParameters(curatorParameters);
296 setContextParmetersInParameterService(contextParameters);
298 final ConfigrationProvider configrationProvider = getConfigrationProvider("CuratorLock",
299 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_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_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
305 final TestContextLongItem actual = result.get(TEST_VALUE);
306 assertNotNull(actual);
307 assertEquals(expected, actual.getLongValue());
308 logger.debug("Ran testConcurrentContextCuratorLock test");
310 stopZookeeperServer();
311 clearContextParmetersInParameterService(contextParameters);
316 * Test concurrent context hazelcast multi jvm hazelcast lock.
318 * @throws Exception the exception
321 public void testConcurrentContextHazelcastMultiJvmHazelcastLock() throws Exception {
322 logger.debug("Running testConcurrentContextHazelcastMultiJVMHazelcastLock test . . .");
324 final ContextParameters contextParameters = new ContextParameters();
325 final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
326 distributorParameters.setPluginClass(HazelcastContextDistributor.class.getName());
327 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getName());
328 setContextParmetersInParameterService(contextParameters);
330 final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastMultiHazelcastlock",
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());
341 clearContextParmetersInParameterService(contextParameters);
342 logger.debug("Ran testConcurrentContextHazelcastMultiJVMHazelcastLock test");
346 * Test concurrent context infinispan multi jvm hazelcastlock.
348 * @throws ApexModelException the apex model exception
349 * @throws IOException Signals that an I/O exception has occurred.
350 * @throws ApexException the apex exception
353 public void testConcurrentContextInfinispanMultiJvmHazelcastlock()
354 throws ApexModelException, IOException, ApexException {
355 logger.debug("Running testConcurrentContextInfinispanMultiJVMHazelcastlock test . . .");
357 final ContextParameters contextParameters = new ContextParameters();
358 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
359 infinispanParameters.setPluginClass(InfinispanContextDistributor.class.getName());
360 infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
361 contextParameters.setDistributorParameters(infinispanParameters);
362 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getName());
363 setContextParmetersInParameterService(contextParameters);
365 final ConfigrationProvider configrationProvider = getConfigrationProvider("InfinispanMultiHazelcastlock",
366 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
368 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
369 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
371 final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
372 final TestContextLongItem actual = result.get(TEST_VALUE);
373 assertNotNull(actual);
374 assertEquals(expected, actual.getLongValue());
376 clearContextParmetersInParameterService(contextParameters);
377 logger.debug("Ran testConcurrentContextInfinispanMultiJVMHazelcastlock test");
381 * Test concurrent context infinispan multi jvm curator lock.
383 * @throws Exception the exception
386 public void testConcurrentContextInfinispanMultiJvmCuratorLock() throws Exception {
387 logger.debug("Running testConcurrentContextInfinispanMultiJVMCuratorLock test . . .");
389 final ContextParameters contextParameters = new ContextParameters();
391 startZookeeperServer();
393 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
394 infinispanParameters.setPluginClass(InfinispanContextDistributor.class.getName());
395 infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
396 contextParameters.setDistributorParameters(infinispanParameters);
398 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
399 curatorParameters.setPluginClass(CuratorLockManager.class.getName());
400 curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
401 contextParameters.setLockManagerParameters(curatorParameters);
402 setContextParmetersInParameterService(contextParameters);
404 final ConfigrationProvider configrationProvider = getConfigrationProvider("InfinispanMultiCuratorLock",
405 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
407 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
408 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
410 final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
411 final TestContextLongItem actual = result.get(TEST_VALUE);
412 assertNotNull(actual);
413 assertEquals(expected, actual.getLongValue());
415 stopZookeeperServer();
416 clearContextParmetersInParameterService(contextParameters);
419 logger.debug("Ran testConcurrentContextInfinispanMultiJVMCuratorLock test");
423 * Test concurrent context hazelcast multi jvm curator lock.
425 * @throws Exception the exception
428 public void testConcurrentContextHazelcastMultiJvmCuratorLock() throws Exception {
429 logger.debug("Running testConcurrentContextHazelcastMultiJVMCuratorLock test . . .");
431 final ContextParameters contextParameters = new ContextParameters();
433 startZookeeperServer();
435 contextParameters.getDistributorParameters()
436 .setPluginClass(HazelcastContextDistributor.class.getName());
438 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
439 curatorParameters.setPluginClass(CuratorLockManager.class.getName());
440 curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
441 contextParameters.setLockManagerParameters(curatorParameters);
442 setContextParmetersInParameterService(contextParameters);
444 final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastMultiCuratorLock",
445 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
446 final Map<String, TestContextLongItem> result =
447 new ConcurrentContext(configrationProvider).testConcurrentContext();
449 final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
450 final TestContextLongItem actual = result.get(TEST_VALUE);
451 assertNotNull(actual);
452 assertEquals(expected, actual.getLongValue());
454 stopZookeeperServer();
455 clearContextParmetersInParameterService(contextParameters);
457 logger.debug("Ran testConcurrentContextHazelcastMultiJVMCuratorLock test");
461 * Gets the configration provider.
463 * @param testType the test type
464 * @param jvmCount the jvm count
465 * @param threadCount the thread count
466 * @param threadLoops the thread loops
467 * @return the configration provider
469 ConfigrationProvider getConfigrationProvider(final String testType, final int jvmCount, final int threadCount,
470 final int threadLoops) {
471 return new ConfigrationProviderImpl(testType, jvmCount, threadCount, threadLoops, 16,
472 LockType.WRITE_LOCK_SINGLE_VALUE_UPDATE.getValue()) {
474 public Map<String, Object> getContextAlbumInitValues() {
475 final Map<String, Object> initValues = super.getContextAlbumInitValues();
476 initValues.put(TEST_VALUE, new TestContextLongItem(0L));
484 * Set the context parameters in the parameter service.
486 * @param contextParameters The parameters to set.
488 private void setContextParmetersInParameterService(final ContextParameters contextParameters) {
489 ParameterService.register(contextParameters);
490 ParameterService.register(contextParameters.getDistributorParameters());
491 ParameterService.register(contextParameters.getLockManagerParameters());
492 ParameterService.register(contextParameters.getPersistorParameters());
496 * Clear the context parameters in the parameter service.
498 * @param contextParameters The parameters to set.
500 private void clearContextParmetersInParameterService(final ContextParameters contextParameters) {
501 ParameterService.deregister(contextParameters.getPersistorParameters());
502 ParameterService.deregister(contextParameters.getLockManagerParameters());
503 ParameterService.deregister(contextParameters.getDistributorParameters());
504 ParameterService.deregister(contextParameters);