Open Kilda Java Documentation
TickBoltTest.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.wfm;
17 
20 
21 import com.google.common.util.concurrent.Uninterruptibles;
22 import org.apache.storm.LocalCluster;
23 import org.apache.storm.state.KeyValueState;
24 import org.apache.storm.testing.FeederSpout;
25 import org.apache.storm.topology.TopologyBuilder;
26 import org.apache.storm.tuple.Fields;
27 import org.apache.storm.tuple.Tuple;
28 import org.apache.storm.utils.Utils;
29 import org.junit.Assert;
30 import org.junit.Test;
31 
32 import java.io.IOException;
33 import java.util.Arrays;
34 import java.util.concurrent.ConcurrentHashMap;
35 import java.util.concurrent.TimeUnit;
36 
40 public class TickBoltTest {
41  // Don't need the extra stuff that comes with the AbstractStormTest yet. Should refactor
42  // so that we can pick and choose what's needed.
43  // extends AbstractStormTest {
44 
48  private static class SimpleStatefulTick
49  extends AbstractTickStatefulBolt<KeyValueState<String, ConcurrentHashMap<String, String>>> {
50 
51  public FileUtil tickFile = new FileUtil().withFileName("tick.log");
52  public FileUtil workFile = new FileUtil().withFileName("work.log");
53 
54  protected SimpleStatefulTick(){
55  super(2);
56  }
57 
58  @Override
59  protected void doTick(Tuple tuple) {
60  tickFile.append("tick\n");
61  }
62 
63  @Override
64  protected void doWork(Tuple tuple) {
65  workFile.append("work\n");
66  }
67 
68  @Override
69  public void initState(KeyValueState<String, ConcurrentHashMap<String, String>> state) {
70  }
71  }
72 
73  public static FeederSpout createFeeder() {
74  return new FeederSpout(new Fields("key", "message"));
75  }
76 
77  @Test
78  public void BasicTickTest() throws IOException {
79  System.out.println("==> Starting BasicTickTest");
80 
81  String spoutId = "feeder.spout";
82  String boltId = "tick.bolt";
83  String topoId = "TestTopology";
84 
85 
86  TopologyBuilder builder = new TopologyBuilder();
87  FeederSpout spout = TickBoltTest.createFeeder();
88  builder.setSpout(spoutId, spout);
89  SimpleStatefulTick tickBolt = new SimpleStatefulTick();
90  builder.setBolt(boltId, tickBolt).shuffleGrouping(spoutId);
91  LocalCluster cluster = new LocalCluster();
92  cluster.submitTopology(topoId, TestUtils.stormConfig(), builder.createTopology());
93 
94  /* Let's Submit Stuff! */
95  Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
96  spout.feed(Arrays.asList(new String[]{"key1", "msg1"}));
97 
98  /* And sleep some more */
99  Uninterruptibles.sleepUninterruptibly(6, TimeUnit.SECONDS);
100 
101  // TODO: this test isn't great .. the number of lines in the file from ticks could vary
102  int expectedLines = 3;
103  Assert.assertTrue("We should have at least " + expectedLines + " lines in the test file.",
104  expectedLines <= tickBolt.tickFile.numLines());
105  Assert.assertEquals(1, tickBolt.workFile.numLines());
106 
107  cluster.killTopology(topoId);
108  Utils.sleep(4 * 1000);
109  }
110 }
static Config stormConfig()
Definition: TestUtils.java:44
FileUtil withFileName(String fileName)
Definition: FileUtil.java:39
static FeederSpout createFeeder()