c29353cafa705b37daadf4f3ff2d9d3292106c43
[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
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;
29
30 import java.util.Map;
31
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;
42
43 /**
44  * The Class TestConcurrentContext tests concurrent use of context.
45  *
46  * @author Liam Fallon (liam.fallon@ericsson.com)
47  */
48 public class TestConcurrentContext {
49
50     // Logger for this class
51     private static final XLogger logger = XLoggerFactory.getXLogger(TestConcurrentContext.class);
52
53     // Test parameters
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;
60
61     @Test
62     public void testConcurrentContextJVMLocalVarSet() throws Exception {
63         logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
64
65         final ContextParameters contextParameters = new ContextParameters();
66         contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
67
68         final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalVarSet",
69                 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
70
71         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
72         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
73
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());
79
80
81         logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
82     }
83
84     @Test
85     public void testConcurrentContextJVMLocalNoVarSet() throws Exception {
86         logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
87
88         new ContextParameters();
89         final ConfigrationProvider configrationProvider = getConfigrationProvider("JVMLocalNoVarSet",
90                 TEST_JVM_COUNT_SINGLE_JVM, TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
91
92         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
93         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
94
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());
100
101         logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
102     }
103
104     @Test
105     public void testConcurrentContextMultiJVMNoLock() throws Exception {
106         logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
107
108         final ContextParameters contextParameters = new ContextParameters();
109         contextParameters.getDistributorParameters().setPluginClass(JVMLocalDistributor.class.getCanonicalName());
110         contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
111
112         final ConfigrationProvider configrationProvider = getConfigrationProvider("testConcurrentContextMultiJVMNoLock",
113                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
114
115         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
116         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
117
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());
123
124         logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
125     }
126
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()) {
131             @Override
132             public Map<String, Object> getContextAlbumInitValues() {
133                 final Map<String, Object> initValues = super.getContextAlbumInitValues();
134                 initValues.put(Constants.TEST_VALUE, new TestContextLongItem(0l));
135                 return initValues;
136             }
137         };
138     }
139 }