-
Notifications
You must be signed in to change notification settings - Fork 40
/
TestingApiDemo.java
383 lines (330 loc) · 11.5 KB
/
TestingApiDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package storm;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
import backtype.storm.Config;
import backtype.storm.ILocalCluster;
import backtype.storm.Testing;
import backtype.storm.generated.AlreadyAliveException;
import backtype.storm.generated.InvalidTopologyException;
import backtype.storm.generated.StormTopology;
import backtype.storm.task.OutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.testing.AckFailMapTracker;
import backtype.storm.testing.AckTracker;
import backtype.storm.testing.CompleteTopologyParam;
import backtype.storm.testing.FeederSpout;
import backtype.storm.testing.IdentityBolt;
import backtype.storm.testing.MkClusterParam;
import backtype.storm.testing.MkTupleParam;
import backtype.storm.testing.MockedSources;
import backtype.storm.testing.TestAggregatesCounter;
import backtype.storm.testing.TestGlobalCount;
import backtype.storm.testing.TestJob;
import backtype.storm.testing.TestWordCounter;
import backtype.storm.testing.TestWordSpout;
import backtype.storm.testing.TrackedTopology;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.TopologyBuilder;
import backtype.storm.topology.base.BaseRichBolt;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Tuple;
import backtype.storm.tuple.Values;
import backtype.storm.utils.Time;
/**
* This class is the unit test for backtype.storm.Testing. Also it provides the samples about how to
* use these testing apis.
*
* @author xumingmingv Jul 21, 2012 12:04:41 AM
*/
public class TestingApiDemo extends TestCase {
public void testWithSimulatedTime() {
assertFalse(Time.isSimulating());
/**
* <code>Testing.withSimulatedTime</code> create a context in which the time is simulated.set * you can use <code>Time.isSimulating</code> to check whether we're simulating the time.
* use <code>Time.advanceTime</code> to advance the simulated time.
*/
Testing.withSimulatedTime(new Runnable() {
@Override
public void run() {
assertTrue(Time.isSimulating());
}
});
assertFalse(Time.isSimulating());
}
public void testWithLocalCluster() {
MkClusterParam mkClusterParam = new MkClusterParam();
mkClusterParam.setSupervisors(2);
mkClusterParam.setPortsPerSupervisor(5);
Config daemonConf = new Config();
daemonConf.put(Config.SUPERVISOR_ENABLE, false);
daemonConf.put(Config.TOPOLOGY_ACKER_EXECUTORS, 0);
/**
* when testing your topology, you need a <code>LocalCluster</code> to run your topologies, you need
* to create it, after using it, you need to stop it. Using <code>Testing.withLocalCluster</code> you
* don't need to do any of this, just use the <code>cluster</code> provided through the param of
* <code>TestJob.run</code>.
*/
Testing.withLocalCluster(mkClusterParam, new TestJob() {
@Override
public void run(ILocalCluster cluster) {
assertNotNull(cluster.getState());
}
});
}
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")));
}
});
}
public void testAckBranching() {
/**
* TODO
*/
Testing.withTrackedCluster(new TestJob() {
@Override
public void run(ILocalCluster cluster) {
AckTracker tracker = new AckTracker();
FeederSpout feederSpout = ackTrackingFeeder(tracker, "num");
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("1", feederSpout);
builder.setBolt("2", new IdentityBolt(new Fields("num")))
.shuffleGrouping("1");
builder.setBolt("3", new IdentityBolt(new Fields("num")))
.shuffleGrouping("1");
builder.setBolt("4", new AggBolt(4)).shuffleGrouping("2")
.shuffleGrouping("3");
StormTopology topology = builder.createTopology();
TrackedTopology tracked = Testing.mkTrackedTopology(cluster,
topology);
try {
cluster.submitTopology(
"test-acking2", new Config(), tracked.getTopology());
} catch (AlreadyAliveException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidTopologyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
feederSpout.feed(new Values(1));
Testing.trackedWait(tracked, 1);
checker(tracker, 0);
feederSpout.feed(new Values(1));
Testing.trackedWait(tracked, 1);
checker(tracker, 2);
}
});
}
public void testTimeout() {
Config daemonConfig = new Config();
daemonConfig.put(Config.TOPOLOGY_ENABLE_MESSAGE_TIMEOUTS, true);
MkClusterParam mkClusterParam = new MkClusterParam();
mkClusterParam.setDaemonConf(daemonConfig);
Testing.withSimulatedTimeLocalCluster(mkClusterParam, new TestJob() {
@Override
public void run(ILocalCluster cluster) {
AckFailMapTracker tracker = new AckFailMapTracker();
FeederSpout feeder = createFeederSpout("field1");
feeder.setAckFailDelegate(tracker);
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("1", feeder);
builder.setBolt("2", new AckEveryOtherBolt()).globalGrouping(
"1");
StormTopology topology = builder.createTopology();
Config topologyConfig = new Config();
topologyConfig.setMessageTimeoutSecs(10);
/**
* TODO
*/
try {
cluster.submitTopology(
"timeout-tester", topologyConfig, topology);
} catch (AlreadyAliveException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidTopologyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
feeder.feed(new Values("a"), 1);
feeder.feed(new Values("b"), 2);
feeder.feed(new Values("c"), 3);
/**
* TODO
*/
Testing.advanceClusterTime(cluster, 9);
assertAcked(tracker, 1, 3);
assertFalse(tracker.isFailed(2));
Testing.advanceClusterTime(cluster, 12);
assertFailed(tracker, 2);
}
});
}
/**
* show how to use testTuple
*/
public void testTestTuple() {
// only specify values
Tuple tuple = Testing.testTuple(new Values("james", "bond"));
Testing.multiseteq(new Values("james", "bond"), tuple.getValues());
// specify stream, component and fields
MkTupleParam param = new MkTupleParam();
param.setStream("test-stream");
param.setComponent("test-component");
param.setFields("fname", "lname");
tuple = Testing.testTuple(new Values("james", "bond"), param);
Testing.multiseteq(new Values("james", "bond"), tuple.getValues());
assertEquals("test-stream", tuple.getSourceStreamId());
assertEquals("test-component", tuple.getSourceComponent());
Testing.multiseteq(new Values("fname", "lname"), tuple.getFields().toList());
}
public static void assertAcked(AckFailMapTracker tracker, Object... ids) {
boolean notAllAcked = true;
while (notAllAcked) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
int notAckedCnt = 0;
for (int i = 0; i < ids.length; i++) {
if (!tracker.isAcked(ids[i])) {
notAckedCnt += 1;
break;
}
}
if (notAckedCnt == 0) {
notAllAcked = false;
}
}
}
public static void assertFailed(AckFailMapTracker tracker, Object... ids) {
boolean notAllFailed = true;
while (notAllFailed) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
int notFailedCnt = 0;
for (int i = 0; i < ids.length; i++) {
if (!tracker.isFailed(ids[i])) {
notFailedCnt += 1;
break;
}
}
if (notFailedCnt == 0) {
notAllFailed = false;
}
}
}
public static FeederSpout ackTrackingFeeder(AckTracker tracker,
String... fields) {
FeederSpout feeder = createFeederSpout(fields);
feeder.setAckFailDelegate(tracker);
return feeder;
}
public static FeederSpout createFeederSpout(String... fields) {
return new FeederSpout(new Fields(fields));
}
public static void checker(AckTracker tracker, int val) {
assertEquals(val, tracker.getNumAcks());
tracker.resetNumAcks();
}
static class AggBolt extends BaseRichBolt {
OutputCollector _collector;
List<Tuple> seen = new ArrayList<Tuple>();
int amt;
public AggBolt(int amt) {
this.amt = amt;
}
@Override
public void prepare(Map conf, TopologyContext context,
OutputCollector collector) {
_collector = collector;
}
public void execute(Tuple input) {
seen.add(input);
if (seen.size() == this.amt) {
_collector.emit(seen, new Values(1));
for (Tuple tuple : seen) {
_collector.ack(tuple);
}
seen.clear();
}
}
@Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("num"));
}
}
static class AckEveryOtherBolt extends BaseRichBolt {
boolean flag = false;
OutputCollector _collector;
@Override
public void prepare(Map conf, TopologyContext context,
OutputCollector collector) {
_collector = collector;
}
public void execute(Tuple input) {
flag = !flag;
if (flag) {
_collector.ack(input);
}
}
@Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
}
}
}