com.twosigma.beakerx.message.Message Java Examples

The following examples show how to use com.twosigma.beakerx.message.Message. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClasspathAddMvnDepsMagicCommandTest.java    From beakerx with Apache License 2.0 6 votes vote down vote up
@Test
public void handleClasspathReset() throws Exception {
  //given
  String allCode = CLASSPATH_ADD_MVN + " com.google.code.gson:gson:2.6.2";
  handleClasspathAddMvnDep(allCode, "gson-2.6.2.jar");
  kernel.clearMessages();
  ClasspathAddMvnMagicCommand mvnMagicCommand = configuration.getClasspathAddMvnMagicCommand(kernel);
  mvnMagicCommand.addRepo("jcenter", "jcenter");
  assertTrue(mvnMagicCommand.getRepos().get().size() == DEFAULT_MAVEN_REPOS.size() + 1);
  //when
  String resetCode = CLASSPATH_RESET;
  ClasspathResetMagicCommand resetMagicCommand = configuration.getClasspathResetMagicCommand(kernel);
  MagicCommand command = new MagicCommand(resetMagicCommand, resetCode);
  Code code = Code.createCode(resetCode, singletonList(command), NO_ERRORS, new Message(new Header(JupyterMessages.COMM_MSG, "session1")));
  code.execute(kernel, 1);
  //then
  List<Message> stderr = EvaluatorResultTestWatcher.getStdouts(kernel.getPublishedMessages());
  String text = (String) stderr.get(0).getContent().get("text");
  assertThat(text).contains("Reset done");
  List<String> deletedFiles = kernel.getFileService().getDeletedFiles();
  Assert.assertTrue(deletedFiles.contains(mvnMagicCommand.getCommandParams().getPathToCache()));
  Assert.assertTrue(deletedFiles.contains(mvnMagicCommand.getCommandParams().getPathToNotebookJars()));
  assertTrue(mvnMagicCommand.getRepos().get().size() == DEFAULT_MAVEN_REPOS.size());
}
 
Example #2
Source File: ClasspathAddRepoMagicCommandTest.java    From beakerx with Apache License 2.0 6 votes vote down vote up
@Test
public void addMvnLocalShouldReturnWarningWhenLocalMavenRepositoryNotExist() {
  //given
  try {
    BeakerxSystemProperty.fixUserHome("/home/xxx");
    String allCode = CLASSPATH_CONFIG_RESOLVER + " " + MVN_LOCAL;
    Code code = CodeFactory.create(allCode, commMsg(), kernel);
    //when
    code.execute(kernel, 1);
    //then
    List<Message> std = EvaluatorResultTestWatcher.getStdouts(kernel.getPublishedMessages());
    String text = (String) std.get(0).getContent().get("text");
    assertThat(text).contains("Warning: directory");
  } finally {
    BeakerxSystemProperty.setSystemUserHome();
  }
}
 
Example #3
Source File: TableDisplayActions.java    From beakerx with Apache License 2.0 6 votes vote down vote up
private void onDoubleClickAction(HashMap content, Message message) {
  CommActions actionType = CommActions.getByAction((String) content.get("event"));
  Object row = content.get("row");
  Object column = content.get("column");
  List<Object> params = new ArrayList<>();
  params.add(row);
  params.add(column);

  TableActionDetails details = new TableActionDetails();
  details.setActionType(actionType);
  details.setRow((Integer) row);
  details.setCol((Integer) column);
  tableDisplay.setDetails(details);

  tableDisplay.fireDoubleClick(params, message);
}
 
Example #4
Source File: KotlinAutotranslationTest.java    From beakerx with Apache License 2.0 6 votes vote down vote up
@Test
public void kotlinAutotranslationGetValue() throws InterruptedException {
  //given
  String code = "(beakerx[\"bar\"] as Map<String, String>)[\"key\"]";
  String serializedMap = "{\"type\":\"TableDisplay\",\"columnNames\":[\"Key\",\"Value\"],\"values\":[[\"key\",\"value\"]],\"subtype\":\"Dictionary\"}";
  String value = "value";
  //when
  autotranslationService.update("bar", serializedMap);
  Message message = getExecuteRequestMessage(code);
  kernelSocketsService.handleMsg(message);
  //then
  Optional<Message> idleMessage = waitForIdleMessage(kernelSocketsService.getKernelSockets());
  assertThat(idleMessage).isPresent();
  Message result = waitForResult(kernelSocketsService.getKernelSockets()).get();
  verifyResultValue(result, value);

}
 
Example #5
Source File: TableDisplayActions.java    From beakerx with Apache License 2.0 6 votes vote down vote up
private void onContextMenu(HashMap content, Message message) {
  CommActions actionType = CommActions.getByAction((String) content.get("event"));
  String menuKey = (String) content.get("itemKey");
  Object row = content.get("row");
  Object column = content.get("column");
  List<Object> params = new ArrayList<>();
  params.add(row);
  params.add(column);

  TableActionDetails details = new TableActionDetails();
  details.setActionType(actionType);
  details.setRow((Integer) row);
  details.setCol((Integer) column);
  tableDisplay.setDetails(details);

  tableDisplay.fireContextMenuClick(menuKey, params, message);
}
 
Example #6
Source File: GroovyKernelInfoHandlerTest.java    From beakerx with Apache License 2.0 5 votes vote down vote up
@Test
public void handle_sentMessageHasHeaderTypeIsKernelInfoReply() throws Exception {
  //when
  handler.handle(message);
  //then
  Message sentMessage = kernel.getSentMessages().get(0);
  Header header = sentMessage.getHeader();
  Assertions.assertThat(header).isNotNull();
  Assertions.assertThat(header.getType()).isEqualTo(KERNEL_INFO_REPLY.getName());
}
 
Example #7
Source File: MagicCommand.java    From beakerx with Apache License 2.0 5 votes vote down vote up
@Override
public void executeLastFrame(Code code, KernelFunctionality kernel, Message message, int executionCount) {
  MagicCommandOutcomeItem execute = execute(code, executionCount, true);
  execute.sendRepliesWithStatus(kernel, message, executionCount);
  TryResult result = execute.getResult();
  SimpleEvaluationObject seo = execute.getSimpleEvaluationObject();
  handleResult(seo, result);
}
 
Example #8
Source File: MessageCreator.java    From beakerx with Apache License 2.0 5 votes vote down vote up
public static Message buildOutputMessage(Message message, String text, boolean hasError) {
  Message reply = initMessage(STREAM, message);
  reply.setContent(new HashMap<>());
  reply.getContent().put(NAME, hasError ? STDERR : STDOUT);
  reply.getContent().put(TEXT, text);
  logger.debug("Console output:", "Error: " + hasError, text);
  return reply;
}
 
Example #9
Source File: MessageCreator.java    From beakerx with Apache License 2.0 5 votes vote down vote up
public static List<MessageHolder> createMessage(SimpleEvaluationObject seo) {
  logger.debug("Creating message response message from: " + seo);
  Message message = seo.getJupyterMessage();
  List<MessageHolder> ret = new ArrayList<>();
  if (isConsoleOutputMessage(seo)) {
    ret.addAll(createConsoleResult(seo, message));
  } else if (isError(seo.getStatus())) {
    ret.addAll(createError(seo, message));
  } else if (isFinish(seo.getStatus()) && seo.isShowResult()) {
    ret.addAll(createFinish(seo, message));
  } else {
    logger.debug("Unhandled status of SimpleEvaluationObject : " + seo.getStatus());
  }
  return ret;
}
 
Example #10
Source File: SparkImplicitSupportTest.java    From beakerx with Apache License 2.0 5 votes vote down vote up
@Test
public void sparkImplicit() throws Exception {
  try {
    //given
    enableSparkSupport(ENABLE_SPARK_SUPPORT, getKernelSocketsService());
    runSparkDataset(getKernelSocketsService());
    //when
    runCode("ds.display(1)", getKernelSocketsService());
    //then
    Optional<Message> table = waitForUpdateMessage(getKernelSocketsService().getKernelSockets(), matcher());
    assertThat(table).isPresent();
  } finally {
    stopSpark(getKernelSocketsService());
  }
}
 
Example #11
Source File: CommCloseHandlerTest.java    From beakerx with Apache License 2.0 5 votes vote down vote up
@Test
public void handleMessage_sentMessageHasSessionId() throws Exception {
  //given
  String expectedSessionId = message.getHeader().getSession();
  //when
  commCloseHandler.handle(message);
  //then
  Assertions.assertThat(kernel.getSentMessages()).isNotEmpty();
  Message sendMessage = kernel.getSentMessages().get(0);
  Assertions.assertThat(sendMessage.getHeader().getSession()).isEqualTo(expectedSessionId);
}
 
Example #12
Source File: CommMsgHandler.java    From beakerx with Apache License 2.0 5 votes vote down vote up
public void handle(Message message) {
  wrapBusyIdle(kernel, message, () -> {
    try {
      kernel.startEvaluation();
      handleMsg(message);
    } finally {
      kernel.endEvaluation();
    }
  });
}
 
Example #13
Source File: BxComm.java    From beakerx with Apache License 2.0 5 votes vote down vote up
public Message createUpdateMessage(List<ChangeItem> changes, HashMap<String, Object> state) {
  HashMap<String, Serializable> content = new HashMap<>();
  content.put(METHOD, UPDATE);
  changes.forEach(x -> state.put(x.getPropertyName(), x.getValue()));
  content.put(STATE, state);
  content.put(BUFFER_PATHS, new HashMap<>());
  return this.createMessage(COMM_MSG, Buffer.EMPTY, new Data(content));
}
 
Example #14
Source File: ClasspathMagicCommandTest.java    From beakerx with Apache License 2.0 5 votes vote down vote up
@Test
public void allowSpacesInJarPath() throws InterruptedException {
  Code code = CodeFactory.create("%classpath add jar \"./src/test/resources/jars/ with space.jar\"", commMsg(), kernel);
  code.execute(kernel, 1);
  Optional<Message> updateMessage = EvaluatorResultTestWatcher.waitForUpdateMessage(kernel);
  String text =  (String)TestWidgetUtils.getState(updateMessage.get()).get("value");
  assertThat(text).contains("with space.jar");
}
 
Example #15
Source File: MessageCreator.java    From beakerx with Apache License 2.0 5 votes vote down vote up
private static List<MessageHolder> createFinish(SimpleEvaluationObject seo, Message message) {
  List<MessageHolder> ret = new ArrayList<>();
  MessageHolder mh = createFinishResult(seo, message);
  if (mh != null) {
    ret.add(mh);
  }
  ret.add(new MessageHolder(SocketEnum.SHELL_SOCKET, buildReply(message, seo)));
  return ret;
}
 
Example #16
Source File: CommTest.java    From beakerx with Apache License 2.0 5 votes vote down vote up
@Test
public void commSend_sentMessageHasTypeIsCommClose() throws NoSuchAlgorithmException {
  //when
  comm.send(COMM_MSG, Buffer.EMPTY, comm.getData());
  //then
  assertThat(kernel.getPublishedMessages()).isNotEmpty();
  Message sendMessage = kernel.getPublishedMessages().get(0);
  assertThat(sendMessage.getHeader().getType())
          .isEqualTo(JupyterMessages.COMM_MSG.getName());
}
 
Example #17
Source File: SparkUI.java    From beakerx with Apache License 2.0 5 votes vote down vote up
private void handleAutoStart(Message message) {
  Map<String, Object> spark_options = sparkUiDefaults.getProfileByName(sparkUiDefaults.getCurrentProfileName());
  TryResult tryResult = initSparkContext(message, spark_options);
  if (tryResult.isError()) {
    sendError(tryResult.error());
  } else {
    sendStartDoneEvent("auto_start");
  }
}
 
Example #18
Source File: JupyterHandlerTest.java    From beakerx with Apache License 2.0 5 votes vote down vote up
@Test
public void handleInfoCommMessages_replyCommMessageHasCommsInfoContent() throws Exception {
  //given
  initKernelCommMapWithOneComm(kernel);
  //when
  commInfoHandler.handle(initInfoMessage());
  //then
  Assertions.assertThat(kernel.getSentMessages()).isNotEmpty();
  Message sendMessage = kernel.getSentMessages().get(0);
  Assertions.assertThat((Map) sendMessage.getContent().get(COMMS)).isNotEmpty();
}
 
Example #19
Source File: ImportMagicCommandTest.java    From beakerx with Apache License 2.0 5 votes vote down vote up
@Test
public void wrongImportFormat() {
  String allCode = "%import ";
  Code wrongFormatImport = CodeFactory.create(allCode, commMsg(), kernel);
  wrongFormatImport.execute(kernel, 1);
  List<Message> std = EvaluatorResultTestWatcher.getStderr(kernel.getPublishedMessages());
  String text = (String) std.get(0).getContent().get("text");
  assertThat(text).contains("Wrong format.");
}
 
Example #20
Source File: ClasspathMagicCommandTest.java    From beakerx with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateMsgWithWrongMagic() {
  //given
  String jar = SRC_TEST_RESOURCES + "BeakerXClasspathTest.jar";
  Code code = CodeFactory.create("%classpath2 add jar" + " " + jar, commMsg(), kernel);
  //when
  code.execute(kernel, 1);
  //then
  List<Message> std = EvaluatorResultTestWatcher.getStderr(kernel.getPublishedMessages());
  String text = (String) std.get(0).getContent().get("text");
  assertThat(text).contains("Inline magic %classpath2 add jar ./src/test/resources/BeakerXClasspathTest.jar not found\n");
  assertThat(kernel.getClasspath().size()).isEqualTo(0);
}
 
Example #21
Source File: ClassPathAddMvnCellMagicCommand.java    From beakerx with Apache License 2.0 5 votes vote down vote up
private MagicCommandOutcomeItem simpleStyle(String command, String commandCodeBlock, Message message) {
  if (commandCodeBlock != null) {
    command += "\n" + commandCodeBlock;
  }
  String[] commandLines = command.split(SPLIT_LINE_REGEX);
  unifyMvnLineFormat(commandLines);
  if (!validateCommandLines(commandLines)) {
    return new MagicCommandOutput(MagicCommandOutput.Status.ERROR, MVN_CELL_FORMAT_ERROR_MESSAGE);
  }
  List<MavenJarResolver.Dependency> dependencies =
          getDepsFromCommand(Arrays.copyOfRange(commandLines, 1, commandLines.length));

  return retrieve(mavenJarResolver -> mavenJarResolver.retrieve(dependencies, message));
}
 
Example #22
Source File: CompiledCodeRunner.java    From beakerx with Apache License 2.0 5 votes vote down vote up
public static void runCompiledCodeAndPublish(Message message, ExecuteCompiledCode handler, Object... params) {
  final SimpleEvaluationObject seo = initOutput(message);
  InternalVariable.setValue(seo);
  KernelManager.get().publish(singletonList(MessageCreator.buildClearOutput(message, true)));
  try {
    Object result = handler.executeCode(params);
    if (result != null) {
      List<MIMEContainer> resultString = MIMEContainerFactory.createMIMEContainers(result);
      KernelManager.get().publish(singletonList(MessageCreator.buildDisplayData(message, resultString)));
    }
  } catch (Exception e) {
    printError(message, seo, e);
  }
  seo.clrOutputHandler();
}
 
Example #23
Source File: SymjaMMACommOpenHandler.java    From symja_android_library with GNU General Public License v3.0 5 votes vote down vote up
public Handler<Message>[] getKernelControlChanelHandlers(String targetName) {
  if (TargetNamesEnum.KERNEL_CONTROL_CHANNEL.getTargetName().equalsIgnoreCase(targetName)) {
    return (Handler<Message>[]) KERNEL_CONTROL_CHANNEL_HANDLERS;
  } else if (TargetNamesEnum.BEAKER_AUTOTRANSLATION.getTargetName().equalsIgnoreCase(targetName)) {
    return (Handler<Message>[]) AUTOTRANSLATION_HANDLER;
  } else {
    return (Handler<Message>[]) new Handler<?>[0];
  }
}
 
Example #24
Source File: ExecuteRequestHandler.java    From beakerx with Apache License 2.0 5 votes vote down vote up
private void announceTheCode(Message message, String code) {
  Message reply = new Message(new Header(EXECUTE_INPUT, message.getHeader().getSession()));
  reply.setParentHeader(message.getHeader());
  reply.setIdentities(message.getIdentities());
  Map<String, Serializable> map1 = new HashMap<>(2);
  map1.put("execution_count", executionCount);
  map1.put("code", code);
  reply.setContent(map1);
  kernel.publish(singletonList(reply));
}
 
Example #25
Source File: JupyterHandlerTest.java    From beakerx with Apache License 2.0 5 votes vote down vote up
@Test
public void handleOpenCommMessage_shouldAddCommMessageToStorageMap() throws Exception {
  //given
  Message message = initCloseMessage();
  String commId = (String) message.getContent().get(COMM_ID);
  //when
  commOpenHandler.handle(message);
  //then
  Assertions.assertThat(kernel.isCommPresent(commId)).isTrue();
}
 
Example #26
Source File: SQLKernelTest.java    From beakerx with Apache License 2.0 5 votes vote down vote up
@Test
public void evaluate() throws Exception {
  //given
  Message message = getExecuteRequestMessage(CREATE_AND_SELECT_ALL);
  //when
  kernelSocketsService.handleMsg(message);
  //then
  Optional<Message> idleMessage = waitForIdleMessage(kernelSocketsService.getKernelSockets());
  verifyIdleMessage(idleMessage);
  verifyResult();
  verifyPublishedMsgs(kernelSocketsService);
  waitForSentMessage(kernelSocketsService.getKernelSockets());
  verifySentMsgs(kernelSocketsService);
}
 
Example #27
Source File: ClojureKernelInfoHandlerTest.java    From beakerx with Apache License 2.0 5 votes vote down vote up
@Test
public void handle_sentMessageHasContent() throws Exception {
  //when
  handler.handle(message);
  //then
  Message sentMessage = kernel.getSentMessages().get(0);
  Assertions.assertThat(sentMessage.getContent()).isNotEmpty();
}
 
Example #28
Source File: JupyterHandlerTest.java    From beakerx with Apache License 2.0 5 votes vote down vote up
@Test
public void handleOpenThenCloseCommMessages_shouldRemoveCommMessageFromStorageMap()
        throws Exception {
  //given
  Message openMessage = initOpenMessage();
  String commId = (String) openMessage.getContent().get(COMM_ID);
  //when
  commOpenHandler.handle(openMessage);
  commCloseHandler.handle(initCloseMessage());
  //then
  Assertions.assertThat(kernel.isCommPresent(commId)).isFalse();
}
 
Example #29
Source File: ClojureCommOpenHandlerTest.java    From beakerx with Apache License 2.0 5 votes vote down vote up
@Test
public void getControlHandlersWithTargetName_returnNotEmptyHandlersArray() throws Exception {
  //when
  Handler<Message>[] handlers = commOpenHandler.getKernelControlChanelHandlers(targetName);
  //then
  Assertions.assertThat(handlers).isNotEmpty();
}
 
Example #30
Source File: KotlinKernelInfoHandlerTest.java    From beakerx with Apache License 2.0 5 votes vote down vote up
@Test
public void handle_messageContentHasKotlinLabel() throws Exception {
  //when
  handler.handle(message);
  //then
  Message sentMessage = kernel.getSentMessages().get(0);
  Map<String, Serializable> map = sentMessage.getContent();
  Assertions.assertThat(map).isNotNull();
  Assertions.assertThat(map.get("implementation")).isEqualTo("kotlin");
}