use of org.apache.apex.malhar.python.test.JepPythonTestContext in project apex-malhar by apache.
the class InterpreterWrapperTest method testDelayedResponseQueue.
@JepPythonTestContext(jepPythonBasedTest = true)
@Test
public void testDelayedResponseQueue() throws Exception {
List<String> sequenceOfCommands = new ArrayList();
sequenceOfCommands.add("import time");
sequenceOfCommands.add("x=4;time.sleep(1)");
PythonInterpreterRequest<Void> requestOne = buildRequestObjectForVoidGenericCommand(sequenceOfCommands, 300, TimeUnit.MILLISECONDS);
PythonRequestResponse<Void> resultOne = interpreterWrapper.runCommands(1L, 1L, requestOne);
HashMap<String, Object> evalParams = new HashMap<>();
evalParams.put("y", 2);
PythonInterpreterRequest<Long> requestTwo = buildRequestObjectForLongEvalCommand("x = x * y;time.sleep(1)", "x", evalParams, 10, TimeUnit.MILLISECONDS, false);
PythonRequestResponse<Long> result = interpreterWrapper.eval(1L, 1L, requestTwo);
Thread.sleep(3000);
// only the next command will result in the queue getting drained of previous requests hence below
sequenceOfCommands = new ArrayList();
sequenceOfCommands.add("x=5");
PythonInterpreterRequest<Void> requestThree = buildRequestObjectForVoidGenericCommand(sequenceOfCommands, 300, TimeUnit.MILLISECONDS);
PythonRequestResponse<Void> resultThree = interpreterWrapper.runCommands(1L, 1L, requestThree);
assertFalse(delayedResponseQueueForWrapper.isEmpty());
assertEquals(2, delayedResponseQueueForWrapper.drainTo(new ArrayList<>()));
}
use of org.apache.apex.malhar.python.test.JepPythonTestContext in project apex-malhar by apache.
the class InterpreterThreadTest method testScriptCall.
@JepPythonTestContext(jepPythonBasedTest = true)
@Test
public void testScriptCall() throws Exception {
File tempFileForScript = File.createTempFile("apexpythonunittestscript-", ".py");
tempFileForScript.deleteOnExit();
String filePathForFactorialScript = tempFileForScript.getAbsolutePath();
migrateFileFromResourcesFolderToTemp("factorial.py", filePathForFactorialScript);
PythonRequestResponse<Void> methodCallRequest = buildRequestResponseObjectForVoidPayload(PythonCommandType.SCRIPT_COMMAND);
methodCallRequest.getPythonInterpreterRequest().getScriptExecutionRequestPayload().setScriptName(filePathForFactorialScript);
pythonEngineThread.getRequestQueue().put(methodCallRequest);
// wait for command to be processed
Thread.sleep(1000);
PythonRequestResponse<Void> methodCallResponse = pythonEngineThread.getResponseQueue().poll(1, TimeUnit.SECONDS);
Map<String, Boolean> commandStatus = methodCallResponse.getPythonInterpreterResponse().getCommandStatus();
assertTrue(commandStatus.get(filePathForFactorialScript));
try (BufferedReader br = new BufferedReader(new FileReader("target/factorial-result.txt"))) {
String line;
while ((line = br.readLine()) != null) {
// asset factorial is calculated as written in script in resources
assertEquals(120, Integer.parseInt(line));
// There is only one line in the file per the python script
break;
}
}
}
use of org.apache.apex.malhar.python.test.JepPythonTestContext in project apex-malhar by apache.
the class PythonExecutorApplicationTest method testApplication.
@JepPythonTestContext(jepPythonBasedTest = true)
@Test
public void testApplication() throws Exception {
Configuration conf = new Configuration(false);
conf.addResource(this.getClass().getResourceAsStream("/META-INF/properties.xml"));
EmbeddedAppLauncher<?> launcher = Launcher.getLauncher(Launcher.LaunchMode.EMBEDDED);
Attribute.AttributeMap launchAttributes = new Attribute.AttributeMap.DefaultAttributeMap();
launchAttributes.put(EmbeddedAppLauncher.RUN_ASYNC, true);
PythonExecutorApplication pythonExecutorApplication = new PythonExecutorApplication();
pythonExecutorApplication.outputFn = outputFn;
Launcher.AppHandle appHandle = launcher.launchApp(pythonExecutorApplication, conf, launchAttributes);
int sleepTimeCounterForLoopExit = 0;
int sleepTimePerIteration = 1000;
// wait until expected result count or timeout
while (results.size() < pythonExecutorApplication.pojoDataGenerator.getMaxTuples()) {
sleepTimeCounterForLoopExit += sleepTimePerIteration;
if (sleepTimeCounterForLoopExit > 30000) {
break;
}
LOG.info("Test sleeping until the application time out is reached");
Thread.sleep(sleepTimePerIteration);
}
appHandle.shutdown(Launcher.ShutdownMode.KILL);
assertEquals(pythonExecutorApplication.pojoDataGenerator.getMaxTuples(), results.size());
}
use of org.apache.apex.malhar.python.test.JepPythonTestContext in project apex-malhar by apache.
the class InterpreterThreadTest method testMethodCall.
@JepPythonTestContext(jepPythonBasedTest = true)
@Test
public void testMethodCall() throws Exception {
String methodName = "jepMultiply";
List<String> commands = new ArrayList();
commands.add("def " + methodName + "(firstnum, secondnum):\n" + // Note that this cannot be split as multiple commands
"\treturn (firstnum * secondnum)\n");
runCommands(commands);
List<Object> params = new ArrayList<>();
params.add(5L);
params.add(25L);
PythonRequestResponse<Long> methodCallRequest = buildRequestResponseObjectForLongPayload(PythonCommandType.METHOD_INVOCATION_COMMAND);
MethodCallRequestPayload requestPayload = methodCallRequest.getPythonInterpreterRequest().getMethodCallRequest();
requestPayload.setNameOfMethod(methodName);
requestPayload.setArgs(params);
methodCallRequest.getPythonInterpreterRequest().setExpectedReturnType(Long.class);
pythonEngineThread.getRequestQueue().put(methodCallRequest);
// wait for command to be processed
Thread.sleep(1000);
PythonRequestResponse<Long> methodCallResponse = pythonEngineThread.getResponseQueue().poll(1, TimeUnit.SECONDS);
assertEquals(methodCallResponse.getPythonInterpreterResponse().getResponse(), 125L);
Map<String, Boolean> commandStatus = methodCallResponse.getPythonInterpreterResponse().getCommandStatus();
assertTrue(commandStatus.get(methodName));
params.remove(1);
methodCallRequest = buildRequestResponseObjectForLongPayload(PythonCommandType.METHOD_INVOCATION_COMMAND);
requestPayload = methodCallRequest.getPythonInterpreterRequest().getMethodCallRequest();
requestPayload.setNameOfMethod(methodName);
requestPayload.setArgs(params);
methodCallRequest.getPythonInterpreterRequest().setExpectedReturnType(Long.class);
pythonEngineThread.getRequestQueue().put(methodCallRequest);
// wait for command to be processed
Thread.sleep(1000);
methodCallResponse = pythonEngineThread.getResponseQueue().poll(1, TimeUnit.SECONDS);
commandStatus = methodCallResponse.getPythonInterpreterResponse().getCommandStatus();
assertFalse(commandStatus.get(methodName));
}
use of org.apache.apex.malhar.python.test.JepPythonTestContext in project apex-malhar by apache.
the class InterpreterThreadTest method testEvalCall.
@JepPythonTestContext(jepPythonBasedTest = true)
@Test
public void testEvalCall() throws Exception {
String expression = new String("x = a + b");
Random random = new Random();
int a = random.nextInt(100);
int b = random.nextInt(100);
Map<String, Object> argsForEval = new HashMap<>();
argsForEval.put("a", a);
argsForEval.put("b", b);
PythonRequestResponse<Long> methodCallRequest = buildRequestResponseObjectForLongPayload(PythonCommandType.EVAL_COMMAND);
EvalCommandRequestPayload payload = methodCallRequest.getPythonInterpreterRequest().getEvalCommandRequestPayload();
payload.setEvalCommand(expression);
payload.setParamsForEvalCommand(argsForEval);
payload.setDeleteVariableAfterEvalCall(true);
payload.setVariableNameToExtractInEvalCall("x");
methodCallRequest.getPythonInterpreterRequest().setExpectedReturnType(Long.class);
pythonEngineThread.getRequestQueue().put(methodCallRequest);
// wait for command to be processed
Thread.sleep(1000);
PythonRequestResponse<Integer> methodCallResponse = pythonEngineThread.getResponseQueue().poll(1, TimeUnit.SECONDS);
Map<String, Boolean> commandStatus = methodCallResponse.getPythonInterpreterResponse().getCommandStatus();
assertTrue(commandStatus.get(expression));
assertEquals(methodCallResponse.getPythonInterpreterResponse().getResponse(), (long) (a + b));
}
Aggregations