Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Side by Side Diff: lib/src/invoker.dart

Issue 955543002: Move a bunch of src/ files into subdirectories. (Closed) Base URL: git@github.com:dart-lang/unittest@master
Patch Set: Fix library tags Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/src/group.dart ('k') | lib/src/io.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library unittest.invoker;
6
7 import 'dart:async';
8
9 import 'package:stack_trace/stack_trace.dart';
10
11 import 'expect.dart';
12 import 'live_test.dart';
13 import 'live_test_controller.dart';
14 import 'state.dart';
15 import 'suite.dart';
16 import 'test.dart';
17 import 'utils.dart';
18
19 /// A test in this isolate.
20 class LocalTest implements Test {
21 /// The name of the test.
22 final String name;
23
24 /// The test body.
25 final AsyncFunction _body;
26
27 /// The callback used to clean up after the test.
28 ///
29 /// This is separated out from [_body] because it needs to run once the test's
30 /// asynchronous computation has finished, even if that's different from the
31 /// completion of the main body of the test.
32 final AsyncFunction _tearDown;
33
34 LocalTest(this.name, body(), {tearDown()})
35 : _body = body,
36 _tearDown = tearDown;
37
38 /// Loads a single runnable instance of this test.
39 LiveTest load(Suite suite) {
40 var invoker = new Invoker._(suite, this);
41 return invoker.liveTest;
42 }
43 }
44
45 /// The class responsible for managing the lifecycle of a single local test.
46 ///
47 /// The current invoker is accessible within the zone scope of the running test
48 /// using [Invoker.current]. It's used to track asynchronous callbacks and
49 /// report asynchronous errors.
50 class Invoker {
51 /// The live test being driven by the invoker.
52 ///
53 /// This provides a view into the state of the test being executed.
54 LiveTest get liveTest => _controller.liveTest;
55 LiveTestController _controller;
56
57 /// The test being run.
58 LocalTest get _test => liveTest.test as LocalTest;
59
60 /// Note that this is meaningless once [_onCompleteCompleter] is complete.
61 var _outstandingCallbacks = 0;
62
63 /// The completer to complete once the test body finishes.
64 ///
65 /// This is distinct from [_controller.completer] because a tear-down may need
66 /// to run before the test is truly finished.
67 final _completer = new Completer();
68
69 /// The current invoker, or `null` if none is defined.
70 ///
71 /// An invoker is only set within the zone scope of a running test.
72 static Invoker get current {
73 // TODO(nweiz): Use a private symbol when dart2js supports it (issue 17526).
74 return Zone.current[#unittest.invoker];
75 }
76
77 Invoker._(Suite suite, LocalTest test) {
78 _controller = new LiveTestController(suite, test, _onRun);
79 }
80
81 /// Tells the invoker that there's a callback running that it should wait for
82 /// before considering the test successful.
83 ///
84 /// Each call to [addOutstandingCallback] should be followed by a call to
85 /// [removeOutstandingCallback] once the callbak is no longer running. Note
86 /// that only successful tests wait for outstanding callbacks; as soon as a
87 /// test experiences an error, any further calls to [addOutstandingCallback]
88 /// or [removeOutstandingCallback] will do nothing.
89 void addOutstandingCallback() {
90 _outstandingCallbacks++;
91 }
92
93 /// Tells the invoker that a callback declared with [addOutstandingCallback]
94 /// is no longer running.
95 void removeOutstandingCallback() {
96 _outstandingCallbacks--;
97
98 if (_outstandingCallbacks != 0) return;
99 if (_completer.isCompleted) return;
100
101 // The test must be passing if we get here, because if there were an error
102 // the completer would already be completed.
103 assert(liveTest.state.result == Result.success);
104 _completer.complete();
105 }
106
107 /// Notifies the invoker of an asynchronous error.
108 ///
109 /// Note that calling this explicitly is rarely necessary, since any
110 /// otherwise-uncaught errors will be forwarded to the invoker anyway.
111 void handleError(error, [StackTrace stackTrace]) {
112 if (stackTrace == null) stackTrace = new Chain.current();
113
114 var afterSuccess = liveTest.isComplete &&
115 liveTest.state.result == Result.success;
116
117 if (error is! TestFailure) {
118 _controller.setState(const State(Status.complete, Result.error));
119 } else if (liveTest.state.result != Result.error) {
120 _controller.setState(const State(Status.complete, Result.failure));
121 }
122
123 _controller.addError(error, stackTrace);
124
125 if (!_completer.isCompleted) _completer.complete();
126
127 // If a test was marked as success but then had an error, that indicates
128 // that it was poorly-written and could be flaky.
129 if (!afterSuccess) return;
130 handleError(
131 "This test failed after it had already completed. Make sure to use "
132 "[expectAsync]\n"
133 "or the [completes] matcher when testing async code.",
134 stackTrace);
135 }
136
137 /// The method that's run when the test is started.
138 void _onRun() {
139 _controller.setState(const State(Status.running, Result.success));
140
141 Chain.capture(() {
142 runZoned(() {
143 // TODO(nweiz): Make the timeout configurable.
144 // TODO(nweiz): Reset this timer whenever the user's code interacts with
145 // the library.
146 var timer = new Timer(new Duration(seconds: 30), () {
147 if (liveTest.isComplete) return;
148 handleError(
149 new TimeoutException(
150 "Test timed out after 30 seconds.",
151 new Duration(seconds: 30)));
152 });
153
154 addOutstandingCallback();
155
156 // Run the test asynchronously so that the "running" state change has a
157 // chance to hit its event handler(s) before the test produces an error.
158 // If an error is emitted before the first state change is handled, we
159 // can end up with [onError] callbacks firing before the corresponding
160 // [onStateChange], which violates the timing guarantees.
161 new Future(_test._body)
162 .then((_) => removeOutstandingCallback());
163
164 // Explicitly handle an error here so that we can return the [Future].
165 // If a [Future] returned from an error zone would throw an error
166 // through the zone boundary, it instead never completes, and we want to
167 // avoid that.
168 _completer.future.then((_) {
169 if (_test._tearDown == null) return null;
170 return new Future.sync(_test._tearDown);
171 }).catchError(Zone.current.handleUncaughtError).then((_) {
172 timer.cancel();
173 _controller.setState(
174 new State(Status.complete, liveTest.state.result));
175
176 // Use [Timer.run] here to avoid starving the DOM or other
177 // non-microtask events.
178 Timer.run(_controller.completer.complete);
179 });
180 }, zoneValues: {#unittest.invoker: this}, onError: handleError);
181 });
182 }
183 }
OLDNEW
« no previous file with comments | « lib/src/group.dart ('k') | lib/src/io.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698