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