From 598f2ab0fbbe67240ea5eeb97f57837663d84400 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Wed, 27 Nov 2019 13:11:04 -0800 Subject: [PATCH] Cleanups to resident runner and flutter device classes (#45673) --- packages/flutter_tools/lib/src/device.dart | 4 +- .../flutter_tools/lib/src/ios/devices.dart | 33 +++++----- .../lib/src/resident_runner.dart | 60 +++++++------------ packages/flutter_tools/lib/src/run_cold.dart | 19 +++--- packages/flutter_tools/lib/src/run_hot.dart | 48 ++++++--------- .../general.shard/resident_runner_test.dart | 4 +- 6 files changed, 69 insertions(+), 99 deletions(-) diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index 3a539b8321..35d83d80d4 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -645,7 +645,7 @@ abstract class DeviceLogReader { /// Some logs can be obtained from a VM service stream. /// Set this after the VM services are connected. - List connectedVMServices; + VMService connectedVMService; @override String toString() => name; @@ -675,7 +675,7 @@ class NoOpDeviceLogReader implements DeviceLogReader { int appPid; @override - List connectedVMServices; + VMService connectedVMService; @override Stream get logLines => const Stream.empty(); diff --git a/packages/flutter_tools/lib/src/ios/devices.dart b/packages/flutter_tools/lib/src/ios/devices.dart index b11a627b7d..52190d448e 100644 --- a/packages/flutter_tools/lib/src/ios/devices.dart +++ b/packages/flutter_tools/lib/src/ios/devices.dart @@ -578,33 +578,30 @@ class IOSDeviceLogReader extends DeviceLogReader { String get name => device.name; @override - List get connectedVMServices => _connectedVMServices; - List _connectedVMServices; + VMService get connectedVMService => _connectedVMService; + VMService _connectedVMService; @override - set connectedVMServices(List connectedVMServices) { - _listenToUnifiedLoggingEvents(connectedVMServices); - _connectedVMServices = connectedVMServices; + set connectedVMService(VMService connectedVmService) { + _listenToUnifiedLoggingEvents(connectedVmService); + _connectedVMService = connectedVmService; } static const int _minimumUniversalLoggingSdkVersion = 13; - Future _listenToUnifiedLoggingEvents(List vmServices) async { + Future _listenToUnifiedLoggingEvents(VMService connectedVmService) async { if (device.majorSdkVersion < _minimumUniversalLoggingSdkVersion) { return; } - for (VMService vmService in vmServices) { - // The VM service will not publish logging events unless the debug stream is being listened to. - // onDebugEvent listens to this stream as a side effect. - unawaited(vmService.onDebugEvent); - _loggingSubscriptions.add((await vmService.onStdoutEvent).listen((ServiceEvent event) { - final String logMessage = event.message; - if (logMessage.isNotEmpty) { - _linesController.add(logMessage); - } - })); - } - _connectedVMServices = connectedVMServices; + // The VM service will not publish logging events unless the debug stream is being listened to. + // onDebugEvent listens to this stream as a side effect. + unawaited(connectedVmService.onDebugEvent); + _loggingSubscriptions.add((await connectedVmService.onStdoutEvent).listen((ServiceEvent event) { + final String logMessage = event.message; + if (logMessage.isNotEmpty) { + _linesController.add(logMessage); + } + })); } void _listenToSysLog () { diff --git a/packages/flutter_tools/lib/src/resident_runner.dart b/packages/flutter_tools/lib/src/resident_runner.dart index 045717227d..726e599f7f 100644 --- a/packages/flutter_tools/lib/src/resident_runner.dart +++ b/packages/flutter_tools/lib/src/resident_runner.dart @@ -134,7 +134,7 @@ class FlutterDevice { final Device device; final ResidentCompiler generator; Stream observatoryUris; - List vmServices; + VMService vmService; DevFS devFS; ApplicationPackage package; List fileSystemRoots; @@ -191,8 +191,8 @@ class FlutterDevice { } printTrace('Successfully connected to service protocol: $observatoryUri'); - vmServices = [service]; - device.getLogReader(app: package).connectedVMServices = vmServices; + vmService = service; + device.getLogReader(app: package).connectedVMService = vmService; completer.complete(); await subscription.cancel(); }, onError: (dynamic error) { @@ -208,37 +208,23 @@ class FlutterDevice { } Future refreshViews() async { - if (vmServices == null || vmServices.isEmpty) { - return Future.value(null); + if (vmService == null) { + return; } - final List> futures = >[ - for (VMService service in vmServices) service.vm.refreshViews(waitForViews: true), - ]; - await Future.wait(futures); + await vmService.vm.refreshViews(waitForViews: true); } List get views { - if (vmServices == null) { + if (vmService == null || vmService.isClosed) { return []; } - return vmServices - .where((VMService service) => !service.isClosed) - .expand( - (VMService service) { - return viewFilter != null - ? service.vm.allViewsWithName(viewFilter) - : service.vm.views; - }, - ) - .toList(); + return (viewFilter != null + ? vmService.vm.allViewsWithName(viewFilter) + : vmService.vm.views).toList(); } - Future getVMs() async { - for (VMService service in vmServices) { - await service.getVM(); - } - } + Future getVMs() => vmService.getVM(); Future exitApps() async { if (!device.supportsFlutterExit) { @@ -249,7 +235,6 @@ class FlutterDevice { if (flutterViews == null || flutterViews.isEmpty) { return; } - final List> futures = >[]; // If any of the flutter views are paused, we might not be able to // cleanly exit since the service extension may not have been registered. if (flutterViews.any((FlutterView view) { @@ -262,6 +247,7 @@ class FlutterDevice { await device.stopApp(package); return; } + final List> futures = >[]; for (FlutterView view in flutterViews) { if (view != null && view.uiIsolate != null) { assert(!view.uiIsolate.pauseEvent.isPauseEvent); @@ -281,7 +267,7 @@ class FlutterDevice { }) { // One devFS per device. Shared by all running instances. devFS = DevFS( - vmServices[0], + vmService, fsName, rootDirectory, packagesFilePath: packagesFilePath, @@ -416,7 +402,7 @@ class FlutterDevice { } void initLogReader() { - device.getLogReader(app: package).appPid = vmServices.first.vm.pid; + device.getLogReader(app: package).appPid = vmService.vm.pid; } Future runHot({ @@ -736,7 +722,7 @@ abstract class ResidentRunner { void writeVmserviceFile() { if (debuggingOptions.vmserviceOutFile != null) { try { - final String address = flutterDevices.first.vmServices.first.wsAddress.toString(); + final String address = flutterDevices.first.vmService.wsAddress.toString(); final File vmserviceOutFile = fs.file(debuggingOptions.vmserviceOutFile); vmserviceOutFile.createSync(recursive: true); vmserviceOutFile.writeAsStringSync(address); @@ -940,15 +926,13 @@ abstract class ResidentRunner { // Listen for service protocol connection to close. for (FlutterDevice device in flutterDevices) { - for (VMService service in device.vmServices) { - // This hooks up callbacks for when the connection stops in the future. - // We don't want to wait for them. We don't handle errors in those callbacks' - // futures either because they just print to logger and is not critical. - unawaited(service.done.then( - _serviceProtocolDone, - onError: _serviceProtocolError, - ).whenComplete(_serviceDisconnected)); - } + // This hooks up callbacks for when the connection stops in the future. + // We don't want to wait for them. We don't handle errors in those callbacks' + // futures either because they just print to logger and is not critical. + unawaited(device.vmService.done.then( + _serviceProtocolDone, + onError: _serviceProtocolError, + ).whenComplete(_serviceDisconnected)); } } diff --git a/packages/flutter_tools/lib/src/run_cold.dart b/packages/flutter_tools/lib/src/run_cold.dart index 98499b857b..0987c5eccb 100644 --- a/packages/flutter_tools/lib/src/run_cold.dart +++ b/packages/flutter_tools/lib/src/run_cold.dart @@ -83,15 +83,15 @@ class ColdRunner extends ResidentRunner { if (flutterDevices.first.observatoryUris != null) { // For now, only support one debugger connection. connectionInfoCompleter?.complete(DebugConnectionInfo( - httpUri: flutterDevices.first.vmServices.first.httpAddress, - wsUri: flutterDevices.first.vmServices.first.wsAddress, + httpUri: flutterDevices.first.vmService.httpAddress, + wsUri: flutterDevices.first.vmService.wsAddress, )); } printTrace('Application running.'); for (FlutterDevice device in flutterDevices) { - if (device.vmServices == null) { + if (device.vmService == null) { continue; } device.initLogReader(); @@ -102,10 +102,10 @@ class ColdRunner extends ResidentRunner { if (traceStartup) { // Only trace startup for the first device. final FlutterDevice device = flutterDevices.first; - if (device.vmServices != null && device.vmServices.isNotEmpty) { + if (device.vmService != null) { printStatus('Tracing startup on ${device.device.name}.'); await downloadStartupTrace( - device.vmServices.first, + device.vmService, awaitFirstFrame: awaitFirstFrameWhenTracing, ); } @@ -183,10 +183,9 @@ class ColdRunner extends ResidentRunner { bool haveAnything = false; for (FlutterDevice device in flutterDevices) { final String dname = device.device.name; - if (device.vmServices != null) { - for (VMService vm in device.vmServices) { - printStatus('An Observatory debugger and profiler on $dname is available at: ${vm.httpAddress}'); - } + if (device.vmService != null) { + printStatus('An Observatory debugger and profiler on $dname is ' + 'available at: ${device.vmService .httpAddress}'); } } if (supportsServiceProtocol) { @@ -212,7 +211,7 @@ class ColdRunner extends ResidentRunner { Future preExit() async { for (FlutterDevice device in flutterDevices) { // If we're running in release mode, stop the app using the device logic. - if (device.vmServices == null || device.vmServices.isEmpty) { + if (device.vmService == null) { await device.device.stopApp(device.package); } } diff --git a/packages/flutter_tools/lib/src/run_hot.dart b/packages/flutter_tools/lib/src/run_hot.dart index 649e3265e3..71e996008d 100644 --- a/packages/flutter_tools/lib/src/run_hot.dart +++ b/packages/flutter_tools/lib/src/run_hot.dart @@ -10,7 +10,6 @@ import 'package:meta/meta.dart'; import 'package:pool/pool.dart'; import 'base/async_guard.dart'; -import 'base/common.dart'; import 'base/context.dart'; import 'base/file_system.dart'; import 'base/logger.dart'; @@ -180,8 +179,8 @@ class HotRunner extends ResidentRunner { // Only handle one debugger connection. connectionInfoCompleter.complete( DebugConnectionInfo( - httpUri: flutterDevices.first.vmServices.first.httpAddress, - wsUri: flutterDevices.first.vmServices.first.wsAddress, + httpUri: flutterDevices.first.vmService.httpAddress, + wsUri: flutterDevices.first.vmService.wsAddress, baseUri: baseUris.first.toString(), ), ); @@ -428,23 +427,18 @@ class HotRunner extends ResidentRunner { continue; } // Reload the isolate. - final Completer completer = Completer(); - futures.add(completer.future); - unawaited(view.uiIsolate.reload().then( - (ServiceObject _) { - final ServiceEvent pauseEvent = view.uiIsolate.pauseEvent; - if ((pauseEvent != null) && pauseEvent.isPauseEvent) { - // Resume the isolate so that it can be killed by the embedder. - return view.uiIsolate.resume(); - } - return null; - }, - ).whenComplete( - () { completer.complete(null); }, - )); + futures.add(view.uiIsolate.reload().then((ServiceObject _) { + final ServiceEvent pauseEvent = view.uiIsolate.pauseEvent; + if ((pauseEvent != null) && pauseEvent.isPauseEvent) { + // Resume the isolate so that it can be killed by the embedder. + return view.uiIsolate.resume(); + } + return null; + })); } } await Future.wait(futures); + // We are now running from source. _runningFromSnapshot = false; await _launchFromDevFS(mainPath + '.dill'); @@ -718,12 +712,10 @@ class HotRunner extends ResidentRunner { // running from snapshot to running from uploaded files. await device.resetAssetDirectory(); } - final Completer completer = Completer(); - allReportsFutures.add(completer.future); final List>> reportFutures = device.reloadSources( entryPath, pause: pause, ); - unawaited(Future.wait(reportFutures).then( + allReportsFutures.add(Future.wait(reportFutures).then( (List> reports) async { // TODO(aam): Investigate why we are validating only first reload report, // which seems to be current behavior @@ -733,7 +725,7 @@ class HotRunner extends ResidentRunner { await device.updateReloadStatus( validateReloadReport(firstReport, printErrors: false), ); - completer.complete(DeviceReloadReport(device, reports)); + return DeviceReloadReport(device, reports); }, )); } @@ -795,13 +787,12 @@ class HotRunner extends ResidentRunner { printTrace('Sending reload event to "${view.uiIsolate.name}"'); futuresViews.add(view.uiIsolate.reload()); } - final Completer deviceCompleter = Completer(); - unawaited(Future.wait(futuresViews).whenComplete(() { - deviceCompleter.complete(device.refreshViews()); + allDevices.add(Future.wait(futuresViews).whenComplete(() { + return device.refreshViews(); })); - allDevices.add(deviceCompleter.future); } await Future.wait(allDevices); + // We are now running from source. _runningFromSnapshot = false; // Check if any isolates are paused. @@ -851,7 +842,7 @@ class HotRunner extends ResidentRunner { } }(), ]; - final Future reassembleFuture = Future.wait(futures).then((List values) { }); + final Future reassembleFuture = Future.wait(futures); await reassembleFuture.timeout( const Duration(seconds: 2), onTimeout: () async { @@ -987,9 +978,8 @@ class HotRunner extends ResidentRunner { printStatus(message); for (FlutterDevice device in flutterDevices) { final String dname = device.device.name; - for (VMService vm in device.vmServices) { - printStatus('An Observatory debugger and profiler on $dname is available at: ${vm.httpAddress}'); - } + printStatus('An Observatory debugger and profiler on $dname is ' + 'available at: ${device.vmService.httpAddress}'); } final String quitMessage = _didAttach ? 'To detach, press "d"; to quit, press "q".' diff --git a/packages/flutter_tools/test/general.shard/resident_runner_test.dart b/packages/flutter_tools/test/general.shard/resident_runner_test.dart index 4af49d6d62..20472d1fec 100644 --- a/packages/flutter_tools/test/general.shard/resident_runner_test.dart +++ b/packages/flutter_tools/test/general.shard/resident_runner_test.dart @@ -104,7 +104,7 @@ void main() { .thenAnswer((Invocation invocation) async { return testUri; }); - when(mockFlutterDevice.vmServices).thenReturn([mockVMService]); + when(mockFlutterDevice.vmService).thenReturn(mockVMService); when(mockFlutterDevice.refreshViews()).thenAnswer((Invocation invocation) async { }); when(mockFlutterDevice.reloadSources(any, pause: anyNamed('pause'))).thenReturn(>>[ Future>.value({ @@ -631,7 +631,7 @@ void main() { ); await flutterDevice.connect(); - verify(mockLogReader.connectedVMServices = [ mockVMService ]); + verify(mockLogReader.connectedVMService = mockVMService); }, overrides: { VMServiceConnector: () => (Uri httpUri, { ReloadSources reloadSources,