Best EvoMaster code snippet using org.evomaster.client.java.controller.ExternalSutController.getWaitingSecondsForIncomingConnection
Source:ExternalSutController.java
...178 }179 //this is not only needed for debugging, but also to check for when SUT is ready180 startExternalProcessPrinter();181 if (instrumentation && serverController != null) {182 boolean connected = serverController.waitForIncomingConnection(getWaitingSecondsForIncomingConnection());183 if (!connected) {184 SimpleLogger.error("Could not establish connection to retrieve code metrics");185 if(errorBuffer != null) {186 SimpleLogger.error("SUT output:\n" + errorBuffer.toString());187 }188 stopSut();189 return null;190 }191 }192 //need to block until server is ready193 long timeout = getMaxAwaitForInitializationInSeconds();194 boolean completed;195 try {196 completed = latch.await(timeout, TimeUnit.SECONDS);197 } catch (InterruptedException e) {198 SimpleLogger.error("Interrupted controller");199 stopSut();200 return null;201 }202 if(! completed){203 SimpleLogger.error("SUT has not started properly within " + timeout + " seconds");204 if(errorBuffer != null) {205 SimpleLogger.error("SUT output:\n" + errorBuffer.toString());206 }207 stopSut();208 return null;209 }210 if (!isSutRunning()) {211 SimpleLogger.error("SUT started but then terminated. Likely a possible misconfiguration");212 if(errorBuffer != null) {213 SimpleLogger.error("SUT output:\n" + errorBuffer.toString());214 }215 //note: actual process might still be running due to Java Agent we started216 stopSut();217 return null;218 }219 if (!initialized) {220 //this could happen if SUT is hanging for some reason221 SimpleLogger.error("SUT is started but not initialized");222 if(errorBuffer != null) {223 SimpleLogger.error("SUT output:\n" + errorBuffer.toString());224 }225 //note: actual process might still be running due to Java Agent we started226 stopSut();227 return null;228 }229 postStart();230 return getBaseURL();231 }232 @Override233 public boolean isSutRunning() {234 return process != null && process.isAlive();235 }236 @Override237 public void stopSut() {238 SimpleLogger.info("Going to stop the SUT");239 preStop();240 if (serverController != null) {241 serverController.closeServer();242 }243 killProcess();244 initialized = false;245 postStop();246 }247 @Override248 public final boolean isInstrumentationActivated() {249 return instrumentation && serverController != null && serverController.isConnectionOn();250 }251 @Override252 public final void newSearch() {253 if (isInstrumentationActivated()) {254 serverController.resetForNewSearch();255 }256 }257 @Override258 public final void newTestSpecificHandler() {259 if (isInstrumentationActivated()) {260 serverController.resetForNewTest();261 }262 }263 @Override264 public final List<TargetInfo> getTargetInfos(Collection<Integer> ids) {265 checkInstrumentation();266 return serverController.getTargetsInfo(ids);267 }268 @Override269 public final List<AdditionalInfo> getAdditionalInfoList(){270 checkInstrumentation();271 return serverController.getAdditionalInfoList();272 }273 @Override274 public final void newActionSpecificHandler(ActionDto dto) {275 if (isInstrumentationActivated()) {276 serverController.setAction(new Action(dto.index, dto.inputVariables));277 }278 }279 @Override280 public final UnitsInfoDto getUnitsInfoDto(){281 if(!isInstrumentationActivated()){282 return null;283 }284 return getUnitsInfoDto(serverController.getUnitsInfoRecorder());285 }286 @Override287 public void setKillSwitch(boolean b) {288 checkInstrumentation();289 serverController.setKillSwitch(b);290 ExecutionTracer.setKillSwitch(b);// store info locally as well, to avoid needing to do call to fetch current value291 }292 //-----------------------------------------293 private void checkInstrumentation() {294 if (!isInstrumentationActivated()) {295 throw new IllegalStateException("Instrumentation is not active");296 }297 }298 private void validateJarPath() {299 String path = getPathToExecutableJar();300 if (!path.endsWith(".jar")) {301 throw new IllegalStateException("Invalid jar path does not end with '.jar': " + path);302 }303 if (!Files.exists(Paths.get(path))) {304 throw new IllegalArgumentException("File does not exist: " + path);305 }306 }307 private void killProcess() {308 try {309 Runtime.getRuntime().removeShutdownHook(processKillHook);310 } catch (Exception e) {311 /* do nothing. this can happen if shutdown is in progress */312 }313 if (process != null) {314 process.destroy();315 try {316 //be sure streamers are closed, otherwise process might hang on Windows317 process.getOutputStream().close();318 process.getInputStream().close();319 process.getErrorStream().close();320 } catch (Exception t) {321 SimpleLogger.error("Failed to close process stream: " + t.toString());322 }323 process = null;324 }325 }326 protected void startExternalProcessPrinter() {327 if (outputPrinter == null || !outputPrinter.isAlive()) {328 outputPrinter = new Thread(() -> {329 try {330 boolean muted = Boolean.parseBoolean(System.getProperty(PROP_MUTE_SUT));331 if(muted){332 errorBuffer = new StringBuffer(4096);333 }334 Scanner scanner = new Scanner(new BufferedReader(335 new InputStreamReader(process.getInputStream())));336 while (scanner.hasNextLine()) {337 String line = scanner.nextLine();338 if(line.startsWith(P6SpyFormatter.PREFIX)){339 StandardOutputTracker.handleSqlLine(this, line);340 }341 if(!muted) {342 SimpleLogger.info("SUT: " + line);343 } else if(errorBuffer != null){344 errorBuffer.append(line);345 errorBuffer.append("\n");346 }347 if (line.contains(getLogMessageOfInitializedServer())) {348 initialized = true;349 errorBuffer = null; //no need to keep track of it if everything is ok350 latch.countDown();351 }352 }353 /*354 if we arrive here, it means the process has no more output.355 this could happen if it was started with some misconfiguration, or356 if it has been stopped357 */358 if(process == null){359 SimpleLogger.warn("SUT was manually terminated ('process' reference is null)");360 } else if(! process.isAlive()){361 SimpleLogger.warn("SUT was terminated before initialization. Exit code: " + process.exitValue());362 } else {363 SimpleLogger.warn("SUT is still alive, but its output was closed before" +364 " producing the initialization message.");365 }366 latch.countDown();367 } catch (Exception e) {368 SimpleLogger.error("Failed to handle external process printer", e);369 }370 });371 outputPrinter.start();372 }373 }374 public int getWaitingSecondsForIncomingConnection() {375 return 20_000;376 }377}...
getWaitingSecondsForIncomingConnection
Using AI Code Generation
1import org.evomaster.client.java.controller.ExternalSutController;2import org.evomaster.client.java.controller.api.dto.SutInfoDto;3import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseInitializationDto;4import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseScriptDto;5import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseTableDto;6import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseType;7import java.time.Duration;8import java.util.List;9public class WaitingTimeForIncomingConnection {10 public static void main(String[] args) {11 Duration waitingTime = ExternalSutController.getWaitingSecondsForIncomingConnection();12 System.out.println("Waiting time for incoming connection from the SUT is " + waitingTime.getSeconds() + " seconds");13 }14}15import org.evomaster.client.java.controller.ExternalSutController;16import org.evomaster.client.java.controller.api.dto.SutInfoDto;17import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseInitializationDto;18import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseScriptDto;19import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseTableDto;20import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseType;21import java.time.Duration;22import java.util.List;23public class WaitingTimeForIncomingConnection {24 public static void main(String[] args) {
getWaitingSecondsForIncomingConnection
Using AI Code Generation
1public class ExternalSutController {2 public static void main(String[] args) throws Exception {3 String host = "localhost";4 int port = 9090;5 for(int i = 0; i < 5; i++) {6 try {7 Socket socket = new Socket(host, port);8 System.out.println("Connected to SUT");9 socket.close();10 break;11 } catch (Exception e) {12 long waitingTime = getWaitingSecondsForIncomingConnection(host, port);13 if(waitingTime == 0) {14 System.out.println("SUT is not ready");15 break;16 } else {17 System.out.println("SUT is not ready, waiting for " + waitingTime + " seconds");18 Thread.sleep(waitingTime * 1000);19 }20 }21 }22 }23 public static long getWaitingSecondsForIncomingConnection(String host, int port) throws Exception {24 Socket socket = new Socket(host, port);25 OutputStream out = socket.getOutputStream();26 InputStream in = socket.getInputStream();27 out.write(0);28 out.flush();29 byte[] buf = new byte[1024];30 int len = in.read(buf);31 if(len == -1) {32 throw new IllegalStateException("Cannot read waiting time");33 }34 String response = new String(buf, 0, len, StandardCharsets.UTF_8);35 socket.close();36 return Long.parseLong(response);37 }38}
getWaitingSecondsForIncomingConnection
Using AI Code Generation
1int waitingSeconds = getWaitingSecondsForIncomingConnection();2Thread.sleep(waitingSeconds * 1000);3startServer();4Thread.sleep(waitingSeconds * 1000);5stopServer();6startServer();7Thread.sleep(waitingSeconds * 1000);8stopServer();9startServer();10Thread.sleep(waitingSeconds * 1000);11stopServer();12startServer();13Thread.sleep(waitingSeconds * 1000);14stopServer();15startServer();16Thread.sleep(waitingSeconds * 1000);17stopServer();18startServer();19Thread.sleep(waitingSeconds * 1000);20stopServer();21startServer();22Thread.sleep(waitingSeconds * 1000);23stopServer();24startServer();25Thread.sleep(waitingSeconds * 1000);26stopServer();27startServer();28Thread.sleep(waitingSeconds * 1000);29stopServer();30startServer();
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!