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.plugins.context.test.locking;
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.TreeSet;
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;
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);
104 final TreeSet<String> ipAddressSet = NetworkUtils.getIPv4NonLoopAddresses();
106 if (ipAddressSet.size() == 0) {
107 throw new Exception("cound not find real IP address for test");
109 logger.info("For Infinispan, setting jgroups.tcp.address to: {}", ipAddressSet.first());
110 System.setProperty("jgroups.tcp.address", ipAddressSet.first());
114 private void startZookeeperServer() throws Exception {
115 final File zookeeperDirectory = folder.newFolder("zookeeperDirectory");
117 zookeeperPort = nextZookeeperPort++;
118 final InetSocketAddress addr = new InetSocketAddress(MessagingUtils.findPort(zookeeperPort));
119 zooKeeperServerServiceProvider = new ZooKeeperServerServiceProvider(zookeeperDirectory, addr);
120 zooKeeperServerServiceProvider.startZookeeperServer();
123 private void stopZookeeperServer() {
124 if (zooKeeperServerServiceProvider != null) {
125 zooKeeperServerServiceProvider.stopZookeeperServer();
130 public void testConcurrentContextJVMLocalVarSet() throws Exception {
131 logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
133 final ContextParameters contextParameters = new ContextParameters();
134 contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
136 final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalVarSet",
137 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
139 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
140 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
142 assertFalse(result.isEmpty());
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());
149 logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
153 public void testConcurrentContextJVMLocalNoVarSet() throws Exception {
154 logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
156 new ContextParameters();
157 final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalNoVarSet",
158 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
160 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
161 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
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());
168 logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
172 public void testConcurrentContextMultiJVMNoLock() throws Exception {
173 logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
175 final ContextParameters contextParameters = new ContextParameters();
176 contextParameters.getDistributorParameters().setPluginClass(JVMLocalDistributor.class.getCanonicalName());
177 contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
179 final ConfigrationProvider configrationProvider = getConfigrationProvider("testConcurrentContextMultiJVMNoLock",
180 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
182 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
183 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
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());
190 logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
194 public void testConcurrentContextHazelcastLock() throws Exception {
195 logger.debug("Running testConcurrentContextHazelcastLock test . . .");
197 final ContextParameters contextParameters = new ContextParameters();
198 contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
199 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
201 final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastLock",
202 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
204 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
205 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
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());
212 logger.debug("Ran testConcurrentContextHazelcastLock test");
216 public void testConcurrentContextCuratorLock() throws Exception {
217 logger.debug("Running testConcurrentContextCuratorLock test . . .");
219 startZookeeperServer();
220 final ContextParameters contextParameters = new ContextParameters();
221 final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
222 distributorParameters.setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
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);
230 final ConfigrationProvider configrationProvider = getConfigrationProvider("CuratorLock",
231 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
233 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
234 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
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");
242 stopZookeeperServer();
247 public void testConcurrentContextHazelcastMultiJVMHazelcastLock() throws Exception {
248 logger.debug("Running testConcurrentContextHazelcastMultiJVMHazelcastLock test . . .");
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());
255 final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastMultiHazelcastlock",
256 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
258 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
259 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
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");
269 public void testConcurrentContextInfinispanMultiJVMHazelcastlock()
270 throws ApexModelException, IOException, ApexException {
271 logger.debug("Running testConcurrentContextInfinispanMultiJVMHazelcastlock test . . .");
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());
280 final ConfigrationProvider configrationProvider = getConfigrationProvider("InfinispanMultiHazelcastlock",
281 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
283 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
284 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
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");
294 public void testConcurrentContextInfinispanMultiJVMCuratorLock() throws Exception {
295 logger.debug("Running testConcurrentContextInfinispanMultiJVMCuratorLock test . . .");
298 startZookeeperServer();
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);
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);
312 final ConfigrationProvider configrationProvider = getConfigrationProvider("InfinispanMultiCuratorLock",
313 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
315 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
316 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
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());
323 stopZookeeperServer();
326 logger.debug("Ran testConcurrentContextInfinispanMultiJVMCuratorLock test");
330 public void testConcurrentContextHazelcastMultiJVMCuratorLock() throws Exception {
331 logger.debug("Running testConcurrentContextHazelcastMultiJVMCuratorLock test . . .");
334 startZookeeperServer();
336 final ContextParameters contextParameters = new ContextParameters();
337 contextParameters.getDistributorParameters()
338 .setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
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);
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();
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());
356 stopZookeeperServer();
358 logger.debug("Ran testConcurrentContextHazelcastMultiJVMCuratorLock test");
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()) {
366 public Map<String, Object> getContextAlbumInitValues() {
367 final Map<String, Object> initValues = super.getContextAlbumInitValues();
368 initValues.put(TEST_VALUE, new TestContextLongItem(0l));