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