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 java.io.Closeable;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.Map.Entry;
30 import java.util.concurrent.ExecutorService;
31 import java.util.concurrent.TimeUnit;
33 import org.onap.policy.apex.context.ContextAlbum;
34 import org.onap.policy.apex.context.ContextException;
35 import org.onap.policy.apex.context.Distributor;
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.model.basicmodel.concepts.ApexException;
39 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
40 import org.slf4j.ext.XLogger;
41 import org.slf4j.ext.XLoggerFactory;
44 * The Class TestConcurrentContext tests concurrent use of context.
46 * @author Liam Fallon (liam.fallon@ericsson.com)
48 public class ConcurrentContext {
49 // Logger for this class
50 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContext.class);
52 // The context distributor and map used by each test
53 private Distributor contextDistributor = null;
54 private ContextAlbum ltypeAlbum = null;
56 private final ConfigrationProvider configrationProvider;
58 public ConcurrentContext(final ConfigrationProvider configrationProvider) {
59 this.configrationProvider = configrationProvider;
63 * The method tests concurrent use of context.
65 * @return the verified context
66 * @throws ApexModelException the exception occurs in model handling
67 * @throws ApexException the Apex exception occurs in handling Apex
69 public Map<String, TestContextLongItem> testConcurrentContext() throws ApexException {
72 setupAndVerifyContext();
73 } catch (final Exception exception) {
74 LOGGER.error("Error occured while setting up and verifying concurrent context", exception);
78 LOGGER.debug("starting JVMs and threads . . .");
80 final ExecutorService executorService = configrationProvider.getExecutorService();
82 final List<Closeable> tasks = new ArrayList<>(configrationProvider.getThreadCount());
84 addShutDownHook(tasks);
86 // Check if we have a single JVM or multiple JVMs
87 if (configrationProvider.getJvmCount() == 1) {
88 // Run everything in this JVM
89 for (int t = 0; t < configrationProvider.getThreadCount(); t++) {
90 final ConcurrentContextThread task = new ConcurrentContextThread(0, t, configrationProvider);
92 executorService.execute(task);
96 // Spawn JVMs to run the tests
97 for (int j = 0; j < configrationProvider.getJvmCount(); j++) {
98 final ConcurrentContextJvmThread task = new ConcurrentContextJvmThread(j, configrationProvider);
100 executorService.execute(task);
105 executorService.shutdown();
106 // wait for threads to finish, if not Timeout
107 executorService.awaitTermination(10, TimeUnit.MINUTES);
108 } catch (final InterruptedException interruptedException) {
109 LOGGER.error("Exception while waiting for threads to finish", interruptedException);
110 // restore the interrupt status
111 Thread.currentThread().interrupt();
114 LOGGER.info("Shutting down now ...");
115 executorService.shutdownNow();
117 return verifyAndClearContext();
121 private void addShutDownHook(final List<Closeable> tasks) {
122 Runtime.getRuntime().addShutdownHook(new Thread() {
125 LOGGER.info("Shutting down ...");
126 for (final Closeable task : tasks) {
129 } catch (final IOException ioException) {
130 LOGGER.error("Unable to close task ... ", ioException);
138 * Setup and verify context.
140 * @throws ContextException the context exception
142 private void setupAndVerifyContext() {
143 contextDistributor = configrationProvider.getDistributor();
144 ltypeAlbum = configrationProvider.getContextAlbum(contextDistributor);
145 final Map<String, Object> initValues = configrationProvider.getContextAlbumInitValues();
147 for (final Entry<String, Object> entry : initValues.entrySet()) {
148 ltypeAlbum.put(entry.getKey(), entry.getValue());
152 private Map<String, TestContextLongItem> verifyAndClearContext() throws ContextException {
153 final Map<String, TestContextLongItem> values = new HashMap<>();
156 for (final Entry<String, Object> entry : ltypeAlbum.entrySet()) {
157 values.put(entry.getKey(), (TestContextLongItem) entry.getValue());
159 } catch (final Exception exception) {
160 LOGGER.error("Error: ", exception);
162 contextDistributor.clear();
163 contextDistributor = null;