### Question:
ThreadPoolScriptEvaluator implements ScriptEvaluator { @Override public <T> T evaluateScript(final ScriptObject script, final Bindings bindings) throws ScriptException { final Future<T> future = threadPool.submit(new ScriptExecutorTask<T>(script, bindings)); final long timeout = scriptEngineManager.getConfiguration().getScriptExecutionTimeout(); try { if (timeout == ScriptEngineConfiguration.NO_TIMEOUT) { return future.get(); } else { return future.get(timeout, TimeUnit.SECONDS); } } catch (ExecutionException ex) { LOGGER.error("Script terminated with exception", ex); throw new ScriptException(ex); } catch (TimeoutException ex) { LOGGER.warn("Script timed out"); throw new ScriptException(ex); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); LOGGER.debug("Interrupted while waiting for script result"); throw new ScriptException(ex); } finally { future.cancel(true); } } @Inject  ThreadPoolScriptEvaluator(final StandardScriptEngineManager scriptEngineManager,
                                     final ExecutorService threadPool,
                                     final ScriptEvaluator delegate); @Override T evaluateScript(final ScriptObject script, final Bindings bindings); @Override void bindVariableInGlobalScope(final String name, final Object object);  }### Answer:
@Test public void shouldStopJavaScriptExecutionWhenTimeoutReached() throws Exception { ScriptObject loopScript = getJavascript("while(true) { }"); setTimeout(1); testEvaluator.<Void>evaluateScript(loopScript, null); verify(mockFuture).get(1, TimeUnit.SECONDS); }
@Test public void shouldNotStopJavaScriptExecutionWhenNoTimeoutConfigured() throws Exception { ScriptObject loopScript = getJavascript("while(true) { }"); setTimeout(0); testEvaluator.<Void>evaluateScript(loopScript, null); verify(mockFuture).get(); }
@Test public void shouldNotStopGroovyScriptExecutionWhenTimeoutReached() throws Exception { ScriptObject loopScript = getGroovyScript("while(true) { }"); setTimeout(0); testEvaluator.<Void>evaluateScript(loopScript, null); verify(mockFuture).get(); }
@Test public void shouldStopGroovyScriptExecutionWhenTimeoutReached() throws Exception { ScriptObject loopScript = getGroovyScript("while(true) { }"); setTimeout(1); testEvaluator.<Void>evaluateScript(loopScript, null); verify(mockFuture).get(1, TimeUnit.SECONDS); }
@Test public void shouldDelegateToConfiguredScriptEvaluator() throws Exception { testEvaluator = new ThreadPoolScriptEvaluator(scriptEngineManager, Executors.newSingleThreadExecutor(), mockEvaluator); ScriptObject testScript = getGroovyScript("x + 1"); Bindings bindings = new SimpleBindings(); bindings.put("x", 3); int expectedResult = 42; given(mockEvaluator.evaluateScript(testScript, bindings)).willReturn(expectedResult); Number result = testEvaluator.evaluateScript(testScript, bindings); verify(mockEvaluator).evaluateScript(testScript, bindings); assertThat(result.intValue()).isEqualTo(expectedResult); }