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