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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.context.test.locking;
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;
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;
49 * The Class TestConcurrentContext tests concurrent use of context.
51 * @author Liam Fallon (liam.fallon@ericsson.com)
53 public class ConcurrentContextTest {
55 // Logger for this class
56 private static final XLogger logger = XLoggerFactory.getXLogger(ConcurrentContextTest.class);
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;
66 private SchemaParameters schemaParameters;
67 private ContextParameters contextParameters;
70 * Set up context for tests.
73 public void beforeTest() {
74 contextParameters = new ContextParameters();
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);
81 ParameterService.register(contextParameters);
82 ParameterService.register(contextParameters.getDistributorParameters());
83 ParameterService.register(contextParameters.getLockManagerParameters());
84 ParameterService.register(contextParameters.getPersistorParameters());
86 schemaParameters = new SchemaParameters();
87 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
88 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
90 ParameterService.register(schemaParameters);
94 * Clear down context for tests.
97 public void afterTest() {
98 ParameterService.deregister(schemaParameters);
100 ParameterService.deregister(contextParameters.getDistributorParameters());
101 ParameterService.deregister(contextParameters.getLockManagerParameters());
102 ParameterService.deregister(contextParameters.getPersistorParameters());
103 ParameterService.deregister(contextParameters);
107 public void testConcurrentContextJvmLocalVarSet() throws Exception {
108 logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
110 contextParameters.getLockManagerParameters().setPluginClass(JvmLocalLockManager.class.getCanonicalName());
112 final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalVarSet",
113 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
115 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
116 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
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());
125 logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
129 public void testConcurrentContextJvmLocalNoVarSet() throws Exception {
130 logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
132 final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalNoVarSet",
133 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
135 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
136 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
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());
144 logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
148 public void testConcurrentContextMultiJvmNoLock() throws Exception {
149 logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
151 contextParameters.getDistributorParameters().setPluginClass(JvmLocalDistributor.class.getCanonicalName());
152 contextParameters.getLockManagerParameters().setPluginClass(JvmLocalLockManager.class.getCanonicalName());
154 final ConfigrationProvider configrationProvider = getConfigrationProvider("testConcurrentContextMultiJVMNoLock",
155 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
157 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
158 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
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());
166 logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
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()) {
174 public Map<String, Object> getContextAlbumInitValues() {
175 final Map<String, Object> initValues = super.getContextAlbumInitValues();
176 initValues.put(Constants.TEST_VALUE, new TestContextLongItem(0L));