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.Test;
32 import org.onap.policy.apex.context.impl.distribution.jvmlocal.JVMLocalDistributor;
33 import org.onap.policy.apex.context.impl.locking.jvmlocal.JVMLocalLockManager;
34 import org.onap.policy.apex.context.parameters.ContextParameters;
35 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
36 import org.onap.policy.apex.context.test.utils.ConfigrationProvider;
37 import org.onap.policy.apex.context.test.utils.ConfigrationProviderImpl;
38 import org.onap.policy.apex.context.test.utils.Constants;
39 import org.slf4j.ext.XLogger;
40 import org.slf4j.ext.XLoggerFactory;
43 * The Class TestConcurrentContext tests concurrent use of context.
45 * @author Liam Fallon (liam.fallon@ericsson.com)
47 public class TestConcurrentContext {
49 // Logger for this class
50 private static final XLogger logger = XLoggerFactory.getXLogger(TestConcurrentContext.class);
53 private static final int ALBUM_SIZE = 16;
54 private static final int TEST_JVM_COUNT_SINGLE_JVM = 1;
55 private static final int TEST_JVM_COUNT_MULTI_JVM = 3;
56 private static final int TEST_THREAD_COUNT_SINGLE_JVM = 64;
57 private static final int TEST_THREAD_COUNT_MULTI_JVM = 20;
58 private static final int TEST_THREAD_LOOPS = 100;
61 public void testConcurrentContextJVMLocalVarSet() throws Exception {
62 logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
64 final ContextParameters contextParameters = new ContextParameters();
65 contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
67 final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalVarSet",
68 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
70 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
71 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
73 assertFalse(result.isEmpty());
74 final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
75 final TestContextLongItem actual = result.get(TEST_VALUE);
76 assertNotNull(actual);
77 assertEquals(expected, actual.getLongValue());
80 logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
84 public void testConcurrentContextJVMLocalNoVarSet() throws Exception {
85 logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
87 new ContextParameters();
88 final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalNoVarSet",
89 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
91 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
92 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
94 assertFalse(result.isEmpty());
95 final int expected = TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS;
96 final TestContextLongItem actual = result.get(TEST_VALUE);
97 assertNotNull(actual);
98 assertEquals(expected, actual.getLongValue());
100 logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
104 public void testConcurrentContextMultiJVMNoLock() throws Exception {
105 logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
107 final ContextParameters contextParameters = new ContextParameters();
108 contextParameters.getDistributorParameters().setPluginClass(JVMLocalDistributor.class.getCanonicalName());
109 contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
111 final ConfigrationProvider configrationProvider = getConfigrationProvider("testConcurrentContextMultiJVMNoLock",
112 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
114 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
115 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
117 // No concurrent map so result will be zero
118 assertFalse(result.isEmpty());
119 final TestContextLongItem actual = result.get(TEST_VALUE);
120 assertNotNull(actual);
121 assertEquals(0, actual.getLongValue());
123 logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
126 private ConfigrationProvider getConfigrationProvider(final String testType, final int jvmCount,
127 final int threadCount, final int threadLoops) {
128 return new ConfigrationProviderImpl(testType, jvmCount, threadCount, threadLoops, ALBUM_SIZE,
129 WRITE_LOCK_SINGLE_VALUE_UPDATE.getValue()) {
131 public Map<String, Object> getContextAlbumInitValues() {
132 final Map<String, Object> initValues = super.getContextAlbumInitValues();
133 initValues.put(Constants.TEST_VALUE, new TestContextLongItem(0L));