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.impl.distribution.jvmlocal.JVMLocalDistributor;
30 import org.onap.policy.apex.context.impl.locking.jvmlocal.JVMLocalLockManager;
31 import org.onap.policy.apex.context.parameters.ContextParameters;
32 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
33 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
34 import org.slf4j.ext.XLogger;
35 import org.slf4j.ext.XLoggerFactory;
38 * The Class TestConcurrentContext tests concurrent use of context.
40 * @author Liam Fallon (liam.fallon@ericsson.com)
42 public class TestConcurrentContext {
43 // Logger for this class
44 private static final XLogger logger = XLoggerFactory.getXLogger(TestConcurrentContext.class);
47 private static final int TEST_JVM_COUNT_SINGLE_JVM = 1;
48 private static final int TEST_JVM_COUNT_MULTI_JVM = 3;
49 private static final int TEST_THREAD_COUNT_SINGLE_JVM = 64;
50 private static final int TEST_THREAD_COUNT_MULTI_JVM = 20;
51 private static final int TEST_THREAD_LOOPS = 100;
54 public void testConcurrentContextJVMLocalVarSet() throws ApexModelException, IOException, ApexException {
55 logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
57 final ContextParameters contextParameters = new ContextParameters();
58 contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
59 final long result = new ConcurrentContext().testConcurrentContext("JVMLocalVarSet", TEST_JVM_COUNT_SINGLE_JVM,
60 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
62 assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
64 logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
68 public void testConcurrentContextJVMLocalNoVarSet() throws ApexModelException, IOException, ApexException {
69 logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
71 new ContextParameters();
72 final long result = new ConcurrentContext().testConcurrentContext("JVMLocalNoVarSet", TEST_JVM_COUNT_SINGLE_JVM,
73 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
75 assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
77 logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
81 public void testConcurrentContextMultiJVMNoLock() throws ApexModelException, IOException, ApexException {
82 logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
84 final ContextParameters contextParameters = new ContextParameters();
85 contextParameters.getDistributorParameters().setPluginClass(JVMLocalDistributor.class.getCanonicalName());
86 contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
88 final long result = new ConcurrentContext().testConcurrentContext("testConcurrentContextMultiJVMNoLock",
89 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
91 // No concurrent map so result will be zero
92 assertEquals(0, result);
94 logger.debug("Ran testConcurrentContextMultiJVMNoLock test");