finished unit tests, better process status reporting

This commit is contained in:
Aidan Hahn 2019-05-23 14:21:40 -07:00
parent 7d8f8111c8
commit eb15da6ae2
No known key found for this signature in database
GPG key ID: 327711E983899316
3 changed files with 48 additions and 16 deletions

View file

@ -32,6 +32,8 @@ class ProcessController {
private Process process; private Process process;
private Boolean killedManually = false;
/* /*
* Constructor * Constructor
* Takes a command and spawns it in a new process * Takes a command and spawns it in a new process
@ -66,6 +68,10 @@ class ProcessController {
* TODO: (for future release) return thread state * TODO: (for future release) return thread state
*/ */
public int getStatus() { public int getStatus() {
if (this.killedManually) {
return 2;
}
try { try {
process.exitValue(); process.exitValue();
return 1; return 1;
@ -77,10 +83,15 @@ class ProcessController {
/* /*
* getReturn() * getReturn()
* returns the exit code of the process * returns the exit code of the process
* or 256 if process is still running * 256 if process is still running
* 257 if process was killed manually and no longer exists
* (unix/posix defines an exit code as a uint8, so 256 is fair game) * (unix/posix defines an exit code as a uint8, so 256 is fair game)
*/ */
public int getReturn() { public int getReturn() {
if (this.killedManually) {
return 257;
}
try { try {
return process.exitValue(); return process.exitValue();
} catch (IllegalThreadStateException e) { } catch (IllegalThreadStateException e) {
@ -93,6 +104,10 @@ class ProcessController {
* gets output from process * gets output from process
*/ */
public String getOutput() { public String getOutput() {
if (this.killedManually) {
return "";
}
String out = ""; String out = "";
while(outputScanner.hasNext()) { while(outputScanner.hasNext()) {
out += outputScanner.next(); out += outputScanner.next();
@ -114,5 +129,7 @@ class ProcessController {
} }
process.destroy(); process.destroy();
this.killedManually = true;
} }
} }

View file

@ -103,8 +103,9 @@ class ProcessManager {
* returns whether or not a process is running. * returns whether or not a process is running.
* 0: running * 0: running
* 1: not running * 1: not running
* 2: doesnt exist * 2: killed manually by a client
* 3: couldnt grab lock * 3: doesnt exist
* 4: couldnt grab lock
*/ */
public int getProcessStatus(int pid) { public int getProcessStatus(int pid) {
int status; int status;
@ -116,7 +117,7 @@ class ProcessManager {
} catch (TimeoutException e) { } catch (TimeoutException e) {
// lock could not be grabbed before timeout // lock could not be grabbed before timeout
System.err.println("Timeout getting process status: " + e.getMessage()); System.err.println("Timeout getting process status: " + e.getMessage());
return 3; return 4;
} }
ProcessController candidate = this.processMap.get(pid); ProcessController candidate = this.processMap.get(pid);
@ -128,14 +129,17 @@ class ProcessManager {
// process must not exist // process must not exist
this.releaseLock(); this.releaseLock();
return 2; return 3;
} }
/* /*
* getProcessReturn() * getProcessReturn()
* returns a code 0-255, or 256 if process still running * returns:
* additionally, returns 257 if lock not grabbable AND * 0-255: process exit code
* a 258 if process doesnt exist. * 256: process still running
* 257: process was killed by a client (TODO: list which client connection killed a process)
* 258: process doesnt exist
* 259: couldnt grab lock in time
*/ */
public int getProcessReturn(int pid) { public int getProcessReturn(int pid) {
int ret; int ret;
@ -146,7 +150,7 @@ class ProcessManager {
} catch (TimeoutException e) { } catch (TimeoutException e) {
System.err.println("Timeout getting process return: " + e.getMessage()); System.err.println("Timeout getting process return: " + e.getMessage());
return 257; return 259;
} }
ProcessController candidate = this.processMap.get(pid); ProcessController candidate = this.processMap.get(pid);
@ -189,25 +193,33 @@ class ProcessManager {
/* /*
* killProcess() * killProcess()
* returns 1 if couldnt grab lock * returns mirror processStatus
* ALSO RETURNS 0 IF PROCESS DOESNT EXIST * returns 0 if still running
* returns 1 if process was killed
* returns 2 if process not found
* returns 3 if couldnt grab lock
*/ */
public int killProcess(int pid) { public int killProcess(int pid) {
int code;
try { try {
this.getLock(); this.getLock();
} catch (TimeoutException e) { } catch (TimeoutException e) {
System.err.println("Timeout killing process: " + e.getMessage()); System.err.println("Timeout killing process: " + e.getMessage());
return 1; return 3;
} }
ProcessController candidate = this.processMap.get(pid); ProcessController candidate = this.processMap.get(pid);
if (candidate != null) { if (candidate != null) {
candidate.kill(); candidate.kill();
code = 1;
} else {
code = 2;
} }
this.releaseLock(); this.releaseLock();
return 0; return code;
} }
/* /*

View file

@ -86,7 +86,7 @@ public class ProcessManagerTest {
@Test @Test
public void getUnknownStatusTest() { public void getUnknownStatusTest() {
int status = manager.getProcessStatus(400); int status = manager.getProcessStatus(400);
assertEquals(2, status); assertEquals(3, status);
} }
/* /*
@ -160,17 +160,20 @@ public class ProcessManagerTest {
/* /*
* killProcessTest() * killProcessTest()
* ensures killing a process works * ensures killing a process works
* also tests if getProcessStatus returns 2
*/ */
@Test @Test
public void killProcessTest() { public void killProcessTest() {
int pid = manager.newProcess("sleep 10"); int pid = manager.newProcess("sleep 10");
int ret = manager.killProcess(pid); int ret = manager.killProcess(pid);
assertEquals(0, ret); assertEquals(1, ret);
int status = manager.getProcessStatus(pid); int status = manager.getProcessStatus(pid);
assertEquals(1, status); assertEquals(2, status);
manager.shutdown();
} }
} }