|
| 1 | +// Copyright (c) 2016, 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 | +import 'dart:async'; |
| 6 | +import 'dart:convert'; |
| 7 | + |
| 8 | +import 'package:async/async.dart'; |
| 9 | +import 'package:path/path.dart' as p; |
| 10 | +import 'package:stream_channel/stream_channel.dart'; |
| 11 | + |
| 12 | +import '../backend/invoker.dart'; |
| 13 | +import '../util/remote_exception.dart'; |
| 14 | +import '../utils.dart'; |
| 15 | + |
| 16 | +/// A transformer that handles messages from the spawned isolate and ensures |
| 17 | +/// that messages sent to it are JSON-encodable. |
| 18 | +final _transformer = new StreamChannelTransformer( |
| 19 | + new StreamTransformer.fromHandlers(handleData: (message, sink) { |
| 20 | + switch (message['type']) { |
| 21 | + case 'data': |
| 22 | + sink.add(message['data']); |
| 23 | + break; |
| 24 | + |
| 25 | + case 'print': |
| 26 | + print(message['line']); |
| 27 | + break; |
| 28 | + |
| 29 | + case 'error': |
| 30 | + var error = RemoteException.deserialize(message['error']); |
| 31 | + sink.addError(error.error, error.stackTrace); |
| 32 | + break; |
| 33 | + } |
| 34 | + }), new StreamSinkTransformer.fromHandlers(handleData: (message, sink) { |
| 35 | + ensureJsonEncodable(message); |
| 36 | + sink.add(message); |
| 37 | + })); |
| 38 | + |
| 39 | +/// Spawns a VM isolate for the given [uri], which may be a [Uri] or a [String]. |
| 40 | +/// |
| 41 | +/// This allows browser tests to spawn servers with which they can communicate |
| 42 | +/// to test client/server interactions. It can also be used by VM tests to |
| 43 | +/// easily spawn an isolate. |
| 44 | +/// |
| 45 | +/// The Dart file at [uri] must define a top-level `hybridMain()` function that |
| 46 | +/// takes a `StreamChannel` argument and, optionally, an `Object` argument to |
| 47 | +/// which [message] will be passed. Note that [message] must be JSON-encodable. |
| 48 | +/// For example: |
| 49 | +/// |
| 50 | +/// ```dart |
| 51 | +/// import "package:stream_channel/stream_channel.dart"; |
| 52 | +/// |
| 53 | +/// hybridMain(StreamChannel channel, Object message) { |
| 54 | +/// // ... |
| 55 | +/// } |
| 56 | +/// ``` |
| 57 | +/// |
| 58 | +/// If [uri] is relative, it will be interpreted relative to the `file:` URL for |
| 59 | +/// the test suite being executed. If it's a `package:` URL, it will be resolved |
| 60 | +/// using the current package's dependency constellation. |
| 61 | +/// |
| 62 | +/// Returns a [StreamChannel] that's connected to the channel passed to |
| 63 | +/// `hybridMain()`. Only JSON-encodable objects may be sent through this |
| 64 | +/// channel. If the channel is closed, the hybrid isolate is killed. If the |
| 65 | +/// isolate is killed, the channel's stream will emit a "done" event. |
| 66 | +/// |
| 67 | +/// Any unhandled errors loading or running the hybrid isolate will be emitted |
| 68 | +/// as errors over the channel's stream. Any calls to `print()` in the hybrid |
| 69 | +/// isolate will be printed as though they came from the test that created the |
| 70 | +/// isolate. |
| 71 | +/// |
| 72 | +/// Code in the hybrid isolate is not considered to be running in a test |
| 73 | +/// context, so it can't access test functions like `expect()` and |
| 74 | +/// `expectAsync()`. |
| 75 | +/// |
| 76 | +/// **Note**: If you use this API, be sure to add a dependency on the |
| 77 | +/// **`stream_channel` package, since you're using its API as well! |
| 78 | +StreamChannel spawnHybridUri(uri, {Object message}) { |
| 79 | + Uri parsedUrl; |
| 80 | + if (uri is Uri) { |
| 81 | + parsedUrl = uri; |
| 82 | + } else if (uri is String) { |
| 83 | + parsedUrl = Uri.parse(uri); |
| 84 | + } else { |
| 85 | + throw new ArgumentError.value(uri, "uri", "must be a Uri or a String."); |
| 86 | + } |
| 87 | + |
| 88 | + String uriString; |
| 89 | + if (parsedUrl.scheme.isEmpty) { |
| 90 | + var suitePath = Invoker.current.liveTest.suite.path; |
| 91 | + uriString = p.url.join( |
| 92 | + p.url.dirname(p.toUri(p.absolute(suitePath)).toString()), |
| 93 | + parsedUrl.toString()); |
| 94 | + } else { |
| 95 | + uriString = uri.toString(); |
| 96 | + } |
| 97 | + |
| 98 | + return _spawn(uriString, message); |
| 99 | +} |
| 100 | + |
| 101 | +/// Spawns a VM isolate that runs the given [dartCode], which is loaded as the |
| 102 | +/// contents of a Dart library. |
| 103 | +/// |
| 104 | +/// This allows browser tests to spawn servers with which they can communicate |
| 105 | +/// to test client/server interactions. It can also be used by VM tests to |
| 106 | +/// easily spawn an isolate. |
| 107 | +/// |
| 108 | +/// The [dartCode] must define a top-level `hybridMain()` function that takes a |
| 109 | +/// `StreamChannel` argument and, optionally, an `Object` argument to which |
| 110 | +/// [message] will be passed. Note that [message] must be JSON-encodable. For |
| 111 | +/// example: |
| 112 | +/// |
| 113 | +/// ```dart |
| 114 | +/// import "package:stream_channel/stream_channel.dart"; |
| 115 | +/// |
| 116 | +/// hybridMain(StreamChannel channel, Object message) { |
| 117 | +/// // ... |
| 118 | +/// } |
| 119 | +/// ``` |
| 120 | +/// |
| 121 | +/// Returns a [StreamChannel] that's connected to the channel passed to |
| 122 | +/// `hybridMain()`. Only JSON-encodable objects may be sent through this |
| 123 | +/// channel. If the channel is closed, the hybrid isolate is killed. If the |
| 124 | +/// isolate is killed, the channel's stream will emit a "done" event. |
| 125 | +/// |
| 126 | +/// Any unhandled errors loading or running the hybrid isolate will be emitted |
| 127 | +/// as errors over the channel's stream. Any calls to `print()` in the hybrid |
| 128 | +/// isolate will be printed as though they came from the test that created the |
| 129 | +/// isolate. |
| 130 | +/// |
| 131 | +/// Code in the hybrid isolate is not considered to be running in a test |
| 132 | +/// context, so it can't access test functions like `expect()` and |
| 133 | +/// `expectAsync()`. |
| 134 | +/// |
| 135 | +/// **Note**: If you use this API, be sure to add a dependency on the |
| 136 | +/// **`stream_channel` package, since you're using its API as well! |
| 137 | +StreamChannel spawnHybridCode(String dartCode, {Object message}) { |
| 138 | + var uri = new Uri.dataFromString(dartCode, |
| 139 | + encoding: UTF8, mimeType: 'application/dart'); |
| 140 | + return _spawn(uri.toString(), message); |
| 141 | +} |
| 142 | + |
| 143 | +/// Like [spawnHybridUri], but doesn't take [Uri] objects and doesn't handle |
| 144 | +/// relative URLs. |
| 145 | +StreamChannel _spawn(String uri, Object message) { |
| 146 | + var channel = Zone.current[#test.runner.test_channel] as MultiChannel; |
| 147 | + if (channel == null) { |
| 148 | + // TODO(nweiz): Link to an issue tracking support when running the test file |
| 149 | + // directly. |
| 150 | + throw new UnsupportedError( |
| 151 | + "Can't connect to the test runner.\n" |
| 152 | + 'spawnHybridUri() is currently only supported within "pub run test".'); |
| 153 | + } |
| 154 | + |
| 155 | + ensureJsonEncodable(message); |
| 156 | + |
| 157 | + var isolateChannel = channel.virtualChannel(); |
| 158 | + channel.sink.add({ |
| 159 | + "type": "spawn-hybrid-uri", |
| 160 | + "url": uri, |
| 161 | + "message": message, |
| 162 | + "channel": isolateChannel.id |
| 163 | + }); |
| 164 | + |
| 165 | + return isolateChannel.transform(_transformer); |
| 166 | +} |
0 commit comments