InterpreterThread implements Runnable { private Map<String,Boolean> runCommands(List<String> commands) { LOG.debug("Executing run commands"); Map<String,Boolean> resultsOfExecution = new HashMap<>(); for (String aCommand : commands) { LOG.debug("Executing command " + aCommand); try { resultsOfExecution.put(aCommand,JEP_INSTANCE.eval(aCommand)); } catch (JepException e) { resultsOfExecution.put(aCommand,Boolean.FALSE); errorEncountered = true; LOG.error("Error while running command " + aCommand, e); return resultsOfExecution; } } return resultsOfExecution; } InterpreterThread(BlockingQueue<PythonRequestResponse> requestQueue,
      BlockingQueue<PythonRequestResponse> responseQueue,String threadID); Jep getEngineReference(); void preInitInterpreter(Map<PythonInterpreterConfig, Object> preInitConfigs); void startInterpreter(); void stopInterpreter(); @Override void run(); Jep getJEP_INSTANCE(); void setJEP_INSTANCE(Jep JEP_INSTANCE); long getTimeOutToPollFromRequestQueue(); void setTimeOutToPollFromRequestQueue(long timeOutToPollFromRequestQueue); TimeUnit getTimeUnitsToPollFromRequestQueue(); void setTimeUnitsToPollFromRequestQueue(TimeUnit timeUnitsToPollFromRequestQueue); boolean isStopped(); void setStopped(boolean stopped); BlockingQueue<PythonRequestResponse> getRequestQueue(); void setRequestQueue(BlockingQueue<PythonRequestResponse> requestQueue); BlockingQueue<PythonRequestResponse> getResponseQueue(); void setResponseQueue(BlockingQueue<PythonRequestResponse> responseQueue); Map<PythonInterpreterConfig, Object> getInitConfigs(); void setInitConfigs(Map<PythonInterpreterConfig, Object> initConfigs); boolean isBusy(); void setBusy(boolean busy); SpinPolicy getSpinPolicy(); void setSpinPolicy(SpinPolicy spinPolicy); static final String JEP_LIBRARY_NAME; static final String PYTHON_DEL_COMMAND; transient Jep JEP_INSTANCE; }
@JepPythonTestContext(jepPythonBasedTest = true) @Test public void testRunCommands() throws Exception { long currentTime = System.currentTimeMillis(); File tempFile = File.createTempFile("apexpythonunittestruncommands-", ".txt"); tempFile.deleteOnExit(); String filePath = tempFile.getAbsolutePath(); assertEquals(0L,tempFile.length()); List<String> commands = new ArrayList(); commands.add("fileHandle = open('" + filePath + "', 'w')"); commands.add("fileHandle.write('" + currentTime + "')"); commands.add("fileHandle.flush()"); commands.add("fileHandle.close()"); runCommands(commands); assertEquals(("" + currentTime).length(), tempFile.length()); List<String> errorCommands = new ArrayList(); errorCommands.add("1+2"); errorCommands.add("3+"); PythonRequestResponse<Void> response = runCommands(errorCommands); Map<String,Boolean> responseStatus = response.getPythonInterpreterResponse().getCommandStatus(); assertTrue(responseStatus.get(errorCommands.get(0))); assertFalse(responseStatus.get(errorCommands.get(1))); }