Chromium Code Reviews| 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 void runDelayed() { |
| 29 scheduleMicrotask(() { throw "scheduleMicrotask"; }); | 31 new Timer(const Duration(milliseconds: 10), |
| 30 throw "delayed error"; | 32 () { |
| 31 }); | 33 if (outerTimerRan) { |
| 34 scheduleMicrotask(() { throw "scheduleMicrotask"; }); | |
| 35 throw "delayed error"; | |
| 36 } else if (outerTimerCounterDelayCount < 100) { | |
|
sra1
2013/11/08 00:09:02
What is significant about "100" ?
It seems to me
floitsch
2013/11/08 00:19:52
I prefer making it deterministic. Added a comment.
| |
| 37 outerTimerCounterDelayCount++; | |
| 38 runDelayed(); | |
| 39 } | |
| 40 }); | |
| 41 } | |
| 42 | |
| 43 runDelayed(); | |
| 32 }).listen((x) { | 44 }).listen((x) { |
| 33 events.add(x); | 45 events.add(x); |
| 34 if (x == "scheduleMicrotask") { | 46 if (x == "scheduleMicrotask") { |
| 35 throw "inner done throw"; | 47 throw "inner done throw"; |
| 36 } | 48 } |
| 37 }); | 49 }); |
| 38 events.add("after inner"); | 50 events.add("after inner"); |
| 39 Timer.run(() { throw "timer outer"; }); | 51 Timer.run(() { |
| 52 outerTimerRan = true; | |
| 53 throw "timer outer"; | |
| 54 }); | |
| 40 throw "inner throw"; | 55 throw "inner throw"; |
| 41 }).listen((x) { | 56 }).listen((x) { |
| 42 events.add(x); | 57 events.add(x); |
| 43 if (x == "inner done throw") done.complete(true); | 58 if (x == "inner done throw") done.complete(true); |
| 44 }, | 59 }, |
| 45 onDone: () { Expect.fail("Unexpected callback"); }); | 60 onDone: () { Expect.fail("Unexpected callback"); }); |
| 46 | 61 |
| 47 done.future.whenComplete(() { | 62 done.future.whenComplete(() { |
| 48 // Give callbacks time to run. | 63 // Give callbacks time to run. |
| 49 Timer.run(() { | 64 Timer.run(() { |
| 50 Expect.listEquals([ | 65 Expect.listEquals([ |
| 51 "catch error entry", | 66 "catch error entry", |
| 52 "catch error entry2", | 67 "catch error entry2", |
| 53 "after inner", | 68 "after inner", |
| 54 "main exit", | 69 "main exit", |
| 55 "inner throw", | 70 "inner throw", |
| 56 "timer error", | 71 "timer error", |
| 57 "timer outer", | 72 "timer outer", |
| 58 "delayed error", | 73 "delayed error", |
| 59 "scheduleMicrotask", | 74 "scheduleMicrotask", |
| 60 "inner done throw" | 75 "inner done throw" |
| 61 ], | 76 ], |
| 62 events); | 77 events); |
| 63 asyncEnd(); | 78 asyncEnd(); |
| 64 }); | 79 }); |
| 65 }); | 80 }); |
| 66 events.add("main exit"); | 81 events.add("main exit"); |
| 67 } | 82 } |
| OLD | NEW |