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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 /// | 60 /// |
61 /// Once the test is closed, [expect] and [expectAsync] will throw | 61 /// Once the test is closed, [expect] and [expectAsync] will throw |
62 /// [ClosedException]s whenever accessed to help the test stop executing as | 62 /// [ClosedException]s whenever accessed to help the test stop executing as |
63 /// soon as possible. | 63 /// soon as possible. |
64 bool get closed => _closed; | 64 bool get closed => _closed; |
65 bool _closed = false; | 65 bool _closed = false; |
66 | 66 |
67 /// The test being run. | 67 /// The test being run. |
68 LocalTest get _test => liveTest.test as LocalTest; | 68 LocalTest get _test => liveTest.test as LocalTest; |
69 | 69 |
| 70 /// The test metadata merged with the suite metadata. |
| 71 final Metadata metadata; |
| 72 |
70 /// Note that this is meaningless once [_onCompleteCompleter] is complete. | 73 /// Note that this is meaningless once [_onCompleteCompleter] is complete. |
71 var _outstandingCallbacks = 0; | 74 var _outstandingCallbacks = 0; |
72 | 75 |
73 /// The completer to complete once the test body finishes. | 76 /// The completer to complete once the test body finishes. |
74 /// | 77 /// |
75 /// This is distinct from [_controller.completer] because a tear-down may need | 78 /// This is distinct from [_controller.completer] because a tear-down may need |
76 /// to run before the test is truly finished. | 79 /// to run before the test is truly finished. |
77 final _completer = new Completer(); | 80 final _completer = new Completer(); |
78 | 81 |
79 /// The current invoker, or `null` if none is defined. | 82 /// The current invoker, or `null` if none is defined. |
80 /// | 83 /// |
81 /// An invoker is only set within the zone scope of a running test. | 84 /// An invoker is only set within the zone scope of a running test. |
82 static Invoker get current { | 85 static Invoker get current { |
83 // TODO(nweiz): Use a private symbol when dart2js supports it (issue 17526). | 86 // TODO(nweiz): Use a private symbol when dart2js supports it (issue 17526). |
84 return Zone.current[#test.invoker]; | 87 return Zone.current[#test.invoker]; |
85 } | 88 } |
86 | 89 |
87 Invoker._(Suite suite, LocalTest test) { | 90 Invoker._(Suite suite, LocalTest test) |
| 91 : metadata = suite.metadata.merge(test.metadata) { |
88 _controller = new LiveTestController(suite, test, _onRun, () { | 92 _controller = new LiveTestController(suite, test, _onRun, () { |
89 _closed = true; | 93 _closed = true; |
90 }); | 94 }); |
91 } | 95 } |
92 | 96 |
93 /// Tells the invoker that there's a callback running that it should wait for | 97 /// Tells the invoker that there's a callback running that it should wait for |
94 /// before considering the test successful. | 98 /// before considering the test successful. |
95 /// | 99 /// |
96 /// Each call to [addOutstandingCallback] should be followed by a call to | 100 /// Each call to [addOutstandingCallback] should be followed by a call to |
97 /// [removeOutstandingCallback] once the callbak is no longer running. Note | 101 /// [removeOutstandingCallback] once the callbak is no longer running. Note |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 | 155 |
152 /// The method that's run when the test is started. | 156 /// The method that's run when the test is started. |
153 void _onRun() { | 157 void _onRun() { |
154 _controller.setState(const State(Status.running, Result.success)); | 158 _controller.setState(const State(Status.running, Result.success)); |
155 | 159 |
156 Chain.capture(() { | 160 Chain.capture(() { |
157 runZoned(() { | 161 runZoned(() { |
158 // TODO(nweiz): Make the timeout configurable. | 162 // TODO(nweiz): Make the timeout configurable. |
159 // TODO(nweiz): Reset this timer whenever the user's code interacts with | 163 // TODO(nweiz): Reset this timer whenever the user's code interacts with |
160 // the library. | 164 // the library. |
161 var timeout = _test.metadata.timeout.apply(new Duration(seconds: 30)); | 165 var timeout = metadata.timeout.apply(new Duration(seconds: 30)); |
162 var timer = new Timer(timeout, () { | 166 var timer = new Timer(timeout, () { |
163 if (liveTest.isComplete) return; | 167 if (liveTest.isComplete) return; |
164 handleError( | 168 handleError( |
165 new TimeoutException( | 169 new TimeoutException( |
166 "Test timed out after ${niceDuration(timeout)}.", timeout)); | 170 "Test timed out after ${niceDuration(timeout)}.", timeout)); |
167 }); | 171 }); |
168 | 172 |
169 addOutstandingCallback(); | 173 addOutstandingCallback(); |
170 | 174 |
171 // Run the test asynchronously so that the "running" state change has a | 175 // Run the test asynchronously so that the "running" state change has a |
(...skipping 17 matching lines...) Expand all Loading... |
189 Timer.run(_controller.completer.complete); | 193 Timer.run(_controller.completer.complete); |
190 }); | 194 }); |
191 }, | 195 }, |
192 zoneSpecification: new ZoneSpecification( | 196 zoneSpecification: new ZoneSpecification( |
193 print: (self, parent, zone, line) => _controller.print(line)), | 197 print: (self, parent, zone, line) => _controller.print(line)), |
194 zoneValues: {#test.invoker: this}, | 198 zoneValues: {#test.invoker: this}, |
195 onError: handleError); | 199 onError: handleError); |
196 }); | 200 }); |
197 } | 201 } |
198 } | 202 } |
OLD | NEW |