ShinyInterpreter extends AbstractInterpreter { @Override public void cancel(InterpreterContext context) throws InterpreterException { String shinyApp = context.getStringLocalProperty("app", DEFAULT_APP_NAME); IRInterpreter irInterpreter = getIRInterpreter(shinyApp); irInterpreter.cancel(context); } ShinyInterpreter(Properties properties); @Override void open(); @Override void close(); @Override void cancel(InterpreterContext context); @Override FormType getFormType(); @Override int getProgress(InterpreterContext context); @Override ZeppelinContext getZeppelinContext(); @Override InterpreterResult internalInterpret(String st, InterpreterContext context);  }
@Test public void testShinyApp() throws IOException, InterpreterException, InterruptedException, UnirestException { InterpreterContext context = getInterpreterContext(); context.getLocalProperties().put("type", "ui"); InterpreterResult result = interpreter.interpret(IOUtils.toString(getClass().getResource("/ui.R")), context); assertEquals(InterpreterResult.Code.SUCCESS, result.code()); context = getInterpreterContext(); context.getLocalProperties().put("type", "server"); result = interpreter.interpret(IOUtils.toString(getClass().getResource("/server.R")), 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(); Thread.sleep(5 * 1000); List<InterpreterResultMessage> resultMessages = context2.out.toInterpreterResultMessage(); assertEquals(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); HttpResponse<String> response = Unirest.get(shinyURL).asString(); assertEquals(200, response.getStatus()); assertTrue(response.getBody(), response.getBody().contains("Shiny Text")); context = getInterpreterContext(); context.getLocalProperties().put("type", "ui"); context.getLocalProperties().put("app", "app2"); result = interpreter.interpret(IOUtils.toString(getClass().getResource("/ui.R")), context); assertEquals(InterpreterResult.Code.SUCCESS, result.code()); context = getInterpreterContext(); context.getLocalProperties().put("type", "server"); context.getLocalProperties().put("app", "app2"); result = interpreter.interpret(IOUtils.toString(getClass().getResource("/server.R")), context); assertEquals(InterpreterResult.Code.SUCCESS, result.code()); final InterpreterContext context3 = getInterpreterContext(); context3.getLocalProperties().put("type", "run"); context3.getLocalProperties().put("app", "app2"); thread = new Thread(() -> { try { interpreter.interpret("", context3); } catch (Exception e) { e.printStackTrace(); } }); thread.start(); Thread.sleep(5 * 1000); resultMessages = context3.out.toInterpreterResultMessage(); assertEquals(1, resultMessages.size()); assertEquals(InterpreterResult.Type.HTML, resultMessages.get(0).getType()); resultMessageData = resultMessages.get(0).getData(); assertTrue(resultMessageData, resultMessageData.contains("<iframe")); matcher = urlPattern.matcher(resultMessageData); if (!matcher.matches()) { fail("Unable to extract url: " + resultMessageData); } String shinyURL2 = matcher.group(1); response = Unirest.get(shinyURL2).asString(); assertEquals(200, response.getStatus()); assertTrue(response.getBody(), response.getBody().contains("Shiny Text")); interpreter.cancel(getInterpreterContext()); 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")); } response = Unirest.get(shinyURL2).asString(); assertEquals(200, response.getStatus()); assertTrue(response.getBody(), response.getBody().contains("Shiny Text")); }
@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")), context); assertEquals(InterpreterResult.Code.SUCCESS, result.code()); context = getInterpreterContext(); context.getLocalProperties().put("type", "server"); result = interpreter.interpret(IOUtils.toString(getClass().getResource("/server.R")), 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(); Thread.sleep(5 * 1000); List<InterpreterResultMessage> resultMessages = context2.out.toInterpreterResultMessage(); assertEquals(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); 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")); interpreter.cancel(getInterpreterContext()); 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")); } }