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