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