Search in sources :

Example 1 with JepPythonTestContext

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<>()));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JepPythonTestContext(org.apache.apex.malhar.python.test.JepPythonTestContext) Test(org.junit.Test)

Example 2 with JepPythonTestContext

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;
        }
    }
}
Also used : BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) JepPythonTestContext(org.apache.apex.malhar.python.test.JepPythonTestContext) Test(org.junit.Test)

Example 3 with JepPythonTestContext

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());
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) Attribute(com.datatorrent.api.Attribute) Launcher(org.apache.apex.api.Launcher) EmbeddedAppLauncher(org.apache.apex.api.EmbeddedAppLauncher) JepPythonTestContext(org.apache.apex.malhar.python.test.JepPythonTestContext) Test(org.junit.Test)

Example 4 with JepPythonTestContext

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));
}
Also used : ArrayList(java.util.ArrayList) MethodCallRequestPayload(org.apache.apex.malhar.python.base.requestresponse.MethodCallRequestPayload) JepPythonTestContext(org.apache.apex.malhar.python.test.JepPythonTestContext) Test(org.junit.Test)

Example 5 with JepPythonTestContext

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));
}
Also used : EvalCommandRequestPayload(org.apache.apex.malhar.python.base.requestresponse.EvalCommandRequestPayload) HashMap(java.util.HashMap) Random(java.util.Random) JepPythonTestContext(org.apache.apex.malhar.python.test.JepPythonTestContext) Test(org.junit.Test)

Aggregations

JepPythonTestContext (org.apache.apex.malhar.python.test.JepPythonTestContext)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)3 File (java.io.File)2 HashMap (java.util.HashMap)2 Attribute (com.datatorrent.api.Attribute)1 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 Random (java.util.Random)1 EmbeddedAppLauncher (org.apache.apex.api.EmbeddedAppLauncher)1 Launcher (org.apache.apex.api.Launcher)1 EvalCommandRequestPayload (org.apache.apex.malhar.python.base.requestresponse.EvalCommandRequestPayload)1 MethodCallRequestPayload (org.apache.apex.malhar.python.base.requestresponse.MethodCallRequestPayload)1 Configuration (org.apache.hadoop.conf.Configuration)1