unit tests for ProcessManager
This commit is contained in:
parent
d9beaff898
commit
7d8f8111c8
2 changed files with 126 additions and 14 deletions
|
|
@ -189,16 +189,16 @@ class ProcessManager {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* killProcess()
|
* killProcess()
|
||||||
* returns false if couldnt grab lock
|
* returns 1 if couldnt grab lock
|
||||||
* ALSO RETURNS TRUE IF PROCESS DOESNT EXIST
|
* ALSO RETURNS 0 IF PROCESS DOESNT EXIST
|
||||||
*/
|
*/
|
||||||
public Boolean killProcess(int pid) {
|
public int killProcess(int pid) {
|
||||||
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 false;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ProcessController candidate = this.processMap.get(pid);
|
ProcessController candidate = this.processMap.get(pid);
|
||||||
|
|
@ -207,7 +207,7 @@ class ProcessManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.releaseLock();
|
this.releaseLock();
|
||||||
return true;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,9 @@ package JobServ;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.JUnit4;
|
import org.junit.runners.JUnit4;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -38,27 +40,137 @@ public class ProcessManagerTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void addProcessesTest() {
|
public void addProcessesTest() {
|
||||||
int pid1 = manager.newProcess("ping google.com");
|
int pid = manager.newProcess("sleep 1");
|
||||||
int pid2 = manager.newProcess("ping google.com");
|
assertNotEquals(-2, pid);
|
||||||
assertEquals(2, pid2);
|
|
||||||
assertEquals(1, pid1);
|
|
||||||
|
|
||||||
manager.shutdown();
|
manager.shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* getStatusTest
|
* getStatusTest
|
||||||
* positive unit test for getStatus
|
* unit test for getStatus
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void getStatusTest() {
|
public void getStatusTest() {
|
||||||
int pid1 = manager.newProcess("ping google.com");
|
int pid = manager.newProcess("sleep 1");
|
||||||
assertEquals(0, pid1);
|
int status = manager.getProcessStatus(pid);
|
||||||
|
|
||||||
int status = manager.getProcessStatus(pid1);
|
|
||||||
assertEquals(0, status);
|
assertEquals(0, status);
|
||||||
|
|
||||||
manager.shutdown();
|
manager.shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* getOldStatusTest
|
||||||
|
* do finished processes return 1
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void getOldStatusTest() {
|
||||||
|
int pid = manager.newProcess("echo 'test'");
|
||||||
|
|
||||||
|
try{
|
||||||
|
Thread.sleep(200);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
int status = manager.getProcessStatus(pid);
|
||||||
|
assertEquals(1, status);
|
||||||
|
|
||||||
|
manager.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* getUnknownStatusTest()
|
||||||
|
* ensures 2 is returned when a status is not known
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void getUnknownStatusTest() {
|
||||||
|
int status = manager.getProcessStatus(400);
|
||||||
|
assertEquals(2, status);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* getReturnTest()
|
||||||
|
* test of process returns
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void getReturnTest() {
|
||||||
|
int pid = manager.newProcess("sleep .5");
|
||||||
|
int ret = manager.getProcessReturn(pid);
|
||||||
|
assertEquals(256, ret);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(550);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = manager.getProcessReturn(pid);
|
||||||
|
assertNotEquals(ret, 256);
|
||||||
|
assertNotEquals(ret, 257);
|
||||||
|
assertNotEquals(ret, 258);
|
||||||
|
|
||||||
|
manager.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* getUNknownProcessReturn
|
||||||
|
* tests process return for unknown processes
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void getUnknownProcessReturnTest() {
|
||||||
|
int ret = manager.getProcessReturn(502);
|
||||||
|
assertEquals(258, ret);
|
||||||
|
manager.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* getProcessOutputTest()
|
||||||
|
* verifies output is grabbed correctly from processes
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void getProcessOutputTest() {
|
||||||
|
int pid = manager.newProcess("echo test");
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(100);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
String out = manager.getProcessOutput(pid);
|
||||||
|
assertEquals("test\n", out); // calls string.equals()
|
||||||
|
|
||||||
|
manager.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* getUnknownOutputTest()
|
||||||
|
* verifies correct information is returned when
|
||||||
|
* output is requested of an unknown process
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void getUnknownOutputTest() {
|
||||||
|
String out = manager.getProcessOutput(532);
|
||||||
|
assertEquals("[-] ERROR: Process not found", out);
|
||||||
|
manager.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* killProcessTest()
|
||||||
|
* ensures killing a process works
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void killProcessTest() {
|
||||||
|
int pid = manager.newProcess("sleep 10");
|
||||||
|
int ret = manager.killProcess(pid);
|
||||||
|
|
||||||
|
assertEquals(0, ret);
|
||||||
|
|
||||||
|
int status = manager.getProcessStatus(pid);
|
||||||
|
|
||||||
|
assertEquals(1, status);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue