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