How to stop a Test Process If that Is Hanged or Went To Infinite loop
This is pretty common problem while going towards CDCI(Continuous Development and Continuous Integration) process that many testcases hang and freeze due to several issues . Improper scripting (waiting for an element for infinite time , infinite loop etc)are the real cause of those issues. It makes the machine busy and does not leave for next execution which are lined up for further execution. Ideally they should be handled by script itself. But when we try to implement CDCI over scripts which are there for years, we hardly get that covered via script.
Now if we look the way we trigger a UFT or selenium execution, we can see we start a AOM process that intern triggers the run. Well that is good but not full proof to avoid such exceptions.
Here is a small thought to improve the triggering mechanism..
Instead of directly triggering an AOM script or a batfile or an ant script what we can do, we start two thread instead. The first thread keeps checking if the execution is going beyond specified time,meanwhile the second thread (child one) starts the exeuction.
Once the second one,if over, it kills itself . And if, it can not end execution then it will be alive. The first thread gets activated and kills the second one and frees up the machine to accept other execution which are lined up..
public class kickStarter {
private static final long timeout=4*60*60*100;
//this is the timeout period , we can wait maximum of that hours to see if the
// execution is over
private static boolean flag=false;
public static void main(String[] args)
{
OtherThread oth=new OtherThread();
oth.start();
//starts the other thread which kicks off the execution
long timer=0;
do
try{
Thread.sleep(1);
timer++;
//below block checks if the executing thread which is executing run
// is over or not. If completed it should not be alive
if(!oth.isAlive())
{
flag=true;
break;
//we want to break it as the other thread executed successfully over the
//specified time
}
}
catch(Exception e)
{
}
while(timer=timeout)
{
oth.kill();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
flag=true;
}
if(flag)
{
//clear UFT or selenium
}
}
}
the child thread will look like:
public class OtherThread extends Thread implements Runnable {
private volatile boolean isRunning=true;
public void run()
{
while(isRunning)
{
try{
Thread.sleep(1);
//call your AOM or batchfile or ant file here
kill();
//kill should be called after control comes back from execution
}
catch(Exception e)
{
}
}
}
public void kill()
{
isRunning=false;
}
}