| 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 the first listen on a `asBroadcastStream` determines the |
| 18 // zone the subscription lives in. In this case the outer listen happens first | 18 // zone the subscription lives in. In this case the outer listen happens first |
| 19 // and the error reaches `handleError`. | 19 // and the error reaches `handleError`. |
| 20 catchErrors(() { | 20 catchErrors(() { |
| 21 catchErrors(() { | 21 catchErrors(() { |
| 22 controller = new StreamController(); | 22 controller = new StreamController(); |
| 23 |
| 24 // Assign to the "global" `stream`. |
| 23 stream = controller.stream | 25 stream = controller.stream |
| 24 .map((x) { | 26 .map((x) { |
| 25 events.add("map $x"); | 27 events.add("map $x"); |
| 26 return x + 100; | 28 return x + 100; |
| 27 }) | 29 }) |
| 28 .transform(new StreamTransformer.fromHandlers( | 30 .transform(new StreamTransformer.fromHandlers( |
| 29 handleError: (e, st, sink) { sink.add("error $e"); })) | 31 handleError: (e, st, sink) { sink.add("error $e"); })) |
| 30 .asBroadcastStream(); | 32 .asBroadcastStream(); |
| 33 |
| 34 // Listen to the `stream` in the inner zone (but wait in a microtask). |
| 31 scheduleMicrotask(() { | 35 scheduleMicrotask(() { |
| 32 stream.listen((x) { | 36 stream.listen((x) { |
| 33 events.add("stream $x"); | 37 events.add("stream $x"); |
| 34 if (x == "error 2") done.complete(true); | 38 if (x == "error 2") done.complete(true); |
| 35 }); | 39 }); |
| 36 }); | 40 }); |
| 37 }).listen((x) { events.add(x); }) | 41 }).listen((x) { events.add(x); }) |
| 38 .asFuture().then((_) { Expect.fail("Unexpected callback"); }); | 42 .asFuture().then((_) { Expect.fail("Unexpected callback"); }); |
| 43 |
| 44 // Listen to `stream` from the outer zone. |
| 39 stream.listen((x) { events.add("stream2 $x"); }); | 45 stream.listen((x) { events.add("stream2 $x"); }); |
| 46 |
| 47 // Feed the controller, but wait in a microtask. |
| 40 scheduleMicrotask(() { | 48 scheduleMicrotask(() { |
| 41 controller.add(1); | 49 controller.add(1); |
| 42 // Errors are not allowed to traverse boundaries, but in this case the | |
| 43 // first listener of the broadcast stream is in the same error-zone. So | |
| 44 // this should work. | |
| 45 controller.addError(2); | 50 controller.addError(2); |
| 46 controller.close(); | 51 controller.close(); |
| 47 }); | 52 }); |
| 48 }).listen((x) { events.add("outer: $x"); }, | 53 }).listen((x) { events.add("outer: $x"); }, |
| 49 onDone: () { Expect.fail("Unexpected callback"); }); | 54 onDone: () { Expect.fail("Unexpected callback"); }); |
| 50 | 55 |
| 51 done.future.whenComplete(() { | 56 done.future.whenComplete(() { |
| 52 // Give handlers time to complete. | 57 // Give handlers time to complete. |
| 53 Timer.run(() { | 58 Timer.run(() { |
| 54 Expect.listEquals(["map 1", | 59 Expect.listEquals(["map 1", |
| 55 "stream2 101", | 60 "stream2 101", |
| 56 "stream 101", | 61 "stream 101", |
| 57 "stream2 error 2", | 62 "stream2 error 2", |
| 58 "stream error 2", | 63 "stream error 2", |
| 59 ], | 64 ], |
| 60 events); | 65 events); |
| 61 asyncEnd(); | 66 asyncEnd(); |
| 62 }); | 67 }); |
| 63 }); | 68 }); |
| 64 } | 69 } |
| OLD | NEW |