| 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 unittest.dart; | 5 library unittest.dart; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
| 10 | 10 |
| 11 import 'package:path/path.dart' as p; | 11 import 'package:path/path.dart' as p; |
| 12 | 12 |
| 13 import 'io.dart'; | 13 import 'io.dart'; |
| 14 import 'isolate_wrapper.dart'; |
| 14 import 'remote_exception.dart'; | 15 import 'remote_exception.dart'; |
| 15 | 16 |
| 16 /// Runs [code] in an isolate. | 17 /// Runs [code] in an isolate. |
| 17 /// | 18 /// |
| 18 /// [code] should be the contents of a Dart entrypoint. It may contain imports; | 19 /// [code] should be the contents of a Dart entrypoint. It may contain imports; |
| 19 /// they will be resolved in the same context as the host isolate. [message] is | 20 /// they will be resolved in the same context as the host isolate. [message] is |
| 20 /// passed to the [main] method of the code being run; the caller is responsible | 21 /// passed to the [main] method of the code being run; the caller is responsible |
| 21 /// for using this to establish communication with the isolate. | 22 /// for using this to establish communication with the isolate. |
| 22 /// | 23 /// |
| 23 /// [packageRoot] controls the package root of the isolate. It may be either a | 24 /// [packageRoot] controls the package root of the isolate. It may be either a |
| 24 /// [String] or a [Uri]. | 25 /// [String] or a [Uri]. |
| 25 Future<Isolate> runInIsolate(String code, message, {packageRoot}) { | 26 Future<Isolate> runInIsolate(String code, message, {packageRoot}) { |
| 26 // TODO(nweiz): load code from a local server rather than from a file. | 27 // TODO(nweiz): load code from a local server rather than from a file. |
| 27 return withTempDir((dir) { | 28 var dir = Directory.systemTemp.createTempSync().path; |
| 28 var dartPath = p.join(dir, 'runInIsolate.dart'); | 29 var dartPath = p.join(dir, 'runInIsolate.dart'); |
| 29 new File(dartPath).writeAsStringSync(code); | 30 new File(dartPath).writeAsStringSync(code); |
| 30 var port = new ReceivePort(); | 31 var port = new ReceivePort(); |
| 31 return Isolate.spawn(_isolateBuffer, { | 32 return Isolate.spawn(_isolateBuffer, { |
| 32 'replyTo': port.sendPort, | 33 'replyTo': port.sendPort, |
| 33 'uri': p.toUri(dartPath).toString(), | 34 'uri': p.toUri(dartPath).toString(), |
| 34 'packageRoot': packageRoot == null ? null : packageRoot.toString(), | 35 'packageRoot': packageRoot == null ? null : packageRoot.toString(), |
| 35 'message': message | 36 'message': message |
| 36 }).then((isolate) { | 37 }).then((isolate) { |
| 37 return port.first.then((response) { | 38 return port.first.then((response) { |
| 38 if (response['type'] != 'error') return isolate; | 39 if (response['type'] != 'error') return isolate; |
| 39 isolate.kill(); | 40 isolate.kill(); |
| 40 var asyncError = RemoteException.deserialize(response['error']); | 41 var asyncError = RemoteException.deserialize(response['error']); |
| 41 return new Future.error(asyncError.error, asyncError.stackTrace); | 42 return new Future.error(asyncError.error, asyncError.stackTrace); |
| 42 }); | |
| 43 }); | 43 }); |
| 44 }).catchError((error) { |
| 45 new Directory(dir).deleteSync(recursive: true); |
| 46 throw error; |
| 47 }).then((isolate) { |
| 48 return new IsolateWrapper(isolate, |
| 49 () => new Directory(dir).deleteSync(recursive: true)); |
| 44 }); | 50 }); |
| 45 } | 51 } |
| 46 | 52 |
| 47 // TODO(nweiz): remove this when issue 12617 is fixed. | 53 // TODO(nweiz): remove this when issue 12617 is fixed. |
| 48 /// A function used as a buffer between the host isolate and [spawnUri]. | 54 /// A function used as a buffer between the host isolate and [spawnUri]. |
| 49 /// | 55 /// |
| 50 /// [spawnUri] synchronously loads the file and its imports, which can deadlock | 56 /// [spawnUri] synchronously loads the file and its imports, which can deadlock |
| 51 /// the host isolate if there's an HTTP import pointing at a server in the host. | 57 /// the host isolate if there's an HTTP import pointing at a server in the host. |
| 52 /// Adding an additional isolate in the middle works around this. | 58 /// Adding an additional isolate in the middle works around this. |
| 53 void _isolateBuffer(message) { | 59 void _isolateBuffer(message) { |
| 54 var replyTo = message['replyTo']; | 60 var replyTo = message['replyTo']; |
| 55 var packageRoot = message['packageRoot']; | 61 var packageRoot = message['packageRoot']; |
| 56 if (packageRoot != null) packageRoot = Uri.parse(packageRoot); | 62 if (packageRoot != null) packageRoot = Uri.parse(packageRoot); |
| 57 Isolate.spawnUri(Uri.parse(message['uri']), [], message['message'], | 63 Isolate.spawnUri(Uri.parse(message['uri']), [], message['message'], |
| 58 packageRoot: packageRoot) | 64 packageRoot: packageRoot) |
| 59 .then((_) => replyTo.send({'type': 'success'})) | 65 .then((_) => replyTo.send({'type': 'success'})) |
| 60 .catchError((error, stackTrace) { | 66 .catchError((error, stackTrace) { |
| 61 replyTo.send({ | 67 replyTo.send({ |
| 62 'type': 'error', | 68 'type': 'error', |
| 63 'error': RemoteException.serialize(error, stackTrace) | 69 'error': RemoteException.serialize(error, stackTrace) |
| 64 }); | 70 }); |
| 65 }); | 71 }); |
| 66 } | 72 } |
| OLD | NEW |