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 import 'dart:collection'; |
| 10 |
| 11 /** |
| 12 * We represent the current stack trace by an integer. From time to time we |
| 13 * increment the variable. This corresponds to a new stack trace. |
| 14 */ |
| 15 int stackTrace = 0; |
| 16 List restoredStackTrace = []; |
| 17 |
| 18 List events = []; |
| 19 |
| 20 debugZoneRegisterCallback(Zone self, ZoneDelegate parent, Zone origin, f()) { |
| 21 List savedTrace = [stackTrace]..addAll(restoredStackTrace); |
| 22 return parent.registerCallback(origin, () { |
| 23 restoredStackTrace = savedTrace; |
| 24 return f(); |
| 25 }); |
| 26 } |
| 27 |
| 28 debugZoneRegisterUnaryCallback(Zone self, ZoneDelegate parent, Zone origin, |
| 29 f(arg)) { |
| 30 List savedTrace = [stackTrace]..addAll(restoredStackTrace); |
| 31 return parent.registerUnaryCallback(origin, (arg) { |
| 32 restoredStackTrace = savedTrace; |
| 33 return f(arg); |
| 34 }); |
| 35 } |
| 36 |
| 37 debugZoneRun(Zone self, ZoneDelegate parent, Zone origin, f()) { |
| 38 stackTrace++; |
| 39 restoredStackTrace = []; |
| 40 return parent.run(origin, f); |
| 41 } |
| 42 |
| 43 debugZoneRunUnary(Zone self, ZoneDelegate parent, Zone origin, f(arg), arg) { |
| 44 stackTrace++; |
| 45 restoredStackTrace = []; |
| 46 return parent.runUnary(origin, f, arg); |
| 47 } |
| 48 |
| 49 List expectedDebugTrace; |
| 50 |
| 51 debugUncaughtHandler( |
| 52 Zone self, ZoneDelegate parent, Zone origin, error, StackTrace stackTrace) { |
| 53 events.add("handling uncaught error $error"); |
| 54 Expect.listEquals(expectedDebugTrace, restoredStackTrace); |
| 55 // Suppress the error and don't propagate to parent. |
| 56 } |
| 57 |
| 58 const DEBUG_SPECIFICATION = const ZoneSpecification( |
| 59 registerCallback: debugZoneRegisterCallback, |
| 60 registerUnaryCallback: debugZoneRegisterUnaryCallback, |
| 61 run: debugZoneRun, |
| 62 runUnary: debugZoneRunUnary, |
| 63 handleUncaughtError: debugUncaughtHandler); |
| 64 |
| 65 main() { |
| 66 Completer done = new Completer(); |
| 67 |
| 68 // runGuarded calls run, captures the synchronous error (if any) and |
| 69 // gives that one to handleUncaughtError. |
| 70 |
| 71 Expect.identical(Zone.ROOT, Zone.current); |
| 72 Zone forked; |
| 73 forked = Zone.current.fork(specification: DEBUG_SPECIFICATION); |
| 74 |
| 75 asyncStart(); |
| 76 |
| 77 int openTests = 0; |
| 78 |
| 79 openTests++; |
| 80 forked.run(() { |
| 81 int forkTrace = stackTrace; |
| 82 scheduleMicrotask(() { |
| 83 int scheduleMicrotaskTrace = stackTrace; |
| 84 scheduleMicrotask(() { |
| 85 expectedDebugTrace = [scheduleMicrotaskTrace, forkTrace]; |
| 86 openTests--; |
| 87 if (openTests == 0) { |
| 88 done.complete(); |
| 89 } |
| 90 throw "foo"; |
| 91 }); |
| 92 expectedDebugTrace = [forkTrace]; |
| 93 throw "bar"; |
| 94 }); |
| 95 }); |
| 96 |
| 97 Expect.listEquals([], restoredStackTrace); |
| 98 Zone forked2 = forked.fork(); |
| 99 Zone forked3 = forked2.fork(); |
| 100 int fork2Trace; |
| 101 int fork3Trace; |
| 102 var f2; |
| 103 var globalTrace = stackTrace; |
| 104 var f = forked3.bindCallback(() { |
| 105 Expect.identical(forked3, Zone.current); |
| 106 fork2Trace = stackTrace; |
| 107 f2 = forked2.bindCallback(() { |
| 108 Expect.identical(forked2, Zone.current); |
| 109 Expect.listEquals([fork2Trace, globalTrace], restoredStackTrace); |
| 110 fork3Trace = stackTrace; |
| 111 openTests--; |
| 112 if (openTests == 0) { |
| 113 done.complete(); |
| 114 } |
| 115 scheduleMicrotask(() { |
| 116 expectedDebugTrace = [fork3Trace, fork2Trace, globalTrace]; |
| 117 throw "gee"; |
| 118 }); |
| 119 }, runGuarded: false); |
| 120 }, runGuarded: false); |
| 121 openTests++; |
| 122 f(); |
| 123 f2(); |
| 124 |
| 125 done.future.whenComplete(() { |
| 126 // We don't really care for the order. |
| 127 events.sort(); |
| 128 Expect.listEquals([ "handling uncaught error bar", |
| 129 "handling uncaught error foo", |
| 130 "handling uncaught error gee"], |
| 131 events); |
| 132 asyncEnd(); |
| 133 }); |
| 134 } |
OLD | NEW |