Unverified Commit d4830fcf authored by Yegor's avatar Yegor Committed by GitHub

smoke test VM service connection before returning VMService object (#12914)

parent e1cbbc1c
...@@ -155,11 +155,11 @@ class FuchsiaReloadCommand extends FlutterCommand { ...@@ -155,11 +155,11 @@ class FuchsiaReloadCommand extends FlutterCommand {
// A cache of VMService connections. // A cache of VMService connections.
final HashMap<int, VMService> _vmServiceCache = new HashMap<int, VMService>(); final HashMap<int, VMService> _vmServiceCache = new HashMap<int, VMService>();
VMService _getVMService(int port) { Future<VMService> _getVMService(int port) async {
if (!_vmServiceCache.containsKey(port)) { if (!_vmServiceCache.containsKey(port)) {
final String addr = 'http://$ipv4Loopback:$port'; final String addr = 'http://$ipv4Loopback:$port';
final Uri uri = Uri.parse(addr); final Uri uri = Uri.parse(addr);
final VMService vmService = VMService.connect(uri); final VMService vmService = await VMService.connect(uri);
_vmServiceCache[port] = vmService; _vmServiceCache[port] = vmService;
} }
return _vmServiceCache[port]; return _vmServiceCache[port];
...@@ -183,7 +183,7 @@ class FuchsiaReloadCommand extends FlutterCommand { ...@@ -183,7 +183,7 @@ class FuchsiaReloadCommand extends FlutterCommand {
for (int port in ports) { for (int port in ports) {
if (!await _checkPort(port)) if (!await _checkPort(port))
continue; continue;
final VMService vmService = _getVMService(port); final VMService vmService = await _getVMService(port);
await vmService.getVM(); await vmService.getVM();
await vmService.waitForViews(); await vmService.waitForViews();
views.addAll(vmService.vm.views); views.addAll(vmService.vm.views);
...@@ -283,7 +283,7 @@ class FuchsiaReloadCommand extends FlutterCommand { ...@@ -283,7 +283,7 @@ class FuchsiaReloadCommand extends FlutterCommand {
Future<Null> _listVMs(List<int> ports) async { Future<Null> _listVMs(List<int> ports) async {
for (int port in ports) { for (int port in ports) {
final VMService vmService = _getVMService(port); final VMService vmService = await _getVMService(port);
await vmService.getVM(); await vmService.getVM();
await vmService.waitForViews(); await vmService.waitForViews();
printStatus(_vmServiceToString(vmService)); printStatus(_vmServiceToString(vmService));
......
...@@ -80,7 +80,7 @@ class ScreenshotCommand extends FlutterCommand { ...@@ -80,7 +80,7 @@ class ScreenshotCommand extends FlutterCommand {
Future<Null> runSkia(File outputFile) async { Future<Null> runSkia(File outputFile) async {
final Uri observatoryUri = new Uri(scheme: 'http', host: '127.0.0.1', final Uri observatoryUri = new Uri(scheme: 'http', host: '127.0.0.1',
port: int.parse(argResults[_kSkia])); port: int.parse(argResults[_kSkia]));
final VMService vmService = VMService.connect(observatoryUri); final VMService vmService = await VMService.connect(observatoryUri);
final Map<String, dynamic> skp = await vmService.vm.invokeRpcRaw('_flutter.screenshotSkp'); final Map<String, dynamic> skp = await vmService.vm.invokeRpcRaw('_flutter.screenshotSkp');
outputFile ??= getUniqueFile(fs.currentDirectory, 'flutter', 'skp'); outputFile ??= getUniqueFile(fs.currentDirectory, 'flutter', 'skp');
......
...@@ -55,7 +55,7 @@ class TraceCommand extends FlutterCommand { ...@@ -55,7 +55,7 @@ class TraceCommand extends FlutterCommand {
Tracing tracing; Tracing tracing;
try { try {
tracing = Tracing.connect(observatoryUri); tracing = await Tracing.connect(observatoryUri);
} catch (error) { } catch (error) {
throwToolExit('Error connecting to observatory: $error'); throwToolExit('Error connecting to observatory: $error');
} }
...@@ -97,8 +97,8 @@ class TraceCommand extends FlutterCommand { ...@@ -97,8 +97,8 @@ class TraceCommand extends FlutterCommand {
class Tracing { class Tracing {
Tracing(this.vmService); Tracing(this.vmService);
static Tracing connect(Uri uri) { static Future<Tracing> connect(Uri uri) async {
final VMService observatory = VMService.connect(uri); final VMService observatory = await VMService.connect(uri);
return new Tracing(observatory); return new Tracing(observatory);
} }
......
...@@ -53,12 +53,12 @@ class FlutterDevice { ...@@ -53,12 +53,12 @@ class FlutterDevice {
/// code of the running application (a.k.a. HotReload). /// code of the running application (a.k.a. HotReload).
/// This ensures that the reload process follows the normal orchestration of /// This ensures that the reload process follows the normal orchestration of
/// the Flutter Tools and not just the VM internal service. /// the Flutter Tools and not just the VM internal service.
void connect({ReloadSources reloadSources}) { Future<Null> _connect({ReloadSources reloadSources}) async {
if (vmServices != null) if (vmServices != null)
return; return;
vmServices = new List<VMService>(observatoryUris.length); vmServices = new List<VMService>(observatoryUris.length);
for (int i = 0; i < observatoryUris.length; i++) { for (int i = 0; i < observatoryUris.length; i++) {
vmServices[i] = VMService.connect(observatoryUris[i], vmServices[i] = await VMService.connect(observatoryUris[i],
reloadSources: reloadSources); reloadSources: reloadSources);
printTrace('Connected to service protocol: ${observatoryUris[i]}'); printTrace('Connected to service protocol: ${observatoryUris[i]}');
} }
...@@ -599,7 +599,7 @@ abstract class ResidentRunner { ...@@ -599,7 +599,7 @@ abstract class ResidentRunner {
bool viewFound = false; bool viewFound = false;
for (FlutterDevice device in flutterDevices) { for (FlutterDevice device in flutterDevices) {
device.viewFilter = viewFilter; device.viewFilter = viewFilter;
device.connect(reloadSources: reloadSources); await device._connect(reloadSources: reloadSources);
await device.getVMs(); await device.getVMs();
await device.waitForViews(); await device.waitForViews();
if (device.views == null) if (device.views == null)
......
...@@ -140,15 +140,19 @@ class VMService { ...@@ -140,15 +140,19 @@ class VMService {
/// protocol itself. /// protocol itself.
/// ///
/// See: https://github.com/dart-lang/sdk/commit/df8bf384eb815cf38450cb50a0f4b62230fba217 /// See: https://github.com/dart-lang/sdk/commit/df8bf384eb815cf38450cb50a0f4b62230fba217
static VMService connect( static Future<VMService> connect(
Uri httpUri, { Uri httpUri, {
Duration requestTimeout: kDefaultRequestTimeout, Duration requestTimeout: kDefaultRequestTimeout,
ReloadSources reloadSources, ReloadSources reloadSources,
}) { }) async {
final Uri wsUri = httpUri.replace(scheme: 'ws', path: fs.path.join(httpUri.path, 'ws')); final Uri wsUri = httpUri.replace(scheme: 'ws', path: fs.path.join(httpUri.path, 'ws'));
final StreamChannel<String> channel = _openChannel(wsUri); final StreamChannel<String> channel = _openChannel(wsUri);
final rpc.Peer peer = new rpc.Peer.withoutJson(jsonDocument.bind(channel)); final rpc.Peer peer = new rpc.Peer.withoutJson(jsonDocument.bind(channel));
return new VMService._(peer, httpUri, wsUri, requestTimeout, reloadSources); final VMService service = new VMService._(peer, httpUri, wsUri, requestTimeout, reloadSources);
// This call is to ensure we are able to establish a connection instead of
// keeping on trucking and failing farther down the process.
await service._sendRequest('getVersion', const <String, dynamic>{});
return service;
} }
final Uri httpAddress; final Uri httpAddress;
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:test/test.dart';
import 'package:flutter_tools/src/base/port_scanner.dart';
import 'package:flutter_tools/src/vmservice.dart';
void main() {
group('VMService', () {
test('fails connection eagerly in the connect() method', () async {
final int port = await const HostPortScanner().findAvailablePort();
expect(
VMService.connect(Uri.parse('http://localhost:$port')),
throwsA(const isInstanceOf<WebSocketChannelException>()),
);
});
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment