private void notifyReleaseHandlers(final V8 runtime) { for (V8Runnable handler : releaseHandlers) { handler.run(runtime); } }
@Override public void run() { runtime = V8.createV8Runtime(); try { target.run(runtime); } finally { synchronized (this) { if (runtime.getLocker().hasLock()) { runtime.close(); runtime = null; } } } }
/** * Runs an {@link V8Runnable} on the V8 thread. * * <b>Note: </b> This method executes synchronously, not asynchronously; * it will not return until the passed {@link V8Runnable} is done * executing. The method is also synchronized, so it will block until it * gets a chance to run. * * @param runnable {@link V8Runnable} to run. */ public synchronized void run(final V8Runnable runnable) { try { v8.getLocker().acquire(); runnable.run(v8); } finally { if ((v8 != null) && (v8.getLocker() != null) && v8.getLocker().hasLock()) { v8.getLocker().release(); } } }
private void notifyReleaseHandlers(final V8 runtime) { for (V8Runnable handler : releaseHandlers) { handler.run(runtime); } }
@Test public void testV8MultipleReleaseHandlers() { V8 testV8 = V8.createV8Runtime(); V8Runnable releaseHandler1 = mock(V8Runnable.class); V8Runnable releaseHandler2 = mock(V8Runnable.class); testV8.addReleaseHandler(releaseHandler1); testV8.addReleaseHandler(releaseHandler2); testV8.close(); verify(releaseHandler1, times(1)).run(any(V8.class)); // cannot check against the real v8 because it's released. verify(releaseHandler2, times(1)).run(any(V8.class)); // cannot check against the real v8 because it's released. }
@Override public void run() { runtime = V8.createV8Runtime(); try { target.run(runtime); } finally { synchronized (this) { if (runtime.getLocker().hasLock()) { runtime.close(); runtime = null; } } } }
@Test public void testV8UnknownReleaseHandleRemoved() { V8 testV8 = V8.createV8Runtime(); V8Runnable releaseHandler1 = mock(V8Runnable.class); V8Runnable releaseHandler2 = mock(V8Runnable.class); testV8.addReleaseHandler(releaseHandler1); testV8.removeReleaseHandler(releaseHandler2); testV8.close(); verify(releaseHandler1, times(1)).run(any(V8.class)); // cannot check against the real v8 because it's released. }
@Test public void testV8ReleaseHandleRemoved() { V8 testV8 = V8.createV8Runtime(); V8Runnable releaseHandler = mock(V8Runnable.class); testV8.addReleaseHandler(releaseHandler); testV8.removeReleaseHandler(releaseHandler); testV8.close(); verify(releaseHandler, never()).run(testV8); }
@Test public void testExceptionInReleaseHandlerStillReleasesV8() { V8 testV8 = V8.createV8Runtime(); V8Runnable releaseHandler = mock(V8Runnable.class); doThrow(new RuntimeException()).when(releaseHandler).run(any(V8.class)); testV8.addReleaseHandler(releaseHandler); try { testV8.close(); } catch (Exception e) { assertTrue(testV8.isReleased()); return; } fail("Exception should have been caught."); }
/** * Runs an {@link V8Runnable} on the V8 thread. * * <b>Note: </b> This method executes synchronously, not asynchronously; * it will not return until the passed {@link V8Runnable} is done * executing. The method is also synchronized, so it will block until it * gets a chance to run. * * @param runnable {@link V8Runnable} to run. */ public synchronized void run(final V8Runnable runnable) { try { v8.getLocker().acquire(); runnable.run(v8); } finally { if ((v8 != null) && (v8.getLocker() != null) && v8.getLocker().hasLock()) { v8.getLocker().release(); } } }
@Override public void run() { runtime = V8.createV8Runtime(); try { target.run(runtime); } finally { synchronized (this) { if (runtime.getLocker().hasLock()) { runtime.release(); runtime = null; } } } }
@Override public void run() { runtime = V8.createV8Runtime(); try { target.run(runtime); } finally { synchronized (this) { if (runtime.getLocker().hasLock()) { runtime.release(); runtime = null; } } } }