| 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 // Work around bug that makes runAsync use Timers. By invoking `runAsync` here | 15 // Work around bug that makes runAsync use Timers. By invoking `runAsync` here |
| 14 // we make sure that asynchronous non-timer events are executed before any | 16 // we make sure that asynchronous non-timer events are executed before any |
| 15 // Timer events. | 17 // Timer events. |
| 16 runAsync(() { }); | 18 runAsync(() { }); |
| 17 | 19 |
| 18 // Test that errors are caught by nested `catchErrors`. Also uses `runAsync` | 20 // Test that errors are caught by nested `catchErrors`. Also uses `runAsync` |
| 19 // in the body of a Timer. | 21 // in the body of a Timer. |
| 20 catchErrors(() { | 22 catchErrors(() { |
| 21 events.add("catch error entry"); | 23 events.add("catch error entry"); |
| 22 catchErrors(() { | 24 catchErrors(() { |
| 23 events.add("catch error entry2"); | 25 events.add("catch error entry2"); |
| 24 Timer.run(() { throw "timer error"; }); | 26 Timer.run(() { throw "timer error"; }); |
| 25 new Timer(const Duration(milliseconds: 50), | 27 new Timer(const Duration(milliseconds: 50), |
| 26 () { | 28 () { |
| 27 runAsync(() { throw "runAsync"; }); | 29 runAsync(() { throw "runAsync"; }); |
| 28 throw "delayed error"; | 30 throw "delayed error"; |
| 29 }); | 31 }); |
| 30 }).listen((x) { events.add(x); }) | 32 }).listen((x) { |
| 31 .asFuture() | 33 events.add(x); |
| 32 .then((_) => events.add("inner done")) | 34 if (x == "runAsync") { |
| 33 .then((_) { throw "inner done throw"; }); | 35 throw "inner done throw"; |
| 36 } |
| 37 }); |
| 34 events.add("after inner"); | 38 events.add("after inner"); |
| 35 Timer.run(() { throw "timer outer"; }); | 39 Timer.run(() { throw "timer outer"; }); |
| 36 throw "inner throw"; | 40 throw "inner throw"; |
| 37 }).listen((x) { | 41 }).listen((x) { |
| 38 events.add(x); | 42 events.add(x); |
| 43 if (x == "inner done throw") done.complete(true); |
| 39 }, | 44 }, |
| 40 onDone: () { | 45 onDone: () { Expect.fail("Unexpected callback"); }); |
| 46 |
| 47 done.future.whenComplete(() { |
| 48 // Give callbacks time to run. |
| 49 Timer.run(() { |
| 41 Expect.listEquals([ | 50 Expect.listEquals([ |
| 42 "catch error entry", | 51 "catch error entry", |
| 43 "catch error entry2", | 52 "catch error entry2", |
| 44 "after inner", | 53 "after inner", |
| 45 "main exit", | 54 "main exit", |
| 46 "inner throw", | 55 "inner throw", |
| 47 "timer error", | 56 "timer error", |
| 48 "timer outer", | 57 "timer outer", |
| 49 "delayed error", | 58 "delayed error", |
| 50 "runAsync", | 59 "runAsync", |
| 51 "inner done", | |
| 52 "inner done throw" | 60 "inner done throw" |
| 53 ], | 61 ], |
| 54 events); | 62 events); |
| 55 asyncEnd(); | 63 asyncEnd(); |
| 56 }); | 64 }); |
| 65 }); |
| 57 events.add("main exit"); | 66 events.add("main exit"); |
| 58 } | 67 } |
| OLD | NEW |