mirror of
https://github.com/apache/zeppelin
synced 2026-05-24 09:38:26 +00:00
add zeppelinhub ws client test
This commit is contained in:
parent
ebcf692b72
commit
2d27ec6711
3 changed files with 84 additions and 9 deletions
|
|
@ -198,18 +198,18 @@ public class ZeppelinhubClient {
|
|||
client.relayToZeppelin(zeppelinMsg, hubMsg.meta.get("noteId"));
|
||||
}
|
||||
|
||||
private void runAllParagraph(String noteId, String hubMsg) {
|
||||
boolean runAllParagraph(String noteId, String hubMsg) {
|
||||
LOG.info("Running paragraph with noteId {}", noteId);
|
||||
try {
|
||||
JSONObject data = new JSONObject(hubMsg);
|
||||
if (data.equals(JSONObject.NULL) || !(data.get("data") instanceof JSONArray)) {
|
||||
LOG.error("Wrong \"data\" format for RUN_NOTEBOOK");
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
Client client = Client.getInstance();
|
||||
if (client == null) {
|
||||
LOG.warn("Base client isn't initialized, returning");
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
Message zeppelinMsg = new Message(OP.RUN_PARAGRAPH);
|
||||
|
||||
|
|
@ -226,7 +226,9 @@ public class ZeppelinhubClient {
|
|||
}
|
||||
} catch (JSONException e) {
|
||||
LOG.error("Failed to parse RUN_NOTEBOOK message from ZeppelinHub ", e);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -83,12 +83,14 @@ public class ZeppelinClientTest {
|
|||
|
||||
@Test
|
||||
public void zeppelinClientSingletonTest() {
|
||||
ZeppelinClient client = ZeppelinClient.getInstance();
|
||||
assertNull(client);
|
||||
client = ZeppelinClient.initialize(validWebsocketUrl, "TOKEN");
|
||||
assertNotNull(client);
|
||||
client = ZeppelinClient.getInstance();
|
||||
assertNotNull(client);
|
||||
ZeppelinClient client1 = ZeppelinClient.getInstance();
|
||||
if (client1 == null) {
|
||||
client1 = ZeppelinClient.initialize(validWebsocketUrl, "TOKEN");
|
||||
}
|
||||
assertNotNull(client1);
|
||||
ZeppelinClient client2 = ZeppelinClient.getInstance();
|
||||
assertNotNull(client2);
|
||||
assertEquals(client1, client2);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
|
|
@ -0,0 +1,71 @@
|
|||
package org.apache.zeppelin.notebook.repo.zeppelinhub.websocket;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.apache.zeppelin.notebook.repo.zeppelinhub.websocket.ZeppelinhubClient;
|
||||
import org.apache.zeppelin.notebook.repo.zeppelinhub.websocket.mock.MockEchoWebsocketServer;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ZeppelinhubClientTest {
|
||||
private Logger LOG = LoggerFactory.getLogger(ZeppelinClientTest.class);
|
||||
private final int zeppelinPort = 8090;
|
||||
private final String validWebsocketUrl = "ws://localhost:" + zeppelinPort + "/ws";
|
||||
private ExecutorService executor;
|
||||
private MockEchoWebsocketServer echoServer;
|
||||
private final String runNotebookMsg =
|
||||
"{\"op\":\"RUN_NOTEBOOK\"," +
|
||||
"\"data\":[{\"id\":\"20150112-172845_1301897143\",\"title\":null,\"config\":{},\"params\":{},\"data\":null}," +
|
||||
"{\"id\":\"20150112-172845_1301897143\",\"title\":null,\"config\":{},\"params\":{},\"data\":null}]," +
|
||||
"\"meta\":{\"owner\":\"author\",\"instance\":\"my-zepp\",\"noteId\":\"2AB7SY361\"}}";
|
||||
private final String invalidRunNotebookMsg = "some random string";
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
startWebsocketServer();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
//tear down routine
|
||||
echoServer.stop();
|
||||
executor.shutdown();
|
||||
}
|
||||
|
||||
private void startWebsocketServer() throws InterruptedException {
|
||||
// mock zeppelin websocket server setup
|
||||
executor = Executors.newFixedThreadPool(1);
|
||||
echoServer = new MockEchoWebsocketServer(zeppelinPort);
|
||||
executor.submit(echoServer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zeppelinhubClientSingletonTest() {
|
||||
ZeppelinhubClient client1 = ZeppelinhubClient.getInstance();
|
||||
if (client1 == null) {
|
||||
client1 = ZeppelinhubClient.initialize(validWebsocketUrl, "TOKEN");
|
||||
}
|
||||
assertNotNull(client1);
|
||||
ZeppelinhubClient client2 = ZeppelinhubClient.getInstance();
|
||||
assertNotNull(client2);
|
||||
assertEquals(client1, client2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runAllParagraphTest() throws Exception {
|
||||
Client.initialize(validWebsocketUrl, validWebsocketUrl, "TOKEN");
|
||||
Client.getInstance().start();
|
||||
ZeppelinhubClient zeppelinhubClient = ZeppelinhubClient.getInstance();
|
||||
boolean runStatus = zeppelinhubClient.runAllParagraph("2AB7SY361", runNotebookMsg);
|
||||
assertTrue(runStatus);
|
||||
runStatus = zeppelinhubClient.runAllParagraph("2AB7SY361", invalidRunNotebookMsg);
|
||||
assertFalse(runStatus);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in a new issue