OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'package:async_helper/async_helper.dart'; | 5 import 'package:async_helper/async_helper.dart'; |
6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'catch_errors.dart'; | 8 import 'catch_errors.dart'; |
9 | 9 |
10 main() { | 10 main() { |
11 asyncStart(); | 11 asyncStart(); |
12 Completer done = new Completer(); | 12 Completer done = new Completer(); |
13 | 13 |
14 var events = []; | 14 var events = []; |
15 StreamController controller; | 15 StreamController controller; |
16 Stream stream; | 16 Stream stream; |
17 // Test `StreamController.broadcast` streams. | 17 // Test `StreamController.broadcast` streams. |
18 catchErrors(() { | 18 catchErrors(() { |
19 catchErrors(() { | 19 catchErrors(() { |
20 controller = new StreamController.broadcast(); | 20 controller = new StreamController.broadcast(); |
21 | 21 |
22 // Listen to the stream from the inner zone. | 22 // Listen to the stream from the inner zone. |
23 controller.stream | 23 controller.stream.map((x) { |
24 .map((x) { | 24 events.add("map $x"); |
25 events.add("map $x"); | 25 return x + 100; |
26 return x + 100; | 26 }).transform( |
| 27 new StreamTransformer.fromHandlers(handleError: (e, st, sink) { |
| 28 sink.add("error $e"); |
| 29 })).listen((x) { |
| 30 events.add("stream $x"); |
| 31 }); |
| 32 }) |
| 33 .listen((x) { |
| 34 events.add(x); |
27 }) | 35 }) |
28 .transform(new StreamTransformer.fromHandlers( | 36 .asFuture() |
29 handleError: (e, st, sink) { sink.add("error $e"); })) | 37 .then((_) { |
30 .listen((x) { events.add("stream $x"); }); | 38 Expect.fail("Unexpected callback"); |
31 | 39 }); |
32 }).listen((x) { events.add(x); }) | |
33 .asFuture().then((_) { Expect.fail("Unexpected callback"); }); | |
34 | 40 |
35 // Listen to the stream from the outer zone. | 41 // Listen to the stream from the outer zone. |
36 controller.stream.listen((x) { events.add("stream2 $x"); }, | 42 controller.stream.listen((x) { |
37 onError: (x) { events.add("stream2 error $x"); }); | 43 events.add("stream2 $x"); |
| 44 }, onError: (x) { |
| 45 events.add("stream2 error $x"); |
| 46 }); |
38 | 47 |
39 // Feed the controller. | 48 // Feed the controller. |
40 controller.add(1); | 49 controller.add(1); |
41 controller.addError("inner stream"); | 50 controller.addError("inner stream"); |
42 new Future.error("outer error"); | 51 new Future.error("outer error"); |
43 controller.close(); | 52 controller.close(); |
44 }).listen((x) { | 53 }).listen((x) { |
45 events.add("outer: $x"); | 54 events.add("outer: $x"); |
46 if (x == "outer error") done.complete(true); | 55 if (x == "outer error") done.complete(true); |
47 }, | 56 }, onDone: () { |
48 onDone: () { Expect.fail("Unexpected callback"); }); | 57 Expect.fail("Unexpected callback"); |
| 58 }); |
49 | 59 |
50 done.future.whenComplete(() { | 60 done.future.whenComplete(() { |
51 // Give handlers time to run. | 61 // Give handlers time to run. |
52 Timer.run(() { | 62 Timer.run(() { |
53 Expect.listEquals(["map 1", | 63 Expect.listEquals([ |
54 "stream 101", | 64 "map 1", |
55 "stream2 1", | 65 "stream 101", |
56 "stream error inner stream", | 66 "stream2 1", |
57 "stream2 error inner stream", | 67 "stream error inner stream", |
58 "outer: outer error", | 68 "stream2 error inner stream", |
59 ], | 69 "outer: outer error", |
60 events); | 70 ], events); |
61 asyncEnd(); | 71 asyncEnd(); |
62 }); | 72 }); |
63 }); | 73 }); |
64 } | 74 } |
OLD | NEW |