Search in sources :

Example 1 with SamplePubSubWebSocketServlet

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();
}
Also used : Connector(org.eclipse.jetty.server.Connector) Server(org.eclipse.jetty.server.Server) HashMap(java.util.HashMap) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) SamplePubSubWebSocketServlet(org.apache.apex.malhar.lib.helper.SamplePubSubWebSocketServlet) URI(java.net.URI) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) HashMap(java.util.HashMap) Map(java.util.Map) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 2 with SamplePubSubWebSocketServlet

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"));
    }
}
Also used : PubSubWebSocketOutputOperator(org.apache.apex.malhar.lib.io.PubSubWebSocketOutputOperator) Connector(org.eclipse.jetty.server.Connector) Configuration(org.apache.hadoop.conf.Configuration) Server(org.eclipse.jetty.server.Server) HashMap(java.util.HashMap) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) SamplePubSubWebSocketServlet(org.apache.apex.malhar.lib.helper.SamplePubSubWebSocketServlet) URI(java.net.URI) PubSubWebSocketInputOperator(org.apache.apex.malhar.lib.io.PubSubWebSocketInputOperator) LocalMode(com.datatorrent.api.LocalMode) Servlet(javax.servlet.Servlet) SamplePubSubWebSocketServlet(org.apache.apex.malhar.lib.helper.SamplePubSubWebSocketServlet) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) HashMap(java.util.HashMap) Map(java.util.Map) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) Test(org.junit.Test)

Example 3 with SamplePubSubWebSocketServlet

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();
}
Also used : Connector(org.eclipse.jetty.server.Connector) Configuration(org.apache.hadoop.conf.Configuration) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) LocalMode(com.datatorrent.api.LocalMode) SamplePubSubWebSocketServlet(org.apache.apex.malhar.lib.helper.SamplePubSubWebSocketServlet) Servlet(javax.servlet.Servlet) SamplePubSubWebSocketServlet(org.apache.apex.malhar.lib.helper.SamplePubSubWebSocketServlet) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Aggregations

SamplePubSubWebSocketServlet (org.apache.apex.malhar.lib.helper.SamplePubSubWebSocketServlet)3 Connector (org.eclipse.jetty.server.Connector)3 Server (org.eclipse.jetty.server.Server)3 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)3 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)3 Test (org.junit.Test)3 LocalMode (com.datatorrent.api.LocalMode)2 URI (java.net.URI)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Servlet (javax.servlet.Servlet)2 CollectorTestSink (org.apache.apex.malhar.lib.testbench.CollectorTestSink)2 Configuration (org.apache.hadoop.conf.Configuration)2 TimeoutException (java.util.concurrent.TimeoutException)1 PubSubWebSocketInputOperator (org.apache.apex.malhar.lib.io.PubSubWebSocketInputOperator)1 PubSubWebSocketOutputOperator (org.apache.apex.malhar.lib.io.PubSubWebSocketOutputOperator)1