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