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 com.google.gson.Gson;
25 import java.io.BufferedReader;
26 import java.io.Closeable;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Map.Entry;
33 import org.onap.policy.apex.context.test.utils.ConfigrationProvider;
34 import org.onap.policy.common.parameters.ParameterGroup;
35 import org.onap.policy.common.parameters.ParameterService;
36 import org.slf4j.ext.XLogger;
37 import org.slf4j.ext.XLoggerFactory;
42 * The Class TestConcurrentContextThread tests concurrent use of context.
44 * @author Liam Fallon (liam.fallon@ericsson.com)
46 public class ConcurrentContextJvmThread implements Runnable, Closeable {
47 // Logger for this class
48 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextJvmThread.class);
50 private final int jvm;
51 private final ConfigrationProvider configrationProvider;
52 private Process process = null;
54 public ConcurrentContextJvmThread(final int jvm, final ConfigrationProvider configrationProvider) {
56 this.configrationProvider = configrationProvider;
62 * @see java.lang.Runnable#run()
66 final List<String> commandList = new ArrayList<>();
67 commandList.add(System.getProperty("java.home") + System.getProperty("file.separator") + "bin"
68 + System.getProperty("file.separator") + "java");
69 commandList.add("-Xms512m");
70 commandList.add("-Xmx512m");
71 commandList.add("-cp");
72 commandList.add(System.getProperty("java.class.path"));
73 commandList.add(ConcurrentContextJvm.class.getCanonicalName());
74 commandList.add(configrationProvider.getTestName());
75 commandList.add(Integer.toString(jvm));
76 commandList.add(Integer.toString(configrationProvider.getThreadCount()));
77 commandList.add(Integer.toString(configrationProvider.getLoopSize()));
78 commandList.add(Integer.toString(configrationProvider.getAlbumSize()));
79 commandList.add(Integer.toString(configrationProvider.getLockType().getValue()));
80 commandList.add(System.getProperty("hazelcast.config", ""));
82 for (final Entry<String, ParameterGroup> parameterServiceEntry : ParameterService.getAll()) {
83 commandList.add(parameterServiceEntry.getValue().getClass().getCanonicalName());
84 commandList.add(new Gson().toJson(parameterServiceEntry.getValue()));
87 LOGGER.info("starting JVM " + jvm);
90 final ProcessBuilder processBuilder = new ProcessBuilder(commandList);
91 processBuilder.redirectErrorStream(true);
94 process = processBuilder.start();
96 final InputStream is = process.getInputStream();
97 final InputStreamReader isr = new InputStreamReader(is);
98 final BufferedReader br = new BufferedReader(isr);
100 LOGGER.info("JVM Output for command " + commandList + "\n");
101 while ((line = br.readLine()) != null) {
107 } catch (final Exception ioException) {
108 LOGGER.error("Error occured while writing JVM Output for command ", ioException);
113 * Wait for an exit value from the the JVM.
115 private void waitForExitValue() {
116 // Wait to get exit value
118 final int exitValue = process.waitFor();
119 LOGGER.info("\n\nJVM " + jvm + " finished, exit value is " + exitValue);
120 } catch (final InterruptedException e) {
121 LOGGER.warn("Thread was interrupted");
122 Thread.currentThread().interrupt();
128 public void close() {
129 LOGGER.info("Shutting down {} thread ...", Thread.currentThread().getName());
130 if (process != null) {
131 LOGGER.info("Destroying process ...");