6d6ea6a588e87bc81d83492bafa65301f39e364f
[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
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      * {@inheritDoc}.
62      */
63     @Override
64     public void run() {
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", ""));
80
81         for (final Entry<String, ParameterGroup> parameterServiceEntry : ParameterService.getAll()) {
82             commandList.add(parameterServiceEntry.getValue().getClass().getName());
83             commandList.add(new Gson().toJson(parameterServiceEntry.getValue()));
84         }
85
86         LOGGER.info("starting JVM " + jvm);
87
88         // Run the JVM
89         final ProcessBuilder processBuilder = new ProcessBuilder(commandList);
90         processBuilder.redirectErrorStream(true);
91
92         try {
93             process = processBuilder.start();
94
95             final InputStream is = process.getInputStream();
96             final InputStreamReader isr = new InputStreamReader(is);
97             final BufferedReader br = new BufferedReader(isr);
98             String line;
99             LOGGER.info("JVM Output for command " + commandList + "\n");
100             while ((line = br.readLine()) != null) {
101                 LOGGER.info(line);
102             }
103
104             waitForExitValue();
105
106         } catch (final Exception ioException) {
107             LOGGER.error("Error occured while writing JVM Output for command ", ioException);
108         }
109     }
110
111     /**
112      * Wait for an exit value from the the JVM.
113      */
114     private void waitForExitValue() {
115         // Wait to get exit value
116         try {
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();
122         }
123     }
124
125
126     @Override
127     public void close() {
128         LOGGER.info("Shutting down {} thread ...", Thread.currentThread().getName());
129         if (process != null) {
130             LOGGER.info("Destroying process ...");
131             process.destroy();
132         }
133     }
134 }