| 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 |
| 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 result = null; |
| 46 forked.runGuarded(() { |
| 44 events.add("runGuarded 2"); | 47 events.add("runGuarded 2"); |
| 45 Expect.identical(forked, Zone.current); | 48 Expect.identical(forked, Zone.current); |
| 46 throw 42; | 49 throw 42; |
| 47 }); | 50 }); |
| 48 Expect.equals(499, result); | 51 Expect.equals(499, result); |
| 49 | 52 |
| 50 Expect.listEquals([ | 53 Expect.listEquals([ |
| 51 "forked.run", | 54 "forked.run", |
| 52 "runGuarded 1", | 55 "runGuarded 1", |
| 53 "after runGuarded 1", | 56 "after runGuarded 1", |
| 54 "forked.run", | 57 "forked.run", |
| 55 "runGuarded 2", | 58 "runGuarded 2", |
| 56 "forked.handleUncaught 42" | 59 "forked.handleUncaught 42" |
| 57 ], events); | 60 ], events); |
| 58 | 61 |
| 62 result = null; |
| 59 events.clear(); | 63 events.clear(); |
| 60 asyncStart(); | 64 asyncStart(); |
| 61 result = forked.runGuarded(() { | 65 forked.runGuarded(() { |
| 62 Expect.identical(forked, Zone.current); | 66 Expect.identical(forked, Zone.current); |
| 63 events.add("run closure"); | 67 events.add("run closure"); |
| 64 forked.scheduleMicrotask(() { | 68 forked.scheduleMicrotask(() { |
| 65 events.add("run closure 2"); | 69 events.add("run closure 2"); |
| 66 Expect.identical(forked, Zone.current); | 70 Expect.identical(forked, Zone.current); |
| 67 done.complete(true); | 71 done.complete(true); |
| 68 throw 88; | 72 throw 88; |
| 69 }); | 73 }); |
| 70 throw 1234; | 74 throw 1234; |
| 71 }); | 75 }); |
| 72 events.add("after nested scheduleMicrotask"); | 76 events.add("after nested scheduleMicrotask"); |
| 73 Expect.equals(499, result); | 77 Expect.equals(499, result); |
| 74 | 78 |
| 75 done.future.whenComplete(() { | 79 done.future.whenComplete(() { |
| 76 Expect.listEquals([ | 80 Expect.listEquals([ |
| 77 "forked.run", | 81 "forked.run", |
| 78 "run closure", | 82 "run closure", |
| 79 "forked.handleUncaught 1234", | 83 "forked.handleUncaught 1234", |
| 80 "after nested scheduleMicrotask", | 84 "after nested scheduleMicrotask", |
| 81 "forked.run", | 85 "forked.run", |
| 82 "run closure 2", | 86 "run closure 2", |
| 83 "forked.handleUncaught 88" | 87 "forked.handleUncaught 88" |
| 84 ], events); | 88 ], events); |
| 85 asyncEnd(); | 89 asyncEnd(); |
| 86 }); | 90 }); |
| 87 } | 91 } |
| OLD | NEW |