8fca90b07f1c12fd80ad226527c4af6db8870bb3
[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 TestConcurrentContext {
54
55     // Logger for this class
56     private static final XLogger logger = XLoggerFactory.getXLogger(TestConcurrentContext.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     @Before
70     public void beforeTest() {
71         contextParameters = new ContextParameters();
72
73         contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
74         contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
75         contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME);
76         contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
77
78         ParameterService.register(contextParameters);
79         ParameterService.register(contextParameters.getDistributorParameters());
80         ParameterService.register(contextParameters.getLockManagerParameters());
81         ParameterService.register(contextParameters.getPersistorParameters());
82         
83         schemaParameters = new SchemaParameters();
84         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
85         schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
86
87         ParameterService.register(schemaParameters);
88     }
89
90     @After
91     public void afterTest() {
92         ParameterService.deregister(schemaParameters);
93
94         ParameterService.deregister(contextParameters.getDistributorParameters());
95         ParameterService.deregister(contextParameters.getLockManagerParameters());
96         ParameterService.deregister(contextParameters.getPersistorParameters());
97         ParameterService.deregister(contextParameters);
98     }
99
100     @Test
101     public void testConcurrentContextJVMLocalVarSet() throws Exception {
102         logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
103
104         contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
105
106         final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalVarSet",
107                 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
108
109         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
110         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
111
112         assertFalse(result.isEmpty());
113         final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
114         final TestContextLongItem actual = result.get(TEST_VALUE);
115         assertNotNull(actual);
116         assertEquals(expected, actual.getLongValue());
117
118
119         logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
120     }
121
122     @Test
123     public void testConcurrentContextJVMLocalNoVarSet() throws Exception {
124         logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
125
126         final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalNoVarSet",
127                 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
128
129         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
130         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
131
132         assertFalse(result.isEmpty());
133         final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
134         final TestContextLongItem actual = result.get(TEST_VALUE);
135         assertNotNull(actual);
136         assertEquals(expected, actual.getLongValue());
137
138         logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
139     }
140
141     @Test
142     public void testConcurrentContextMultiJVMNoLock() throws Exception {
143         logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
144
145         contextParameters.getDistributorParameters().setPluginClass(JVMLocalDistributor.class.getCanonicalName());
146         contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
147
148         final ConfigrationProvider configrationProvider = getConfigrationProvider("testConcurrentContextMultiJVMNoLock",
149                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
150
151         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
152         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
153
154         // No concurrent map so result will be zero
155         assertFalse(result.isEmpty());
156         final TestContextLongItem actual = result.get(TEST_VALUE);
157         assertNotNull(actual);
158         assertEquals(0, actual.getLongValue());
159
160         logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
161     }
162
163     private ConfigrationProvider getConfigrationProvider(final String testType, final int jvmCount,
164             final int threadCount, final int threadLoops) {
165         return new ConfigrationProviderImpl(testType, jvmCount, threadCount, threadLoops, ALBUM_SIZE,
166                 WRITE_LOCK_SINGLE_VALUE_UPDATE.getValue()) {
167             @Override
168             public Map<String, Object> getContextAlbumInitValues() {
169                 final Map<String, Object> initValues = super.getContextAlbumInitValues();
170                 initValues.put(Constants.TEST_VALUE, new TestContextLongItem(0L));
171                 return initValues;
172             }
173         };
174     }
175 }