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;
26 import java.io.IOException;
28 import org.junit.Test;
29 import org.onap.policy.apex.context.parameters.ContextParameters;
30 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
31 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
36 * The Class TestConcurrentContext tests concurrent use of context.
38 * @author Liam Fallon (liam.fallon@ericsson.com)
40 public class TestConcurrentContext {
41 // Logger for this class
42 private static final XLogger logger = XLoggerFactory.getXLogger(TestConcurrentContext.class);
45 private static final int TEST_JVM_COUNT_SINGLE_JVM = 1;
46 private static final int TEST_JVM_COUNT_MULTI_JVM = 3;
47 private static final int TEST_THREAD_COUNT_SINGLE_JVM = 64;
48 private static final int TEST_THREAD_COUNT_MULTI_JVM = 20;
49 private static final int TEST_THREAD_LOOPS = 100;
52 public void testConcurrentContextJVMLocalVarSet() throws ApexModelException, IOException, ApexException {
53 logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
55 final ContextParameters contextParameters = new ContextParameters();
56 contextParameters.getLockManagerParameters()
57 .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JVMLocalLockManager");
58 final long result = new ConcurrentContext().testConcurrentContext("JVMLocalVarSet", TEST_JVM_COUNT_SINGLE_JVM,
59 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
61 assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
63 logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
67 public void testConcurrentContextJVMLocalNoVarSet() throws ApexModelException, IOException, ApexException {
68 logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
70 new ContextParameters();
71 final long result = new ConcurrentContext().testConcurrentContext("JVMLocalNoVarSet", TEST_JVM_COUNT_SINGLE_JVM,
72 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
74 assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
76 logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
80 public void testConcurrentContextMultiJVMNoLock() throws ApexModelException, IOException, ApexException {
81 logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
83 final ContextParameters contextParameters = new ContextParameters();
84 contextParameters.getDistributorParameters()
85 .setPluginClass("org.onap.policy.apex.context.impl.distribution.jvmlocal.JVMLocalDistributor");
86 contextParameters.getLockManagerParameters()
87 .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JVMLocalLockManager");
89 final long result = new ConcurrentContext().testConcurrentContext("testConcurrentContextMultiJVMNoLock",
90 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
92 // No concurrent map so result will be zero
93 assertEquals(0, result);
95 logger.debug("Ran testConcurrentContextMultiJVMNoLock test");