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