Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(429)

Side by Side Diff: tests/lib/async/zone_debug_test.dart

Issue 23875032: Expose Zones. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: rebase Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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.registerCallback(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.run1(origin, f, arg);
47 }
48
49 List expectedDebugTrace;
50
51 debugUncaughtHandler(Zone self, ZoneDelegate parent, Zone origin, error) {
52 events.add("handling uncaught error $error");
53 Expect.listEquals(expectedDebugTrace, restoredStackTrace);
54 // Suppress the error and don't propagate to parent.
55 }
56
57 const DEBUG_SPECIFICATION = const ZoneSpecification(
58 registerCallback: debugZoneRegisterCallback,
59 registerUnaryCallback: debugZoneRegisterUnaryCallback,
60 run: debugZoneRun,
61 runUnary: debugZoneRunUnary,
62 handleUncaughtError: debugUncaughtHandler);
63
64 main() {
65 Completer done = new Completer();
66
67 // runGuarded calls run, captures the synchronous error (if any) and
68 // gives that one to handleUncaughtError.
69
70 Expect.identical(Zone.ROOT, Zone.current);
71 Zone forked;
72 forked = Zone.current.fork(specification: DEBUG_SPECIFICATION);
73
74 asyncStart();
75
76 int openTests = 0;
77
78 openTests++;
79 forked.run(() {
80 int forkTrace = stackTrace;
81 runAsync(() {
82 int runAsyncTrace = stackTrace;
83 runAsync(() {
84 expectedDebugTrace = [runAsyncTrace, forkTrace];
85 openTests--;
86 if (openTests == 0) {
87 done.complete();
88 }
89 throw "foo";
90 });
91 expectedDebugTrace = [forkTrace];
92 throw "bar";
93 });
94 });
95
96 Expect.listEquals([], restoredStackTrace);
97 Zone forked2 = forked.fork();
98 Zone forked3 = forked2.fork();
99 int fork2Trace;
100 int fork3Trace;
101 var f2;
102 var globalTrace = stackTrace;
103 var f = forked3.bindCallback(() {
104 Expect.identical(forked3, Zone.current);
105 fork2Trace = stackTrace;
106 f2 = forked2.bindCallback(() {
107 Expect.identical(forked2, Zone.current);
108 Expect.listEquals([fork2Trace, globalTrace], restoredStackTrace);
109 fork3Trace = stackTrace;
110 openTests--;
111 if (openTests == 0) {
112 done.complete();
113 }
114 runAsync(() {
115 expectedDebugTrace = [fork3Trace, fork2Trace, globalTrace];
116 throw "gee";
117 });
118 }, runGuarded: false);
119 }, runGuarded: false);
120 openTests++;
121 f();
122 f2();
123
124 done.future.whenComplete(() {
125 // We don't really care for the order.
126 events.sort();
127 Expect.listEquals([ "handling uncaught error bar",
128 "handling uncaught error foo",
129 "handling uncaught error gee"],
130 events);
131 asyncEnd();
132 });
133 }
OLDNEW
« no previous file with comments | « tests/lib/async/zone_create_timer_test.dart ('k') | tests/lib/async/zone_empty_description2_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698