added a test for Mutex properties of the locking system

This commit is contained in:
Aidan Hahn 2019-05-23 19:31:03 -07:00
parent f14265fe5b
commit 8a513164ab
No known key found for this signature in database
GPG key ID: 327711E983899316
4 changed files with 86 additions and 13 deletions

View file

@ -124,12 +124,12 @@ class ProcessController {
try { try {
this.input.close(); this.input.close();
this.output.close(); this.output.close();
this.outputScanner.close();
this.process.destroy();
} catch (IOException e) { } catch (IOException e) {
// streams already closed // streams already closed
} }
process.destroy();
this.killedManually = true; this.killedManually = true;
} }
} }

View file

@ -225,10 +225,11 @@ class ProcessManager {
/* /*
* getLock() * getLock()
* Locks access to this.processQueue * Locks access to this.processQueue
* Waits for a predefined timeout period and then grabs the mutex * Waits for a predefined timeout period for mutex to be avail.
* Synchronized so two things cannot grab lock at once.
* Throws TimeoutException when it fails to get the lock. * Throws TimeoutException when it fails to get the lock.
*/ */
private void getLock() throws TimeoutException { protected synchronized void getLock() throws TimeoutException {
Future<Object> future = this.threadPool.submit(this.lockCallable); Future<Object> future = this.threadPool.submit(this.lockCallable);
try { try {
@ -266,7 +267,7 @@ class ProcessManager {
* releaseLock() * releaseLock()
* releases mutex so other threads can operate on processqueue * releases mutex so other threads can operate on processqueue
*/ */
private void releaseLock() { protected void releaseLock() {
this.processQueueMutex = false; this.processQueueMutex = false;
} }

View file

@ -15,6 +15,10 @@ import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
import java.util.concurrent.Future;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
/* /*
* ProcessManagerTest * ProcessManagerTest
@ -24,15 +28,16 @@ import static org.junit.Assert.assertNotEquals;
* tests it with ProcessController. * tests it with ProcessController.
*/ */
public class ProcessManagerTest { public class ProcessManagerTest {
ProcessManager manager; private ProcessManagerTestImplementation manager = new ProcessManagerTestImplementation();
private ExecutorService threadPool = Executors.newCachedThreadPool();
/* // calls a test function that simulates load by holding the lock for a long time
* ProcessManagerTest constructor private Callable<Object> holdLockSevenSeconds = new Callable<Object>() {
* initializes the process manager public Object call() {
*/ manager.longCallHoldsLock();
public ProcessManagerTest() { return true;
manager = new ProcessManager();
} }
};
/* /*
* addProcessTest() * addProcessTest()
@ -175,5 +180,31 @@ public class ProcessManagerTest {
manager.shutdown(); manager.shutdown();
} }
/*
* asyncLockTimeoutTest
* ensures that two things cannot grab the lock at the same time
*/
@Test
public void asyncLockTimeoutTest() {
Future<Object> future = this.threadPool.submit(this.holdLockSevenSeconds);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
System.err.println("[!!] Thread for async test interrupted!");
}
System.err.println("[2] attempting to grab (held) lock");
int pid = this.manager.newProcess("Test Process");
assertEquals(-1, pid);
future.cancel(true);
int pid2 = this.manager.newProcess("echo test");
assertNotEquals(-1, pid2);
manager.shutdown();
}
} }

View file

@ -0,0 +1,41 @@
/*
* ProcessManagerTestImplementation
*
* v1.0
*
* May 23, 2019
*/
package JobServ;
import java.util.concurrent.TimeoutException;
/*
* ProcessManagerTestImplementation
* inherits ProcessManager and adds useful functions for testing
*/
class ProcessManagerTestImplementation extends ProcessManager {
public void longCallHoldsLock() {
try {
super.getLock();
System.err.println("[1] Long Call Has Lock");
// hold lock for 7 seconds, more than double normal timeout.
Thread.sleep(4000);
super.releaseLock();
} catch (TimeoutException e) {
System.err.println("[!!] Long Call wasnt able to grab lock!");
return;
} catch (InterruptedException e) {
super.releaseLock();
System.err.println("[3] Released lock: interrupted");
return;
}
}
}