From c6f7792311d5c9353cbd95c22250e5f4b2682ae7 Mon Sep 17 00:00:00 2001 From: "James D. Lin" Date: Thu, 21 May 2020 17:47:02 -0700 Subject: [PATCH] [flutter tools] Improve messages when we fail to connect to the Observatory (#57355) --- packages/flutter_tools/lib/src/vmservice.dart | 21 ++++++++-- .../test/general.shard/vmservice_test.dart | 42 +++++++++++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/packages/flutter_tools/lib/src/vmservice.dart b/packages/flutter_tools/lib/src/vmservice.dart index 14bc858cff..624a185e74 100644 --- a/packages/flutter_tools/lib/src/vmservice.dart +++ b/packages/flutter_tools/lib/src/vmservice.dart @@ -89,13 +89,26 @@ Future _defaultOpenChannel(String url, { io.WebSocket socket; Future handleError(dynamic e) async { - globals.printTrace('Exception attempting to connect to Observatory: $e'); - globals.printTrace('This was attempt #$attempts. Will retry in $delay.'); - + void Function(String) printVisibleTrace = globals.printTrace; if (attempts == 10) { - globals.printStatus('This is taking longer than expected...'); + globals.printStatus('Connecting to the VM Service is taking longer than expected...'); + } else if (attempts == 20) { + globals.printStatus('Still attempting to connect to the VM Service...'); + globals.printStatus( + 'If you do NOT see the Flutter application running, it might have ' + 'crashed. The device logs (e.g. from adb or XCode) might have more ' + 'details.'); + globals.printStatus( + 'If you do see the Flutter application running on the device, try ' + 're-running with --host-vmservice-port to use a specific port known to ' + 'be available.'); + } else if (attempts % 50 == 0) { + printVisibleTrace = globals.printStatus; } + printVisibleTrace('Exception attempting to connect to the VM Service: $e'); + printVisibleTrace('This was attempt #$attempts. Will retry in $delay.'); + // Delay next attempt. await Future.delayed(delay); diff --git a/packages/flutter_tools/test/general.shard/vmservice_test.dart b/packages/flutter_tools/test/general.shard/vmservice_test.dart index 3ae406206f..e28c3d0b68 100644 --- a/packages/flutter_tools/test/general.shard/vmservice_test.dart +++ b/packages/flutter_tools/test/general.shard/vmservice_test.dart @@ -5,6 +5,7 @@ import 'dart:async'; import 'package:flutter_tools/src/base/common.dart'; +import 'package:flutter_tools/src/base/io.dart' as io; import 'package:flutter_tools/src/convert.dart'; import 'package:vm_service/vm_service.dart' as vm_service; import 'package:mockito/mockito.dart'; @@ -12,6 +13,7 @@ import 'package:flutter_tools/src/base/logger.dart'; import 'package:flutter_tools/src/device.dart'; import 'package:flutter_tools/src/version.dart'; import 'package:flutter_tools/src/vmservice.dart'; +import 'package:quiver/testing/async.dart'; import '../src/common.dart'; import '../src/context.dart'; @@ -161,6 +163,38 @@ void main() { FlutterVersion: () => MockFlutterVersion(), }); + testUsingContext('VMService prints messages for connection failures', () { + FakeAsync().run((FakeAsync time) { + final Uri uri = Uri.parse('ws://127.0.0.1:12345/QqL7EFEDNG0=/ws'); + unawaited(connectToVmService(uri)); + + time.elapse(const Duration(seconds: 5)); + expect(testLogger.statusText, isEmpty); + + time.elapse(const Duration(minutes: 2)); + + final String statusText = testLogger.statusText; + expect( + statusText, + containsIgnoringWhitespace('Connecting to the VM Service is taking longer than expected...'), + ); + expect( + statusText, + containsIgnoringWhitespace('try re-running with --host-vmservice-port'), + ); + expect( + statusText, + containsIgnoringWhitespace('Exception attempting to connect to the VM Service:'), + ); + expect( + statusText, + containsIgnoringWhitespace('This was attempt #50. Will retry'), + ); + }); + }, overrides: { + WebSocketConnector: () => failingWebSocketConnector, + }); + testWithoutContext('setAssetDirectory forwards arguments correctly', () async { final Completer completer = Completer(); final vm_service.VmService vmService = vm_service.VmService( @@ -323,3 +357,11 @@ class MockFlutterVersion extends Mock implements FlutterVersion { @override Map toJson() => const {'Mock': 'Version'}; } + +/// A [WebSocketConnector] that always throws an [io.SocketException]. +Future failingWebSocketConnector( + String url, { + io.CompressionOptions compression, +}) { + throw const io.SocketException('Failed WebSocket connection'); +}