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:expect/expect.dart'; | 5 import 'package:expect/expect.dart'; |
6 import 'package:async_helper/async_helper.dart'; | 6 import 'package:async_helper/async_helper.dart'; |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 main() { | 9 main() { |
| 10 var result; |
| 11 |
10 Completer done = new Completer(); | 12 Completer done = new Completer(); |
11 List events = []; | 13 List events = []; |
12 | 14 |
13 // runGuarded calls run, captures the synchronous error (if any) and | 15 // runGuarded calls run, captures the synchronous error (if any) and |
14 // gives that one to handleUncaughtError. | 16 // gives that one to handleUncaughtError. |
15 | 17 |
16 Expect.identical(Zone.ROOT, Zone.current); | 18 Expect.identical(Zone.ROOT, Zone.current); |
17 Zone forked; | 19 Zone forked; |
18 forked = Zone.current.fork( | 20 forked = Zone.current.fork( |
19 specification: new ZoneSpecification( | 21 specification: new ZoneSpecification( |
20 run: (Zone self, ZoneDelegate parent, Zone origin, f()) { | 22 run: <R>(Zone self, ZoneDelegate parent, Zone origin, R f()) { |
21 // The zone is still the same as when origin.run was invoked, which | 23 // The zone is still the same as when origin.run was invoked, which |
22 // is the root zone. (The origin zone hasn't been set yet). | 24 // is the root zone. (The origin zone hasn't been set yet). |
23 Expect.identical(Zone.ROOT, Zone.current); | 25 Expect.identical(Zone.ROOT, Zone.current); |
24 events.add("forked.run"); | 26 events.add("forked.run"); |
25 return parent.run(origin, f); | 27 return parent.run(origin, f); |
26 }, handleUncaughtError: | 28 }, handleUncaughtError: |
27 (Zone self, ZoneDelegate parent, Zone origin, error, stackTrace) { | 29 (Zone self, ZoneDelegate parent, Zone origin, error, stackTrace) { |
28 Expect.identical(Zone.ROOT, Zone.current); | 30 Expect.identical(Zone.ROOT, Zone.current); |
29 Expect.identical(forked, origin); | 31 Expect.identical(forked, origin); |
30 events.add("forked.handleUncaught $error"); | 32 events.add("forked.handleUncaught $error"); |
31 return 499; | 33 result = 499; |
32 })); | 34 })); |
33 | 35 |
34 var result = forked.runGuarded(() { | 36 forked.runGuarded(() { |
35 events.add("runGuarded 1"); | 37 events.add("runGuarded 1"); |
36 Expect.identical(forked, Zone.current); | 38 Expect.identical(forked, Zone.current); |
37 return 42; | 39 result = 42; |
38 }); | 40 }); |
39 Expect.identical(Zone.ROOT, Zone.current); | 41 Expect.identical(Zone.ROOT, Zone.current); |
40 Expect.equals(42, result); | 42 Expect.equals(42, result); |
41 events.add("after runGuarded 1"); | 43 events.add("after runGuarded 1"); |
42 | 44 |
43 result = forked.runGuarded(() { | 45 forked.runGuarded(() { |
44 events.add("runGuarded 2"); | 46 events.add("runGuarded 2"); |
45 Expect.identical(forked, Zone.current); | 47 Expect.identical(forked, Zone.current); |
46 throw 42; | 48 throw 42; |
47 }); | 49 }); |
48 Expect.equals(499, result); | 50 Expect.equals(499, result); |
49 | 51 |
50 Expect.listEquals([ | 52 Expect.listEquals([ |
51 "forked.run", | 53 "forked.run", |
52 "runGuarded 1", | 54 "runGuarded 1", |
53 "after runGuarded 1", | 55 "after runGuarded 1", |
54 "forked.run", | 56 "forked.run", |
55 "runGuarded 2", | 57 "runGuarded 2", |
56 "forked.handleUncaught 42" | 58 "forked.handleUncaught 42" |
57 ], events); | 59 ], events); |
58 | 60 |
| 61 result = null; |
59 events.clear(); | 62 events.clear(); |
60 asyncStart(); | 63 asyncStart(); |
61 result = forked.runGuarded(() { | 64 forked.runGuarded(() { |
62 Expect.identical(forked, Zone.current); | 65 Expect.identical(forked, Zone.current); |
63 events.add("run closure"); | 66 events.add("run closure"); |
64 forked.scheduleMicrotask(() { | 67 forked.scheduleMicrotask(() { |
65 events.add("run closure 2"); | 68 events.add("run closure 2"); |
66 Expect.identical(forked, Zone.current); | 69 Expect.identical(forked, Zone.current); |
67 done.complete(true); | 70 done.complete(true); |
68 throw 88; | 71 throw 88; |
69 }); | 72 }); |
70 throw 1234; | 73 throw 1234; |
71 }); | 74 }); |
72 events.add("after nested scheduleMicrotask"); | 75 events.add("after nested scheduleMicrotask"); |
73 Expect.equals(499, result); | 76 Expect.equals(499, result); |
74 | 77 |
75 done.future.whenComplete(() { | 78 done.future.whenComplete(() { |
76 Expect.listEquals([ | 79 Expect.listEquals([ |
77 "forked.run", | 80 "forked.run", |
78 "run closure", | 81 "run closure", |
79 "forked.handleUncaught 1234", | 82 "forked.handleUncaught 1234", |
80 "after nested scheduleMicrotask", | 83 "after nested scheduleMicrotask", |
81 "forked.run", | 84 "forked.run", |
82 "run closure 2", | 85 "run closure 2", |
83 "forked.handleUncaught 88" | 86 "forked.handleUncaught 88" |
84 ], events); | 87 ], events); |
85 asyncEnd(); | 88 asyncEnd(); |
86 }); | 89 }); |
87 } | 90 } |
OLD | NEW |