Search in sources :

Example 1 with JStormUnitTestValidator

use of com.jstorm.example.unittests.utils.JStormUnitTestValidator in project jstorm by alibaba.

the class TridentWordCountTest method testTridentWordCount.

// to make sure the validator is right
@Test
public void testTridentWordCount() {
    LocalDRPC localDRPC = new LocalDRPC();
    FixedLimitBatchSpout spout = new FixedLimitBatchSpout(SPOUT_LIMIT, new Fields("sentence"), SPOUT_BATCH_SIZE, new Values("the cow jumped over the moon"), new Values("the man went to the store and bought some candy"), new Values("four score and seven years ago"), new Values("how many apples can you eat"), new Values("to be or not to be the person"));
    TridentTopology tridentTopology = new TridentTopology();
    TridentState wordCount = tridentTopology.newStream("spout", spout).parallelismHint(1).each(new Fields("sentence"), new Split(), new Fields("word")).groupBy(new Fields("word")).persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")).parallelismHint(16);
    tridentTopology.newDRPCStream("words", localDRPC).each(new Fields("args"), new Split(), new Fields("keyword")).groupBy(new Fields("keyword")).stateQuery(wordCount, new Fields("keyword"), new MapGet(), new Fields("result")).each(new Fields("result"), new FilterNull()).aggregate(new Fields("result"), new Sum(), new Fields("sum"));
    Map config = new HashMap();
    config.put(Config.TOPOLOGY_NAME, "TridentWordCountTest");
    JStormUnitTestValidator validator = new JStormUnitTestDRPCValidator(localDRPC) {

        Logger LOG = LoggerFactory.getLogger(JStormUnitTestValidator.class);

        @Override
        public boolean validate(Map config) {
            String queryResult = executeLocalDRPC("words", "the");
            // the result is like [[8080]], so remove the [[]]
            queryResult = queryResult.substring(2, queryResult.length() - 2);
            // how many times of emit can finish a loop
            int oneLoopNeedEmits = (int) Math.ceil(SPOUT_CONTENT_TYPES / (float) SPOUT_BATCH_SIZE);
            // of all the spout content
            // the loop time of the LimitFixBatchSpout content
            int loopTime = SPOUT_LIMIT / oneLoopNeedEmits;
            int receiveCountOfThe = Integer.valueOf(queryResult);
            LOG.info("Final receive total " + receiveCountOfThe + " \"the\" when expected " + (loopTime * 5));
            // 5 "the" are in one loop
            boolean isCountOfTheRight = (receiveCountOfThe == loopTime * 5);
            // query the word count of these 3 words total
            queryResult = executeLocalDRPC("words", "be store kujou");
            queryResult = queryResult.substring(2, queryResult.length() - 2);
            int receiveCountOfBeAndStore = Integer.valueOf(queryResult);
            LOG.info("Final receive total " + receiveCountOfBeAndStore + " \"be\" and \"store\" and \"kujou\" " + "when expected " + (loopTime * 3));
            // 2 "be" 1 "store" 0 "kujou" are in one loop
            boolean isCountOfBeAndStoreRight = (receiveCountOfBeAndStore == loopTime * 3);
            return isCountOfTheRight && isCountOfBeAndStoreRight;
        }
    };
    try {
        boolean result = JStormUnitTestRunner.submitTopology(tridentTopology.build(), config, 90, validator);
        assertTrue("Topology should pass the validator", result);
    } finally {
        localDRPC.shutdown();
    }
}
Also used : FilterNull(storm.trident.operation.builtin.FilterNull) JStormUnitTestValidator(com.jstorm.example.unittests.utils.JStormUnitTestValidator) TridentState(storm.trident.TridentState) HashMap(java.util.HashMap) Values(backtype.storm.tuple.Values) LoggerFactory(org.slf4j.LoggerFactory) MapGet(storm.trident.operation.builtin.MapGet) Sum(storm.trident.operation.builtin.Sum) JStormUnitTestDRPCValidator(com.jstorm.example.unittests.utils.JStormUnitTestDRPCValidator) Count(storm.trident.operation.builtin.Count) Logger(org.slf4j.Logger) Fields(backtype.storm.tuple.Fields) TridentTopology(storm.trident.TridentTopology) LocalDRPC(backtype.storm.LocalDRPC) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with JStormUnitTestValidator

use of com.jstorm.example.unittests.utils.JStormUnitTestValidator in project jstorm by alibaba.

the class TickTupleTest method testTickTuple.

@Test
public void testTickTuple() {
    TopologyBuilder topologyBuilder = new TopologyBuilder();
    // the spout is useless in this case, all the methods are empty
    topologyBuilder.setSpout("spout", new TickTupleTestSpout());
    // note that there is no grouping here, the bolt should not receive any tuple from spout
    // I increase the parallelism of the bolt to check if it is correct when we have a
    // parallelism greater than 1.
    topologyBuilder.setBolt("bolt", new TickTupleTestBolt(), TICK_TUPLE_BOLT_PARALLELISM).addConfiguration(Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS, TICK_TUPLE_CYCLE);
    Set<String> userDefineMetrics = new HashSet<String>();
    userDefineMetrics.add("TickTupleTest.TickMeter");
    userDefineMetrics.add("TickTupleTest.NonTickCounter");
    JStormUnitTestValidator validator = new JStormUnitTestMetricValidator(userDefineMetrics) {

        @Override
        public boolean validateMetrics(Map<String, Double> metrics) {
            // there is $TICK_TUPLE_BOLT_PARALLELISM bolts, so the TickMeter need divided by $TICK_TUPLE_BOLT_PARALLELISM
            double cycle = 1 / (metrics.get("TickTupleTest.TickMeter") / TICK_TUPLE_BOLT_PARALLELISM);
            LOG.info("TickTupleTest.TickMeter = " + metrics.get("TickTupleTest.TickMeter"));
            LOG.info("Tick cycle  = " + cycle);
            assertTrue("The tick cycle should be in range of 0.8*TICK_TUPLE_CYCLE and 1.2*TICK_TUPLE_CYCLE", cycle > 0.9f * TICK_TUPLE_CYCLE && cycle < 1.1f * TICK_TUPLE_CYCLE);
            assertEquals(0, (int) (metrics.get("TickTupleTest.NonTickCounter").doubleValue()));
            return true;
        }
    };
    Config config = new Config();
    config.put(Config.TOPOLOGY_NAME, "TickTupleTest");
    JStormUnitTestRunner.submitTopology(topologyBuilder.createTopology(), config, 120, validator);
}
Also used : JStormUnitTestValidator(com.jstorm.example.unittests.utils.JStormUnitTestValidator) TopologyBuilder(backtype.storm.topology.TopologyBuilder) Config(backtype.storm.Config) JStormUnitTestMetricValidator(com.jstorm.example.unittests.utils.JStormUnitTestMetricValidator) Map(java.util.Map) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with JStormUnitTestValidator

use of com.jstorm.example.unittests.utils.JStormUnitTestValidator in project jstorm by alibaba.

the class ManualDRPCTest method testManualDRPC.

@Test
public void testManualDRPC() {
    TopologyBuilder topologyBuilder = new TopologyBuilder();
    LocalDRPC localDRPC = new LocalDRPC();
    DRPCSpout spout = new DRPCSpout("exclamation", localDRPC);
    topologyBuilder.setSpout("drpc", spout);
    topologyBuilder.setBolt("exclaim", new ManualDRPC.ExclamationBolt(), 3).shuffleGrouping("drpc");
    topologyBuilder.setBolt("return", new ReturnResults(), 3).shuffleGrouping("exclaim");
    Config config = new Config();
    config.put(Config.TOPOLOGY_NAME, "ManualDRPCTest");
    JStormUnitTestValidator validator = new JStormUnitTestDRPCValidator(localDRPC) {

        @Override
        public boolean validate(Map config) {
            assertEquals("hello!!!", executeLocalDRPC("exclamation", "hello"));
            assertEquals("good bye!!!", executeLocalDRPC("exclamation", "good bye"));
            return true;
        }
    };
    try {
        JStormUnitTestRunner.submitTopology(topologyBuilder.createTopology(), config, 60, validator);
    } finally {
        localDRPC.shutdown();
    }
}
Also used : JStormUnitTestValidator(com.jstorm.example.unittests.utils.JStormUnitTestValidator) TopologyBuilder(backtype.storm.topology.TopologyBuilder) Config(backtype.storm.Config) LocalDRPC(backtype.storm.LocalDRPC) ReturnResults(backtype.storm.drpc.ReturnResults) JStormUnitTestDRPCValidator(com.jstorm.example.unittests.utils.JStormUnitTestDRPCValidator) DRPCSpout(backtype.storm.drpc.DRPCSpout) Map(java.util.Map) Test(org.junit.Test)

Example 4 with JStormUnitTestValidator

use of com.jstorm.example.unittests.utils.JStormUnitTestValidator in project jstorm by alibaba.

the class TridentMapTest method testTridentMap.

// to make sure the validator is right
@Test
public void testTridentMap() {
    LocalDRPC localDRPC = new LocalDRPC();
    FixedLimitBatchSpout spout = new FixedLimitBatchSpout(SPOUT_LIMIT, new Fields("word"), SPOUT_BATCH_SIZE, new Values("the cow jumped over the moon"), new Values("the man went to the store and bought some candy"), new Values("four score and seven years ago"), new Values("how many apples can you eat"), new Values("to be or not to be the person"));
    TridentTopology tridentTopology = new TridentTopology();
    TridentState wordCount = tridentTopology.newStream("spout", spout).parallelismHint(1).flatMap(new SplitFlatMapFunction()).map(new UpperMapFunction()).filter(new Fields("word"), new WordFilter("THE")).peek(new LogConsumer()).groupBy(new Fields("word")).persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")).parallelismHint(16);
    tridentTopology.newDRPCStream("words", localDRPC).flatMap(new SplitFlatMapFunction()).groupBy(new Fields("args")).stateQuery(wordCount, new Fields("args"), new MapGet(), new Fields("result")).filter(new Fields("result"), new FilterNull()).aggregate(new Fields("result"), new Sum(), new Fields("sum"));
    Map config = new HashMap();
    config.put(Config.TOPOLOGY_NAME, "TridentMapTest");
    JStormUnitTestValidator validator = new JStormUnitTestDRPCValidator(localDRPC) {

        Logger LOG = LoggerFactory.getLogger(JStormUnitTestValidator.class);

        @Override
        public boolean validate(Map config) {
            String queryResult = executeLocalDRPC("words", "THE");
            // the result is like [[8080]], so remove the [[]]
            queryResult = queryResult.substring(2, queryResult.length() - 2);
            // how many times of emit can finish a loop
            int oneLoopNeedEmits = (int) Math.ceil(SPOUT_CONTENT_TYPES / (float) SPOUT_BATCH_SIZE);
            // of all the spout content
            // the loop time of the LimitFixBatchSpout content
            int loopTime = SPOUT_LIMIT / oneLoopNeedEmits;
            int receiveCountOfUpperCase = Integer.valueOf(queryResult);
            LOG.info("Final receive total " + receiveCountOfUpperCase + " \"THE\" when expected " + (loopTime * 5));
            // 5 "the" are in one loop
            boolean isCountOfUpperCaseRight = (receiveCountOfUpperCase == loopTime * 5);
            // query the word count of these 3 words total
            queryResult = executeLocalDRPC("words", "the tHe thE");
            queryResult = queryResult.substring(2, queryResult.length() - 2);
            int receiveCountOfLowerCase = Integer.valueOf(queryResult);
            LOG.info("Final receive total " + receiveCountOfLowerCase + " \"the\" and \"tHe\" and \"thE\" " + "when expected " + 0);
            // no the tHe thE should pass the UpperFilter
            boolean isCountOfLowerCaseRight = (receiveCountOfLowerCase == 0);
            return isCountOfUpperCaseRight && isCountOfLowerCaseRight;
        }
    };
    try {
        boolean result = JStormUnitTestRunner.submitTopology(tridentTopology.build(), config, 90, validator);
        assertTrue("Topology should pass the validator", result);
    } finally {
        localDRPC.shutdown();
    }
}
Also used : FilterNull(storm.trident.operation.builtin.FilterNull) JStormUnitTestValidator(com.jstorm.example.unittests.utils.JStormUnitTestValidator) TridentState(storm.trident.TridentState) Values(backtype.storm.tuple.Values) LoggerFactory(org.slf4j.LoggerFactory) MapGet(storm.trident.operation.builtin.MapGet) Sum(storm.trident.operation.builtin.Sum) JStormUnitTestDRPCValidator(com.jstorm.example.unittests.utils.JStormUnitTestDRPCValidator) Count(storm.trident.operation.builtin.Count) Logger(org.slf4j.Logger) Fields(backtype.storm.tuple.Fields) TridentTopology(storm.trident.TridentTopology) LocalDRPC(backtype.storm.LocalDRPC) Test(org.junit.Test)

Example 5 with JStormUnitTestValidator

use of com.jstorm.example.unittests.utils.JStormUnitTestValidator in project jstorm by alibaba.

the class SlidingTupleTsTopologyTest method testSlidingTupleTsTopology.

@Test
public void testSlidingTupleTsTopology() {
    TopologyBuilder topologyBuilder = new TopologyBuilder();
    BaseWindowedBolt bolt = new SlidingTupleTestBolt().withWindow(new BaseWindowedBolt.Duration(WINDOW_LENGTH_SEC, TimeUnit.SECONDS), new BaseWindowedBolt.Duration(WINDOW_SLIDE_SEC, TimeUnit.SECONDS)).withTimestampField("ts").withLag(new BaseWindowedBolt.Duration(WINDOW_LAG_SEC, TimeUnit.SECONDS));
    topologyBuilder.setSpout("spout", new SlidingTupleTestRandomSpout(SPOUT_LIMIT), 1);
    topologyBuilder.setBolt("sum", bolt, 1).shuffleGrouping("spout");
    Map config = new HashMap();
    config.put(Config.TOPOLOGY_NAME, "SlidingTupleTsTopologyTest");
    Set<String> userDefineMetrics = new HashSet<String>();
    userDefineMetrics.add("SlidingTupleTsTopologyTest.SpoutSum");
    userDefineMetrics.add("SlidingTupleTsTopologyTest.BoltSum");
    JStormUnitTestValidator validator = new JStormUnitTestMetricValidator(userDefineMetrics) {

        @Override
        public boolean validateMetrics(Map<String, Double> metrics) {
            int spoutSum = (int) metrics.get("SlidingTupleTsTopologyTest.SpoutSum").doubleValue();
            int boltSum = (int) metrics.get("SlidingTupleTsTopologyTest.BoltSum").doubleValue();
            assertEquals(spoutSum, boltSum);
            return true;
        }
    };
    JStormUnitTestRunner.submitTopology(topologyBuilder.createTopology(), config, 120, validator);
}
Also used : JStormUnitTestValidator(com.jstorm.example.unittests.utils.JStormUnitTestValidator) TopologyBuilder(backtype.storm.topology.TopologyBuilder) JStormUnitTestMetricValidator(com.jstorm.example.unittests.utils.JStormUnitTestMetricValidator) BaseWindowedBolt(backtype.storm.topology.base.BaseWindowedBolt) Test(org.junit.Test)

Aggregations

JStormUnitTestValidator (com.jstorm.example.unittests.utils.JStormUnitTestValidator)6 Test (org.junit.Test)6 TopologyBuilder (backtype.storm.topology.TopologyBuilder)4 Map (java.util.Map)4 LocalDRPC (backtype.storm.LocalDRPC)3 JStormUnitTestDRPCValidator (com.jstorm.example.unittests.utils.JStormUnitTestDRPCValidator)3 JStormUnitTestMetricValidator (com.jstorm.example.unittests.utils.JStormUnitTestMetricValidator)3 Config (backtype.storm.Config)2 BaseWindowedBolt (backtype.storm.topology.base.BaseWindowedBolt)2 Fields (backtype.storm.tuple.Fields)2 Values (backtype.storm.tuple.Values)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 TridentState (storm.trident.TridentState)2 TridentTopology (storm.trident.TridentTopology)2 Count (storm.trident.operation.builtin.Count)2 FilterNull (storm.trident.operation.builtin.FilterNull)2 MapGet (storm.trident.operation.builtin.MapGet)2