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.onap.policy.apex.context.parameters.DistributorParameters.DEFAULT_DISTRIBUTOR_PLUGIN_CLASS;
27 import java.io.IOException;
28 import java.net.InetAddress;
29 import java.net.InetSocketAddress;
30 import java.net.NetworkInterface;
31 import java.util.Collections;
32 import java.util.Enumeration;
33 import java.util.TreeSet;
35 import org.apache.zookeeper.server.NIOServerCnxnFactory;
36 import org.apache.zookeeper.server.ZooKeeperServer;
37 import org.junit.AfterClass;
38 import org.junit.BeforeClass;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.rules.TemporaryFolder;
42 import org.onap.policy.apex.context.impl.distribution.jvmlocal.JVMLocalDistributor;
43 import org.onap.policy.apex.context.impl.locking.jvmlocal.JVMLocalLockManager;
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.LockManagerParameters;
47 import org.onap.policy.apex.context.test.locking.ConcurrentContext;
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.model.basicmodel.service.ParameterService;
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.slf4j.ext.XLogger;
58 import org.slf4j.ext.XLoggerFactory;
60 import com.hazelcast.config.Config;
63 * The Class TestConcurrentContext tests concurrent use of context.
65 * @author Liam Fallon (liam.fallon@ericsson.com)
67 public class TestConcurrentContext {
68 // Logger for this class
69 private static final XLogger logger = XLoggerFactory.getXLogger(TestConcurrentContext.class);
72 private static final String ZOOKEEPER_ADDRESS = "127.0.0.1";
73 private static final int ZOOKEEPER_START_PORT = 62181;
74 private static final int TEST_JVM_COUNT_SINGLE_JVM = 1;
75 private static final int TEST_JVM_COUNT_MULTI_JVM = 3;
76 private static final int TEST_THREAD_COUNT_SINGLE_JVM = 64;
77 private static final int TEST_THREAD_COUNT_MULTI_JVM = 20;
78 private static final int TEST_THREAD_LOOPS = 100;
80 private NIOServerCnxnFactory zookeeperFactory;
82 // We need to increment the Zookeeper port because sometimes the port is not released at the end
83 // of the test for a few seconds.
84 private static int nextZookeeperPort = ZOOKEEPER_START_PORT;
85 private int zookeeperPort;
88 public final TemporaryFolder folder = new TemporaryFolder();
91 public static void configure() throws Exception {
92 System.setProperty("java.net.preferIPv4Stack", "true");
93 System.setProperty("hazelcast.config", "src/test/resources/hazelcast/hazelcast.xml");
95 // The JGroups IP address must be set to a real (not loopback) IP address for Infinispan to
96 // work. IN order to ensure that all
97 // the JVMs in a test pick up the same IP address, this function sets the address to be the
98 // first non-loopback IPv4 address
100 final TreeSet<String> ipAddressSet = new TreeSet<String>();
102 final Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
103 for (final NetworkInterface netint : Collections.list(nets)) {
104 final Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
105 for (final InetAddress inetAddress : Collections.list(inetAddresses)) {
106 // Look for real IPv4 internet addresses
107 if (!inetAddress.isLoopbackAddress() && inetAddress.getAddress().length == 4) {
108 ipAddressSet.add(inetAddress.getHostAddress());
113 if (ipAddressSet.size() == 0) {
114 throw new Exception("cound not find real IP address for test");
116 System.out.println("For Infinispan, setting jgroups.tcp.address to: " + ipAddressSet.first());
117 System.setProperty("jgroups.tcp.address", ipAddressSet.first());
119 final Config config = new Config();
120 config.getNetworkConfig().setPublicAddress(ipAddressSet.first());
121 config.getNetworkConfig().getInterfaces().addInterface(ipAddressSet.first());
125 public static void teardown() throws IOException {}
127 private void startZookeeperServer() throws IOException, InterruptedException {
128 final File zookeeperDirectory = folder.newFolder("zookeeperDirectory");
130 zookeeperPort = nextZookeeperPort++;
132 final ZooKeeperServer server = new ZooKeeperServer(zookeeperDirectory, zookeeperDirectory, 5000);
133 zookeeperFactory = new NIOServerCnxnFactory();
134 zookeeperFactory.configure(new InetSocketAddress(zookeeperPort), 100);
136 zookeeperFactory.startup(server);
139 private void stopZookeeperServer() {
140 zookeeperFactory.shutdown();
144 public void testConcurrentContextJVMLocalVarSet() throws ApexModelException, IOException, ApexException {
145 logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
147 final ContextParameters contextParameters = new ContextParameters();
148 contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
149 final long result = new ConcurrentContext().testConcurrentContext("JVMLocalVarSet", TEST_JVM_COUNT_SINGLE_JVM,
150 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
152 assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
154 logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
158 public void testConcurrentContextJVMLocalNoVarSet() throws ApexModelException, IOException, ApexException {
159 logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
161 new ContextParameters();
162 final long result = new ConcurrentContext().testConcurrentContext("JVMLocalNoVarSet", TEST_JVM_COUNT_SINGLE_JVM,
163 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
165 assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
167 logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
171 public void testConcurrentContextMultiJVMNoLock() throws ApexModelException, IOException, ApexException {
172 logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
174 final ContextParameters contextParameters = new ContextParameters();
175 contextParameters.getDistributorParameters().setPluginClass(JVMLocalDistributor.class.getCanonicalName());
176 contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
178 final long result = new ConcurrentContext().testConcurrentContext("testConcurrentContextMultiJVMNoLock",
179 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
181 // No concurrent map so result will be zero
182 assertEquals(0, result);
184 logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
188 public void testConcurrentContextHazelcastLock() throws ApexModelException, IOException, ApexException {
189 logger.debug("Running testConcurrentContextHazelcastLock test . . .");
191 final ContextParameters contextParameters = new ContextParameters();
192 contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
193 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
194 final long result = new ConcurrentContext().testConcurrentContext("HazelcastLock", TEST_JVM_COUNT_SINGLE_JVM,
195 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
197 assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
198 logger.debug("Ran testConcurrentContextHazelcastLock test");
202 public void testConcurrentContextCuratorLock()
203 throws ApexModelException, IOException, ApexException, InterruptedException {
204 logger.debug("Running testConcurrentContextCuratorLock test . . .");
206 startZookeeperServer();
208 final ContextParameters contextParameters = new ContextParameters();
209 contextParameters.getDistributorParameters()
210 .setPluginClass(DistributorParameters.DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
212 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
213 curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
214 curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
215 contextParameters.setLockManagerParameters(curatorParameters);
216 ParameterService.registerParameters(LockManagerParameters.class, curatorParameters);
218 final long result = new ConcurrentContext().testConcurrentContext("CuratorLock", TEST_JVM_COUNT_SINGLE_JVM,
219 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
221 assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
223 stopZookeeperServer();
224 logger.debug("Ran testConcurrentContextCuratorLock test");
228 public void testConcurrentContextHazelcastMultiJVMHazelcastLock()
229 throws ApexModelException, IOException, ApexException {
230 logger.debug("Running testConcurrentContextHazelcastMultiJVMHazelcastLock test . . .");
232 final ContextParameters contextParameters = new ContextParameters();
233 contextParameters.getDistributorParameters()
234 .setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
235 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
236 final long result = new ConcurrentContext().testConcurrentContext("HazelcastMultiHazelcastlock",
237 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
239 assertEquals(TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS, result);
240 logger.debug("Ran testConcurrentContextHazelcastMultiJVMHazelcastLock test");
244 public void testConcurrentContextInfinispanMultiJVMHazelcastlock()
245 throws ApexModelException, IOException, ApexException {
246 logger.debug("Running testConcurrentContextInfinispanMultiJVMHazelcastlock test . . .");
248 final ContextParameters contextParameters = new ContextParameters();
249 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
250 infinispanParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
251 infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
252 contextParameters.setDistributorParameters(infinispanParameters);
253 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
255 final long result = new ConcurrentContext().testConcurrentContext("InfinispanMultiHazelcastlock",
256 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
258 assertEquals(TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS, result);
259 logger.debug("Ran testConcurrentContextInfinispanMultiJVMHazelcastlock test");
263 public void testConcurrentContextInfinispanMultiJVMCuratorLock()
264 throws ApexModelException, IOException, ApexException, InterruptedException {
265 logger.debug("Running testConcurrentContextInfinispanMultiJVMCuratorLock test . . .");
267 startZookeeperServer();
269 final ContextParameters contextParameters = new ContextParameters();
270 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
271 infinispanParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
272 infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
273 contextParameters.setDistributorParameters(infinispanParameters);
275 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
276 curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
277 curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
278 contextParameters.setLockManagerParameters(curatorParameters);
279 ParameterService.registerParameters(LockManagerParameters.class, curatorParameters);
281 final long result = new ConcurrentContext().testConcurrentContext("InfinispanMultiCuratorLock",
282 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
284 assertEquals(TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS, result);
286 stopZookeeperServer();
288 logger.debug("Ran testConcurrentContextInfinispanMultiJVMCuratorLock test");
292 public void testConcurrentContextHazelcastMultiJVMCuratorLock()
293 throws ApexModelException, IOException, ApexException, InterruptedException {
294 logger.debug("Running testConcurrentContextHazelcastMultiJVMCuratorLock test . . .");
296 startZookeeperServer();
298 final ContextParameters contextParameters = new ContextParameters();
299 contextParameters.getDistributorParameters()
300 .setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
302 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
303 curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
304 curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
305 contextParameters.setLockManagerParameters(curatorParameters);
306 ParameterService.registerParameters(LockManagerParameters.class, curatorParameters);
308 final long result = new ConcurrentContext().testConcurrentContext("HazelcastMultiCuratorLock",
309 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
311 assertEquals(TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS, result);
313 stopZookeeperServer();
314 logger.debug("Ran testConcurrentContextHazelcastMultiJVMCuratorLock test");