| 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 that the first listen on a `asBroadcastStream` determines the | 17 // Test that streams live in the zone they have been listened too. |
| 18 // zone the subscription lives in. The inner listen happens first, and | 18 // It doesn't matter how many zone-boundaries the stream traverses. What |
| 19 // the outer listener must not see the error since it would cross a | 19 // counts is the zone where `listen` was invoked. |
| 20 // zone boundary. It is therefore given to the inner `catchErrors`. | |
| 21 catchErrors(() { | 20 catchErrors(() { |
| 22 catchErrors(() { | 21 catchErrors(() { |
| 23 controller = new StreamController(); | 22 controller = new StreamController(); |
| 23 |
| 24 // Assignment to "global" `stream`. |
| 24 stream = controller.stream | 25 stream = controller.stream |
| 25 .map((x) { | 26 .map((x) { |
| 26 events.add("map $x"); | 27 events.add("map $x"); |
| 27 return x + 100; | 28 return x + 100; |
| 28 }) | 29 }) |
| 29 .asBroadcastStream(); | 30 .asBroadcastStream(); |
| 31 |
| 32 // Consume stream in the nested zone. |
| 30 stream | 33 stream |
| 31 .transform(new StreamTransformer.fromHandlers( | 34 .transform(new StreamTransformer.fromHandlers( |
| 32 handleError: (e, st, sink) { sink.add("error $e"); })) | 35 handleError: (e, st, sink) { sink.add("error $e"); })) |
| 33 .listen((x) { events.add("stream $x"); }); | 36 .listen((x) { events.add("stream $x"); }); |
| 37 |
| 38 // Feed the controller in the nested zone. |
| 34 scheduleMicrotask(() { | 39 scheduleMicrotask(() { |
| 35 controller.add(1); | 40 controller.add(1); |
| 36 // Errors are not allowed to traverse boundaries, but in this case the | |
| 37 // first listener of the broadcast stream is in the same error-zone. So | |
| 38 // this should work. | |
| 39 controller.addError(2); | 41 controller.addError(2); |
| 40 controller.close(); | 42 controller.close(); |
| 43 new Future.error("done"); |
| 41 }); | 44 }); |
| 45 |
| 42 }).listen((x) { | 46 }).listen((x) { |
| 43 events.add(x); | 47 events.add("listen: $x"); |
| 44 if (x == 2) done.complete(true); | 48 if (x == "done") done.complete(true); |
| 45 }) | 49 }) |
| 46 .asFuture().then((_) { Expect.fail("Unexpected callback"); }); | 50 .asFuture().then((_) { Expect.fail("Unexpected callback"); }); |
| 51 |
| 52 // Listen to stream in outer zone. |
| 47 stream.listen((x) { events.add("stream2 $x"); }); | 53 stream.listen((x) { events.add("stream2 $x"); }); |
| 48 }).listen((x) { events.add("outer: $x"); }, | 54 }).listen((x) { events.add("outer: $x"); }, |
| 49 onDone: () { Expect.fail("Unexpected callback"); }); | 55 onDone: () { Expect.fail("Unexpected callback"); }); |
| 50 | 56 |
| 51 done.future.whenComplete(() { | 57 done.future.whenComplete(() { |
| 52 // Give handlers time to run. | 58 // Give handlers time to run. |
| 53 Timer.run(() { | 59 Timer.run(() { |
| 54 Expect.listEquals(["map 1", | 60 Expect.listEquals(["map 1", |
| 55 "stream 101", | 61 "stream 101", |
| 56 "stream2 101", | 62 "stream2 101", |
| 57 "stream error 2", | 63 "stream error 2", |
| 58 2, // Caught by the inner `catchErrors`. | 64 "listen: done", |
| 65 "outer: 2", |
| 59 ], | 66 ], |
| 60 events); | 67 events); |
| 61 asyncEnd(); | 68 asyncEnd(); |
| 62 }); | 69 }); |
| 63 }); | 70 }); |
| 64 } | 71 } |
| OLD | NEW |