7a3a29cc15e40cc296174a43d2ec608f3346c311
[policy/apex-pdp.git] /
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.context.test.locking;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.onap.policy.apex.context.parameters.DistributorParameters.DEFAULT_DISTRIBUTOR_PLUGIN_CLASS;
25
26 import java.io.File;
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;
34
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;
59
60 import com.hazelcast.config.Config;
61
62 /**
63  * The Class TestConcurrentContext tests concurrent use of context.
64  *
65  * @author Liam Fallon (liam.fallon@ericsson.com)
66  */
67 public class TestConcurrentContext {
68     // Logger for this class
69     private static final XLogger logger = XLoggerFactory.getXLogger(TestConcurrentContext.class);
70
71     // Test parameters
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;
79
80     private NIOServerCnxnFactory zookeeperFactory;
81
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;
86
87     @Rule
88     public final TemporaryFolder folder = new TemporaryFolder();
89
90     @BeforeClass
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");
94
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
99         // on a host
100         final TreeSet<String> ipAddressSet = new TreeSet<String>();
101
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());
109                 }
110             }
111         }
112
113         if (ipAddressSet.size() == 0) {
114             throw new Exception("cound not find real IP address for test");
115         }
116         System.out.println("For Infinispan, setting jgroups.tcp.address to: " + ipAddressSet.first());
117         System.setProperty("jgroups.tcp.address", ipAddressSet.first());
118
119         final Config config = new Config();
120         config.getNetworkConfig().setPublicAddress(ipAddressSet.first());
121         config.getNetworkConfig().getInterfaces().addInterface(ipAddressSet.first());
122     }
123
124     @AfterClass
125     public static void teardown() throws IOException {}
126
127     private void startZookeeperServer() throws IOException, InterruptedException {
128         final File zookeeperDirectory = folder.newFolder("zookeeperDirectory");
129
130         zookeeperPort = nextZookeeperPort++;
131
132         final ZooKeeperServer server = new ZooKeeperServer(zookeeperDirectory, zookeeperDirectory, 5000);
133         zookeeperFactory = new NIOServerCnxnFactory();
134         zookeeperFactory.configure(new InetSocketAddress(zookeeperPort), 100);
135
136         zookeeperFactory.startup(server);
137     }
138
139     private void stopZookeeperServer() {
140         zookeeperFactory.shutdown();
141     }
142
143     @Test
144     public void testConcurrentContextJVMLocalVarSet() throws ApexModelException, IOException, ApexException {
145         logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
146
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);
151
152         assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
153
154         logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
155     }
156
157     @Test
158     public void testConcurrentContextJVMLocalNoVarSet() throws ApexModelException, IOException, ApexException {
159         logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
160
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);
164
165         assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
166
167         logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
168     }
169
170     @Test
171     public void testConcurrentContextMultiJVMNoLock() throws ApexModelException, IOException, ApexException {
172         logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
173
174         final ContextParameters contextParameters = new ContextParameters();
175         contextParameters.getDistributorParameters().setPluginClass(JVMLocalDistributor.class.getCanonicalName());
176         contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
177
178         final long result = new ConcurrentContext().testConcurrentContext("testConcurrentContextMultiJVMNoLock",
179                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
180
181         // No concurrent map so result will be zero
182         assertEquals(0, result);
183
184         logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
185     }
186
187     @Test
188     public void testConcurrentContextHazelcastLock() throws ApexModelException, IOException, ApexException {
189         logger.debug("Running testConcurrentContextHazelcastLock test . . .");
190
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);
196
197         assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
198         logger.debug("Ran testConcurrentContextHazelcastLock test");
199     }
200
201     @Test
202     public void testConcurrentContextCuratorLock()
203             throws ApexModelException, IOException, ApexException, InterruptedException {
204         logger.debug("Running testConcurrentContextCuratorLock test . . .");
205
206         startZookeeperServer();
207
208         final ContextParameters contextParameters = new ContextParameters();
209         contextParameters.getDistributorParameters()
210                 .setPluginClass(DistributorParameters.DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
211
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);
217
218         final long result = new ConcurrentContext().testConcurrentContext("CuratorLock", TEST_JVM_COUNT_SINGLE_JVM,
219                 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
220
221         assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
222
223         stopZookeeperServer();
224         logger.debug("Ran testConcurrentContextCuratorLock test");
225     }
226
227     @Test
228     public void testConcurrentContextHazelcastMultiJVMHazelcastLock()
229             throws ApexModelException, IOException, ApexException {
230         logger.debug("Running testConcurrentContextHazelcastMultiJVMHazelcastLock test . . .");
231
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);
238
239         assertEquals(TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS, result);
240         logger.debug("Ran testConcurrentContextHazelcastMultiJVMHazelcastLock test");
241     }
242
243     @Test
244     public void testConcurrentContextInfinispanMultiJVMHazelcastlock()
245             throws ApexModelException, IOException, ApexException {
246         logger.debug("Running testConcurrentContextInfinispanMultiJVMHazelcastlock test . . .");
247
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());
254
255         final long result = new ConcurrentContext().testConcurrentContext("InfinispanMultiHazelcastlock",
256                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
257
258         assertEquals(TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS, result);
259         logger.debug("Ran testConcurrentContextInfinispanMultiJVMHazelcastlock test");
260     }
261
262     @Test
263     public void testConcurrentContextInfinispanMultiJVMCuratorLock()
264             throws ApexModelException, IOException, ApexException, InterruptedException {
265         logger.debug("Running testConcurrentContextInfinispanMultiJVMCuratorLock test . . .");
266
267         startZookeeperServer();
268
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);
274
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);
280
281         final long result = new ConcurrentContext().testConcurrentContext("InfinispanMultiCuratorLock",
282                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
283
284         assertEquals(TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS, result);
285
286         stopZookeeperServer();
287
288         logger.debug("Ran testConcurrentContextInfinispanMultiJVMCuratorLock test");
289     }
290
291     @Test
292     public void testConcurrentContextHazelcastMultiJVMCuratorLock()
293             throws ApexModelException, IOException, ApexException, InterruptedException {
294         logger.debug("Running testConcurrentContextHazelcastMultiJVMCuratorLock test . . .");
295
296         startZookeeperServer();
297
298         final ContextParameters contextParameters = new ContextParameters();
299         contextParameters.getDistributorParameters()
300                 .setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
301
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);
307
308         final long result = new ConcurrentContext().testConcurrentContext("HazelcastMultiCuratorLock",
309                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
310
311         assertEquals(TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS, result);
312
313         stopZookeeperServer();
314         logger.debug("Ran testConcurrentContextHazelcastMultiJVMCuratorLock test");
315     }
316 }