ForkingExecutor implements Closeable { <T> T execute(Fn<T> f) throws IOException { try (final Execution<T> execution = new Execution<>(f)) { executions.add(execution); execution.start(); return execution.waitFor(); } }  @Override void close();  }
@Test public void returnsResult() throws IOException { final String result = forkingExecutor.execute(() -> "hello world!"); assertThat(result, is("hello world!")); }
@Test public void propagatesException() throws IOException { exception.expect(FoobarException.class); exception.expectMessage("foobar!"); forkingExecutor.execute(() -> { throw new FoobarException("foobar!"); }); }
@Test public void captures() throws IOException { final Map<String, String> map = new HashMap<>(); map.put("foo", "bar"); final Map<String, String> result = forkingExecutor.execute(() -> map); assertThat(result, is(map)); }
@Test public void executesInSubprocess() throws IOException { final String thisJvm = ManagementFactory.getRuntimeMXBean().getName(); final String subprocessJvm = forkingExecutor.execute(() -> ManagementFactory.getRuntimeMXBean().getName()); assertThat(thisJvm, is(not(subprocessJvm))); }