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