Search in sources :

Example 1 with MockedSources

use of backtype.storm.testing.MockedSources in project storm-lib by xumingming.

the class TestingApiDemo method testBasicTopology.

public void testBasicTopology() {
    MkClusterParam mkClusterParam = new MkClusterParam();
    mkClusterParam.setSupervisors(4);
    Config daemonConf = new Config();
    daemonConf.put(Config.STORM_LOCAL_MODE_ZMQ, false);
    mkClusterParam.setDaemonConf(daemonConf);
    /**
     * This is a combination of <code>Testing.withLocalCluster</code> and <code>Testing.withSimulatedTime</code>.
     */
    Testing.withSimulatedTimeLocalCluster(mkClusterParam, new TestJob() {

        @Override
        public void run(ILocalCluster cluster) {
            // build the test topology
            TopologyBuilder builder = new TopologyBuilder();
            builder.setSpout("1", new TestWordSpout(true), 3);
            builder.setBolt("2", new TestWordCounter(), 4).fieldsGrouping("1", new Fields("word"));
            builder.setBolt("3", new TestGlobalCount()).globalGrouping("1");
            builder.setBolt("4", new TestAggregatesCounter()).globalGrouping("2");
            StormTopology topology = builder.createTopology();
            // complete the topology
            // prepare the mock data
            MockedSources mockedSources = new MockedSources();
            mockedSources.addMockData("1", new Values("nathan"), new Values("bob"), new Values("joey"), new Values("nathan"));
            // prepare the config
            Config conf = new Config();
            conf.setNumWorkers(2);
            CompleteTopologyParam completeTopologyParam = new CompleteTopologyParam();
            completeTopologyParam.setMockedSources(mockedSources);
            completeTopologyParam.setStormConf(conf);
            /**
             * TODO
             */
            Map result = Testing.completeTopology(cluster, topology, completeTopologyParam);
            // check whether the result is right
            assertTrue(Testing.multiseteq(new Values(new Values("nathan"), new Values("bob"), new Values("joey"), new Values("nathan")), Testing.readTuples(result, "1")));
            assertTrue(Testing.multiseteq(new Values(new Values("nathan", 1), new Values("nathan", 2), new Values("bob", 1), new Values("joey", 1)), Testing.readTuples(result, "2")));
            assertTrue(Testing.multiseteq(new Values(new Values(1), new Values(2), new Values(3), new Values(4)), Testing.readTuples(result, "3")));
            assertTrue(Testing.multiseteq(new Values(new Values(1), new Values(2), new Values(3), new Values(4)), Testing.readTuples(result, "4")));
        }
    });
}
Also used : TestJob(backtype.storm.testing.TestJob) TopologyBuilder(backtype.storm.topology.TopologyBuilder) Config(backtype.storm.Config) StormTopology(backtype.storm.generated.StormTopology) Values(backtype.storm.tuple.Values) TestWordCounter(backtype.storm.testing.TestWordCounter) TestAggregatesCounter(backtype.storm.testing.TestAggregatesCounter) MkClusterParam(backtype.storm.testing.MkClusterParam) ILocalCluster(backtype.storm.ILocalCluster) MockedSources(backtype.storm.testing.MockedSources) Fields(backtype.storm.tuple.Fields) TestGlobalCount(backtype.storm.testing.TestGlobalCount) CompleteTopologyParam(backtype.storm.testing.CompleteTopologyParam) TestWordSpout(backtype.storm.testing.TestWordSpout) Map(java.util.Map)

Aggregations

Config (backtype.storm.Config)1 ILocalCluster (backtype.storm.ILocalCluster)1 StormTopology (backtype.storm.generated.StormTopology)1 CompleteTopologyParam (backtype.storm.testing.CompleteTopologyParam)1 MkClusterParam (backtype.storm.testing.MkClusterParam)1 MockedSources (backtype.storm.testing.MockedSources)1 TestAggregatesCounter (backtype.storm.testing.TestAggregatesCounter)1 TestGlobalCount (backtype.storm.testing.TestGlobalCount)1 TestJob (backtype.storm.testing.TestJob)1 TestWordCounter (backtype.storm.testing.TestWordCounter)1 TestWordSpout (backtype.storm.testing.TestWordSpout)1 TopologyBuilder (backtype.storm.topology.TopologyBuilder)1 Fields (backtype.storm.tuple.Fields)1 Values (backtype.storm.tuple.Values)1 Map (java.util.Map)1