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;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.onap.policy.apex.context.test.lock.modifier.LockType.WRITE_LOCK_SINGLE_VALUE_UPDATE;
28 import static org.onap.policy.apex.context.test.utils.Constants.TEST_VALUE;
32 import org.junit.Test;
33 import org.onap.policy.apex.context.impl.distribution.jvmlocal.JVMLocalDistributor;
34 import org.onap.policy.apex.context.impl.locking.jvmlocal.JVMLocalLockManager;
35 import org.onap.policy.apex.context.parameters.ContextParameters;
36 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
37 import org.onap.policy.apex.context.test.utils.ConfigrationProvider;
38 import org.onap.policy.apex.context.test.utils.ConfigrationProviderImpl;
39 import org.onap.policy.apex.context.test.utils.Constants;
40 import org.slf4j.ext.XLogger;
41 import org.slf4j.ext.XLoggerFactory;
44 * The Class TestConcurrentContext tests concurrent use of context.
46 * @author Liam Fallon (liam.fallon@ericsson.com)
48 public class TestConcurrentContext {
50 // Logger for this class
51 private static final XLogger logger = XLoggerFactory.getXLogger(TestConcurrentContext.class);
54 private static final int ALBUM_SIZE = 16;
55 private static final int TEST_JVM_COUNT_SINGLE_JVM = 1;
56 private static final int TEST_JVM_COUNT_MULTI_JVM = 3;
57 private static final int TEST_THREAD_COUNT_SINGLE_JVM = 64;
58 private static final int TEST_THREAD_COUNT_MULTI_JVM = 20;
59 private static final int TEST_THREAD_LOOPS = 100;
62 public void testConcurrentContextJVMLocalVarSet() throws Exception {
63 logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
65 final ContextParameters contextParameters = new ContextParameters();
66 contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
68 final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalVarSet",
69 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
71 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
72 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
74 assertFalse(result.isEmpty());
75 final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
76 final TestContextLongItem actual = result.get(TEST_VALUE);
77 assertNotNull(actual);
78 assertEquals(expected, actual.getLongValue());
81 logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
85 public void testConcurrentContextJVMLocalNoVarSet() throws Exception {
86 logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
88 new ContextParameters();
89 final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalNoVarSet",
90 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
92 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
93 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
95 assertFalse(result.isEmpty());
96 final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
97 final TestContextLongItem actual = result.get(TEST_VALUE);
98 assertNotNull(actual);
99 assertEquals(expected, actual.getLongValue());
101 logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
105 public void testConcurrentContextMultiJVMNoLock() throws Exception {
106 logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
108 final ContextParameters contextParameters = new ContextParameters();
109 contextParameters.getDistributorParameters().setPluginClass(JVMLocalDistributor.class.getCanonicalName());
110 contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
112 final ConfigrationProvider configrationProvider = getConfigrationProvider("testConcurrentContextMultiJVMNoLock",
113 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
115 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
116 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
118 // No concurrent map so result will be zero
119 assertFalse(result.isEmpty());
120 final TestContextLongItem actual = result.get(TEST_VALUE);
121 assertNotNull(actual);
122 assertEquals(0, actual.getLongValue());
124 logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
127 private ConfigrationProvider getConfigrationProvider(final String testType, final int jvmCount,
128 final int threadCount, final int threadLoops) {
129 return new ConfigrationProviderImpl(testType, jvmCount, threadCount, threadLoops, ALBUM_SIZE,
130 WRITE_LOCK_SINGLE_VALUE_UPDATE.getValue()) {
132 public Map<String, Object> getContextAlbumInitValues() {
133 final Map<String, Object> initValues = super.getContextAlbumInitValues();
134 initValues.put(Constants.TEST_VALUE, new TestContextLongItem(0l));