Skip to content

Commit 975fbfd

Browse files
committed
Add a VmListener and IsolateTest class for running tests in other isolates.
This also adds LiveTest.close, which releases resources associated with a LiveTest. [email protected] See #2 Review URL: https://codereview.chromium.org//914963003
1 parent 6de347f commit 975fbfd

9 files changed

Lines changed: 667 additions & 88 deletions

‎.status‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ packages/*: Skip
1414
# difference between transformed an untransformed code.
1515
test/*: Skip
1616

17-
lib/src/live_test: Skip # Don't run library files as tests
17+
# Don't run library files as tests
18+
lib/*: Skip
19+
lib/*/*: Skip
20+
lib/*/*/*: Skip
21+
lib/*/*/*/*: Skip
1822

1923
[ $runtime == jsshell ]
2024
build/test/missing_tick_test: Fail # Timer interface not supported: dartbug.com/7728

‎lib/src/isolate_test.dart‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.isolate_test;
6+
7+
import 'dart:isolate';
8+
9+
import 'live_test.dart';
10+
import 'live_test_controller.dart';
11+
import 'remote_exception.dart';
12+
import 'state.dart';
13+
import 'suite.dart';
14+
import 'test.dart';
15+
16+
/// A test in another isolate.
17+
class IsolateTest implements Test {
18+
final String name;
19+
20+
/// The port on which to communicate with the remote test.
21+
final SendPort _sendPort;
22+
23+
IsolateTest(this.name, this._sendPort);
24+
25+
/// Loads a single runnable instance of this test.
26+
LiveTest load(Suite suite) {
27+
var receivePort;
28+
var controller;
29+
controller = new LiveTestController(suite, this, () {
30+
controller.setState(const State(Status.running, Result.success));
31+
32+
receivePort = new ReceivePort();
33+
_sendPort.send({
34+
'command': 'run',
35+
'reply': receivePort.sendPort
36+
});
37+
38+
receivePort.listen((message) {
39+
if (message['type'] == 'error') {
40+
var asyncError = RemoteException.deserialize(message['error']);
41+
controller.addError(asyncError.error, asyncError.stackTrace);
42+
} else if (message['type'] == 'state-change') {
43+
controller.setState(
44+
new State(
45+
new Status.parse(message['status']),
46+
new Result.parse(message['result'])));
47+
} else {
48+
assert(message['type'] == 'complete');
49+
controller.completer.complete();
50+
}
51+
});
52+
}, onClose: () {
53+
if (receivePort != null) receivePort.close();
54+
});
55+
return controller.liveTest;
56+
}
57+
}

‎lib/src/live_test.dart‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,17 @@ abstract class LiveTest {
101101
/// This returns the same [Future] as [onComplete]. It may not be called more
102102
/// than once.
103103
Future run();
104+
105+
/// Signals that this test should stop emitting events and release any
106+
/// resources it may have allocated.
107+
///
108+
/// Once [close] is called, [onComplete] will complete if it hasn't already
109+
/// and [onStateChange] and [onError] will close immediately. This means that,
110+
/// if the test was running at the time [close] is called, it will never emit
111+
/// a [Status.complete] state-change event.
112+
///
113+
/// This doesn't automatically happen after the test completes because there
114+
/// may be more asynchronous work going on in the background that could
115+
/// produce new errors.
116+
Future close();
104117
}

‎lib/src/live_test_controller.dart‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'live_test.dart';
1313
import 'state.dart';
1414
import 'suite.dart';
1515
import 'test.dart';
16+
import 'utils.dart';
1617

1718
/// An implementation of [LiveTest] that's controlled by a [LiveTestController].
1819
class _LiveTest extends LiveTest {
@@ -35,6 +36,8 @@ class _LiveTest extends LiveTest {
3536

3637
Future run() => _controller._run();
3738

39+
Future close() => _controller._close();
40+
3841
_LiveTest(this._controller);
3942
}
4043

@@ -61,6 +64,11 @@ class LiveTestController {
6164
/// The function that will actually start the test running.
6265
final Function _onRun;
6366

67+
/// A function to run when the test is closed.
68+
///
69+
/// This may be `null`.
70+
final AsyncFunction _onClose;
71+
6472
/// The list of errors caught by the test.
6573
final _errors = new List<AsyncError>();
6674

@@ -79,6 +87,9 @@ class LiveTestController {
7987
/// Whether [run] has been called.
8088
var _runCalled = false;
8189

90+
/// Whether [close] has been called.
91+
bool get _isClosed => _onErrorController.isClosed;
92+
8293
/// Creates a new controller for a [LiveTest].
8394
///
8495
/// [test] is the test being run; [suite] is the suite that contains it.
@@ -87,8 +98,13 @@ class LiveTestController {
8798
/// start the test running. The controller takes care of ensuring that
8899
/// [LiveTest.run] isn't called more than once and that [LiveTest.onComplete]
89100
/// is returned.
90-
LiveTestController(this._suite, this._test, void onRun())
91-
: _onRun = onRun {
101+
///
102+
/// If [onClose] is passed, it's called the first [LiveTest.close] is called.
103+
/// It should clean up any resources that have been allocated for the test. It
104+
/// may return a [Future].
105+
LiveTestController(this._suite, this._test, void onRun(), {onClose()})
106+
: _onRun = onRun,
107+
_onClose = onClose {
92108
_liveTest = new _LiveTest(this);
93109
}
94110

@@ -125,4 +141,15 @@ class LiveTestController {
125141
_onRun();
126142
return liveTest.onComplete;
127143
}
144+
145+
/// A wrapper for [_onClose] that ensures that all controllers are closed.
146+
Future _close() {
147+
if (_isClosed) return new Future.value();
148+
_onStateChangeController.close();
149+
_onErrorController.close();
150+
if (!completer.isCompleted) completer.complete();
151+
152+
if (_onClose != null) return new Future.sync(_onClose);
153+
return new Future.value();
154+
}
128155
}

‎lib/src/remote_exception.dart‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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.remote_exception;
6+
7+
import 'dart:async';
8+
9+
import 'package:stack_trace/stack_trace.dart';
10+
11+
import 'expect.dart';
12+
13+
/// An exception that was thrown remotely.
14+
///
15+
/// This could be an exception thrown in a different isolate, a different
16+
/// process, or on an entirely different computer.
17+
class RemoteException implements Exception {
18+
/// The original exception's message, if it had one.
19+
///
20+
/// If the original exception was a plain string, this will contain that
21+
/// string.
22+
final String message;
23+
24+
/// The value of the original exception's `runtimeType.toString()`.
25+
final String type;
26+
27+
/// The value of the original exception's `toString()`.
28+
final String _toString;
29+
30+
/// Serializes [error] and [stackTrace] into a JSON-safe object.
31+
///
32+
/// Other than JSON- and isolate-safety, no guarantees are made about the
33+
/// serialized format.
34+
static serialize(error, StackTrace stackTrace) {
35+
var message;
36+
if (error is String) {
37+
message = error;
38+
} else {
39+
try {
40+
message = error.message.toString();
41+
} on NoSuchMethodError catch (_) {
42+
// Do nothing.
43+
}
44+
}
45+
46+
return {
47+
'message': message,
48+
'type': error.runtimeType.toString(),
49+
'isTestFailure': error is TestFailure,
50+
'toString': error.toString(),
51+
'stackChain': new Chain.forTrace(stackTrace).toString()
52+
};
53+
}
54+
55+
/// Deserializes an exception serialized with [RemoteException.serialize].
56+
///
57+
/// The returned [AsyncError] is guaranteed to have a [RemoteException] as its
58+
/// error and a [Chain] as its stack trace.
59+
static AsyncError deserialize(serialized) {
60+
var exception;
61+
if (serialized['isTestFailure']) {
62+
exception = new RemoteTestFailure._(
63+
serialized['message'],
64+
serialized['type'],
65+
serialized['toString']);
66+
} else {
67+
exception = new RemoteException._(
68+
serialized['message'],
69+
serialized['type'],
70+
serialized['toString']);
71+
}
72+
73+
return new AsyncError(exception, new Chain.parse(serialized['stackChain']));
74+
}
75+
76+
RemoteException._(this.message, this.type, this._toString);
77+
78+
String toString() => _toString;
79+
}
80+
81+
/// A subclass of [RemoteException] that implements [TestFailure].
82+
///
83+
/// It's important to preserve [TestFailure]-ness, because tests have different
84+
/// results depending on whether an exception was a failure or an error.
85+
class RemoteTestFailure extends RemoteException implements TestFailure {
86+
RemoteTestFailure._(String message, String type, String toString)
87+
: super._(message, type, toString);
88+
}

‎lib/src/vm_listener.dart‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.vm_listener;
6+
7+
import 'dart:isolate';
8+
import 'dart:async';
9+
10+
import 'declarer.dart';
11+
import 'remote_exception.dart';
12+
import 'suite.dart';
13+
import 'test.dart';
14+
15+
/// A class that runs tests in a separate isolate and communicates the results
16+
/// back to the main isolate.
17+
class VmListener {
18+
/// The test suite to run.
19+
final Suite _suite;
20+
21+
/// Extracts metadata about all the tests in [main] and sends information
22+
/// about them over [sendPort].
23+
///
24+
/// Once that's done, this starts listening for commands about which tests to
25+
/// run.
26+
static void start(SendPort sendPort, main()) {
27+
var declarer = new Declarer();
28+
runZoned(main, zoneValues: {#unittest.declarer: declarer});
29+
new VmListener._(new Suite("VmListener", declarer.tests))
30+
._listen(sendPort);
31+
}
32+
33+
VmListener._(this._suite);
34+
35+
/// Send information about [_suite] across [sendPort] and start listening for
36+
/// commands to run the tests.
37+
void _listen(SendPort sendPort) {
38+
var tests = [];
39+
for (var i = 0; i < _suite.tests.length; i++) {
40+
var test = _suite.tests[i];
41+
var receivePort = new ReceivePort();
42+
tests.add({"name": test.name, "sendPort": receivePort.sendPort});
43+
44+
receivePort.listen((message) {
45+
assert(message['command'] == 'run');
46+
_runTest(test, message['reply']);
47+
});
48+
}
49+
50+
sendPort.send(tests);
51+
}
52+
53+
/// Runs [test] and send the results across [sendPort].
54+
void _runTest(Test test, SendPort sendPort) {
55+
var liveTest = test.load(_suite);
56+
57+
liveTest.onStateChange.listen((state) {
58+
sendPort.send({
59+
"type": "state-change",
60+
"status": state.status.name,
61+
"result": state.result.name
62+
});
63+
});
64+
65+
liveTest.onError.listen((asyncError) {
66+
sendPort.send({
67+
"type": "error",
68+
"error": RemoteException.serialize(
69+
asyncError.error, asyncError.stackTrace)
70+
});
71+
});
72+
73+
liveTest.run().then((_) => sendPort.send({"type": "complete"}));
74+
}
75+
}

0 commit comments

Comments
 (0)