This repository was archived by the owner on Feb 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathzone.dart
289 lines (257 loc) · 7.99 KB
/
zone.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
library angular.mock_zone;
import 'dart:async' as dart_async;
// async and sync are function compositions.
class FunctionComposition {
Function outer;
Function inner;
FunctionComposition(this.outer, this.inner);
call() => outer(inner)();
}
final _asyncQueue = <Function>[];
final _timerQueue = <_TimerSpec>[];
final _asyncErrors = [];
bool _noMoreAsync = false;
/**
* Processes the asynchronous queue established by [async].
*
* [microLeap] will process all items in the asynchronous queue,
* including new items queued during its execution. It will re-raise
* any exceptions that occur.
*
* NOTE: [microLeap] can only be used in [async] tests.
*
* Example:
*
* it('should run async code', async(() {
* var thenRan = false;
* new Future.value('s').then((_) { thenRan = true; });
* expect(thenRan).toBe(false);
* microLeap();
* expect(thenRan).toBe(true);
* }));
*
* it('should run chained thens', async(() {
* var log = [];
* new Future.value('s')
* .then((_) { log.add('firstThen'); })
* .then((_) { log.add('2ndThen'); });
* expect(log.join(' ')).toEqual('');
* microLeap();
* expect(log.join(' ')).toEqual('firstThen 2ndThen');
* }));
*
*/
microLeap() {
while (_asyncQueue.isNotEmpty) {
// copy the queue as it may change.
var toRun = new List.from(_asyncQueue);
_asyncQueue.clear();
// TODO: Support the case where multiple exceptions are thrown.
// e.g. with a throwNextException() method.
toRun.forEach((fn) => fn());
if (_asyncErrors.isNotEmpty) {
var e = _asyncErrors.removeAt(0);
throw ['Async error', e[0], e[1]];
}
}
}
/**
* Returns whether the async queue is empty.
*/
isAsyncQueueEmpty() => _asyncQueue.isEmpty;
/**
* Returns whether there are outstanding timers.
*/
isTimerQueueEmpty() => _timerQueue.isEmpty;
/**
* Returns whether there are outstanding non-periodic timers.
*/
isNonPeriodicTimerQueueEmpty() => _timerQueue
.where((_TimerSpec spec) => !spec.periodic)
.isEmpty;
/**
* Returns whether there are outstanding periodic timers.
*/
isPeriodicTimerQueueEmpty() => _timerQueue
.where((_TimerSpec spec) => spec.periodic)
.isEmpty;
/**
* Simulates a clock tick by running any scheduled timers. Can only be used
* in [async] tests.Clock tick will call [microLeap] to process the microtask
* queue before each timer callback.
*
* Note: microtasks scheduled form the last timer are not going to be processed.
*
* Example:
*
* it('should run queued timer after sufficient clock ticks', async(() {
* bool timerRan = false;
* new Timer(new Duration(milliseconds: 10), () => timerRan = true);
*
* clockTick(milliseconds: 9);
* expect(timerRan).toBeFalsy();
* clockTick(milliseconds: 1);
* expect(timerRan).toBeTruthy();
* }));
*
* it('should run periodic timer', async(() {
* int timerRan = 0;
* new Timer.periodic(new Duration(milliseconds: 10), (_) => timerRan++);
*
* clockTick(milliseconds: 9);
* expect(timerRan).toBe(0);
* clockTick(milliseconds: 1);
* expect(timerRan).toBe(1);
* clockTick(milliseconds: 30);
* expect(timerRan).toBe(4);
* }));
*/
void clockTick({int days: 0,
int hours: 0,
int minutes: 0,
int seconds: 0,
int milliseconds: 0,
int microseconds: 0}) {
var tickDuration = new Duration(days: days, hours: hours, minutes: minutes,
seconds: seconds, milliseconds: milliseconds, microseconds: microseconds);
var remainingTimers = [];
var queue = new List.from(_timerQueue);
_timerQueue.clear();
queue
.where((_TimerSpec spec) => spec.isActive)
.forEach((_TimerSpec spec) {
if (spec.periodic) {
// We always add back the periodic timer unless it's cancelled.
remainingTimers.add(spec);
// Ignore ZERO duration ticks for periodic timers.
if (tickDuration == Duration.ZERO) return;
spec.elapsed += tickDuration;
// Run the timer as many times as the timer priod fits into the tick.
while (spec.elapsed >= spec.duration) {
spec.elapsed -= spec.duration;
microLeap();
spec.fn(spec);
}
} else {
spec.duration -= tickDuration;
if (spec.duration <= Duration.ZERO) {
microLeap();
spec.fn();
spec.isActive = false;
} else {
remainingTimers.add(spec);
}
}
});
// Remaining timers should come before anything else scheduled after them.
_timerQueue.insertAll(0, remainingTimers);
}
/**
* Causes scheduleMicrotask calls to throw exceptions.
*
* This function is useful while debugging async tests: the exception
* is thrown from the scheduleMicrotask call-site instead later in the test.
*/
noMoreAsync() {
_noMoreAsync = true;
}
/**
* Captures all scheduleMicrotask calls and newly created Timers
* inside of a function.
*
* [async] will raise an exception if there are still active Timers
* when the function completes.
*
* Use [clockTick] to process timers, and [microLeap] to process
* scheduleMicrotask calls.
*
* NOTE: [async] will not return the result of [fn].
*
* Typically used within a test:
*
* it('should be async', async(() {
* ...
* }));
*/
async(Function fn) => new FunctionComposition(_asyncOuter, fn);
_asyncOuter(Function fn) => () {
_noMoreAsync = false;
_asyncErrors.clear();
_timerQueue.clear();
var zoneSpec = new dart_async.ZoneSpecification(
scheduleMicrotask: (_, __, ___, asyncFn) {
if (_noMoreAsync) {
throw ['scheduleMicrotask called after noMoreAsync()'];
} else {
_asyncQueue.add(asyncFn);
}
},
createTimer: (_, __, ____, Duration duration, void f()) =>
_createTimer(f, duration, false),
createPeriodicTimer:
(_, __, ___, Duration period, void f(dart_async.Timer timer)) =>
_createTimer(f, period, true),
handleUncaughtError: (_, __, ___, e, s) => _asyncErrors.add([e, s])
);
dart_async.runZoned(() {
fn();
microLeap();
}, zoneSpecification: zoneSpec);
_asyncErrors.forEach((e) {
throw "During runZoned: ${e[0]}. Stack:\n${e[1]}";
});
var activeTimers = _timerQueue.fold(0, (nb, _TimerSpec spec) {
return spec.isActive ? nb + 1 : nb;
});
if (activeTimers > 0) {
throw ["$activeTimers active timer(s) are still in the queue."];
}
};
_createTimer(Function fn, Duration duration, bool periodic) {
var timer = new _TimerSpec(fn, duration, periodic);
_timerQueue.add(timer);
return timer;
}
/**
* Enforces synchronous code. Any calls to scheduleMicrotask inside of 'sync'
* will throw an exception.
*/
sync(Function fn) => new FunctionComposition(_syncOuter, fn);
_syncOuter(Function fn) => () {
_asyncErrors.clear();
dart_async.runZoned(fn, zoneSpecification: new dart_async.ZoneSpecification(
scheduleMicrotask: (_, __, ___, asyncFn) {
throw ['scheduleMicrotask called from sync function.'];
},
createTimer: (_, __, ____, Duration duration, void f()) {
throw ['Timer created from sync function.'];
},
createPeriodicTimer:
(_, __, ___, Duration period, void f(dart_async.Timer timer)) {
throw ['periodic Timer created from sync function.'];
},
handleUncaughtError: (_, __, ___, e, s) => _asyncErrors.add([e, s])
));
_asyncErrors.forEach((e) {
throw "During runZoned: ${e[0]}. Stack:\n${e[1]}";
});
};
class _TimerSpec implements dart_async.Timer {
Function fn;
Duration duration;
Duration elapsed = Duration.ZERO;
bool periodic;
bool isActive = true;
_TimerSpec(this.fn, this.duration, this.periodic);
void cancel() {
isActive = false;
}
}
class MockZone {
MockZone._internal();
MockZone get current => Zone.current['AngularMockZone'];
static Zone fork(Zone zone) {
MockZone mockZone = new MockZone._internal();
return zone.fork(zoneValues: { 'AngularMockZone': mockZone });
}
}