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