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 import 'dart:collection'; | 9 import 'dart:collection'; |
10 | 10 |
11 /** | 11 /** |
12 * We represent the current stack trace by an integer. From time to time we | 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. | 13 * increment the variable. This corresponds to a new stack trace. |
14 */ | 14 */ |
15 int stackTrace = 0; | 15 int stackTrace = 0; |
16 List restoredStackTrace = []; | 16 List restoredStackTrace = []; |
17 | 17 |
18 List events = []; | 18 List events = []; |
19 | 19 |
20 debugZoneRegisterCallback(Zone self, ZoneDelegate parent, Zone origin, f()) { | 20 ZoneCallback<R> debugZoneRegisterCallback<R>( |
| 21 Zone self, ZoneDelegate parent, Zone origin, R f()) { |
21 List savedTrace = [stackTrace]..addAll(restoredStackTrace); | 22 List savedTrace = [stackTrace]..addAll(restoredStackTrace); |
22 return parent.registerCallback(origin, () { | 23 return parent.registerCallback(origin, () { |
23 restoredStackTrace = savedTrace; | 24 restoredStackTrace = savedTrace; |
24 return f(); | 25 return f(); |
25 }); | 26 }); |
26 } | 27 } |
27 | 28 |
28 debugZoneRegisterUnaryCallback( | 29 ZoneUnaryCallback<R, T> debugZoneRegisterUnaryCallback<R, T>( |
29 Zone self, ZoneDelegate parent, Zone origin, f(arg)) { | 30 Zone self, ZoneDelegate parent, Zone origin, R f(T arg)) { |
30 List savedTrace = [stackTrace]..addAll(restoredStackTrace); | 31 List savedTrace = [stackTrace]..addAll(restoredStackTrace); |
31 return parent.registerUnaryCallback(origin, (arg) { | 32 return parent.registerUnaryCallback(origin, (arg) { |
32 restoredStackTrace = savedTrace; | 33 restoredStackTrace = savedTrace; |
33 return f(arg); | 34 return f(arg); |
34 }); | 35 }); |
35 } | 36 } |
36 | 37 |
37 debugZoneRun(Zone self, ZoneDelegate parent, Zone origin, f()) { | 38 T debugZoneRun<T>(Zone self, ZoneDelegate parent, Zone origin, T f()) { |
38 stackTrace++; | 39 stackTrace++; |
39 restoredStackTrace = []; | 40 restoredStackTrace = []; |
40 return parent.run(origin, f); | 41 return parent.run(origin, f); |
41 } | 42 } |
42 | 43 |
43 debugZoneRunUnary(Zone self, ZoneDelegate parent, Zone origin, f(arg), arg) { | 44 R debugZoneRunUnary<R, T>( |
| 45 Zone self, ZoneDelegate parent, Zone origin, R f(T arg), T arg) { |
44 stackTrace++; | 46 stackTrace++; |
45 restoredStackTrace = []; | 47 restoredStackTrace = []; |
46 return parent.runUnary(origin, f, arg); | 48 return parent.runUnary(origin, f, arg); |
47 } | 49 } |
48 | 50 |
49 List expectedDebugTrace; | 51 List expectedDebugTrace; |
50 | 52 |
51 debugUncaughtHandler( | 53 void debugUncaughtHandler( |
52 Zone self, ZoneDelegate parent, Zone origin, error, StackTrace stackTrace) { | 54 Zone self, ZoneDelegate parent, Zone origin, error, StackTrace stackTrace) { |
53 events.add("handling uncaught error $error"); | 55 events.add("handling uncaught error $error"); |
54 Expect.listEquals(expectedDebugTrace, restoredStackTrace); | 56 Expect.listEquals(expectedDebugTrace, restoredStackTrace); |
55 // Suppress the error and don't propagate to parent. | 57 // Suppress the error and don't propagate to parent. |
56 } | 58 } |
57 | 59 |
58 const DEBUG_SPECIFICATION = const ZoneSpecification( | 60 const DEBUG_SPECIFICATION = const ZoneSpecification( |
59 registerCallback: debugZoneRegisterCallback, | 61 registerCallback: debugZoneRegisterCallback, |
60 registerUnaryCallback: debugZoneRegisterUnaryCallback, | 62 registerUnaryCallback: debugZoneRegisterUnaryCallback, |
61 run: debugZoneRun, | 63 run: debugZoneRun, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 Expect.listEquals([fork2Trace, globalTrace], restoredStackTrace); | 111 Expect.listEquals([fork2Trace, globalTrace], restoredStackTrace); |
110 fork3Trace = stackTrace; | 112 fork3Trace = stackTrace; |
111 openTests--; | 113 openTests--; |
112 if (openTests == 0) { | 114 if (openTests == 0) { |
113 done.complete(); | 115 done.complete(); |
114 } | 116 } |
115 scheduleMicrotask(() { | 117 scheduleMicrotask(() { |
116 expectedDebugTrace = [fork3Trace, fork2Trace, globalTrace]; | 118 expectedDebugTrace = [fork3Trace, fork2Trace, globalTrace]; |
117 throw "gee"; | 119 throw "gee"; |
118 }); | 120 }); |
119 }, runGuarded: false); | 121 }); |
120 }, runGuarded: false); | 122 }); |
121 openTests++; | 123 openTests++; |
122 f(); | 124 f(); |
123 f2(); | 125 f2(); |
124 | 126 |
125 done.future.whenComplete(() { | 127 done.future.whenComplete(() { |
126 // We don't really care for the order. | 128 // We don't really care for the order. |
127 events.sort(); | 129 events.sort(); |
128 Expect.listEquals([ | 130 Expect.listEquals([ |
129 "handling uncaught error bar", | 131 "handling uncaught error bar", |
130 "handling uncaught error foo", | 132 "handling uncaught error foo", |
131 "handling uncaught error gee" | 133 "handling uncaught error gee" |
132 ], events); | 134 ], events); |
133 asyncEnd(); | 135 asyncEnd(); |
134 }); | 136 }); |
135 } | 137 } |
OLD | NEW |