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 streams live in the zone they have been listened too. | |
18 // It doesn't matter how many zone-boundaries the stream traverses. What | |
19 // counts is the zone where `listen` was invoked. | |
20 catchErrors(() { | |
21 catchErrors(() { | |
22 controller = new StreamController(); | |
23 | |
24 // Assignment to "global" `stream`. | |
25 stream = controller.stream.map((x) { | |
26 events.add("map $x"); | |
27 return x + 100; | |
28 }).asBroadcastStream(); | |
29 | |
30 // Consume stream in the nested zone. | |
31 stream.transform( | |
32 new StreamTransformer.fromHandlers(handleError: (e, st, sink) { | |
33 sink.add("error $e"); | |
34 })).listen((x) { | |
35 events.add("stream $x"); | |
36 }); | |
37 | |
38 // Feed the controller in the nested zone. | |
39 scheduleMicrotask(() { | |
40 controller.add(1); | |
41 controller.addError(2); | |
42 controller.close(); | |
43 new Future.error("done"); | |
44 }); | |
45 }) | |
46 .listen((x) { | |
47 events.add("listen: $x"); | |
48 if (x == "done") done.complete(true); | |
49 }) | |
50 .asFuture() | |
51 .then((_) { | |
52 Expect.fail("Unexpected callback"); | |
53 }); | |
54 | |
55 // Listen to stream in outer zone. | |
56 stream.listen((x) { | |
57 events.add("stream2 $x"); | |
58 }); | |
59 }).listen((x) { | |
60 events.add("outer: $x"); | |
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 2", | |
73 "listen: done", | |
74 "outer: 2", | |
75 ], events); | |
76 asyncEnd(); | |
77 }); | |
78 }); | |
79 } | |
OLD | NEW |