/** * Releases the underlying {@link V8} instance. * * This method should be invoked once you're done using this object, * otherwise a large amount of garbage could be left on the JVM due to * native resources. * * <b>Note:</b> If this method has already been called once, it * will do nothing. */ public void release() { if ((v8 != null) && !v8.isReleased()) { // Release the V8 instance from the V8 thread context. run(new V8Runnable() { @Override public void run(final V8 v8) { if ((v8 != null) && !v8.isReleased()) { v8.close(); } } }); } } }
@Test public void testLockAquiredDuringExecution() { ConcurrentV8 concurrentV8 = new ConcurrentV8(); try { concurrentV8.run(new V8Runnable() { @Override public void run(final V8 runtime) { assertTrue(runtime.getLocker().hasLock()); } }); } catch (Exception e) { // do nothing } concurrentV8.release(); }
@Test public void testLockNotAcquiredAfterExecution() { ConcurrentV8 concurrentV8 = new ConcurrentV8(); concurrentV8.run(new V8Runnable() { @Override public void run(final V8 runtime) { runtime.executeScript("foo = 7;"); } }); assertFalse(concurrentV8.getV8().getLocker().hasLock()); concurrentV8.release(); }
@Test public void testConstructorWithoutV8() { ConcurrentV8 concurrentV8 = new ConcurrentV8(); assertNotNull(concurrentV8.getV8()); concurrentV8.release(); }
@Test public void exceptionsShouldBeRaised() { ConcurrentV8 v8 = new ConcurrentV8(); final ExceptionThrower exceptionThrower = new ExceptionThrower(); v8.run(new V8Runnable() { @Override public void run(final V8 v8) { v8.registerJavaMethod(exceptionThrower, "whine", "whine", new Class[0]); } }); try { v8.run(new V8Runnable() { @Override public void run(final V8 v8) { v8.executeScript("whine();"); } }); Assert.fail("Regular concurrent V8 invocations should pass on exceptions."); } catch (Exception e) { assertEquals("Whaaa!", e.getCause().getMessage()); } v8.release(); } }
@Test public void testLockNotAquiredAfterException() { ConcurrentV8 concurrentV8 = new ConcurrentV8(); try { concurrentV8.run(new V8Runnable() { @Override public void run(final V8 runtime) { runtime.executeScript("throw 'my exception';"); } }); } catch (Exception e) { // do nothing } assertFalse(concurrentV8.getV8().getLocker().hasLock()); concurrentV8.release(); }
@Test public void testLockNotAquiredAfterCreation() { ConcurrentV8 concurrentV8 = new ConcurrentV8(); assertFalse(concurrentV8.getV8().getLocker().hasLock()); concurrentV8.release(); }
@Test public void shouldShareV8AcrossThreads() { final ConcurrentV8 v8 = new ConcurrentV8(); v8.run(new V8Runnable() { @Override public void run(final V8 v8) { v8.release();
@Override public void run() { v8.run(new V8Runnable() { @Override public void run(final V8 v8) { v8.executeVoidScript("var i = 3000;"); } }); } });
@Override public void run() { v8.run(new V8Runnable() { @Override public void run(final V8 v8) { v8.executeVoidScript("i += 344;"); } }); } });
/** * Releases the underlying {@link V8} instance. * * This method should be invoked once you're done using this object, * otherwise a large amount of garbage could be left on the JVM due to * native resources. * * <b>Note:</b> If this method has already been called once, it * will do nothing. */ public void release() { if ((v8 != null) && !v8.isReleased()) { // Release the V8 instance from the V8 thread context. run(new V8Runnable() { @Override public void run(final V8 v8) { if ((v8 != null) && !v8.isReleased()) { v8.close(); } } }); } } }