Changes for checkstyle 8.32
[policy/apex-pdp.git] / testsuites / integration / integration-context-test / src / test / java / org / onap / policy / apex / testsuites / integration / context / locking / ConcurrentContextJvmThread.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.integration.context.locking;
23
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;
37
38
39
40 /**
41  * The Class TestConcurrentContextThread tests concurrent use of context.
42  *
43  * @author Liam Fallon (liam.fallon@ericsson.com)
44  */
45 public class ConcurrentContextJvmThread implements Runnable, Closeable {
46     // Logger for this class
47     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextJvmThread.class);
48
49     private final int jvm;
50     private final ConfigrationProvider configrationProvider;
51     private Process process = null;
52
53     public ConcurrentContextJvmThread(final int jvm, final ConfigrationProvider configrationProvider) {
54         this.jvm = jvm;
55         this.configrationProvider = configrationProvider;
56     }
57
58     /**
59      * {@inheritDoc}.
60      */
61     @Override
62     public void run() {
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", ""));
78
79         for (final Entry<String, ParameterGroup> parameterServiceEntry : ParameterService.getAll()) {
80             commandList.add(parameterServiceEntry.getValue().getClass().getName());
81             commandList.add(new Gson().toJson(parameterServiceEntry.getValue()));
82         }
83
84         LOGGER.info("starting JVM " + jvm);
85
86         // Run the JVM
87         final ProcessBuilder processBuilder = new ProcessBuilder(commandList);
88         processBuilder.redirectErrorStream(true);
89
90         try {
91             process = processBuilder.start();
92
93             final InputStream is = process.getInputStream();
94             final InputStreamReader isr = new InputStreamReader(is);
95             final BufferedReader br = new BufferedReader(isr);
96             String line;
97             LOGGER.info("JVM Output for command " + commandList + "\n");
98             while ((line = br.readLine()) != null) {
99                 LOGGER.info(line);
100             }
101
102             waitForExitValue();
103
104         } catch (final Exception ioException) {
105             LOGGER.error("Error occured while writing JVM Output for command ", ioException);
106         }
107     }
108
109     /**
110      * Wait for an exit value from the the JVM.
111      */
112     private void waitForExitValue() {
113         // Wait to get exit value
114         try {
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();
120         }
121     }
122
123
124     @Override
125     public void close() {
126         LOGGER.info("Shutting down {} thread ...", Thread.currentThread().getName());
127         if (process != null) {
128             LOGGER.info("Destroying process ...");
129             process.destroy();
130         }
131     }
132 }