How to use killProcess method of org.evomaster.client.java.controller.ExternalSutController class

Best EvoMaster code snippet using org.evomaster.client.java.controller.ExternalSutController.killProcess

Source:ExternalSutController.java Github

copy

Full Screen

...106 /*107 the following thread is important to make sure that the external process is killed108 when current process ends109 */110 processKillHook = new Thread(() -> killProcess());111 Runtime.getRuntime().addShutdownHook(processKillHook);112 //we need a mechanism to wait until the SUT is ready113 latch = new CountDownLatch(1);114 List<String> command = new ArrayList<>();115 command.add("java");116 if (instrumentation) {117 if (serverController == null) {118 serverController = new ServerController();119 }120 int port = serverController.startServer();121 command.add("-D" + InputProperties.EXTERNAL_PORT_PROP + "=" + port);122 String driver = getDatabaseDriverName();123 if (driver != null && !driver.isEmpty()) {124 command.add("-D" + InputProperties.SQL_DRIVER + "=" + driver);125 }126 String jarPath = JarAgentLocator.getAgentJarPath();127 if (jarPath == null) {128 throw new IllegalStateException("Cannot locate JAR file with EvoMaster Java Agent");129 }130 command.add("-javaagent:" + jarPath + "=" + getPackagePrefixesToCover());131 }132 for (String s : getJVMParameters()) {133 if (s != null) {134 String token = s.trim();135 if (!token.isEmpty()) {136 command.add(token);137 }138 }139 }140 if (command.stream().noneMatch(s -> s.startsWith("-Xmx"))) {141 command.add("-Xmx2G");142 }143 if (command.stream().noneMatch(s -> s.startsWith("-Xms"))) {144 command.add("-Xms1G");145 }146 command.add("-jar");147 command.add(getPathToExecutableJar());148 for (String s : getInputParameters()) {149 if (s != null) {150 String token = s.trim();151 if (!token.isEmpty()) {152 command.add(token);153 }154 }155 }156 SimpleLogger.info("Going to start SUT with command:\n" + String.join(" ", command));157 // now start the process158 ProcessBuilder builder = new ProcessBuilder(command);159 builder.redirectErrorStream(true);160 try {161 process = builder.start();162 } catch (IOException e) {163 SimpleLogger.error("Failed to start external process", e);164 return null;165 }166 //this is not only needed for debugging, but also to check for when SUT is ready167 startExternalProcessPrinter();168 if (instrumentation && serverController != null) {169 boolean connected = serverController.waitForIncomingConnection();170 if (!connected) {171 SimpleLogger.error("Could not establish connection to retrieve code metrics");172 return null;173 }174 }175 //need to block until server is ready176 long timeout = getMaxAwaitForInitializationInSeconds();177 boolean completed;178 try {179 completed = latch.await(timeout, TimeUnit.SECONDS);180 } catch (InterruptedException e) {181 SimpleLogger.error("Interrupted controller");182 stopSut();183 return null;184 }185 if(! completed){186 SimpleLogger.error("SUT has not started properly within " + timeout + " seconds");187 if(errorBuffer != null) {188 SimpleLogger.error("SUT output:\n" + errorBuffer.toString());189 }190 stopSut();191 return null;192 }193 if (!isSutRunning()) {194 SimpleLogger.error("SUT started but then terminated. Likely a possible misconfiguration");195 if(errorBuffer != null) {196 SimpleLogger.error("SUT output:\n" + errorBuffer.toString());197 }198 //note: actual process might still be running due to Java Agent we started199 stopSut();200 return null;201 }202 if (!initialized) {203 //this could happen if SUT is hanging for some reason204 SimpleLogger.error("SUT is started but not initialized");205 if(errorBuffer != null) {206 SimpleLogger.error("SUT output:\n" + errorBuffer.toString());207 }208 //note: actual process might still be running due to Java Agent we started209 stopSut();210 return null;211 }212 postStart();213 return getBaseURL();214 }215 @Override216 public boolean isSutRunning() {217 return process != null && process.isAlive();218 }219 @Override220 public void stopSut() {221 SimpleLogger.info("Going to stop the SUT");222 preStop();223 if (serverController != null) {224 serverController.closeServer();225 }226 killProcess();227 initialized = false;228 postStop();229 }230 @Override231 public final boolean isInstrumentationActivated() {232 return instrumentation && serverController != null && serverController.isConnectionOn();233 }234 @Override235 public final void newSearch() {236 if (isInstrumentationActivated()) {237 serverController.resetForNewSearch();238 }239 }240 @Override241 public final void newTestSpecificHandler() {242 if (isInstrumentationActivated()) {243 serverController.resetForNewTest();244 }245 }246 @Override247 public final List<TargetInfo> getTargetInfos(Collection<Integer> ids) {248 checkInstrumentation();249 return serverController.getTargetsInfo(ids);250 }251 @Override252 public final List<AdditionalInfo> getAdditionalInfoList(){253 checkInstrumentation();254 return serverController.getAdditionalInfoList();255 }256 @Override257 public final void newActionSpecificHandler(ActionDto dto) {258 if (isInstrumentationActivated()) {259 serverController.setAction(new Action(dto.index, dto.inputVariables));260 }261 }262 @Override263 public final UnitsInfoDto getUnitsInfoDto(){264 if(!isInstrumentationActivated()){265 return null;266 }267 return getUnitsInfoDto(serverController.getUnitsInfoRecorder());268 }269 //-----------------------------------------270 private void checkInstrumentation() {271 if (!isInstrumentationActivated()) {272 throw new IllegalStateException("Instrumentation is not active");273 }274 }275 private void validateJarPath() {276 String path = getPathToExecutableJar();277 if (!path.endsWith(".jar")) {278 throw new IllegalStateException("Invalid jar path does not end with '.jar': " + path);279 }280 if (!Files.exists(Paths.get(path))) {281 throw new IllegalArgumentException("File does not exist: " + path);282 }283 }284 private void killProcess() {285 try {286 Runtime.getRuntime().removeShutdownHook(processKillHook);287 } catch (Exception e) {288 /* do nothing. this can happen if shutdown is in progress */289 }290 if (process != null) {291 try {292 //be sure streamers are closed, otherwise process might hang on Windows293 process.getOutputStream().close();294 process.getInputStream().close();295 process.getErrorStream().close();296 } catch (Exception t) {297 SimpleLogger.error("Failed to close process stream: " + t.toString());298 }...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful