OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import 'package:async_helper/async_helper.dart'; | |
6 import "package:expect/expect.dart"; | |
7 import 'dart:async'; | |
8 import 'catch_errors.dart'; | |
9 | |
10 main() { | |
11 asyncStart(); | |
12 Completer done = new Completer(); | |
13 | |
14 var events = []; | |
15 StreamController controller; | |
16 Stream stream; | |
17 // Test that errors are not traversing zone boundaries. | |
18 // Note that the first listener of `asBroadcastStream` determines in which | |
19 // zone the subscription lives. | |
20 catchErrors(() { | |
21 catchErrors(() { | |
22 controller = new StreamController(); | |
23 | |
24 // Assign to "global" `stream`. | |
25 stream = controller.stream.map((x) { | |
26 events.add("map $x"); | |
27 return x + 100; | |
28 }).transform( | |
29 new StreamTransformer.fromHandlers(handleError: (e, st, sink) { | |
30 sink.add("error $e"); | |
31 })).asBroadcastStream(); | |
32 | |
33 // Listen to `stream` in the inner zone. | |
34 stream.listen((x) { | |
35 events.add("stream $x"); | |
36 }); | |
37 }) | |
38 .listen((x) { | |
39 events.add(x); | |
40 }) | |
41 .asFuture() | |
42 .then((_) { | |
43 Expect.fail("Unexpected callback"); | |
44 }); | |
45 | |
46 // Listen to `stream` in the outer zone. | |
47 stream.listen((x) { | |
48 events.add("stream2 $x"); | |
49 }); | |
50 | |
51 // Feed the controller from the outer zone. | |
52 controller.add(1); | |
53 // `addError` does not count as zone-traversal. It should be caught by | |
54 // the inner error handler. | |
55 controller.addError("inner error"); | |
56 new Future.error("caught by outer"); | |
57 controller.close(); | |
58 }).listen((x) { | |
59 events.add("outer: $x"); | |
60 if (x == "caught by outer") done.complete(true); | |
61 }, onDone: () { | |
62 Expect.fail("Unexpected callback"); | |
63 }); | |
64 | |
65 done.future.whenComplete(() { | |
66 // Give handlers time to run. | |
67 Timer.run(() { | |
68 Expect.listEquals([ | |
69 "map 1", | |
70 "stream 101", | |
71 "stream2 101", | |
72 "stream error inner error", | |
73 "stream2 error inner error", | |
74 "outer: caught by outer", | |
75 ], events); | |
76 asyncEnd(); | |
77 }); | |
78 }); | |
79 } | |
OLD | NEW |