| 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 // Work around bug that makes scheduleMicrotask use Timers. By invoking | 15 // Work around bug that makes scheduleMicrotask use Timers. By invoking |
| 16 // `scheduleMicrotask` here we make sure that asynchronous non-timer events | 16 // `scheduleMicrotask` here we make sure that asynchronous non-timer events |
| 17 // are executed before any Timer events. | 17 // are executed before any Timer events. |
| 18 scheduleMicrotask(() { }); | 18 scheduleMicrotask(() { }); |
| 19 | 19 |
| 20 // Test that errors are caught by nested `catchErrors`. Also uses | 20 // Test that errors are caught by nested `catchErrors`. Also uses |
| 21 // `scheduleMicrotask` in the body of a Timer. | 21 // `scheduleMicrotask` in the body of a Timer. |
| 22 bool outerTimerRan = false; |
| 23 int outerTimerCounterDelayCount = 0; |
| 22 catchErrors(() { | 24 catchErrors(() { |
| 23 events.add("catch error entry"); | 25 events.add("catch error entry"); |
| 24 catchErrors(() { | 26 catchErrors(() { |
| 25 events.add("catch error entry2"); | 27 events.add("catch error entry2"); |
| 26 Timer.run(() { throw "timer error"; }); | 28 Timer.run(() { throw "timer error"; }); |
| 27 new Timer(const Duration(milliseconds: 50), | 29 |
| 28 () { | 30 // We want this timer to run after the timer below. When the machine is |
| 29 scheduleMicrotask(() { throw "scheduleMicrotask"; }); | 31 // slow we saw that the timer below was scheduled more than 50ms after |
| 30 throw "delayed error"; | 32 // this line. A duration of 50ms was therefore not enough to guarantee |
| 31 }); | 33 // that this timer runs before the timer below. |
| 34 // Instead of increasing the duration to a bigger amount we decided to |
| 35 // verify that the other timer already ran, and reschedule if not. |
| 36 // This way we could reduce the timeout (to 10ms now), while still |
| 37 // allowing for more time, in case the machine is slow. |
| 38 void runDelayed() { |
| 39 new Timer(const Duration(milliseconds: 10), |
| 40 () { |
| 41 if (outerTimerRan) { |
| 42 scheduleMicrotask(() { throw "scheduleMicrotask"; }); |
| 43 throw "delayed error"; |
| 44 } else if (outerTimerCounterDelayCount < 100) { |
| 45 outerTimerCounterDelayCount++; |
| 46 runDelayed(); |
| 47 } |
| 48 }); |
| 49 } |
| 50 |
| 51 runDelayed(); |
| 32 }).listen((x) { | 52 }).listen((x) { |
| 33 events.add(x); | 53 events.add(x); |
| 34 if (x == "scheduleMicrotask") { | 54 if (x == "scheduleMicrotask") { |
| 35 throw "inner done throw"; | 55 throw "inner done throw"; |
| 36 } | 56 } |
| 37 }); | 57 }); |
| 38 events.add("after inner"); | 58 events.add("after inner"); |
| 39 Timer.run(() { throw "timer outer"; }); | 59 Timer.run(() { |
| 60 outerTimerRan = true; |
| 61 throw "timer outer"; |
| 62 }); |
| 40 throw "inner throw"; | 63 throw "inner throw"; |
| 41 }).listen((x) { | 64 }).listen((x) { |
| 42 events.add(x); | 65 events.add(x); |
| 43 if (x == "inner done throw") done.complete(true); | 66 if (x == "inner done throw") done.complete(true); |
| 44 }, | 67 }, |
| 45 onDone: () { Expect.fail("Unexpected callback"); }); | 68 onDone: () { Expect.fail("Unexpected callback"); }); |
| 46 | 69 |
| 47 done.future.whenComplete(() { | 70 done.future.whenComplete(() { |
| 48 // Give callbacks time to run. | 71 // Give callbacks time to run. |
| 49 Timer.run(() { | 72 Timer.run(() { |
| 50 Expect.listEquals([ | 73 Expect.listEquals([ |
| 51 "catch error entry", | 74 "catch error entry", |
| 52 "catch error entry2", | 75 "catch error entry2", |
| 53 "after inner", | 76 "after inner", |
| 54 "main exit", | 77 "main exit", |
| 55 "inner throw", | 78 "inner throw", |
| 56 "timer error", | 79 "timer error", |
| 57 "timer outer", | 80 "timer outer", |
| 58 "delayed error", | 81 "delayed error", |
| 59 "scheduleMicrotask", | 82 "scheduleMicrotask", |
| 60 "inner done throw" | 83 "inner done throw" |
| 61 ], | 84 ], |
| 62 events); | 85 events); |
| 63 asyncEnd(); | 86 asyncEnd(); |
| 64 }); | 87 }); |
| 65 }); | 88 }); |
| 66 events.add("main exit"); | 89 events.add("main exit"); |
| 67 } | 90 } |
| OLD | NEW |