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.model.utilities.ResourceUtils;
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.slf4j.ext.XLogger;
59 import org.slf4j.ext.XLoggerFactory;
61 import com.hazelcast.config.Config;
64 * The Class TestConcurrentContext tests concurrent use of context.
66 * @author Liam Fallon (liam.fallon@ericsson.com)
68 public class TestConcurrentContext {
69 private static final String HAZELCAST_CONFIG = "hazelcast.config";
71 private static final String JAVA_NET_PREFER_IPV4_STACK = "java.net.preferIPv4Stack";
72 private static final String HAZELCAST_XML_FILE = "src/test/resources/hazelcast/hazelcast.xml";
74 // Logger for this class
75 private static final XLogger logger = XLoggerFactory.getXLogger(TestConcurrentContext.class);
78 private static final String ZOOKEEPER_ADDRESS = "127.0.0.1";
79 private static final int ZOOKEEPER_START_PORT = 62181;
80 private static final int TEST_JVM_COUNT_SINGLE_JVM = 1;
81 private static final int TEST_JVM_COUNT_MULTI_JVM = 3;
82 private static final int TEST_THREAD_COUNT_SINGLE_JVM = 64;
83 private static final int TEST_THREAD_COUNT_MULTI_JVM = 20;
84 private static final int TEST_THREAD_LOOPS = 100;
86 private NIOServerCnxnFactory zookeeperFactory;
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();
97 public static void configure() throws Exception {
98 System.setProperty(JAVA_NET_PREFER_IPV4_STACK, "true");
99 final String hazelCastfileLocation = ResourceUtils.getFilePath4Resource(HAZELCAST_XML_FILE);
100 System.setProperty(HAZELCAST_CONFIG, hazelCastfileLocation);
102 // The JGroups IP address must be set to a real (not loopback) IP address for Infinispan to
103 // work. IN order to ensure that all
104 // the JVMs in a test pick up the same IP address, this function sets the address to be the
105 // first non-loopback IPv4 address
107 final TreeSet<String> ipAddressSet = new TreeSet<String>();
109 final Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
110 for (final NetworkInterface netint : Collections.list(nets)) {
111 final Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
112 for (final InetAddress inetAddress : Collections.list(inetAddresses)) {
113 // Look for real IPv4 internet addresses
114 if (!inetAddress.isLoopbackAddress() && inetAddress.getAddress().length == 4) {
115 ipAddressSet.add(inetAddress.getHostAddress());
120 if (ipAddressSet.size() == 0) {
121 throw new Exception("cound not find real IP address for test");
123 System.out.println("For Infinispan, setting jgroups.tcp.address to: " + ipAddressSet.first());
124 System.setProperty("jgroups.tcp.address", ipAddressSet.first());
126 final Config config = new Config();
127 config.getNetworkConfig().setPublicAddress(ipAddressSet.first());
128 config.getNetworkConfig().getInterfaces().addInterface(ipAddressSet.first());
132 public static void teardown() throws IOException {}
134 private void startZookeeperServer() throws IOException, InterruptedException {
135 final File zookeeperDirectory = folder.newFolder("zookeeperDirectory");
137 zookeeperPort = nextZookeeperPort++;
139 final ZooKeeperServer server = new ZooKeeperServer(zookeeperDirectory, zookeeperDirectory, 5000);
140 zookeeperFactory = new NIOServerCnxnFactory();
141 zookeeperFactory.configure(new InetSocketAddress(zookeeperPort), 100);
143 zookeeperFactory.startup(server);
146 private void stopZookeeperServer() {
147 zookeeperFactory.shutdown();
151 public void testConcurrentContextJVMLocalVarSet() throws ApexModelException, IOException, ApexException {
152 logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
154 final ContextParameters contextParameters = new ContextParameters();
155 contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
156 final long result = new ConcurrentContext().testConcurrentContext("JVMLocalVarSet", TEST_JVM_COUNT_SINGLE_JVM,
157 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
159 assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
161 logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
165 public void testConcurrentContextJVMLocalNoVarSet() throws ApexModelException, IOException, ApexException {
166 logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
168 new ContextParameters();
169 final long result = new ConcurrentContext().testConcurrentContext("JVMLocalNoVarSet", TEST_JVM_COUNT_SINGLE_JVM,
170 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
172 assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
174 logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
178 public void testConcurrentContextMultiJVMNoLock() throws ApexModelException, IOException, ApexException {
179 logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
181 final ContextParameters contextParameters = new ContextParameters();
182 contextParameters.getDistributorParameters().setPluginClass(JVMLocalDistributor.class.getCanonicalName());
183 contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
185 final long result = new ConcurrentContext().testConcurrentContext("testConcurrentContextMultiJVMNoLock",
186 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
188 // No concurrent map so result will be zero
189 assertEquals(0, result);
191 logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
195 public void testConcurrentContextHazelcastLock() throws ApexModelException, IOException, ApexException {
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());
201 final long result = new ConcurrentContext().testConcurrentContext("HazelcastLock", TEST_JVM_COUNT_SINGLE_JVM,
202 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
204 assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
205 logger.debug("Ran testConcurrentContextHazelcastLock test");
209 public void testConcurrentContextCuratorLock()
210 throws ApexModelException, IOException, ApexException, InterruptedException {
211 logger.debug("Running testConcurrentContextCuratorLock test . . .");
213 startZookeeperServer();
215 final ContextParameters contextParameters = new ContextParameters();
216 contextParameters.getDistributorParameters()
217 .setPluginClass(DistributorParameters.DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
219 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
220 curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
221 curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
222 contextParameters.setLockManagerParameters(curatorParameters);
223 ParameterService.registerParameters(LockManagerParameters.class, curatorParameters);
225 final long result = new ConcurrentContext().testConcurrentContext("CuratorLock", TEST_JVM_COUNT_SINGLE_JVM,
226 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
228 assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
230 stopZookeeperServer();
231 logger.debug("Ran testConcurrentContextCuratorLock test");
235 public void testConcurrentContextHazelcastMultiJVMHazelcastLock()
236 throws ApexModelException, IOException, ApexException {
237 logger.debug("Running testConcurrentContextHazelcastMultiJVMHazelcastLock test . . .");
239 final ContextParameters contextParameters = new ContextParameters();
240 contextParameters.getDistributorParameters()
241 .setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
242 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
243 final long result = new ConcurrentContext().testConcurrentContext("HazelcastMultiHazelcastlock",
244 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
246 assertEquals(TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS, result);
247 logger.debug("Ran testConcurrentContextHazelcastMultiJVMHazelcastLock test");
251 public void testConcurrentContextInfinispanMultiJVMHazelcastlock()
252 throws ApexModelException, IOException, ApexException {
253 logger.debug("Running testConcurrentContextInfinispanMultiJVMHazelcastlock test . . .");
255 final ContextParameters contextParameters = new ContextParameters();
256 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
257 infinispanParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
258 infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
259 contextParameters.setDistributorParameters(infinispanParameters);
260 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
262 final long result = new ConcurrentContext().testConcurrentContext("InfinispanMultiHazelcastlock",
263 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
265 assertEquals(TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS, result);
266 logger.debug("Ran testConcurrentContextInfinispanMultiJVMHazelcastlock test");
270 public void testConcurrentContextInfinispanMultiJVMCuratorLock()
271 throws ApexModelException, IOException, ApexException, InterruptedException {
272 logger.debug("Running testConcurrentContextInfinispanMultiJVMCuratorLock test . . .");
274 startZookeeperServer();
276 final ContextParameters contextParameters = new ContextParameters();
277 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
278 infinispanParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
279 infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
280 contextParameters.setDistributorParameters(infinispanParameters);
282 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
283 curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
284 curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
285 contextParameters.setLockManagerParameters(curatorParameters);
286 ParameterService.registerParameters(LockManagerParameters.class, curatorParameters);
288 final long result = new ConcurrentContext().testConcurrentContext("InfinispanMultiCuratorLock",
289 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
291 assertEquals(TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS, result);
293 stopZookeeperServer();
295 logger.debug("Ran testConcurrentContextInfinispanMultiJVMCuratorLock test");
299 public void testConcurrentContextHazelcastMultiJVMCuratorLock()
300 throws ApexModelException, IOException, ApexException, InterruptedException {
301 logger.debug("Running testConcurrentContextHazelcastMultiJVMCuratorLock test . . .");
303 startZookeeperServer();
305 final ContextParameters contextParameters = new ContextParameters();
306 contextParameters.getDistributorParameters()
307 .setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
309 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
310 curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
311 curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
312 contextParameters.setLockManagerParameters(curatorParameters);
313 ParameterService.registerParameters(LockManagerParameters.class, curatorParameters);
315 final long result = new ConcurrentContext().testConcurrentContext("HazelcastMultiCuratorLock",
316 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
318 assertEquals(TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS, result);
320 stopZookeeperServer();
321 logger.debug("Ran testConcurrentContextHazelcastMultiJVMCuratorLock test");