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

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

Issue 916533003: Add an Invoker class that manages a running test. (Closed) Base URL: git@github.com:dart-lang/unittest@master
Patch Set: 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 | « no previous file | pubspec.yaml » ('j') | test/invoker_test.dart » ('J')
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
18 /// A test in this isolate.
19 class LocalTest implements Test {
20 /// The name of the test.
21 final String name;
22
23 /// The test body.
24 final Function _body;
kevmoo 2015/02/11 00:35:09 Consider adding a matching typedef for this is uti
nweiz 2015/02/11 02:07:06 Done.
25
26 /// The callback used to clean up after the test.
27 ///
28 /// This is separated out from [_body] because it needs to run once the test's
29 /// asynchronous computation has finished, even if that's different from the
30 /// completion of the main body of the test.
31 final Function _tearDown;
32
33 LocalTest(this.name, body(), {tearDown()})
34 : _body = body,
35 _tearDown = tearDown;
36
37 /// Loads a single runnable instance of this test.
38 LiveTest load(Suite suite) {
39 var invoker = new Invoker._(suite, this);
40 return invoker.liveTest;
41 }
42 }
43
44 /// The class responsible for managing the lifecycle of a single local test.
45 ///
46 /// The current invoker is accessible within the zone scope of the running test
47 /// using [Invoker.current]. It's used to track asynchronous callbacks and
48 /// report asynchronous errors.
49 class Invoker {
50 /// The live test being driven by the invoker.
51 ///
52 /// This provides a view into the state of the test being executed.
53 LiveTest get liveTest => _controller.liveTest;
54 LiveTestController _controller;
55
56 /// The test being run.
57 LocalTest get _test => liveTest.test as LocalTest;
58
59 /// Note that this is meaningless once [_onCompleteCompleter] is complete.
60 var _outstandingCallbacks = 0;
kevmoo 2015/02/11 00:35:09 int -> guard against invalid assignment
nweiz 2015/02/11 02:07:06 The definition makes it clear that this is an int
61
62 /// The completer to complete once the test body finishes.
63 ///
64 /// This is distinct from [_controller.completer] because a tear-down may need
65 /// to run before the test is truly finished.
66 final _completer = new Completer();
67
68 /// The current invoker, or `null` if none is defined.
69 ///
70 /// An invoker is only set within the zone scope of a running test.
71 static Invoker get current => Zone.current[#unittest._invoker];
72
73 Invoker._(Suite suite, LocalTest test) {
74 _controller = new LiveTestController(suite, test, _onRun);
75 }
76
77 /// Tells the invoker that there's a callback running that it should wait for
78 /// before considering the test successful.
79 ///
80 /// Each call to [addOutstandingCallback] should be followed by a call to
81 /// [removeOutstandingCallback] once the callbak is no longer running. Note
82 /// that only successful tests wait for outstanding callbacks; as soon as a
83 /// test experiences an error, any further calls to [addOutstandingCallback]
84 /// or [removeOutstandingCallback] will do nothing.
85 void addOutstandingCallback() {
86 _outstandingCallbacks++;
87 }
88
89 /// Tells the invoker that a callback declared with [addOutstandingCallback]
90 /// is no longer running.
91 void removeOutstandingCallback() {
92 _outstandingCallbacks--;
93
94 if (_outstandingCallbacks != 0) return;
95 if (_completer.isCompleted) return;
96
97 // The test must be passing if we get here, because if there were an error
98 // the completer would already be completed.
99 assert(liveTest.state.result == Result.success);
100 _completer.complete();
101 }
102
103 /// Notifies the invoker of an asynchronous error.
104 ///
105 /// Note that calling this explicitly is rarely necessary, since any
106 /// otherwise-uncaught errors will be forwarded to the invoker anyway.
107 void handleError(error, [StackTrace stackTrace]) {
108 if (stackTrace == null) stackTrace = new Chain.current();
109
110 var afterSuccess = liveTest.isComplete &&
111 liveTest.state.result == Result.success;
112
113 if (error is! TestFailure) {
114 _controller.setState(const State(Status.complete, Result.error));
115 } else if (liveTest.state.result != Result.error) {
116 _controller.setState(const State(Status.complete, Result.failure));
117 }
118
119 _controller.addError(error, stackTrace);
120
121 if (!_completer.isCompleted) _completer.complete();
122
123 // If a test was marked as success but then had an error, that indicates
124 // that it was poorly-written and could be flaky.
125 if (!afterSuccess) return;
126 handleError(
127 "This test failed after it had already completed. Make sure to use "
128 "[expectAsync]\n"
129 "or the [completes] matcher when testing async code.",
130 stackTrace);
131 }
132
133 /// The method that's run when the test is started.
134 void _onRun() {
135 _controller.setState(const State(Status.running, Result.success));
136
137 Chain.capture(() {
138 runZoned(() {
139 // TODO(nweiz): Make the timeout configurable.
140 // TODO(nweiz): Reset this timer whenever the user's code interacts with the
kevmoo 2015/02/11 00:35:09 long line
nweiz 2015/02/11 02:07:06 Done.
141 // library.
142 var timer = new Timer(new Duration(seconds: 30), () {
143 if (liveTest.isComplete) return;
144 handleError(
145 new TimeoutException(
146 "Test timed out after 30 seconds.",
147 new Duration(seconds: 30)));
148 });
149
150 addOutstandingCallback();
151
152 // Run the test asynchronously so that the "running" state change has a
153 // chance to hit its event handler(s) before the test produces an error.
154 // If an error is emitted before the first state change is handled, we
155 // can end up with [onError] callbacks firing before the
156 // corresponding [onStateChange], which violates the timing guarantees.
157 new Future(_test._body)
158 .then((_) => removeOutstandingCallback());
159
160 // Explicitly handle an error here so that we can return the [Future]. I f
kevmoo 2015/02/11 00:35:09 long line
nweiz 2015/02/11 02:07:06 Done.
161 // a [Future] returned from an error zone would throw an error through t he
162 // zone boundary, it instead never completes, and we want to avoid that.
163 _completer.future.then((_) {
164 if (_test._tearDown == null) return null;
165 return new Future.sync(_test._tearDown);
166 }).catchError(Zone.current.handleUncaughtError).then((_) {
167 timer.cancel();
168 _controller.setState(
169 new State(Status.complete, liveTest.state.result));
170 _controller.completer.complete();
171 });
172 }, zoneValues: {#unittest._invoker: this}, onError: handleError);
173 });
174 }
175 }
OLDNEW
« no previous file with comments | « no previous file | pubspec.yaml » ('j') | test/invoker_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698