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