7788fb4116e8c4080fdd1743238234dab48aae9c
[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.context.test.locking;
22
23
24 import static org.junit.Assert.assertEquals;
25
26 import java.io.IOException;
27
28 import org.junit.Test;
29 import org.onap.policy.apex.context.impl.distribution.jvmlocal.JVMLocalDistributor;
30 import org.onap.policy.apex.context.impl.locking.jvmlocal.JVMLocalLockManager;
31 import org.onap.policy.apex.context.parameters.ContextParameters;
32 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
33 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
34 import org.slf4j.ext.XLogger;
35 import org.slf4j.ext.XLoggerFactory;
36
37 /**
38  * The Class TestConcurrentContext tests concurrent use of context.
39  *
40  * @author Liam Fallon (liam.fallon@ericsson.com)
41  */
42 public class TestConcurrentContext {
43     // Logger for this class
44     private static final XLogger logger = XLoggerFactory.getXLogger(TestConcurrentContext.class);
45
46     // Test parameters
47     private static final int TEST_JVM_COUNT_SINGLE_JVM = 1;
48     private static final int TEST_JVM_COUNT_MULTI_JVM = 3;
49     private static final int TEST_THREAD_COUNT_SINGLE_JVM = 64;
50     private static final int TEST_THREAD_COUNT_MULTI_JVM = 20;
51     private static final int TEST_THREAD_LOOPS = 100;
52
53     @Test
54     public void testConcurrentContextJVMLocalVarSet() throws ApexModelException, IOException, ApexException {
55         logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
56
57         final ContextParameters contextParameters = new ContextParameters();
58         contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
59         final long result = new ConcurrentContext().testConcurrentContext("JVMLocalVarSet", TEST_JVM_COUNT_SINGLE_JVM,
60                 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
61
62         assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
63
64         logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
65     }
66
67     @Test
68     public void testConcurrentContextJVMLocalNoVarSet() throws ApexModelException, IOException, ApexException {
69         logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
70
71         new ContextParameters();
72         final long result = new ConcurrentContext().testConcurrentContext("JVMLocalNoVarSet", TEST_JVM_COUNT_SINGLE_JVM,
73                 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
74
75         assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
76
77         logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
78     }
79
80     @Test
81     public void testConcurrentContextMultiJVMNoLock() throws ApexModelException, IOException, ApexException {
82         logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
83
84         final ContextParameters contextParameters = new ContextParameters();
85         contextParameters.getDistributorParameters().setPluginClass(JVMLocalDistributor.class.getCanonicalName());
86         contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
87
88         final long result = new ConcurrentContext().testConcurrentContext("testConcurrentContextMultiJVMNoLock",
89                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
90
91         // No concurrent map so result will be zero
92         assertEquals(0, result);
93
94         logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
95     }
96 }