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.parameters.LockManagerParameters;
44 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
45 import org.onap.policy.apex.context.test.lock.modifier.LockType;
46 import org.onap.policy.apex.context.test.locking.ConcurrentContext;
47 import org.onap.policy.apex.context.test.utils.ConfigrationProvider;
48 import org.onap.policy.apex.context.test.utils.ConfigrationProviderImpl;
49 import org.onap.policy.apex.context.test.utils.Constants;
50 import org.onap.policy.apex.context.test.utils.NetworkUtils;
51 import org.onap.policy.apex.context.test.utils.ZooKeeperServerServiceProvider;
52 import org.onap.policy.apex.core.infrastructure.messaging.util.MessagingUtils;
53 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
54 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
55 import org.onap.policy.apex.model.basicmodel.service.ParameterService;
56 import org.onap.policy.apex.plugins.context.distribution.hazelcast.HazelcastContextDistributor;
57 import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanContextDistributor;
58 import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanDistributorParameters;
59 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManager;
60 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManagerParameters;
61 import org.onap.policy.apex.plugins.context.locking.hazelcast.HazelcastLockManager;
62 import org.onap.policy.common.utils.resources.ResourceUtils;
63 import org.slf4j.ext.XLogger;
64 import org.slf4j.ext.XLoggerFactory;
67 * The Class TestConcurrentContext tests concurrent use of context.
69 * @author Liam Fallon (liam.fallon@ericsson.com)
71 public class TestConcurrentContext {
72 private static final String HAZELCAST_CONFIG = "hazelcast.config";
74 private static final String JAVA_NET_PREFER_IPV4_STACK = "java.net.preferIPv4Stack";
75 private static final String HAZELCAST_XML_FILE = "src/test/resources/hazelcast/hazelcast.xml";
77 // Logger for this class
78 private static final XLogger logger = XLoggerFactory.getXLogger(TestConcurrentContext.class);
81 private static final String ZOOKEEPER_ADDRESS = "127.0.0.1";
82 private static final int ZOOKEEPER_START_PORT = 62181;
83 private static final int TEST_JVM_COUNT_SINGLE_JVM = 1;
84 private static final int TEST_JVM_COUNT_MULTI_JVM = 3;
85 private static final int TEST_THREAD_COUNT_SINGLE_JVM = 64;
86 private static final int TEST_THREAD_COUNT_MULTI_JVM = 20;
87 private static final int TEST_THREAD_LOOPS = 100;
89 // We need to increment the Zookeeper port because sometimes the port is not released at the end
90 // of the test for a few seconds.
91 private static int nextZookeeperPort = ZOOKEEPER_START_PORT;
92 private int zookeeperPort;
95 public final TemporaryFolder folder = new TemporaryFolder();
97 private ZooKeeperServerServiceProvider zooKeeperServerServiceProvider;
100 public static void configure() throws Exception {
101 System.setProperty(JAVA_NET_PREFER_IPV4_STACK, "true");
102 final String hazelCastfileLocation = ResourceUtils.getFilePath4Resource(HAZELCAST_XML_FILE);
103 System.setProperty(HAZELCAST_CONFIG, hazelCastfileLocation);
105 final TreeSet<String> ipAddressSet = NetworkUtils.getIPv4NonLoopAddresses();
107 if (ipAddressSet.size() == 0) {
108 throw new Exception("cound not find real IP address for test");
110 logger.info("For Infinispan, setting jgroups.tcp.address to: {}", ipAddressSet.first());
111 System.setProperty("jgroups.tcp.address", ipAddressSet.first());
115 private void startZookeeperServer() throws Exception {
116 final File zookeeperDirectory = folder.newFolder("zookeeperDirectory");
118 zookeeperPort = nextZookeeperPort++;
119 final InetSocketAddress addr = new InetSocketAddress(MessagingUtils.findPort(zookeeperPort));
120 zooKeeperServerServiceProvider = new ZooKeeperServerServiceProvider(zookeeperDirectory, addr);
121 zooKeeperServerServiceProvider.startZookeeperServer();
124 private void stopZookeeperServer() {
125 if (zooKeeperServerServiceProvider != null) {
126 zooKeeperServerServiceProvider.stopZookeeperServer();
131 public void testConcurrentContextJVMLocalVarSet() throws Exception {
132 logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
134 final ContextParameters contextParameters = new ContextParameters();
135 contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
137 final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalVarSet",
138 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
140 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
141 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
143 assertFalse(result.isEmpty());
145 final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
146 final TestContextLongItem actual = result.get(TEST_VALUE);
147 assertNotNull(actual);
148 assertEquals(expected, actual.getLongValue());
150 logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
154 public void testConcurrentContextJVMLocalNoVarSet() throws Exception {
155 logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
157 new ContextParameters();
158 final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalNoVarSet",
159 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
161 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
162 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
164 final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
165 final TestContextLongItem actual = result.get(Constants.TEST_VALUE);
166 assertNotNull(actual);
167 assertEquals(expected, actual.getLongValue());
169 logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
173 public void testConcurrentContextMultiJVMNoLock() throws Exception {
174 logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
176 final ContextParameters contextParameters = new ContextParameters();
177 contextParameters.getDistributorParameters().setPluginClass(JVMLocalDistributor.class.getCanonicalName());
178 contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
180 final ConfigrationProvider configrationProvider = getConfigrationProvider("testConcurrentContextMultiJVMNoLock",
181 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
183 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
184 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
186 // No concurrent map so result will be zero
187 final TestContextLongItem actual = result.get(TEST_VALUE);
188 assertNotNull(actual);
189 assertEquals(0, actual.getLongValue());
191 logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
195 public void testConcurrentContextHazelcastLock() throws Exception {
196 logger.debug("Running testConcurrentContextHazelcastLock test . . .");
198 final ContextParameters contextParameters = new ContextParameters();
199 contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
200 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
202 final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastLock",
203 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
205 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
206 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
208 final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
209 final TestContextLongItem actual = result.get(TEST_VALUE);
210 assertNotNull(actual);
211 assertEquals(expected, actual.getLongValue());
213 logger.debug("Ran testConcurrentContextHazelcastLock test");
217 public void testConcurrentContextCuratorLock() throws Exception {
218 logger.debug("Running testConcurrentContextCuratorLock test . . .");
220 startZookeeperServer();
221 final ContextParameters contextParameters = new ContextParameters();
222 final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
223 distributorParameters.setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
225 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
226 curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
227 curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
228 contextParameters.setLockManagerParameters(curatorParameters);
229 ParameterService.registerParameters(LockManagerParameters.class, curatorParameters);
231 final ConfigrationProvider configrationProvider = getConfigrationProvider("CuratorLock",
232 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
234 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
235 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
237 final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
238 final TestContextLongItem actual = result.get(TEST_VALUE);
239 assertNotNull(actual);
240 assertEquals(expected, actual.getLongValue());
241 logger.debug("Ran testConcurrentContextCuratorLock test");
243 stopZookeeperServer();
248 public void testConcurrentContextHazelcastMultiJVMHazelcastLock() throws Exception {
249 logger.debug("Running testConcurrentContextHazelcastMultiJVMHazelcastLock test . . .");
251 final ContextParameters contextParameters = new ContextParameters();
252 final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
253 distributorParameters.setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
254 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
256 final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastMultiHazelcastlock",
257 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
259 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
260 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
262 final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
263 final TestContextLongItem actual = result.get(TEST_VALUE);
264 assertNotNull(actual);
265 assertEquals(expected, actual.getLongValue());
266 logger.debug("Ran testConcurrentContextHazelcastMultiJVMHazelcastLock test");
270 public void testConcurrentContextInfinispanMultiJVMHazelcastlock()
271 throws ApexModelException, IOException, ApexException {
272 logger.debug("Running testConcurrentContextInfinispanMultiJVMHazelcastlock test . . .");
274 final ContextParameters contextParameters = new ContextParameters();
275 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
276 infinispanParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
277 infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
278 contextParameters.setDistributorParameters(infinispanParameters);
279 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
281 final ConfigrationProvider configrationProvider = getConfigrationProvider("InfinispanMultiHazelcastlock",
282 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
284 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
285 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
287 final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
288 final TestContextLongItem actual = result.get(TEST_VALUE);
289 assertNotNull(actual);
290 assertEquals(expected, actual.getLongValue());
291 logger.debug("Ran testConcurrentContextInfinispanMultiJVMHazelcastlock test");
295 public void testConcurrentContextInfinispanMultiJVMCuratorLock() throws Exception {
296 logger.debug("Running testConcurrentContextInfinispanMultiJVMCuratorLock test . . .");
299 startZookeeperServer();
301 final ContextParameters contextParameters = new ContextParameters();
302 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
303 infinispanParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
304 infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
305 contextParameters.setDistributorParameters(infinispanParameters);
307 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
308 curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
309 curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
310 contextParameters.setLockManagerParameters(curatorParameters);
311 ParameterService.registerParameters(LockManagerParameters.class, curatorParameters);
313 final ConfigrationProvider configrationProvider = getConfigrationProvider("InfinispanMultiCuratorLock",
314 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
316 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
317 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
319 final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
320 final TestContextLongItem actual = result.get(TEST_VALUE);
321 assertNotNull(actual);
322 assertEquals(expected, actual.getLongValue());
324 stopZookeeperServer();
327 logger.debug("Ran testConcurrentContextInfinispanMultiJVMCuratorLock test");
331 public void testConcurrentContextHazelcastMultiJVMCuratorLock() throws Exception {
332 logger.debug("Running testConcurrentContextHazelcastMultiJVMCuratorLock test . . .");
335 startZookeeperServer();
337 final ContextParameters contextParameters = new ContextParameters();
338 contextParameters.getDistributorParameters()
339 .setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
341 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
342 curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
343 curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
344 contextParameters.setLockManagerParameters(curatorParameters);
345 ParameterService.registerParameters(LockManagerParameters.class, curatorParameters);
347 final ConfigrationProvider configrationProvider = getConfigrationProvider("HazelcastMultiCuratorLock",
348 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
349 final Map<String, TestContextLongItem> result =
350 new ConcurrentContext(configrationProvider).testConcurrentContext();
352 final int expected = TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS;
353 final TestContextLongItem actual = result.get(TEST_VALUE);
354 assertNotNull(actual);
355 assertEquals(expected, actual.getLongValue());
357 stopZookeeperServer();
359 logger.debug("Ran testConcurrentContextHazelcastMultiJVMCuratorLock test");
362 ConfigrationProvider getConfigrationProvider(final String testType, final int jvmCount, final int threadCount,
363 final int threadLoops) {
364 return new ConfigrationProviderImpl(testType, jvmCount, threadCount, threadLoops, 16,
365 LockType.WRITE_LOCK_SINGLE_VALUE_UPDATE.getValue()) {
367 public Map<String, Object> getContextAlbumInitValues() {
368 final Map<String, Object> initValues = super.getContextAlbumInitValues();
369 initValues.put(TEST_VALUE, new TestContextLongItem(0l));