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 TestConcurrentContext {
55 // Logger for this class
56 private static final XLogger logger = XLoggerFactory.getXLogger(TestConcurrentContext.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 public void beforeTest() {
71 contextParameters = new ContextParameters();
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);
78 ParameterService.register(contextParameters);
79 ParameterService.register(contextParameters.getDistributorParameters());
80 ParameterService.register(contextParameters.getLockManagerParameters());
81 ParameterService.register(contextParameters.getPersistorParameters());
83 schemaParameters = new SchemaParameters();
84 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
85 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
87 ParameterService.register(schemaParameters);
91 public void afterTest() {
92 ParameterService.deregister(schemaParameters);
94 ParameterService.deregister(contextParameters.getDistributorParameters());
95 ParameterService.deregister(contextParameters.getLockManagerParameters());
96 ParameterService.deregister(contextParameters.getPersistorParameters());
97 ParameterService.deregister(contextParameters);
101 public void testConcurrentContextJVMLocalVarSet() throws Exception {
102 logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
104 contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
106 final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalVarSet",
107 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
109 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
110 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
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());
119 logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
123 public void testConcurrentContextJVMLocalNoVarSet() throws Exception {
124 logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
126 final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalNoVarSet",
127 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
129 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
130 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
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());
138 logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
142 public void testConcurrentContextMultiJVMNoLock() throws Exception {
143 logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
145 contextParameters.getDistributorParameters().setPluginClass(JVMLocalDistributor.class.getCanonicalName());
146 contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
148 final ConfigrationProvider configrationProvider = getConfigrationProvider("testConcurrentContextMultiJVMNoLock",
149 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
151 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
152 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
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());
160 logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
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()) {
168 public Map<String, Object> getContextAlbumInitValues() {
169 final Map<String, Object> initValues = super.getContextAlbumInitValues();
170 initValues.put(Constants.TEST_VALUE, new TestContextLongItem(0L));