/**
* tests for when child process finished after wait() is called
*/
public void test_wait2() throws IOException {
final ScriptProcess p = datastore.newProcess(TEST_USER, "wait2.js", false, "", "owner", TransportType.Dummy);
p.save();
ScriptAction r = p.call();
assertTrue("Forked correctly", r instanceof Fork);
final ThreadLocal<Boolean> executedParentPostFork = new ThreadLocal<Boolean>();
final ThreadLocal<Boolean> executedParentPostWait = new ThreadLocal<Boolean>();
final ThreadLocal<Boolean> executedChild = new ThreadLocal<Boolean>();
final ThreadLocal<Boolean> executedChildPostSleep = new ThreadLocal<Boolean>();
ScriptusFacade testFacade = new ScriptusFacade(datastore, c, m, conf) {
private UUID childPid;
@Override
public void execute(UUID pid) {
if( ! pid.equals(p.getPid())) {
//executing child
if(Boolean.TRUE.equals(executedChild.get())) {
executedChildPostSleep.set(Boolean.TRUE);
ScriptProcess p2 = datastore.getProcess(pid);
ScriptAction r2 = p2.call();
assertTrue("in child termination", r2 instanceof Termination);
p2.save();
r2.visit(this, p2);
} else {
executedChild.set(Boolean.TRUE);
childPid = pid;
ScriptProcess p2 = datastore.getProcess(pid);
ScriptAction r2 = p2.call();
p2.save();
assertTrue("in child sleep", r2 instanceof Sleep);
}
return;
}
if(pid.equals(p.getPid())) {
if(Boolean.TRUE.equals(executedParentPostFork.get())) {
executedParentPostWait.set(Boolean.TRUE);
ScriptAction enfin = datastore.getProcess(pid).call();
assertTrue("script finished", enfin instanceof Termination);
assertEquals("script result OK", "waitedfooslept"+childPid, ((Termination)enfin).getResult());
} else {
executedParentPostFork.set(Boolean.TRUE);
ScriptProcess p2 = datastore.getProcess(pid);
ScriptAction r2 = p2.call();
p2.save();
assertTrue("Waited correctly", r2 instanceof Wait);
//pause thread until child has termination