use of org.apache.zeppelin.common.Message in project zeppelin by apache.
the class NotebookServerTest method testMakeSureNoAngularObjectBroadcastToWebsocketWhoFireTheEvent.
@Test
public void testMakeSureNoAngularObjectBroadcastToWebsocketWhoFireTheEvent() throws IOException, InterruptedException {
String note1Id = null;
try {
// create a notebook
note1Id = notebook.createNote("note1", anonymous);
// get reference to interpreterGroup
InterpreterGroup interpreterGroup = null;
List<InterpreterSetting> settings = notebook.getInterpreterSettingManager().get();
for (InterpreterSetting setting : settings) {
if (setting.getName().equals("md")) {
interpreterGroup = setting.getOrCreateInterpreterGroup("anonymous", note1Id);
break;
}
}
notebook.processNote(note1Id, note1 -> {
// start interpreter process
Paragraph p1 = note1.addNewParagraph(AuthenticationInfo.ANONYMOUS);
p1.setText("%md start remote interpreter process");
p1.setAuthenticationInfo(anonymous);
note1.run(p1.getId());
return null;
});
Status status = notebook.processNote(note1Id, note1 -> note1.getParagraph(0).getStatus());
// wait for paragraph finished
while (true) {
if (status == Job.Status.FINISHED) {
break;
}
Thread.sleep(100);
status = notebook.processNote(note1Id, note1 -> note1.getParagraph(0).getStatus());
}
// sleep for 1 second to make sure job running thread finish to fire event. See ZEPPELIN-3277
Thread.sleep(1000);
// add angularObject
interpreterGroup.getAngularObjectRegistry().add("object1", "value1", note1Id, null);
// create two sockets and open it
NotebookSocket sock1 = createWebSocket();
NotebookSocket sock2 = createWebSocket();
assertEquals(sock1, sock1);
assertNotEquals(sock1, sock2);
notebookServer.onOpen(sock1);
notebookServer.onOpen(sock2);
// getNote, getAngularObject
verify(sock1, times(0)).send(anyString());
// open the same notebook from sockets
notebookServer.onMessage(sock1, new Message(OP.GET_NOTE).put("id", note1Id).toJson());
notebookServer.onMessage(sock2, new Message(OP.GET_NOTE).put("id", note1Id).toJson());
reset(sock1);
reset(sock2);
// update object from sock1
notebookServer.onMessage(sock1, new Message(OP.ANGULAR_OBJECT_UPDATED).put("noteId", note1Id).put("name", "object1").put("value", "value1").put("interpreterGroupId", interpreterGroup.getId()).toJson());
// expect object is broadcasted except for where the update is created
verify(sock1, times(0)).send(anyString());
verify(sock2, times(1)).send(anyString());
} finally {
if (note1Id != null) {
notebook.removeNote(note1Id, anonymous);
}
}
}
use of org.apache.zeppelin.common.Message in project zeppelin by apache.
the class NotebookServerTest method unbindAngularObjectFromRemoteForParagraphs.
@Test
public void unbindAngularObjectFromRemoteForParagraphs() throws Exception {
// Given
final String varName = "name";
final String value = "val";
final Message messageReceived = new Message(OP.ANGULAR_OBJECT_CLIENT_UNBIND).put("noteId", "noteId").put("name", varName).put("paragraphId", "paragraphId");
try {
final Notebook notebook = mock(Notebook.class);
notebookServer.setNotebook(() -> notebook);
notebookServer.setNotebookService(() -> notebookService);
final Note note = mock(Note.class, RETURNS_DEEP_STUBS);
when(notebook.processNote(eq("noteId"), Mockito.any())).then(e -> e.getArgumentAt(1, NoteProcessor.class).process(note));
final Paragraph paragraph = mock(Paragraph.class, RETURNS_DEEP_STUBS);
when(note.getParagraph("paragraphId")).thenReturn(paragraph);
final RemoteAngularObjectRegistry mdRegistry = mock(RemoteAngularObjectRegistry.class);
final InterpreterGroup mdGroup = new InterpreterGroup("mdGroup");
mdGroup.setAngularObjectRegistry(mdRegistry);
when(paragraph.getBindedInterpreter().getInterpreterGroup()).thenReturn(mdGroup);
final AngularObject<String> ao1 = AngularObjectBuilder.build(varName, value, "noteId", "paragraphId");
when(mdRegistry.removeAndNotifyRemoteProcess(varName, "noteId", "paragraphId")).thenReturn(ao1);
NotebookSocket conn = mock(NotebookSocket.class);
NotebookSocket otherConn = mock(NotebookSocket.class);
final String mdMsg1 = notebookServer.serializeMessage(new Message(OP.ANGULAR_OBJECT_REMOVE).put("angularObject", ao1).put("interpreterGroupId", "mdGroup").put("noteId", "noteId").put("paragraphId", "paragraphId"));
notebookServer.getConnectionManager().noteSocketMap.put("noteId", asList(conn, otherConn));
// When
notebookServer.angularObjectClientUnbind(conn, messageReceived);
// Then
verify(mdRegistry, never()).removeAndNotifyRemoteProcess(varName, "noteId", null);
verify(otherConn).send(mdMsg1);
} finally {
// reset these so that it won't affect other tests
notebookServer.setNotebook(() -> NotebookServerTest.notebook);
notebookServer.setNotebookService(() -> NotebookServerTest.notebookService);
}
}
use of org.apache.zeppelin.common.Message in project zeppelin by apache.
the class NotebookServerTest method testCollaborativeEditing.
@Test
public void testCollaborativeEditing() throws IOException {
if (!ZeppelinConfiguration.create().isZeppelinNotebookCollaborativeModeEnable()) {
return;
}
NotebookSocket sock1 = createWebSocket();
NotebookSocket sock2 = createWebSocket();
String noteName = "Note with millis " + System.currentTimeMillis();
notebookServer.onMessage(sock1, new Message(OP.NEW_NOTE).put("name", noteName).toJson());
NoteInfo createdNoteInfo = null;
for (NoteInfo noteInfo : notebook.getNotesInfo()) {
if (notebook.processNote(noteInfo.getId(), Note::getName).equals(noteName)) {
createdNoteInfo = noteInfo;
break;
}
}
Message message = new Message(OP.GET_NOTE).put("id", createdNoteInfo.getId());
notebookServer.onMessage(sock1, message.toJson());
notebookServer.onMessage(sock2, message.toJson());
Paragraph paragraph = notebook.processNote(createdNoteInfo.getId(), createdNote -> {
return createdNote.getParagraphs().get(0);
});
String paragraphId = paragraph.getId();
String[] patches = new String[] { // ABC
"@@ -0,0 +1,3 @@\n+ABC\n", // press Enter
"@@ -1,3 +1,4 @@\n ABC\n+%0A\n", // abc
"@@ -1,4 +1,7 @@\n ABC%0A\n+abc\n", // add text in two string
"@@ -1,7 +1,45 @@\n ABC\n-%0Aabc\n+ ssss%0Aabc ssss\n" };
int sock1SendCount = 0;
int sock2SendCount = 0;
reset(sock1);
reset(sock2);
patchParagraph(sock1, paragraphId, patches[0]);
assertEquals("ABC", paragraph.getText());
verify(sock1, times(sock1SendCount)).send(anyString());
verify(sock2, times(++sock2SendCount)).send(anyString());
patchParagraph(sock2, paragraphId, patches[1]);
assertEquals("ABC\n", paragraph.getText());
verify(sock1, times(++sock1SendCount)).send(anyString());
verify(sock2, times(sock2SendCount)).send(anyString());
patchParagraph(sock1, paragraphId, patches[2]);
assertEquals("ABC\nabc", paragraph.getText());
verify(sock1, times(sock1SendCount)).send(anyString());
verify(sock2, times(++sock2SendCount)).send(anyString());
patchParagraph(sock2, paragraphId, patches[3]);
assertEquals("ABC ssss\nabc ssss", paragraph.getText());
verify(sock1, times(++sock1SendCount)).send(anyString());
verify(sock2, times(sock2SendCount)).send(anyString());
notebook.removeNote(createdNoteInfo.getId(), anonymous);
}
use of org.apache.zeppelin.common.Message in project zeppelin by apache.
the class NotebookServerTest method patchParagraph.
private void patchParagraph(NotebookSocket noteSocket, String paragraphId, String patch) {
Message message = new Message(OP.PATCH_PARAGRAPH);
message.put("patch", patch);
message.put("id", paragraphId);
notebookServer.onMessage(noteSocket, message.toJson());
}
use of org.apache.zeppelin.common.Message in project zeppelin by apache.
the class NotebookServer method onOutputUpdated.
/**
* When application update output.
*/
@Override
public void onOutputUpdated(String noteId, String paragraphId, int index, String appId, InterpreterResult.Type type, String output) {
Message msg = new Message(OP.APP_UPDATE_OUTPUT).put("noteId", noteId).put("paragraphId", paragraphId).put("index", index).put("type", type).put("appId", appId).put("data", output);
connectionManager.broadcast(noteId, msg);
}
Aggregations