@Test public void testSetup() throws InterruptedException { V8Executor executor = new V8Executor("callback()") { @Override protected void setup(final V8 runtime) { runtime.registerJavaMethod(V8ExecutorTest.this, "callback", "callback", new Class<?>[] {}); } }; executor.start(); executor.join(); assertTrue(passed); }
@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 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 testNoReturn() throws InterruptedException { V8Executor executor = new V8Executor("var x = 7;"); executor.start(); executor.join(); assertEquals("undefined", executor.getResult()); }
@Test public void testNonStringReturnType() throws InterruptedException { V8Executor executor = new V8Executor("3+4"); executor.start(); executor.join(); assertEquals("7", executor.getResult()); }
@Test public void testNullReturn() throws InterruptedException { V8Executor executor = new V8Executor("null;"); executor.start(); executor.join(); assertNull(executor.getResult()); }
@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 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 testTerminateAfterExecution() throws InterruptedException { V8Executor executor = new V8Executor("'fooBar'"); executor.start(); executor.join(); executor.forceTermination(); assertEquals("fooBar", executor.getResult()); }
@Test public void testTerminateBeforeExecution() throws InterruptedException { V8Executor executor = new V8Executor("'fooBar'"); executor.forceTermination(); executor.start(); executor.join(); assertNull(executor.getResult()); }
@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 testSimpleScript() throws InterruptedException { V8Executor executor = new V8Executor("'fooBar'"); executor.start(); executor.join(); assertEquals("fooBar", executor.getResult()); assertFalse(executor.hasException()); }
@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 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 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 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(); }