OLD | NEW |
1 library angular.mock_zone; | 1 library angular.mock_zone; |
2 | 2 |
3 import 'dart:async' as dart_async; | 3 import 'dart:async' as dart_async; |
4 | 4 |
| 5 // async and sync are function compositions. |
| 6 class FunctionComposition { |
| 7 Function outer; |
| 8 Function inner; |
| 9 |
| 10 FunctionComposition(this.outer, this.inner); |
| 11 |
| 12 call() => outer(inner)(); |
| 13 } |
| 14 |
5 final _asyncQueue = <Function>[]; | 15 final _asyncQueue = <Function>[]; |
6 final _timerQueue = <_TimerSpec>[]; | 16 final _timerQueue = <_TimerSpec>[]; |
7 final _asyncErrors = []; | 17 final _asyncErrors = []; |
8 bool _noMoreAsync = false; | 18 bool _noMoreAsync = false; |
9 | 19 |
10 /** | 20 /** |
11 * Runs any queued up async calls and any async calls queued with | 21 * Processes the asynchronous queue established by [async]. |
12 * running microLeap. Example: | 22 * |
| 23 * [microLeap] will process all items in the asynchronous queue, |
| 24 * including new items queued during its execution. It will re-raise |
| 25 * any exceptions that occur. |
| 26 * |
| 27 * NOTE: [microLeap] can only be used in [async] tests. |
| 28 * |
| 29 * Example: |
13 * | 30 * |
14 * it('should run async code', async(() { | 31 * it('should run async code', async(() { |
15 * var thenRan = false; | 32 * var thenRan = false; |
16 * new Future.value('s').then((_) { thenRan = true; }); | 33 * new Future.value('s').then((_) { thenRan = true; }); |
17 * expect(thenRan).toBe(false); | 34 * expect(thenRan).toBe(false); |
18 * microLeap(); | 35 * microLeap(); |
19 * expect(thenRan).toBe(true); | 36 * expect(thenRan).toBe(true); |
20 * })); | 37 * })); |
21 * | 38 * |
22 * it('should run chained thens', async(() { | 39 * it('should run chained thens', async(() { |
23 * var log = []; | 40 * var log = []; |
24 * new Future.value('s') | 41 * new Future.value('s') |
25 * .then((_) { log.add('firstThen'); }) | 42 * .then((_) { log.add('firstThen'); }) |
26 * .then((_) { log.add('2ndThen'); }); | 43 * .then((_) { log.add('2ndThen'); }); |
27 * expect(log.join(' ')).toEqual(''); | 44 * expect(log.join(' ')).toEqual(''); |
28 * microLeap(); | 45 * microLeap(); |
29 * expect(log.join(' ')).toEqual('firstThen 2ndThen'); | 46 * expect(log.join(' ')).toEqual('firstThen 2ndThen'); |
30 * })); | 47 * })); |
31 * | 48 * |
32 */ | 49 */ |
33 microLeap() { | 50 microLeap() { |
34 while (!_asyncQueue.isEmpty) { | 51 while (_asyncQueue.isNotEmpty) { |
35 // copy the queue as it may change. | 52 // copy the queue as it may change. |
36 var toRun = new List.from(_asyncQueue); | 53 var toRun = new List.from(_asyncQueue); |
37 _asyncQueue.clear(); | 54 _asyncQueue.clear(); |
38 // TODO: Support the case where multiple exceptions are thrown. | 55 // TODO: Support the case where multiple exceptions are thrown. |
39 // e.g. with a throwNextException() method. | 56 // e.g. with a throwNextException() method. |
40 assert(_asyncErrors.isEmpty); | 57 assert(_asyncErrors.isEmpty); |
41 toRun.forEach((fn) => fn()); | 58 toRun.forEach((fn) => fn()); |
42 if (_asyncErrors.isNotEmpty) { | 59 if (_asyncErrors.isNotEmpty) { |
43 var e = _asyncErrors.removeAt(0); | 60 var e = _asyncErrors.removeAt(0); |
44 throw ['Async error', e[0], e[1]]; | 61 throw ['Async error', e[0], e[1]]; |
45 } | 62 } |
46 } | 63 } |
47 } | 64 } |
48 | 65 |
49 /** | 66 /** |
| 67 * Returns whether the async queue is empty. |
| 68 */ |
| 69 isAsyncQueueEmpty() => _asyncQueue.isEmpty; |
| 70 |
| 71 /** |
50 * Simulates a clock tick by running any scheduled timers. Can only be used | 72 * Simulates a clock tick by running any scheduled timers. Can only be used |
51 * in [async] tests.Clock tick will call [microLeap] to process the microtask | 73 * in [async] tests.Clock tick will call [microLeap] to process the microtask |
52 * queue before each timer callback. | 74 * queue before each timer callback. |
53 * | 75 * |
54 * Note: microtasks scheduled form the last timer are not going to be processed. | 76 * Note: microtasks scheduled form the last timer are not going to be processed. |
55 * | 77 * |
56 * Example: | 78 * Example: |
57 * | 79 * |
58 * it('should run queued timer after sufficient clock ticks', async(() { | 80 * it('should run queued timer after sufficient clock ticks', async(() { |
59 * bool timerRan = false; | 81 * bool timerRan = false; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 * Causes scheduleMicrotask calls to throw exceptions. | 146 * Causes scheduleMicrotask calls to throw exceptions. |
125 * | 147 * |
126 * This function is useful while debugging async tests: the exception | 148 * This function is useful while debugging async tests: the exception |
127 * is thrown from the scheduleMicrotask call-site instead later in the test. | 149 * is thrown from the scheduleMicrotask call-site instead later in the test. |
128 */ | 150 */ |
129 noMoreAsync() { | 151 noMoreAsync() { |
130 _noMoreAsync = true; | 152 _noMoreAsync = true; |
131 } | 153 } |
132 | 154 |
133 /** | 155 /** |
134 * Captures all scheduleMicrotask calls inside of a function. | 156 * Captures all scheduleMicrotask calls and newly created Timers |
| 157 * inside of a function. |
| 158 * |
| 159 * [async] will raise an exception if there are still active Timers |
| 160 * when the function completes. |
| 161 * |
| 162 * Use [clockTick] to process timers, and [microLeap] to process |
| 163 * scheduleMicrotask calls. |
| 164 * |
| 165 * NOTE: [async] will not return the result of [fn]. |
135 * | 166 * |
136 * Typically used within a test: | 167 * Typically used within a test: |
137 * | 168 * |
138 * it('should be async', async(() { | 169 * it('should be async', async(() { |
139 * ... | 170 * ... |
140 * })); | 171 * })); |
141 */ | 172 */ |
142 async(Function fn) => () { | 173 async(Function fn) => new FunctionComposition(_asyncOuter, fn); |
| 174 |
| 175 _asyncOuter(Function fn) => () { |
143 _noMoreAsync = false; | 176 _noMoreAsync = false; |
144 _asyncErrors.clear(); | 177 _asyncErrors.clear(); |
145 _timerQueue.clear(); | 178 _timerQueue.clear(); |
146 var zoneSpec = new dart_async.ZoneSpecification( | 179 var zoneSpec = new dart_async.ZoneSpecification( |
147 scheduleMicrotask: (_, __, ___, asyncFn) { | 180 scheduleMicrotask: (_, __, ___, asyncFn) { |
148 if (_noMoreAsync) { | 181 if (_noMoreAsync) { |
149 throw ['scheduleMicrotask called after noMoreAsync()']; | 182 throw ['scheduleMicrotask called after noMoreAsync()']; |
150 } else { | 183 } else { |
151 _asyncQueue.add(asyncFn); | 184 _asyncQueue.add(asyncFn); |
152 } | 185 } |
(...skipping 26 matching lines...) Expand all Loading... |
179 _createTimer(Function fn, Duration duration, bool periodic) { | 212 _createTimer(Function fn, Duration duration, bool periodic) { |
180 var timer = new _TimerSpec(fn, duration, periodic); | 213 var timer = new _TimerSpec(fn, duration, periodic); |
181 _timerQueue.add(timer); | 214 _timerQueue.add(timer); |
182 return timer; | 215 return timer; |
183 } | 216 } |
184 | 217 |
185 /** | 218 /** |
186 * Enforces synchronous code. Any calls to scheduleMicrotask inside of 'sync' | 219 * Enforces synchronous code. Any calls to scheduleMicrotask inside of 'sync' |
187 * will throw an exception. | 220 * will throw an exception. |
188 */ | 221 */ |
189 sync(Function fn) => () { | 222 sync(Function fn) => new FunctionComposition(_syncOuter, fn); |
| 223 |
| 224 _syncOuter(Function fn) => () { |
| 225 _asyncErrors.clear(); |
| 226 |
190 dart_async.runZoned(fn, zoneSpecification: new dart_async.ZoneSpecification( | 227 dart_async.runZoned(fn, zoneSpecification: new dart_async.ZoneSpecification( |
191 scheduleMicrotask: (_, __, ___, asyncFn) { | 228 scheduleMicrotask: (_, __, ___, asyncFn) { |
192 throw ['scheduleMicrotask called from sync function.']; | 229 throw ['scheduleMicrotask called from sync function.']; |
193 }, | 230 }, |
194 createTimer: (_, __, ____, Duration duration, void f()) { | 231 createTimer: (_, __, ____, Duration duration, void f()) { |
195 throw ['Timer created from sync function.']; | 232 throw ['Timer created from sync function.']; |
196 }, | 233 }, |
197 createPeriodicTimer: | 234 createPeriodicTimer: |
198 (_, __, ___, Duration period, void f(dart_async.Timer timer)) { | 235 (_, __, ___, Duration period, void f(dart_async.Timer timer)) { |
199 throw ['periodic Timer created from sync function.']; | 236 throw ['periodic Timer created from sync function.']; |
200 } | 237 }, |
| 238 handleUncaughtError: (_, __, ___, e, s) => _asyncErrors.add([e, s]) |
201 )); | 239 )); |
| 240 |
| 241 _asyncErrors.forEach((e) { |
| 242 throw "During runZoned: ${e[0]}. Stack:\n${e[1]}"; |
| 243 }); |
202 }; | 244 }; |
203 | 245 |
204 class _TimerSpec implements dart_async.Timer { | 246 class _TimerSpec implements dart_async.Timer { |
205 Function fn; | 247 Function fn; |
206 Duration duration; | 248 Duration duration; |
207 Duration elapsed = Duration.ZERO; | 249 Duration elapsed = Duration.ZERO; |
208 bool periodic; | 250 bool periodic; |
209 bool isActive = true; | 251 bool isActive = true; |
210 | 252 |
211 _TimerSpec(this.fn, this.duration, this.periodic); | 253 _TimerSpec(this.fn, this.duration, this.periodic); |
212 | 254 |
213 void cancel() { | 255 void cancel() { |
214 isActive = false; | 256 isActive = false; |
215 } | 257 } |
216 } | 258 } |
OLD | NEW |