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;
26 import java.io.IOException;
27 import java.net.InetAddress;
28 import java.net.InetSocketAddress;
29 import java.net.NetworkInterface;
30 import java.util.Collections;
31 import java.util.Enumeration;
32 import java.util.TreeSet;
34 import org.apache.curator.shaded.com.google.common.io.Files;
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.Test;
40 import org.onap.policy.apex.context.parameters.ContextParameters;
41 import org.onap.policy.apex.context.parameters.DistributorParameters;
42 import org.onap.policy.apex.context.parameters.LockManagerParameters;
43 import org.onap.policy.apex.context.test.locking.ConcurrentContext;
44 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
45 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
46 import org.onap.policy.apex.model.basicmodel.service.ParameterService;
47 import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanDistributorParameters;
48 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManagerParameters;
49 import org.slf4j.ext.XLogger;
50 import org.slf4j.ext.XLoggerFactory;
52 import com.hazelcast.config.Config;
55 * The Class TestConcurrentContext tests concurrent use of context.
57 * @author Liam Fallon (liam.fallon@ericsson.com)
59 public class TestConcurrentContext {
60 // Logger for this class
61 private static final XLogger logger = XLoggerFactory.getXLogger(TestConcurrentContext.class);
64 private static final int ZOOKEEPER_START_PORT = 62181;
65 private static final int TEST_JVM_COUNT_SINGLE_JVM = 1;
66 private static final int TEST_JVM_COUNT_MULTI_JVM = 3;
67 private static final int TEST_THREAD_COUNT_SINGLE_JVM = 64;
68 private static final int TEST_THREAD_COUNT_MULTI_JVM = 20;
69 private static final int TEST_THREAD_LOOPS = 100;
71 private NIOServerCnxnFactory zookeeperFactory;
73 // We need to increment the Zookeeper port because sometimes the port is not released at the end
74 // of the test for a few seconds.
75 private static int nextZookeeperPort = ZOOKEEPER_START_PORT;
76 private int zookeeperPort;
79 public static void configure() throws Exception {
80 System.setProperty("java.net.preferIPv4Stack", "true");
81 System.setProperty("hazelcast.config", "src/test/resources/hazelcast/hazelcast.xml");
83 // The JGroups IP address must be set to a real (not loopback) IP address for Infinispan to
84 // work. IN order to ensure that all
85 // the JVMs in a test pick up the same IP address, this function sets the address to be the
86 // first non-loopback IPv4 address
88 final TreeSet<String> ipAddressSet = new TreeSet<String>();
90 final Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
91 for (final NetworkInterface netint : Collections.list(nets)) {
92 final Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
93 for (final InetAddress inetAddress : Collections.list(inetAddresses)) {
94 // Look for real IPv4 internet addresses
95 if (!inetAddress.isLoopbackAddress() && inetAddress.getAddress().length == 4) {
96 ipAddressSet.add(inetAddress.getHostAddress());
101 if (ipAddressSet.size() == 0) {
102 throw new Exception("cound not find real IP address for test");
104 System.out.println("For Infinispan, setting jgroups.tcp.address to: " + ipAddressSet.first());
105 System.setProperty("jgroups.tcp.address", ipAddressSet.first());
107 final Config config = new Config();
108 config.getNetworkConfig().setPublicAddress(ipAddressSet.first());
109 config.getNetworkConfig().getInterfaces().addInterface(ipAddressSet.first());
113 public static void teardown() throws IOException {}
115 private void startZookeeperServer() throws IOException, InterruptedException {
116 final File zookeeperDirectory = Files.createTempDir();
118 zookeeperPort = nextZookeeperPort++;
120 final ZooKeeperServer server = new ZooKeeperServer(zookeeperDirectory, zookeeperDirectory, 5000);
121 zookeeperFactory = new NIOServerCnxnFactory();
122 zookeeperFactory.configure(new InetSocketAddress(zookeeperPort), 100);
124 zookeeperFactory.startup(server);
127 private void stopZookeeperServer() {
128 zookeeperFactory.shutdown();
132 public void testConcurrentContextJVMLocalVarSet() throws ApexModelException, IOException, ApexException {
133 logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
135 final ContextParameters contextParameters = new ContextParameters();
136 contextParameters.getLockManagerParameters()
137 .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JVMLocalLockManager");
138 final long result = new ConcurrentContext().testConcurrentContext("JVMLocalVarSet", TEST_JVM_COUNT_SINGLE_JVM,
139 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
141 assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
143 logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
147 public void testConcurrentContextJVMLocalNoVarSet() throws ApexModelException, IOException, ApexException {
148 logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
150 new ContextParameters();
151 final long result = new ConcurrentContext().testConcurrentContext("JVMLocalNoVarSet", TEST_JVM_COUNT_SINGLE_JVM,
152 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
154 assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
156 logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
160 public void testConcurrentContextMultiJVMNoLock() throws ApexModelException, IOException, ApexException {
161 logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
163 final ContextParameters contextParameters = new ContextParameters();
164 contextParameters.getDistributorParameters()
165 .setPluginClass("org.onap.policy.apex.context.impl.distribution.jvmlocal.JVMLocalDistributor");
166 contextParameters.getLockManagerParameters()
167 .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JVMLocalLockManager");
169 final long result = new ConcurrentContext().testConcurrentContext("testConcurrentContextMultiJVMNoLock",
170 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
172 // No concurrent map so result will be zero
173 assertEquals(0, result);
175 logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
179 public void testConcurrentContextHazelcastLock() throws ApexModelException, IOException, ApexException {
180 logger.debug("Running testConcurrentContextHazelcastLock test . . .");
182 final ContextParameters contextParameters = new ContextParameters();
183 contextParameters.getDistributorParameters()
184 .setPluginClass(DistributorParameters.DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
185 contextParameters.getLockManagerParameters()
186 .setPluginClass("org.onap.policy.apex.plugins.context.locking.hazelcast.HazelcastLockManager");
187 final long result = new ConcurrentContext().testConcurrentContext("HazelcastLock", TEST_JVM_COUNT_SINGLE_JVM,
188 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
190 assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
191 logger.debug("Ran testConcurrentContextHazelcastLock test");
195 public void testConcurrentContextCuratorLock()
196 throws ApexModelException, IOException, ApexException, InterruptedException {
197 logger.debug("Running testConcurrentContextCuratorLock test . . .");
199 startZookeeperServer();
201 final ContextParameters contextParameters = new ContextParameters();
202 contextParameters.getDistributorParameters()
203 .setPluginClass(DistributorParameters.DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
205 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
206 curatorParameters.setPluginClass("org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManager");
207 curatorParameters.setZookeeperAddress("127.0.0.1:" + zookeeperPort);
208 contextParameters.setLockManagerParameters(curatorParameters);
209 ParameterService.registerParameters(LockManagerParameters.class, curatorParameters);
211 final long result = new ConcurrentContext().testConcurrentContext("CuratorLock", TEST_JVM_COUNT_SINGLE_JVM,
212 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
214 assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
216 stopZookeeperServer();
217 logger.debug("Ran testConcurrentContextCuratorLock test");
221 public void testConcurrentContextHazelcastMultiJVMHazelcastLock()
222 throws ApexModelException, IOException, ApexException {
223 logger.debug("Running testConcurrentContextHazelcastMultiJVMHazelcastLock test . . .");
225 final ContextParameters contextParameters = new ContextParameters();
226 contextParameters.getDistributorParameters().setPluginClass(
227 "org.onap.policy.apex.plugins.context.distribution.hazelcast.HazelcastContextDistributor");
228 contextParameters.getLockManagerParameters()
229 .setPluginClass("org.onap.policy.apex.plugins.context.locking.hazelcast.HazelcastLockManager");
230 final long result = new ConcurrentContext().testConcurrentContext("HazelcastMultiHazelcastlock",
231 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
233 assertEquals(TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS, result);
234 logger.debug("Ran testConcurrentContextHazelcastMultiJVMHazelcastLock test");
238 public void testConcurrentContextInfinispanMultiJVMHazelcastlock()
239 throws ApexModelException, IOException, ApexException {
240 logger.debug("Running testConcurrentContextInfinispanMultiJVMHazelcastlock test . . .");
242 final ContextParameters contextParameters = new ContextParameters();
243 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
244 infinispanParameters.setPluginClass(
245 "org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanContextDistributor");
246 infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
247 contextParameters.setDistributorParameters(infinispanParameters);
248 contextParameters.getLockManagerParameters()
249 .setPluginClass("org.onap.policy.apex.plugins.context.locking.hazelcast.HazelcastLockManager");
251 final long result = new ConcurrentContext().testConcurrentContext("InfinispanMultiHazelcastlock",
252 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
254 assertEquals(TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS, result);
255 logger.debug("Ran testConcurrentContextInfinispanMultiJVMHazelcastlock test");
259 public void testConcurrentContextInfinispanMultiJVMCuratorLock()
260 throws ApexModelException, IOException, ApexException, InterruptedException {
261 logger.debug("Running testConcurrentContextInfinispanMultiJVMCuratorLock test . . .");
263 startZookeeperServer();
265 final ContextParameters contextParameters = new ContextParameters();
266 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
267 infinispanParameters.setPluginClass(
268 "org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanContextDistributor");
269 infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
270 contextParameters.setDistributorParameters(infinispanParameters);
272 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
273 curatorParameters.setPluginClass("org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManager");
274 curatorParameters.setZookeeperAddress("127.0.0.1:" + zookeeperPort);
275 contextParameters.setLockManagerParameters(curatorParameters);
276 ParameterService.registerParameters(LockManagerParameters.class, curatorParameters);
278 final long result = new ConcurrentContext().testConcurrentContext("InfinispanMultiCuratorLock",
279 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
281 assertEquals(TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS, result);
283 stopZookeeperServer();
285 logger.debug("Ran testConcurrentContextInfinispanMultiJVMCuratorLock test");
289 public void testConcurrentContextHazelcastMultiJVMCuratorLock()
290 throws ApexModelException, IOException, ApexException, InterruptedException {
291 logger.debug("Running testConcurrentContextHazelcastMultiJVMCuratorLock test . . .");
293 startZookeeperServer();
295 final ContextParameters contextParameters = new ContextParameters();
296 contextParameters.getDistributorParameters().setPluginClass(
297 "org.onap.policy.apex.plugins.context.distribution.hazelcast.HazelcastContextDistributor");
299 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
300 curatorParameters.setPluginClass("org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManager");
301 curatorParameters.setZookeeperAddress("127.0.0.1:" + zookeeperPort);
302 contextParameters.setLockManagerParameters(curatorParameters);
303 ParameterService.registerParameters(LockManagerParameters.class, curatorParameters);
305 final long result = new ConcurrentContext().testConcurrentContext("HazelcastMultiCuratorLock",
306 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
308 assertEquals(TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS, result);
310 stopZookeeperServer();
311 logger.debug("Ran testConcurrentContextHazelcastMultiJVMCuratorLock test");