Need advice on starting three threads to run three Windows or Linux commands in parallel

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Need advice on starting three threads to run three Windows or Linux commands in parallel

karen
I'm writing my first test case using TestNG and I'm also new to Java.  I need to spawn three threads to simultaneously execute three different commands (either Windows or Linux; Windows snippet fleshed out below).  The first command runs until it times out.  The second two run to completion.  

I'm using ProcessBuilder to execute the three commands and I have that working if I don't try to do any signalling between the processes.  However, when I create a monitor and then try to wait on it, the test blocks until the first command times out.  Notify doesn't seem to be giving me a problem but I don't really know.  I'm sure I'm doing something wrong but have no idea what it is and can't find anything similar on the internet.  I've stripped the program down to the basics, hopefully without destroying anything, and coded everything inline to eliminate any logic errors (initially had it as a separate executeCommand method like you see in the Linux clause).  This really isn't a TestNG problem but hoped someone in this community has done this in the past.

Rather than waiting for the first command to time out, I want to destroy it once the other two processes have completed

Thanks in advance.

public class SmokeTest {
  @Test
  public void executeTest() throws IOException, InterruptedException, IllegalMonitorStateException {
 System.out.println("Begin executeTest");
 final Object monitor = new Object();

 String osname = System.getProperty("os.name");
 boolean processComplete = false;

 if (osname.equals("Linux")) {
//  String[] cmd = new String[]{"/bin/sh", "cd mp && ./runx1.sh"};
//  executeCommand(cmd, true, false, false);
 } else {
 String[] cmd = new String[]{"cmd.exe ", "/c cd c:\\mp && runx2.bat "};
 ProcessBuilder pb1 = new ProcessBuilder(cmd);
 pb1.inheritIO();
 pb1.redirectErrorStream(true);
 Process process1 = pb1.start();
//  synchronized(monitor){
//  while (!processComplete){
//  try { 
//  monitor.wait(); 
//  } catch(InterruptedException e) { 
//  System.out.println("InterruptedException caught"); 
//  } 
//  }
//  }
   
 cmd = new String[]{"cmd.exe ", "/c cd c:\\mp && runy.bat "};

 ProcessBuilder pb2 = new ProcessBuilder(cmd);
 pb2.inheritIO();
 pb2.redirectErrorStream(true);
 Process process2 = pb2.start();
 synchronized(monitor){
  monitor.notifyAll();
 }
   
 cmd = new String[]{"cmd.exe ", "/c cd c:\\mp && runz.bat "};
 ProcessBuilder pb3 = new ProcessBuilder(cmd);
 pb3.inheritIO();
 pb3.redirectErrorStream(true);
 Process process3 = pb3.start();
 
 System.out.println("End executeTest");
 }
  }

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.