2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.testsuites.integration.context.locking;
24 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;
32 import org.onap.policy.apex.testsuites.integration.context.utils.ConfigrationProvider;
33 import org.onap.policy.common.parameters.ParameterGroup;
34 import org.onap.policy.common.parameters.ParameterService;
35 import org.slf4j.ext.XLogger;
36 import org.slf4j.ext.XLoggerFactory;
41 * The Class TestConcurrentContextThread tests concurrent use of context.
43 * @author Liam Fallon (liam.fallon@ericsson.com)
45 public class ConcurrentContextJvmThread implements Runnable, Closeable {
46 // Logger for this class
47 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextJvmThread.class);
49 private final int jvm;
50 private final ConfigrationProvider configrationProvider;
51 private Process process = null;
53 public ConcurrentContextJvmThread(final int jvm, final ConfigrationProvider configrationProvider) {
55 this.configrationProvider = configrationProvider;
63 final List<String> commandList = new ArrayList<>();
64 commandList.add(System.getProperty("java.home") + System.getProperty("file.separator") + "bin"
65 + System.getProperty("file.separator") + "java");
66 commandList.add("-Xms512m");
67 commandList.add("-Xmx512m");
68 commandList.add("-cp");
69 commandList.add(System.getProperty("java.class.path"));
70 commandList.add(ConcurrentContextJvm.class.getName());
71 commandList.add(configrationProvider.getTestName());
72 commandList.add(Integer.toString(jvm));
73 commandList.add(Integer.toString(configrationProvider.getThreadCount()));
74 commandList.add(Integer.toString(configrationProvider.getLoopSize()));
75 commandList.add(Integer.toString(configrationProvider.getAlbumSize()));
76 commandList.add(Integer.toString(configrationProvider.getLockType().getValue()));
77 commandList.add(System.getProperty("hazelcast.config", ""));
79 for (final Entry<String, ParameterGroup> parameterServiceEntry : ParameterService.getAll()) {
80 commandList.add(parameterServiceEntry.getValue().getClass().getName());
81 commandList.add(new Gson().toJson(parameterServiceEntry.getValue()));
84 LOGGER.info("starting JVM " + jvm);
87 final ProcessBuilder processBuilder = new ProcessBuilder(commandList);
88 processBuilder.redirectErrorStream(true);
91 process = processBuilder.start();
93 final InputStream is = process.getInputStream();
94 final InputStreamReader isr = new InputStreamReader(is);
95 final BufferedReader br = new BufferedReader(isr);
97 LOGGER.info("JVM Output for command " + commandList + "\n");
98 while ((line = br.readLine()) != null) {
104 } catch (final Exception ioException) {
105 LOGGER.error("Error occured while writing JVM Output for command ", ioException);
110 * Wait for an exit value from the the JVM.
112 private void waitForExitValue() {
113 // Wait to get exit value
115 final int exitValue = process.waitFor();
116 LOGGER.info("\n\nJVM " + jvm + " finished, exit value is " + exitValue);
117 } catch (final InterruptedException e) {
118 LOGGER.warn("Thread was interrupted");
119 Thread.currentThread().interrupt();
125 public void close() {
126 LOGGER.info("Shutting down {} thread ...", Thread.currentThread().getName());
127 if (process != null) {
128 LOGGER.info("Destroying process ...");