4915e7b89f8928e3a8710a48f27bb7d97115cec2
[policy/apex-pdp.git] /
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
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;
33
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;
39
40
41
42 /**
43  * The Class TestConcurrentContextThread tests concurrent use of context.
44  *
45  * @author Liam Fallon (liam.fallon@ericsson.com)
46  */
47 public class ConcurrentContextJvmThread implements Runnable, Closeable {
48     // Logger for this class
49     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextJvmThread.class);
50
51     private final int jvm;
52     private final ConfigrationProvider configrationProvider;
53     private Process process = null;
54
55     public ConcurrentContextJvmThread(final int jvm, final ConfigrationProvider configrationProvider) {
56         this.jvm = jvm;
57         this.configrationProvider = configrationProvider;
58     }
59
60     /*
61      * (non-Javadoc)
62      *
63      * @see java.lang.Runnable#run()
64      */
65     @Override
66     public void run() {
67         final List<String> commandList = new ArrayList<>();
68         commandList.add(System.getProperty("java.home") + System.getProperty("file.separator") + "bin"
69                 + System.getProperty("file.separator") + "java");
70         commandList.add("-Xms512m");
71         commandList.add("-Xmx512m");
72         commandList.add("-cp");
73         commandList.add(System.getProperty("java.class.path"));
74         commandList.add(ConcurrentContextJvm.class.getName());
75         commandList.add(configrationProvider.getTestName());
76         commandList.add(Integer.toString(jvm));
77         commandList.add(Integer.toString(configrationProvider.getThreadCount()));
78         commandList.add(Integer.toString(configrationProvider.getLoopSize()));
79         commandList.add(Integer.toString(configrationProvider.getAlbumSize()));
80         commandList.add(Integer.toString(configrationProvider.getLockType().getValue()));
81         commandList.add(System.getProperty("hazelcast.config", ""));
82
83         for (final Entry<String, ParameterGroup> parameterServiceEntry : ParameterService.getAll()) {
84             commandList.add(parameterServiceEntry.getValue().getClass().getName());
85             commandList.add(new Gson().toJson(parameterServiceEntry.getValue()));
86         }
87
88         LOGGER.info("starting JVM " + jvm);
89
90         // Run the JVM
91         final ProcessBuilder processBuilder = new ProcessBuilder(commandList);
92         processBuilder.redirectErrorStream(true);
93
94         try {
95             process = processBuilder.start();
96
97             final InputStream is = process.getInputStream();
98             final InputStreamReader isr = new InputStreamReader(is);
99             final BufferedReader br = new BufferedReader(isr);
100             String line;
101             LOGGER.info("JVM Output for command " + commandList + "\n");
102             while ((line = br.readLine()) != null) {
103                 LOGGER.info(line);
104             }
105
106             waitForExitValue();
107
108         } catch (final Exception ioException) {
109             LOGGER.error("Error occured while writing JVM Output for command ", ioException);
110         }
111     }
112
113     /**
114      * Wait for an exit value from the the JVM.
115      */
116     private void waitForExitValue() {
117         // Wait to get exit value
118         try {
119             final int exitValue = process.waitFor();
120             LOGGER.info("\n\nJVM " + jvm + " finished, exit value is " + exitValue);
121         } catch (final InterruptedException e) {
122             LOGGER.warn("Thread was interrupted");
123             Thread.currentThread().interrupt();
124         }
125     }
126
127
128     @Override
129     public void close() {
130         LOGGER.info("Shutting down {} thread ...", Thread.currentThread().getName());
131         if (process != null) {
132             LOGGER.info("Destroying process ...");
133             process.destroy();
134         }
135     }
136 }