@Test public void testTerminateLongRunningThread() throws InterruptedException { V8Executor executor = new V8Executor("while(true){}"); executor.start(); executor.forceTermination(); executor.join(); // We should not wait forever }
/** * 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(); } } }
runtime = V8.createV8Runtime(); runtime.registerJavaMethod(new ExecutorTermination(), "__j2v8__checkThreadTerminate"); setup(runtime); Object scriptResult = runtime.executeScript("__j2v8__checkThreadTerminate();\n" + script, getName(), -1); if (scriptResult != null) { result = scriptResult.toString();
@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 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 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 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 testForceTerminateNestedExecutors() throws InterruptedException { V8 runtime = V8.createV8Runtime(); runtime.terminateExecution(); V8Executor executor = new V8Executor("while (true){}"); executor.start(); V8Object key = new V8Object(runtime); runtime.registerV8Executor(key, executor); runtime.shutdownExecutors(true); assertTrue(runtime.getExecutor(key).isShuttingDown()); executor.join(); key.close(); runtime.close(); }
@Test public void testIsNotTerminating() { V8Executor executor = new V8Executor(""); assertFalse(executor.isTerminating()); assertFalse(executor.isShuttingDown()); }
@Test public void testNestedExecutorExecution() throws InterruptedException { V8 runtime = V8.createV8Runtime(); runtime.terminateExecution(); V8Executor executor = new V8Executor(""); executor.start(); V8Object key = new V8Object(runtime); runtime.registerV8Executor(key, executor); key.close(); runtime.close(); executor.join(); }
@Test public void testTerminateNestedExecutors() { V8 runtime = V8.createV8Runtime(); runtime.terminateExecution(); V8Executor executor = new V8Executor("while (true){}"); V8Object key = new V8Object(runtime); runtime.registerV8Executor(key, executor); runtime.shutdownExecutors(false); assertTrue(runtime.getExecutor(key).isShuttingDown()); key.close(); runtime.close(); }
@Test public void testGetNestedExecutor() { V8 runtime = V8.createV8Runtime(); runtime.terminateExecution(); V8Executor executor = new V8Executor(""); V8Object key = new V8Object(runtime); runtime.registerV8Executor(key, executor); assertEquals(executor, runtime.getExecutor(key)); key.close(); runtime.close(); }
@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); }
@Test public void testRemoveNestedExecutor() { V8 runtime = V8.createV8Runtime(); runtime.terminateExecution(); V8Executor executor = new V8Executor(""); V8Object key = new V8Object(runtime); runtime.registerV8Executor(key, executor); V8Executor result = runtime.removeExecutor(key); assertEquals(executor, result); assertNull(runtime.getExecutor(key)); key.close(); runtime.close(); }
@Test public void testGetParseException() throws InterruptedException { V8Executor executor = new V8Executor("'a"); executor.start(); executor.join(); executor.forceTermination(); assertTrue(executor.getException() instanceof V8ScriptCompilationException); }
/** * 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(); } } }
runtime = V8.createV8Runtime(); runtime.registerJavaMethod(new ExecutorTermination(), "__j2v8__checkThreadTerminate"); setup(runtime); Object scriptResult = runtime.executeScript("__j2v8__checkThreadTerminate();\n" + script, getName(), -1); if (scriptResult != null) { result = scriptResult.toString();
@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 testGetMissingExecutor() { V8 runtime = V8.createV8Runtime(); runtime.terminateExecution(); V8Executor executor = new V8Executor(""); V8Object key = new V8Object(runtime); runtime.registerV8Executor(key, executor); V8Object anotherKey = new V8Object(runtime); V8Executor result = runtime.getExecutor(anotherKey); assertNull(result); key.close(); anotherKey.close(); runtime.close(); }