| 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 | |
| 12 Completer done = new Completer(); | 10 Completer done = new Completer(); |
| 13 List events = []; | 11 List events = []; |
| 14 | 12 |
| 15 // runGuarded calls run, captures the synchronous error (if any) and | 13 // runGuarded calls run, captures the synchronous error (if any) and |
| 16 // gives that one to handleUncaughtError. | 14 // gives that one to handleUncaughtError. |
| 17 | 15 |
| 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 forked.runGuarded(() { | 43 result = forked.runGuarded(() { |
| 46 events.add("runGuarded 2"); | 44 events.add("runGuarded 2"); |
| 47 Expect.identical(forked, Zone.current); | 45 Expect.identical(forked, Zone.current); |
| 48 throw 42; | 46 throw 42; |
| 49 }); | 47 }); |
| 50 Expect.equals(499, result); | 48 Expect.equals(499, result); |
| 51 | 49 |
| 52 Expect.listEquals([ | 50 Expect.listEquals([ |
| 53 "forked.run", | 51 "forked.run", |
| 54 "runGuarded 1", | 52 "runGuarded 1", |
| 55 "after runGuarded 1", | 53 "after runGuarded 1", |
| 56 "forked.run", | 54 "forked.run", |
| 57 "runGuarded 2", | 55 "runGuarded 2", |
| 58 "forked.handleUncaught 42" | 56 "forked.handleUncaught 42" |
| 59 ], events); | 57 ], events); |
| 60 | 58 |
| 61 result = null; | |
| 62 events.clear(); | 59 events.clear(); |
| 63 asyncStart(); | 60 asyncStart(); |
| 64 forked.runGuarded(() { | 61 result = forked.runGuarded(() { |
| 65 Expect.identical(forked, Zone.current); | 62 Expect.identical(forked, Zone.current); |
| 66 events.add("run closure"); | 63 events.add("run closure"); |
| 67 forked.scheduleMicrotask(() { | 64 forked.scheduleMicrotask(() { |
| 68 events.add("run closure 2"); | 65 events.add("run closure 2"); |
| 69 Expect.identical(forked, Zone.current); | 66 Expect.identical(forked, Zone.current); |
| 70 done.complete(true); | 67 done.complete(true); |
| 71 throw 88; | 68 throw 88; |
| 72 }); | 69 }); |
| 73 throw 1234; | 70 throw 1234; |
| 74 }); | 71 }); |
| 75 events.add("after nested scheduleMicrotask"); | 72 events.add("after nested scheduleMicrotask"); |
| 76 Expect.equals(499, result); | 73 Expect.equals(499, result); |
| 77 | 74 |
| 78 done.future.whenComplete(() { | 75 done.future.whenComplete(() { |
| 79 Expect.listEquals([ | 76 Expect.listEquals([ |
| 80 "forked.run", | 77 "forked.run", |
| 81 "run closure", | 78 "run closure", |
| 82 "forked.handleUncaught 1234", | 79 "forked.handleUncaught 1234", |
| 83 "after nested scheduleMicrotask", | 80 "after nested scheduleMicrotask", |
| 84 "forked.run", | 81 "forked.run", |
| 85 "run closure 2", | 82 "run closure 2", |
| 86 "forked.handleUncaught 88" | 83 "forked.handleUncaught 88" |
| 87 ], events); | 84 ], events); |
| 88 asyncEnd(); | 85 asyncEnd(); |
| 89 }); | 86 }); |
| 90 } | 87 } |
| OLD | NEW |