use of org.apache.apex.malhar.lib.helper.SamplePubSubWebSocketServlet in project apex-malhar by apache.
the class PubSubWebSocketOperatorTest method testPubSubWebSocket.
@Test
@SuppressWarnings("SleepWhileInLoop")
public void testPubSubWebSocket() throws Exception {
Server server = new Server(0);
SamplePubSubWebSocketServlet servlet = new SamplePubSubWebSocketServlet();
ServletHolder sh = new ServletHolder(servlet);
ServletContextHandler contextHandler = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
contextHandler.addServlet(sh, "/pubsub");
contextHandler.addServlet(sh, "/*");
server.start();
Connector[] connector = server.getConnectors();
URI uri = PubSubHelper.getURI("localhost:" + connector[0].getLocalPort());
PubSubWebSocketOutputOperator<Object> outputOperator = new PubSubWebSocketOutputOperator<Object>();
outputOperator.setUri(uri);
outputOperator.setTopic("testTopic");
PubSubWebSocketInputOperator<Object> inputOperator = new PubSubWebSocketInputOperator<Object>();
inputOperator.setUri(uri);
inputOperator.setTopic("testTopic");
CollectorTestSink<Object> sink = new CollectorTestSink<Object>();
inputOperator.outputPort.setSink(sink);
inputOperator.setup(null);
outputOperator.setup(null);
inputOperator.activate(null);
long timeout = System.currentTimeMillis() + 3000;
while (!servlet.hasSubscriber()) {
Thread.sleep(10);
if (System.currentTimeMillis() > timeout) {
throw new TimeoutException("No subscribers connected after 3 seconds");
}
}
inputOperator.beginWindow(1000);
outputOperator.beginWindow(1000);
Map<String, String> data = new HashMap<String, String>();
data.put("hello", "world");
outputOperator.input.process(data);
String stringData = "StringMessage";
outputOperator.input.process(stringData);
int timeoutMillis = 2000;
while (sink.collectedTuples.size() < 2 && timeoutMillis > 0) {
inputOperator.emitTuples();
timeoutMillis -= 20;
Thread.sleep(20);
}
outputOperator.endWindow();
inputOperator.endWindow();
Assert.assertTrue("tuples emitted", sink.collectedTuples.size() > 1);
@SuppressWarnings("unchecked") Map<String, String> tuple = (Map<String, String>) sink.collectedTuples.get(0);
Assert.assertEquals("Expects {\"hello\":\"world\"} as data", "world", tuple.get("hello"));
String stringResult = (String) sink.collectedTuples.get(1);
Assert.assertEquals("Expects {\"hello\":\"world\"} as data", stringData, stringResult);
inputOperator.deactivate();
outputOperator.teardown();
inputOperator.teardown();
server.stop();
}
use of org.apache.apex.malhar.lib.helper.SamplePubSubWebSocketServlet in project apex-malhar by apache.
the class ApplicationTest method testGetApplication.
/**
* Test of getApplication method, of class Application.
*/
@Test
public void testGetApplication() throws Exception {
Configuration conf = new Configuration(false);
conf.addResource("dt-site-mobile.xml");
Server server = new Server(0);
Servlet servlet = new SamplePubSubWebSocketServlet();
ServletHolder sh = new ServletHolder(servlet);
ServletContextHandler contextHandler = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
contextHandler.addServlet(sh, "/pubsub");
contextHandler.addServlet(sh, "/*");
server.start();
Connector[] connector = server.getConnectors();
conf.set("dt.attr.GATEWAY_CONNECT_ADDRESS", "localhost:" + connector[0].getLocalPort());
URI uri = PubSubHelper.getURI("localhost:" + connector[0].getLocalPort());
PubSubWebSocketOutputOperator<Object> outputOperator = new PubSubWebSocketOutputOperator<Object>();
outputOperator.setUri(uri);
outputOperator.setTopic(conf.get("dt.application.MobileExample.operator.QueryLocation.topic"));
PubSubWebSocketInputOperator<Map<String, String>> inputOperator = new PubSubWebSocketInputOperator<Map<String, String>>();
inputOperator.setUri(uri);
inputOperator.setTopic(conf.get("dt.application.MobileExample.operator.LocationResults.topic"));
CollectorTestSink<Object> sink = new CollectorTestSink<Object>();
inputOperator.outputPort.setSink(sink);
Map<String, String> data = new HashMap<String, String>();
data.put("command", "add");
data.put("phone", "5559990");
Application app = new Application();
LocalMode lma = LocalMode.newInstance();
lma.prepareDAG(app, conf);
LocalMode.Controller lc = lma.getController();
lc.setHeartbeatMonitoringEnabled(false);
lc.runAsync();
Thread.sleep(5000);
inputOperator.setup(null);
outputOperator.setup(null);
inputOperator.activate(null);
outputOperator.beginWindow(0);
outputOperator.input.process(data);
outputOperator.endWindow();
inputOperator.beginWindow(0);
int timeoutMillis = 5000;
while (sink.collectedTuples.size() < 5 && timeoutMillis > 0) {
inputOperator.emitTuples();
timeoutMillis -= 20;
Thread.sleep(20);
}
inputOperator.endWindow();
lc.shutdown();
inputOperator.teardown();
outputOperator.teardown();
server.stop();
Assert.assertTrue("size of output is 5 ", sink.collectedTuples.size() == 5);
for (Object obj : sink.collectedTuples) {
Assert.assertEquals("Expected phone number", "5559990", ((Map<String, String>) obj).get("phone"));
}
}
use of org.apache.apex.malhar.lib.helper.SamplePubSubWebSocketServlet in project apex-malhar by apache.
the class MrMonitoringApplicationTest method testApplication.
@Test
public void testApplication() throws Exception {
Configuration conf = new Configuration(false);
conf.addResource("dt-site-monitoring.xml");
Server server = new Server(0);
Servlet servlet = new SamplePubSubWebSocketServlet();
ServletHolder sh = new ServletHolder(servlet);
ServletContextHandler contextHandler = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
contextHandler.addServlet(sh, "/pubsub");
contextHandler.addServlet(sh, "/*");
server.start();
Connector[] connector = server.getConnectors();
conf.set("dt.attr.GATEWAY_CONNECT_ADDRESS", "localhost:" + connector[0].getLocalPort());
MRMonitoringApplication application = new MRMonitoringApplication();
LocalMode lma = LocalMode.newInstance();
lma.prepareDAG(application, conf);
LocalMode.Controller lc = lma.getController();
lc.run(10000);
server.stop();
}
Aggregations