@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 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 testIsNotTerminating() { V8Executor executor = new V8Executor(""); assertFalse(executor.isTerminating()); assertFalse(executor.isShuttingDown()); }
@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 testTerminateBeforeExecution() throws InterruptedException { V8Executor executor = new V8Executor("'fooBar'"); executor.forceTermination(); executor.start(); executor.join(); assertNull(executor.getResult()); }
@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 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 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 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 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 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(); }
@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(); }