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

Unified Diff: lib/src/dart.dart

Issue 920703006: Add a Loader class for loading tests from files. (Closed) Base URL: git@github.com:dart-lang/unittest@master
Patch Set: Code review changes 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | lib/src/io.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/dart.dart
diff --git a/lib/src/dart.dart b/lib/src/dart.dart
new file mode 100644
index 0000000000000000000000000000000000000000..5f4aea5fa09a3454a9bce8c7d0adfc3ee547b91a
--- /dev/null
+++ b/lib/src/dart.dart
@@ -0,0 +1,66 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library unittest.dart;
+
+import 'dart:async';
+import 'dart:io';
+import 'dart:isolate';
+
+import 'package:path/path.dart' as p;
+
+import 'io.dart';
+import 'remote_exception.dart';
+
+/// Runs [code] in an isolate.
+///
+/// [code] should be the contents of a Dart entrypoint. It may contain imports;
+/// they will be resolved in the same context as the host isolate. [message] is
+/// passed to the [main] method of the code being run; the caller is responsible
+/// for using this to establish communication with the isolate.
+///
+/// [packageRoot] controls the package root of the isolate. It may be either a
+/// [String] or a [Uri].
+Future<Isolate> runInIsolate(String code, message, {packageRoot}) {
+ // TODO(nweiz): load code from a local server rather than from a file.
+ return withTempDir((dir) {
+ var dartPath = p.join(dir, 'runInIsolate.dart');
+ new File(dartPath).writeAsStringSync(code);
+ var port = new ReceivePort();
+ return Isolate.spawn(_isolateBuffer, {
+ 'replyTo': port.sendPort,
+ 'uri': p.toUri(dartPath).toString(),
+ 'packageRoot': packageRoot == null ? null : packageRoot.toString(),
+ 'message': message
+ }).then((isolate) {
+ return port.first.then((response) {
+ if (response['type'] != 'error') return isolate;
+ isolate.kill();
+ var asyncError = RemoteException.deserialize(response['error']);
+ return new Future.error(asyncError.error, asyncError.stackTrace);
+ });
+ });
+ });
+}
+
+// TODO(nweiz): remove this when issue 12617 is fixed.
+/// A function used as a buffer between the host isolate and [spawnUri].
+///
+/// [spawnUri] synchronously loads the file and its imports, which can deadlock
+/// the host isolate if there's an HTTP import pointing at a server in the host.
+/// Adding an additional isolate in the middle works around this.
+void _isolateBuffer(message) {
+ var replyTo = message['replyTo'];
+ var packageRoot = message['packageRoot'];
+ if (packageRoot != null) packageRoot = Uri.parse(packageRoot);
+ Isolate.spawnUri(Uri.parse(message['uri']), [], message['message'],
+ packageRoot: packageRoot)
+ .then((_) => replyTo.send({'type': 'success'}))
+ .catchError((error, stackTrace) {
+ replyTo.send({
+ 'type': 'error',
+ 'error': RemoteException.serialize(error, stackTrace)
+ });
+ });
+}
« no previous file with comments | « no previous file | lib/src/io.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698