/** * Shutdown all executors associated with this runtime. * If force terminate is specified, it will forcefully terminate * the executors, otherwise it will simply signal that they * should terminate. * * @param forceTerminate Specify if the executors should be * forcefully terminated, or simply notified to shutdown when ready. */ public void shutdownExecutors(final boolean forceTerminate) { checkThread(); if (executors == null) { return; } for (V8Executor executor : executors.values()) { if (forceTerminate) { executor.forceTermination(); } else { executor.shutdown(); } } }
/** * Shutdown all executors associated with this runtime. * If force terminate is specified, it will forcefully terminate * the executors, otherwise it will simply signal that they * should terminate. * * @param forceTerminate Specify if the executors should be * forcefully terminated, or simply notified to shutdown when ready. */ public void shutdownExecutors(final boolean forceTerminate) { checkThread(); if (executors == null) { return; } for (V8Executor executor : executors.values()) { if (forceTerminate) { executor.forceTermination(); } else { executor.shutdown(); } } }
@Test public void testTerminateLongRunningThread() throws InterruptedException { V8Executor executor = new V8Executor("while(true){}"); executor.start(); executor.forceTermination(); executor.join(); // We should not wait forever }
@Test public void testIsTerminating() { V8Executor executor = new V8Executor(""); executor.forceTermination(); assertTrue(executor.isTerminating()); }
@Test public void testLongRunningExecutorWithNoMessage() throws InterruptedException { V8Executor executor = new V8Executor("messageHandler = function(e) { postMessage(e); }", true, "messageHandler") { @Override protected void setup(final V8 runtime) { runtime.registerJavaMethod(V8ExecutorTest.this, "postMessage", "postMessage", new Class<?>[] { Object[].class }); } }; executor.start(); executor.forceTermination(); executor.join(); assertFalse(passed); }
@Test public void testPostEmptyMessageToLongRunningTask() throws InterruptedException { V8Executor executor = new V8Executor("messageHandler = function(e) { postMessage(e); }", true, "messageHandler") { @Override protected void setup(final V8 runtime) { runtime.registerJavaMethod(V8ExecutorTest.this, "postMessage", "postMessage", new Class<?>[] { Object[].class }); } }; executor.start(); executor.postMessage(); executor.forceTermination(); executor.join(); assertEquals("", result); }
@Test public void testTerminateBeforeExecution() throws InterruptedException { V8Executor executor = new V8Executor("'fooBar'"); executor.forceTermination(); executor.start(); executor.join(); assertNull(executor.getResult()); }
@Test public void testTerminateAfterExecution() throws InterruptedException { V8Executor executor = new V8Executor("'fooBar'"); executor.start(); executor.join(); executor.forceTermination(); assertEquals("fooBar", executor.getResult()); }
@Test public void testGetParseException() throws InterruptedException { V8Executor executor = new V8Executor("'a"); executor.start(); executor.join(); executor.forceTermination(); assertTrue(executor.getException() instanceof V8ScriptCompilationException); }
@Test public void testShutdownDoesNotTerminateLongRunningTask() throws InterruptedException { V8Executor executor = new V8Executor("while(true)", true, "messageHandler"); executor.shutdown(); assertFalse(executor.hasTerminated()); executor.forceTermination(); executor.join(); }
@Test public void testExceptionHasCorrectLineNumber() throws InterruptedException { V8Executor executor = new V8Executor("'a"); executor.start(); executor.join(); executor.forceTermination(); assertEquals(1, ((V8ScriptException) executor.getException()).getLineNumber()); }
@Test public void testLongRunningExecutorWithMessageHandler() throws InterruptedException { V8Executor executor = new V8Executor("messageHandler = function(e) { postMessage(e); }", true, "messageHandler") { @Override protected void setup(final V8 runtime) { runtime.registerJavaMethod(V8ExecutorTest.this, "postMessage", "postMessage", new Class<?>[] { Object[].class }); } }; executor.start(); executor.postMessage(""); waitForPassed(); executor.forceTermination(); executor.join(); assertTrue(passed); }
@Test public void testLongRunningExecutorWithMultiPartMessage() throws InterruptedException { V8Executor executor = new V8Executor("messageHandler = function(e) { postMessage(e[0], e[1]); }", true, "messageHandler") { @Override protected void setup(final V8 runtime) { runtime.registerJavaMethod(V8ExecutorTest.this, "postMessage", "postMessage", new Class<?>[] { Object[].class }); } }; executor.start(); executor.postMessage("1", "3"); waitForResult("13"); executor.forceTermination(); executor.join(); assertEquals("13", result); }
@Test public void testTerminateHasException() throws InterruptedException { V8Executor executor = new V8Executor("postMessage(''); while(true){}") { @Override protected void setup(final V8 runtime) { runtime.registerJavaMethod(V8ExecutorTest.this, "postMessage", "postMessage", new Class<?>[] { Object[].class }); } }; executor.start(); waitForPassed(); // wait for the executor to start executor.forceTermination(); executor.join(); assertTrue(executor.hasException()); }
@Test public void testHasException() throws InterruptedException { V8Executor executor = new V8Executor("(function() {throw 'foo';})();"); executor.start(); executor.join(); executor.forceTermination(); assertNull(executor.getResult()); assertTrue(executor.hasException()); }
@Test public void testGetExecutionException() throws InterruptedException { V8Executor executor = new V8Executor("(function() {throw 'foo';})();"); executor.start(); executor.join(); executor.forceTermination(); assertTrue(executor.getException() instanceof V8ScriptExecutionException); assertEquals("foo", ((V8ScriptExecutionException) executor.getException()).getJSMessage()); }
@Test public void testLongRunningExecutorWithSeveralMessages() throws InterruptedException { V8Executor executor = new V8Executor("messageHandler = function(e) { postMessage(e); }", true, "messageHandler") { @Override protected void setup(final V8 runtime) { runtime.registerJavaMethod(V8ExecutorTest.this, "postMessage", "postMessage", new Class<?>[] { Object[].class }); } }; executor.start(); executor.postMessage("1"); executor.postMessage("2"); executor.postMessage("3"); waitForResult("123"); executor.forceTermination(); executor.join(); assertEquals("123", result); }
/** * Shutdown all executors associated with this runtime. * If force terminate is specified, it will forcefully terminate * the executors, otherwise it will simply signal that they * should terminate. * * @param forceTerminate Specify if the executors should be * forcefully terminated, or simply notified to shutdown when ready. */ public void shutdownExecutors(final boolean forceTerminate) { checkThread(); if (executors == null) { return; } for (V8Executor executor : executors.values()) { if (forceTerminate) { executor.forceTermination(); } else { executor.shutdown(); } } }
/** * Shutdown all executors associated with this runtime. * If force terminate is specified, it will forcefully terminate * the executors, otherwise it will simply signal that they * should terminate. * * @param forceTerminate Specify if the executors should be * forcefully terminated, or simply notified to shutdown when ready. */ public void shutdownExecutors(final boolean forceTerminate) { checkThread(); if (executors == null) { return; } for (V8Executor executor : executors.values()) { if (forceTerminate) { executor.forceTermination(); } else { executor.shutdown(); } } }