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.IOException;
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.model.basicmodel.concepts.ApexException;
34 import org.onap.policy.apex.model.basicmodel.service.AbstractParameters;
35 import org.onap.policy.apex.model.basicmodel.service.ParameterService;
36 import org.slf4j.ext.XLogger;
37 import org.slf4j.ext.XLoggerFactory;
40 * The Class TestConcurrentContextThread tests concurrent use of context.
42 * @author Liam Fallon (liam.fallon@ericsson.com)
44 public class ConcurrentContextJVMThread implements Runnable {
45 // Logger for this class
46 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextJVMThread.class);
48 private final String testType;
49 private final int jvm;
50 private final int threadCount;
51 private final int target;
56 * @param testType the test type
58 * @param threadCount the thread count
59 * @param target the target
60 * @throws ApexException the apex exception
62 public ConcurrentContextJVMThread(final String testType, final int jvm, final int threadCount, final int target)
63 throws ApexException {
64 this.testType = testType;
66 this.threadCount = threadCount;
73 * @see java.lang.Runnable#run()
77 final List<String> commandList = new ArrayList<>();
78 commandList.add(System.getProperty("java.home") + System.getProperty("file.separator") + "bin"
79 + System.getProperty("file.separator") + "java");
80 commandList.add("-cp");
81 commandList.add(System.getProperty("java.class.path"));
82 commandList.add("org.onap.policy.apex.context.test.locking.ConcurrentContextJVM");
83 commandList.add(testType);
84 commandList.add(new Integer(jvm).toString());
85 commandList.add(new Integer(threadCount).toString());
86 commandList.add(new Integer(target).toString());
88 for (final Entry<Class<?>, AbstractParameters> parameterServiceEntry : ParameterService.getAll()) {
89 commandList.add(parameterServiceEntry.getKey().getCanonicalName());
90 commandList.add(new Gson().toJson(parameterServiceEntry.getValue()));
93 LOGGER.info("starting JVM " + jvm);
96 final ProcessBuilder processBuilder = new ProcessBuilder(commandList);
97 processBuilder.redirectErrorStream(true);
101 process = processBuilder.start();
103 final InputStream is = process.getInputStream();
104 final InputStreamReader isr = new InputStreamReader(is);
105 final BufferedReader br = new BufferedReader(isr);
107 LOGGER.info("JVM Output for command " + commandList + "\n");
108 while ((line = br.readLine()) != null) {
112 // Wait to get exit value
114 final int exitValue = process.waitFor();
115 LOGGER.info("\n\nJVM " + jvm + " finished, exit value is " + exitValue);
116 } catch (final InterruptedException e) {
117 LOGGER.warn("Thread was interrupted");
118 Thread.currentThread().interrupt();
120 } catch (final IOException ioException) {
121 LOGGER.error("Error occured while writing JVM Output for command ", ioException);