[flutter tools] Improve messages when we fail to connect to the Observatory (#57355)
This commit is contained in:
@@ -89,13 +89,26 @@ Future<io.WebSocket> _defaultOpenChannel(String url, {
|
||||
io.WebSocket socket;
|
||||
|
||||
Future<void> 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<void>.delayed(delay);
|
||||
|
||||
|
||||
@@ -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: <Type, Generator>{
|
||||
WebSocketConnector: () => failingWebSocketConnector,
|
||||
});
|
||||
|
||||
testWithoutContext('setAssetDirectory forwards arguments correctly', () async {
|
||||
final Completer<String> completer = Completer<String>();
|
||||
final vm_service.VmService vmService = vm_service.VmService(
|
||||
@@ -323,3 +357,11 @@ class MockFlutterVersion extends Mock implements FlutterVersion {
|
||||
@override
|
||||
Map<String, Object> toJson() => const <String, Object>{'Mock': 'Version'};
|
||||
}
|
||||
|
||||
/// A [WebSocketConnector] that always throws an [io.SocketException].
|
||||
Future<io.WebSocket> failingWebSocketConnector(
|
||||
String url, {
|
||||
io.CompressionOptions compression,
|
||||
}) {
|
||||
throw const io.SocketException('Failed WebSocket connection');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user