Search in sources :

Example 41 with InterpreterResultMessage

use of org.apache.zeppelin.interpreter.InterpreterResultMessage in project metron by apache.

the class StellarInterpreterTest method testExecuteNoop.

/**
 * The interpreter should handle if no value is returned.  Some Stellar expressions
 * will result in no value.
 */
@Test
public void testExecuteNoop() {
    // x is undefined and will have no result
    InterpreterResult result = interpreter.interpret("x", context);
    // validate the result
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertEquals(1, result.message().size());
    // validate the message
    InterpreterResultMessage message = result.message().get(0);
    assertEquals(0, message.getData().length());
    assertEquals(InterpreterResult.Type.TEXT, message.getType());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) InterpreterResultMessage(org.apache.zeppelin.interpreter.InterpreterResultMessage) Test(org.junit.jupiter.api.Test)

Example 42 with InterpreterResultMessage

use of org.apache.zeppelin.interpreter.InterpreterResultMessage in project metron by apache.

the class StellarInterpreterTest method testExecuteWithStellarMap.

/**
 * Ensure that Stellar maps are displayed correctly in Zeppelin.
 */
@Test
public void testExecuteWithStellarMap() {
    final String expected = "{foo=2, key=val}";
    InterpreterResult result = interpreter.interpret("{ 'foo':2, 'key':'val' }", context);
    // validate the result
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertEquals(1, result.message().size());
    // validate the message
    InterpreterResultMessage message = result.message().get(0);
    assertEquals(expected, message.getData());
    assertEquals(InterpreterResult.Type.TEXT, message.getType());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) InterpreterResultMessage(org.apache.zeppelin.interpreter.InterpreterResultMessage) Test(org.junit.jupiter.api.Test)

Example 43 with InterpreterResultMessage

use of org.apache.zeppelin.interpreter.InterpreterResultMessage in project zeppelin by apache.

the class ShinyInterpreterTest method testInvalidShinyApp.

@Test
public void testInvalidShinyApp() throws IOException, InterpreterException, InterruptedException, UnirestException {
    InterpreterContext context = getInterpreterContext();
    context.getLocalProperties().put("type", "ui");
    InterpreterResult result = interpreter.interpret(IOUtils.toString(getClass().getResource("/invalid_ui.R"), StandardCharsets.UTF_8), context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    context = getInterpreterContext();
    context.getLocalProperties().put("type", "server");
    result = interpreter.interpret(IOUtils.toString(getClass().getResource("/server.R"), StandardCharsets.UTF_8), context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    final InterpreterContext context2 = getInterpreterContext();
    context2.getLocalProperties().put("type", "run");
    Thread thread = new Thread(() -> {
        try {
            interpreter.interpret("", context2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    thread.start();
    // wait for the shiny app start
    Thread.sleep(5 * 1000);
    List<InterpreterResultMessage> resultMessages = context2.out.toInterpreterResultMessage();
    assertEquals(resultMessages.toString(), 1, resultMessages.size());
    assertEquals(InterpreterResult.Type.HTML, resultMessages.get(0).getType());
    String resultMessageData = resultMessages.get(0).getData();
    assertTrue(resultMessageData, resultMessageData.contains("<iframe"));
    Pattern urlPattern = Pattern.compile(".*src=\"(http\\S*)\".*", Pattern.DOTALL);
    Matcher matcher = urlPattern.matcher(resultMessageData);
    if (!matcher.matches()) {
        fail("Unable to extract url: " + resultMessageData);
    }
    String shinyURL = matcher.group(1);
    // call shiny app via rest api
    HttpResponse<String> response = Unirest.get(shinyURL).asString();
    assertEquals(500, response.getStatus());
    resultMessages = context2.out.toInterpreterResultMessage();
    assertTrue(resultMessages.get(1).getData(), resultMessages.get(1).getData().contains("object 'Invalid_code' not found"));
    // cancel paragraph to stop shiny app
    interpreter.cancel(getInterpreterContext());
    // wait for shiny app to be stopped
    Thread.sleep(1000);
    try {
        Unirest.get(shinyURL).asString();
        fail("Should fail to connect to shiny app");
    } catch (Exception e) {
        assertTrue(e.getMessage(), e.getMessage().contains("Connection refused"));
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) InterpreterResultMessage(org.apache.zeppelin.interpreter.InterpreterResultMessage) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) IOException(java.io.IOException) Test(org.junit.Test)

Example 44 with InterpreterResultMessage

use of org.apache.zeppelin.interpreter.InterpreterResultMessage in project zeppelin by apache.

the class IPythonInterpreterTest method testGrpcFrameSize.

@Test
public void testGrpcFrameSize() throws InterpreterException, IOException {
    tearDown();
    Properties properties = initIntpProperties();
    properties.setProperty("zeppelin.jupyter.kernel.grpc.message_size", "4000");
    startInterpreter(properties);
    // to make this test can run under both python2 and python3
    InterpreterResult result = interpreter.interpret("from __future__ import print_function", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    InterpreterContext context = getInterpreterContext();
    result = interpreter.interpret("print('1'*4000)", context);
    assertEquals(InterpreterResult.Code.ERROR, result.code());
    List<InterpreterResultMessage> interpreterResultMessages = context.out.toInterpreterResultMessage();
    assertEquals(1, interpreterResultMessages.size());
    assertTrue(interpreterResultMessages.get(0).getData().contains("exceeds maximum size 4000"));
    // next call continue work
    result = interpreter.interpret("print(1)", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    tearDown();
    // increase framesize to make it work
    properties.setProperty("zeppelin.ipython.grpc.message_size", "5000");
    startInterpreter(properties);
    // to make this test can run under both python2 and python3
    result = interpreter.interpret("from __future__ import print_function", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    context = getInterpreterContext();
    result = interpreter.interpret("print('1'*3000)", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Properties(java.util.Properties) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) InterpreterResultMessage(org.apache.zeppelin.interpreter.InterpreterResultMessage) Test(org.junit.Test)

Example 45 with InterpreterResultMessage

use of org.apache.zeppelin.interpreter.InterpreterResultMessage in project zeppelin by apache.

the class IPythonInterpreterTest method testIPythonAdvancedFeatures.

@Test
public void testIPythonAdvancedFeatures() throws InterpreterException, InterruptedException, IOException {
    // ipython help
    InterpreterContext context = getInterpreterContext();
    InterpreterResult result = interpreter.interpret("range?", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    List<InterpreterResultMessage> interpreterResultMessages = context.out.toInterpreterResultMessage();
    assertTrue(interpreterResultMessages.get(0).getData().contains("range(stop)"));
    // timeit
    context = getInterpreterContext();
    result = interpreter.interpret("%timeit range(100)", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    interpreterResultMessages = context.out.toInterpreterResultMessage();
    assertTrue(interpreterResultMessages.get(0).getData().contains("loops"));
    // cancel
    final InterpreterContext context2 = getInterpreterContext();
    new Thread() {

        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                interpreter.cancel(context2);
            } catch (InterpreterException e) {
                e.printStackTrace();
            }
        }
    }.start();
    result = interpreter.interpret("import time\ntime.sleep(10)", context2);
    assertEquals(InterpreterResult.Code.ERROR, result.code());
    interpreterResultMessages = context2.out.toInterpreterResultMessage();
    assertTrue(interpreterResultMessages.get(0).getData().contains("KeyboardInterrupt"));
}
Also used : InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) InterpreterResultMessage(org.apache.zeppelin.interpreter.InterpreterResultMessage) Test(org.junit.Test)

Aggregations

InterpreterResultMessage (org.apache.zeppelin.interpreter.InterpreterResultMessage)80 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)78 Test (org.junit.Test)60 InterpreterContext (org.apache.zeppelin.interpreter.InterpreterContext)55 Properties (java.util.Properties)17 InterpreterException (org.apache.zeppelin.interpreter.InterpreterException)15 IOException (java.io.IOException)13 File (java.io.File)8 TimeoutException (java.util.concurrent.TimeoutException)7 Waiter (net.jodah.concurrentunit.Waiter)7 AuthenticationInfo (org.apache.zeppelin.user.AuthenticationInfo)6 Test (org.junit.jupiter.api.Test)6 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)3 HashMap (java.util.HashMap)3 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 Map (java.util.Map)2 AngularObjectRegistry (org.apache.zeppelin.display.AngularObjectRegistry)2 CheckBox (org.apache.zeppelin.display.ui.CheckBox)2 Select (org.apache.zeppelin.display.ui.Select)2