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