OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'package:async_helper/async_helper.dart'; |
| 6 import "package:expect/expect.dart"; |
| 7 import 'dart:async'; |
| 8 import 'catch_errors.dart'; |
| 9 |
| 10 main() { |
| 11 asyncStart(); |
| 12 Completer done = new Completer(); |
| 13 |
| 14 var events = []; |
| 15 // Work around bug that makes scheduleMicrotask use Timers. By invoking |
| 16 // `scheduleMicrotask` here we make sure that asynchronous non-timer events |
| 17 // are executed before any Timer events. |
| 18 scheduleMicrotask(() { }); |
| 19 |
| 20 // Test that errors are caught by nested `catchErrors`. Also uses |
| 21 // `scheduleMicrotask` in the body of a Timer. |
| 22 bool outerTimerRan = false; |
| 23 int outerTimerCounterDelayCount = 0; |
| 24 catchErrors(() { |
| 25 events.add("catch error entry"); |
| 26 catchErrors(() { |
| 27 events.add("catch error entry2"); |
| 28 Timer.run(() { throw "timer error"; }); |
| 29 |
| 30 // 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 |
| 32 // this line. A duration of 50ms was therefore not enough to guarantee |
| 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(); |
| 52 }).listen((x) { |
| 53 events.add(x); |
| 54 if (x == "scheduleMicrotask") { |
| 55 throw "inner done throw"; |
| 56 } |
| 57 }); |
| 58 events.add("after inner"); |
| 59 Timer.run(() { |
| 60 outerTimerRan = true; |
| 61 throw "timer outer"; |
| 62 }); |
| 63 throw "inner throw"; |
| 64 }).listen((x) { |
| 65 events.add(x); |
| 66 if (x == "inner done throw") done.complete(true); |
| 67 }, |
| 68 onDone: () { Expect.fail("Unexpected callback"); }); |
| 69 |
| 70 done.future.whenComplete(() { |
| 71 // Give callbacks time to run. |
| 72 Timer.run(() { |
| 73 Expect.listEquals([ |
| 74 "catch error entry", |
| 75 "catch error entry2", |
| 76 "after inner", |
| 77 "main exit", |
| 78 "inner throw", |
| 79 "timer error", |
| 80 "timer outer", |
| 81 "delayed error", |
| 82 "scheduleMicrotask", |
| 83 "inner done throw" |
| 84 ], |
| 85 events); |
| 86 asyncEnd(); |
| 87 }); |
| 88 }); |
| 89 events.add("main exit"); |
| 90 } |
OLD | NEW |