PythonExecutionCachedEngine extends ExecutionCachedEngine<PythonExecutor> implements PythonExecutionEngine { @Override public PythonExecutionResult exec(Set<String> dependencies, String script, Map<String, Serializable> vars) { PythonExecutor executor = allocateExecutor(dependencies); try { return executor.exec(script, vars); } finally { releaseExecutor(executor); } }  @Override PythonExecutionResult exec(Set<String> dependencies, String script, Map<String, Serializable> vars); @Override PythonEvaluationResult eval(String prepareEnvironmentScript, String script, Map<String, Serializable> vars);  }
@Test public void testPythonExecutorReleasedAfterSuccessExecution() { final PythonExecutor pythonExecutor = mock(PythonExecutor.class); PythonExecutionCachedEngine engine = new PythonExecutionCachedEngine() { public PythonExecutor allocateExecutor(Set<String> dependencies) { return pythonExecutor; } }; engine.exec(Collections.<String>emptySet(),"", new HashMap<String, Serializable>()); verify(pythonExecutor).release(); }
@Test public void testPythonExecutorReleasedAfterException() { final String script = ""; final Map<String, Serializable> args = new HashMap<>(); final PythonExecutor pythonExecutor = mock(PythonExecutor.class); when(pythonExecutor.exec(script, args)).thenThrow(new IllegalArgumentException("")); PythonExecutionCachedEngine engine = new PythonExecutionCachedEngine() { public PythonExecutor allocateExecutor(Set<String> dependencies) { return pythonExecutor; } }; try { engine.exec(Collections.<String>emptySet(),script, args); } catch (Throwable t) { assertTrue(t instanceof IllegalArgumentException); } verify(pythonExecutor).release(); }