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