Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue #55: ThreadMonitor waits unnecessarily #56

Merged
merged 1 commit into from
May 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ void run() {
try {
// wait, for threadToMonitor to be set in JS evaluator thread
synchronized (monitor) {
monitor.wait(maxCPUTime / MILI_TO_NANO);
if (threadToMonitor == null) {
monitor.wait(maxCPUTime / MILI_TO_NANO);
}
}
if (threadToMonitor == null) {
throw new IllegalStateException("Executor thread not set after " + maxCPUTime / MILI_TO_NANO + " ms");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package delight.nashornsandbox.internal;

import org.junit.Assert;
import org.junit.Test;

/**
* JUnit test for ThreadMonitor.
*/
public class ThreadMonitorTest {

/**
* This is a simple test that verifies that if the thread to monitor is already set, the ThreadMonitor does not
* wait unnecessarily when it is run.
*/
@Test
public void when_run_and_threadToMonitor_set_then_do_not_wait() {
final ThreadMonitor threadMonitor = new ThreadMonitor(1000, 0);
Thread threadToMonitor = new Thread();
threadMonitor.setThreadToMonitor(threadToMonitor);
threadMonitor.stopMonitor();

long startTime = System.currentTimeMillis();
threadMonitor.run();
Assert.assertTrue(System.currentTimeMillis() - startTime < 500);
}
}