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