7274f0d5742638349f6e34b693cce081458642ac
[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 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.onap.policy.apex.context.test.lock.modifier.LockType.WRITE_LOCK_SINGLE_VALUE_UPDATE;
27 import static org.onap.policy.apex.context.test.utils.Constants.TEST_VALUE;
28
29 import java.util.Map;
30
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.apex.context.impl.distribution.jvmlocal.JvmLocalDistributor;
35 import org.onap.policy.apex.context.impl.locking.jvmlocal.JvmLocalLockManager;
36 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
37 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
38 import org.onap.policy.apex.context.parameters.ContextParameters;
39 import org.onap.policy.apex.context.parameters.SchemaParameters;
40 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
41 import org.onap.policy.apex.context.test.utils.ConfigrationProvider;
42 import org.onap.policy.apex.context.test.utils.ConfigrationProviderImpl;
43 import org.onap.policy.apex.context.test.utils.Constants;
44 import org.onap.policy.common.parameters.ParameterService;
45 import org.slf4j.ext.XLogger;
46 import org.slf4j.ext.XLoggerFactory;
47
48 /**
49  * The Class TestConcurrentContext tests concurrent use of context.
50  *
51  * @author Liam Fallon (liam.fallon@ericsson.com)
52  */
53 public class ConcurrentContextTest {
54
55     // Logger for this class
56     private static final XLogger logger = XLoggerFactory.getXLogger(ConcurrentContextTest.class);
57
58     // Test parameters
59     private static final int ALBUM_SIZE = 16;
60     private static final int TEST_JVM_COUNT_SINGLE_JVM = 1;
61     private static final int TEST_JVM_COUNT_MULTI_JVM = 3;
62     private static final int TEST_THREAD_COUNT_SINGLE_JVM = 64;
63     private static final int TEST_THREAD_COUNT_MULTI_JVM = 20;
64     private static final int TEST_THREAD_LOOPS = 100;
65
66     private SchemaParameters schemaParameters;
67     private ContextParameters contextParameters;
68
69     /**
70      * Set up context for tests.
71      */
72     @Before
73     public void beforeTest() {
74         contextParameters = new ContextParameters();
75
76         contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
77         contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
78         contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME);
79         contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
80
81         ParameterService.register(contextParameters);
82         ParameterService.register(contextParameters.getDistributorParameters());
83         ParameterService.register(contextParameters.getLockManagerParameters());
84         ParameterService.register(contextParameters.getPersistorParameters());
85         
86         schemaParameters = new SchemaParameters();
87         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
88         schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
89
90         ParameterService.register(schemaParameters);
91     }
92
93     /**
94      * Clear down context for tests.
95      */
96     @After
97     public void afterTest() {
98         ParameterService.deregister(schemaParameters);
99
100         ParameterService.deregister(contextParameters.getDistributorParameters());
101         ParameterService.deregister(contextParameters.getLockManagerParameters());
102         ParameterService.deregister(contextParameters.getPersistorParameters());
103         ParameterService.deregister(contextParameters);
104     }
105
106     @Test
107     public void testConcurrentContextJvmLocalVarSet() throws Exception {
108         logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
109
110         contextParameters.getLockManagerParameters().setPluginClass(JvmLocalLockManager.class.getCanonicalName());
111
112         final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalVarSet",
113                 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
114
115         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
116         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
117
118         assertFalse(result.isEmpty());
119         final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
120         final TestContextLongItem actual = result.get(TEST_VALUE);
121         assertNotNull(actual);
122         assertEquals(expected, actual.getLongValue());
123
124
125         logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
126     }
127
128     @Test
129     public void testConcurrentContextJvmLocalNoVarSet() throws Exception {
130         logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
131
132         final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalNoVarSet",
133                 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
134
135         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
136         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
137
138         assertFalse(result.isEmpty());
139         final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
140         final TestContextLongItem actual = result.get(TEST_VALUE);
141         assertNotNull(actual);
142         assertEquals(expected, actual.getLongValue());
143
144         logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
145     }
146
147     @Test
148     public void testConcurrentContextMultiJvmNoLock() throws Exception {
149         logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
150
151         contextParameters.getDistributorParameters().setPluginClass(JvmLocalDistributor.class.getCanonicalName());
152         contextParameters.getLockManagerParameters().setPluginClass(JvmLocalLockManager.class.getCanonicalName());
153
154         final ConfigrationProvider configrationProvider = getConfigrationProvider("testConcurrentContextMultiJVMNoLock",
155                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
156
157         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
158         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
159
160         // No concurrent map so result will be zero
161         assertFalse(result.isEmpty());
162         final TestContextLongItem actual = result.get(TEST_VALUE);
163         assertNotNull(actual);
164         assertEquals(0, actual.getLongValue());
165
166         logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
167     }
168
169     private ConfigrationProvider getConfigrationProvider(final String testType, final int jvmCount,
170             final int threadCount, final int threadLoops) {
171         return new ConfigrationProviderImpl(testType, jvmCount, threadCount, threadLoops, ALBUM_SIZE,
172                 WRITE_LOCK_SINGLE_VALUE_UPDATE.getValue()) {
173             @Override
174             public Map<String, Object> getContextAlbumInitValues() {
175                 final Map<String, Object> initValues = super.getContextAlbumInitValues();
176                 initValues.put(Constants.TEST_VALUE, new TestContextLongItem(0L));
177                 return initValues;
178             }
179         };
180     }
181 }