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 Boolean killedManually = false;
/*
* Constructor
* Takes a command and spawns it in a new process
@ -66,6 +68,10 @@ class ProcessController {
* TODO: (for future release) return thread state
*/
public int getStatus() {
if (this.killedManually) {
return 2;
}
try {
process.exitValue();
return 1;
@ -77,10 +83,15 @@ class ProcessController {
/*
* getReturn()
* 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)
*/
public int getReturn() {
if (this.killedManually) {
return 257;
}
try {
return process.exitValue();
} catch (IllegalThreadStateException e) {
@ -93,6 +104,10 @@ class ProcessController {
* gets output from process
*/
public String getOutput() {
if (this.killedManually) {
return "";
}
String out = "";
while(outputScanner.hasNext()) {
out += outputScanner.next();
@ -114,5 +129,7 @@ class ProcessController {
}
process.destroy();
this.killedManually = true;
}
}

View file

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

View file

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