| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 library test.backend.invoker; | 5 library test.backend.invoker; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:stack_trace/stack_trace.dart'; | 9 import 'package:stack_trace/stack_trace.dart'; |
| 10 | 10 |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 void addOutstandingCallback() { | 113 void addOutstandingCallback() { |
| 114 if (closed) throw new ClosedException(); | 114 if (closed) throw new ClosedException(); |
| 115 _outstandingCallbacks.addOutstandingCallback(); | 115 _outstandingCallbacks.addOutstandingCallback(); |
| 116 } | 116 } |
| 117 | 117 |
| 118 /// Tells the invoker that a callback declared with [addOutstandingCallback] | 118 /// Tells the invoker that a callback declared with [addOutstandingCallback] |
| 119 /// is no longer running. | 119 /// is no longer running. |
| 120 void removeOutstandingCallback() => | 120 void removeOutstandingCallback() => |
| 121 _outstandingCallbacks.removeOutstandingCallback(); | 121 _outstandingCallbacks.removeOutstandingCallback(); |
| 122 | 122 |
| 123 /// Runs [fn] and returns once all (registered) outstanding callbacks it |
| 124 /// transitively invokes have completed. |
| 125 /// |
| 126 /// If [fn] itself returns a future, this will automatically wait until that |
| 127 /// future completes as well. |
| 128 /// |
| 129 /// Note that outstanding callbacks registered within [fn] will *not* be |
| 130 /// registered as outstanding callback outside of [fn]. |
| 131 Future waitForOutstandingCallbacks(fn()) { |
| 132 var counter = new OutstandingCallbackCounter(); |
| 133 runZoned(() { |
| 134 // TODO(nweiz): Use async/await here once issue 23497 has been fixed in |
| 135 // two stable versions. |
| 136 new Future.sync(fn).then((_) => counter.removeOutstandingCallback()); |
| 137 }, zoneValues: { |
| 138 // Use the invoker as a key so that multiple invokers can have different |
| 139 // outstanding callback counters at once. |
| 140 this: counter |
| 141 }); |
| 142 |
| 143 return counter.noOutstandingCallbacks; |
| 144 } |
| 145 |
| 123 /// Notifies the invoker of an asynchronous error. | 146 /// Notifies the invoker of an asynchronous error. |
| 124 /// | 147 /// |
| 125 /// Note that calling this explicitly is rarely necessary, since any | 148 /// Note that calling this explicitly is rarely necessary, since any |
| 126 /// otherwise-uncaught errors will be forwarded to the invoker anyway. | 149 /// otherwise-uncaught errors will be forwarded to the invoker anyway. |
| 127 void handleError(error, [StackTrace stackTrace]) { | 150 void handleError(error, [StackTrace stackTrace]) { |
| 128 if (stackTrace == null) stackTrace = new Chain.current(); | 151 if (stackTrace == null) stackTrace = new Chain.current(); |
| 129 | 152 |
| 130 var afterSuccess = liveTest.isComplete && | 153 var afterSuccess = liveTest.isComplete && |
| 131 liveTest.state.result == Result.success; | 154 liveTest.state.result == Result.success; |
| 132 | 155 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 // corresponding [onStateChange], which violates the timing | 199 // corresponding [onStateChange], which violates the timing |
| 177 // guarantees. | 200 // guarantees. |
| 178 new Future(_test._body) | 201 new Future(_test._body) |
| 179 .then((_) => removeOutstandingCallback()); | 202 .then((_) => removeOutstandingCallback()); |
| 180 | 203 |
| 181 _outstandingCallbacks.noOutstandingCallbacks.then((_) { | 204 _outstandingCallbacks.noOutstandingCallbacks.then((_) { |
| 182 if (_test._tearDown == null) return null; | 205 if (_test._tearDown == null) return null; |
| 183 | 206 |
| 184 // Reset the outstanding callback counter to wait for callbacks from | 207 // Reset the outstanding callback counter to wait for callbacks from |
| 185 // the test's `tearDown` to complete. | 208 // the test's `tearDown` to complete. |
| 186 var outstandingCallbacksForTearDown = new OutstandingCallbackCounter()
; | 209 return waitForOutstandingCallbacks(() => |
| 187 runZonedWithValues(() { | 210 runZoned(_test._tearDown, onError: handleError)); |
| 188 new Future.sync(_test._tearDown) | |
| 189 .then((_) => removeOutstandingCallback()); | |
| 190 }, onError: handleError, zoneValues: { | |
| 191 this: outstandingCallbacksForTearDown | |
| 192 }); | |
| 193 | |
| 194 return outstandingCallbacksForTearDown.noOutstandingCallbacks; | |
| 195 }).then((_) { | 211 }).then((_) { |
| 196 timer.cancel(); | 212 timer.cancel(); |
| 197 _controller.setState( | 213 _controller.setState( |
| 198 new State(Status.complete, liveTest.state.result)); | 214 new State(Status.complete, liveTest.state.result)); |
| 199 | 215 |
| 200 // Use [Timer.run] here to avoid starving the DOM or other | 216 // Use [Timer.run] here to avoid starving the DOM or other |
| 201 // non-microtask events. | 217 // non-microtask events. |
| 202 Timer.run(_controller.completer.complete); | 218 Timer.run(_controller.completer.complete); |
| 203 }); | 219 }); |
| 204 }, zoneValues: { | 220 }, zoneValues: { |
| 205 #test.invoker: this, | 221 #test.invoker: this, |
| 206 // Use the invoker as a key so that multiple invokers can have different | 222 // Use the invoker as a key so that multiple invokers can have different |
| 207 // outstanding callback counters at once. | 223 // outstanding callback counters at once. |
| 208 this: outstandingCallbacksForBody | 224 this: outstandingCallbacksForBody |
| 209 }, | 225 }, |
| 210 zoneSpecification: new ZoneSpecification( | 226 zoneSpecification: new ZoneSpecification( |
| 211 print: (self, parent, zone, line) => _controller.print(line)), | 227 print: (self, parent, zone, line) => _controller.print(line)), |
| 212 onError: handleError); | 228 onError: handleError); |
| 213 }); | 229 }); |
| 214 } | 230 } |
| 215 } | 231 } |
| OLD | NEW |