// launch a jvm
JobConf taskConf = new JobConf(ttConf);
TaskAttemptID attemptID = new TaskAttemptID("test", 0, true, 0, 0);
Task task = new MapTask(null, attemptID, 0, null, MAP_SLOTS);
task.setConf(taskConf);
TaskInProgress tip = tt.new TaskInProgress(task, taskConf);
File pidFile = new File(TEST_DIR, "pid");
final TaskRunner taskRunner = task.createRunner(tt, tip);
// launch a jvm which sleeps for 60 seconds
final Vector<String> vargs = new Vector<String>(2);
vargs.add(writeScript("SLEEP", "sleep 60\n", pidFile).getAbsolutePath());
final File workDir = new File(TEST_DIR, "work");
workDir.mkdir();
final File stdout = new File(TEST_DIR, "stdout");
final File stderr = new File(TEST_DIR, "stderr");
// launch the process and wait in a thread, till it finishes
Thread launcher = new Thread() {
public void run() {
try {
taskRunner.launchJvmAndWait(null, vargs, stdout, stderr, 100,
workDir, null);
} catch (InterruptedException e) {
e.printStackTrace();
return;
}
}
};
launcher.start();
// wait till the jvm is launched
// this loop waits for at most 1 second
for (int i = 0; i < 10; i++) {
if (pidFile.exists()) {
break;
}
UtilsForTests.waitFor(100);
}
// assert that the process is launched
assertTrue("pidFile is not present", pidFile.exists());
// imitate Child code.
// set pid in jvmManager
BufferedReader in = new BufferedReader(new FileReader(pidFile));
String pid = in.readLine();
in.close();
JVMId jvmid = mapJvmManager.runningTaskToJvm.get(taskRunner);
jvmManager.setPidToJvm(jvmid, pid);
// kill JvmRunner
final JvmRunner jvmRunner = mapJvmManager.jvmIdToRunner.get(jvmid);
Thread killer = new Thread() {
public void run() {
jvmRunner.kill();
}
};
killer.start();
//wait for a while so that killer thread is started.
Thread.sleep(100);
// kill the jvm externally
taskRunner.kill();
assertTrue(jvmRunner.killed);
// launch another jvm and see it finishes properly
attemptID = new TaskAttemptID("test", 0, true, 0, 1);
task = new MapTask(null, attemptID, 0, null, MAP_SLOTS);
task.setConf(taskConf);
tip = tt.new TaskInProgress(task, taskConf);
TaskRunner taskRunner2 = task.createRunner(tt, tip);
// build dummy vargs to call ls
Vector<String> vargs2 = new Vector<String>(1);
vargs2.add(writeScript("LS", "ls", pidFile).getAbsolutePath());
File workDir2 = new File(TEST_DIR, "work2");