From fb5e49082b181a006ed40764b70dd98d2cee7e56 Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Tue, 19 Nov 2019 23:14:25 -0800 Subject: [PATCH] Revert "Attach looks at future observatory URIs (#45228)" (#45237) This reverts commit 2135e9fb1e7829fb6d627e4cd1ddbeafd4f272b3. --- .../lib/src/android/android_device.dart | 33 ++- .../lib/src/commands/attach.dart | 177 ++++++------ packages/flutter_tools/lib/src/devfs.dart | 8 +- .../lib/src/protocol_discovery.dart | 112 ++------ .../lib/src/resident_runner.dart | 126 ++------- packages/flutter_tools/lib/src/run_cold.dart | 9 +- packages/flutter_tools/lib/src/run_hot.dart | 6 +- .../commands.shard/hermetic/attach_test.dart | 251 ++---------------- .../test/general.shard/devfs_test.dart | 4 - .../protocol_discovery_test.dart | 164 +++--------- .../general.shard/resident_runner_test.dart | 12 +- packages/flutter_tools/test/src/mocks.dart | 31 +-- 12 files changed, 245 insertions(+), 688 deletions(-) diff --git a/packages/flutter_tools/lib/src/android/android_device.dart b/packages/flutter_tools/lib/src/android/android_device.dart index 38959ff092..a47581d360 100644 --- a/packages/flutter_tools/lib/src/android/android_device.dart +++ b/packages/flutter_tools/lib/src/android/android_device.dart @@ -855,9 +855,25 @@ class _AdbLogReader extends DeviceLogReader { @override String get name => device.name; + DateTime _timeOrigin; + + DateTime _adbTimestampToDateTime(String adbTimestamp) { + // The adb timestamp format is: mm-dd hours:minutes:seconds.milliseconds + // Dart's DateTime parse function accepts this format so long as we provide + // the year, resulting in: + // yyyy-mm-dd hours:minutes:seconds.milliseconds. + return DateTime.parse('${DateTime.now().year}-$adbTimestamp'); + } + void _start() { - // Start the adb logcat process and filter logs by the "flutter" tag. - final List args = ['shell', '-x', 'logcat', '-v', 'time', '-s', 'flutter']; + // Start the adb logcat process. + final List args = ['shell', '-x', 'logcat', '-v', 'time']; + final String lastTimestamp = device.lastLogcatTimestamp; + if (lastTimestamp != null) { + _timeOrigin = _adbTimestampToDateTime(lastTimestamp); + } else { + _timeOrigin = null; + } processUtils.start(device.adbCommandForDevice(args)).then((Process process) { _process = process; // We expect logcat streams to occasionally contain invalid utf-8, @@ -907,7 +923,18 @@ class _AdbLogReader extends DeviceLogReader { // mm-dd hh:mm:ss.milliseconds Priority/Tag( PID): .... void _onLine(String line) { final Match timeMatch = AndroidDevice._timeRegExp.firstMatch(line); - if (timeMatch == null || line.length == timeMatch.end) { + if (timeMatch == null) { + return; + } + if (_timeOrigin != null) { + final String timestamp = timeMatch.group(0); + final DateTime time = _adbTimestampToDateTime(timestamp); + if (!time.isAfter(_timeOrigin)) { + // Ignore log messages before the origin. + return; + } + } + if (line.length == timeMatch.end) { return; } // Chop off the time. diff --git a/packages/flutter_tools/lib/src/commands/attach.dart b/packages/flutter_tools/lib/src/commands/attach.dart index c82a2bd300..1e397582a5 100644 --- a/packages/flutter_tools/lib/src/commands/attach.dart +++ b/packages/flutter_tools/lib/src/commands/attach.dart @@ -4,8 +4,6 @@ import 'dart:async'; -import 'package:meta/meta.dart'; - import '../artifacts.dart'; import '../base/common.dart'; import '../base/context.dart'; @@ -202,14 +200,16 @@ class AttachCommand extends FlutterCommand { notifyingLogger: NotifyingLogger(), logToStdout: true) : null; - Stream observatoryUri; + Uri observatoryUri; bool usesIpv6 = ipv6; final String ipv6Loopback = InternetAddress.loopbackIPv6.address; final String ipv4Loopback = InternetAddress.loopbackIPv4.address; final String hostname = usesIpv6 ? ipv6Loopback : ipv4Loopback; + bool attachLogger = false; if (devicePort == null && debugUri == null) { if (device is FuchsiaDevice) { + attachLogger = true; final String module = stringArg('module'); if (module == null) { throwToolExit('\'--module\' is required for attaching to a Fuchsia device'); @@ -218,7 +218,8 @@ class AttachCommand extends FlutterCommand { FuchsiaIsolateDiscoveryProtocol isolateDiscoveryProtocol; try { isolateDiscoveryProtocol = device.getIsolateDiscoveryProtocol(module); - observatoryUri = Stream.fromFuture(isolateDiscoveryProtocol.uri).asBroadcastStream(); + observatoryUri = await isolateDiscoveryProtocol.uri; + printStatus('Done.'); // FYI, this message is used as a sentinel in tests. } catch (_) { isolateDiscoveryProtocol?.dispose(); final List ports = device.portForwarder.forwardedPorts.toList(); @@ -228,55 +229,85 @@ class AttachCommand extends FlutterCommand { rethrow; } } else if ((device is IOSDevice) || (device is IOSSimulator)) { - observatoryUri = Stream - .fromFuture( - MDnsObservatoryDiscovery.instance.getObservatoryUri( - appId, - device, - usesIpv6: usesIpv6, - deviceVmservicePort: deviceVmservicePort, - ) - ).asBroadcastStream(); + observatoryUri = await MDnsObservatoryDiscovery.instance.getObservatoryUri( + appId, + device, + usesIpv6: usesIpv6, + deviceVmservicePort: deviceVmservicePort, + ); } // If MDNS discovery fails or we're not on iOS, fallback to ProtocolDiscovery. if (observatoryUri == null) { - final ProtocolDiscovery observatoryDiscovery = - ProtocolDiscovery.observatory( + ProtocolDiscovery observatoryDiscovery; + try { + observatoryDiscovery = ProtocolDiscovery.observatory( device.getLogReader(), portForwarder: device.portForwarder, ipv6: ipv6, devicePort: deviceVmservicePort, hostPort: hostVmservicePort, ); - printStatus('Waiting for a connection from Flutter on ${device.name}...'); - observatoryUri = observatoryDiscovery.uris; - // Determine ipv6 status from the scanned logs. - usesIpv6 = observatoryDiscovery.ipv6; + printStatus('Waiting for a connection from Flutter on ${device.name}...'); + observatoryUri = await observatoryDiscovery.uri; + // Determine ipv6 status from the scanned logs. + usesIpv6 = observatoryDiscovery.ipv6; + printStatus('Done.'); // FYI, this message is used as a sentinel in tests. + } catch (error) { + throwToolExit('Failed to establish a debug connection with ${device.name}: $error'); + } finally { + await observatoryDiscovery?.cancel(); + } } } else { - observatoryUri = Stream - .fromFuture( - buildObservatoryUri( - device, - debugUri?.host ?? hostname, - devicePort ?? debugUri.port, - hostVmservicePort, - debugUri?.path, - ) - ).asBroadcastStream(); + observatoryUri = await buildObservatoryUri( + device, + debugUri?.host ?? hostname, + devicePort ?? debugUri.port, + hostVmservicePort, + debugUri?.path, + ); } - - terminal.usesTerminalUi = daemon == null; - try { + final bool useHot = getBuildInfo().isDebug; + final FlutterDevice flutterDevice = await FlutterDevice.create( + device, + flutterProject: flutterProject, + trackWidgetCreation: boolArg('track-widget-creation'), + fileSystemRoots: stringsArg('filesystem-root'), + fileSystemScheme: stringArg('filesystem-scheme'), + viewFilter: stringArg('isolate-filter'), + target: stringArg('target'), + targetModel: TargetModel(stringArg('target-model')), + buildMode: getBuildMode(), + dartDefines: dartDefines, + ); + flutterDevice.observatoryUris = [ observatoryUri ]; + final List flutterDevices = [flutterDevice]; + final DebuggingOptions debuggingOptions = DebuggingOptions.enabled(getBuildInfo()); + terminal.usesTerminalUi = daemon == null; + final ResidentRunner runner = useHot ? + hotRunnerFactory.build( + flutterDevices, + target: targetFile, + debuggingOptions: debuggingOptions, + packagesFilePath: globalResults['packages'] as String, + projectRootPath: stringArg('project-root'), + dillOutputPath: stringArg('output-dill'), + ipv6: usesIpv6, + flutterProject: flutterProject, + ) + : ColdRunner( + flutterDevices, + target: targetFile, + debuggingOptions: debuggingOptions, + ipv6: usesIpv6, + ); + if (attachLogger) { + flutterDevice.startEchoingDeviceLog(); + } + int result; if (daemon != null) { - final ResidentRunner runner = await createResidentRunner( - observatoryUris: observatoryUri, - device: device, - flutterProject: flutterProject, - usesIpv6: usesIpv6, - ); AppInstance app; try { app = await daemon.appDomain.launch( @@ -293,34 +324,20 @@ class AttachCommand extends FlutterCommand { } result = await app.runner.waitForAppToFinish(); assert(result != null); - return; - } - while (true) { - final ResidentRunner runner = await createResidentRunner( - observatoryUris: observatoryUri, - device: device, - flutterProject: flutterProject, - usesIpv6: usesIpv6, - ); + } else { final Completer onAppStart = Completer.sync(); - TerminalHandler terminalHandler; unawaited(onAppStart.future.whenComplete(() { - terminalHandler = TerminalHandler(runner) + TerminalHandler(runner) ..setupTerminal() ..registerSignalHandlers(); })); result = await runner.attach( appStartedCompleter: onAppStart, ); - if (result != 0) { - throwToolExit(null, exitCode: result); - } - terminalHandler?.stop(); assert(result != null); - if (runner.exited || !runner.isWaitingForObservatory) { - break; - } - printStatus('Waiting for a new connection from Flutter on ${device.name}...'); + } + if (result != 0) { + throwToolExit(null, exitCode: result); } } finally { final List ports = device.portForwarder.forwardedPorts.toList(); @@ -330,52 +347,6 @@ class AttachCommand extends FlutterCommand { } } - Future createResidentRunner({ - @required Stream observatoryUris, - @required Device device, - @required FlutterProject flutterProject, - @required bool usesIpv6, - }) async { - assert(observatoryUris != null); - assert(device != null); - assert(flutterProject != null); - assert(usesIpv6 != null); - - final FlutterDevice flutterDevice = await FlutterDevice.create( - device, - flutterProject: flutterProject, - trackWidgetCreation: boolArg('track-widget-creation'), - fileSystemRoots: stringsArg('filesystem-root'), - fileSystemScheme: stringArg('filesystem-scheme'), - viewFilter: stringArg('isolate-filter'), - target: stringArg('target'), - targetModel: TargetModel(stringArg('target-model')), - buildMode: getBuildMode(), - dartDefines: dartDefines, - ); - flutterDevice.observatoryUris = observatoryUris; - final List flutterDevices = [flutterDevice]; - final DebuggingOptions debuggingOptions = DebuggingOptions.enabled(getBuildInfo()); - - return getBuildInfo().isDebug - ? hotRunnerFactory.build( - flutterDevices, - target: targetFile, - debuggingOptions: debuggingOptions, - packagesFilePath: globalResults['packages'] as String, - projectRootPath: stringArg('project-root'), - dillOutputPath: stringArg('output-dill'), - ipv6: usesIpv6, - flutterProject: flutterProject, - ) - : ColdRunner( - flutterDevices, - target: targetFile, - debuggingOptions: debuggingOptions, - ipv6: usesIpv6, - ); - } - Future _validateArguments() async { } } diff --git a/packages/flutter_tools/lib/src/devfs.dart b/packages/flutter_tools/lib/src/devfs.dart index 9bba2d775f..2bfc8911c1 100644 --- a/packages/flutter_tools/lib/src/devfs.dart +++ b/packages/flutter_tools/lib/src/devfs.dart @@ -11,7 +11,6 @@ import 'asset.dart'; import 'base/context.dart'; import 'base/file_system.dart'; import 'base/io.dart'; -import 'base/net.dart'; import 'build_info.dart'; import 'bundle.dart'; import 'compile.dart'; @@ -266,20 +265,17 @@ class DevFSException implements Exception { class _DevFSHttpWriter { _DevFSHttpWriter(this.fsName, VMService serviceProtocol) - : httpAddress = serviceProtocol.httpAddress, - _client = (context.get() == null) - ? HttpClient() - : context.get()(); + : httpAddress = serviceProtocol.httpAddress; final String fsName; final Uri httpAddress; - final HttpClient _client; static const int kMaxInFlight = 6; int _inFlight = 0; Map _outstanding; Completer _completer; + final HttpClient _client = HttpClient(); Future write(Map entries) async { _client.maxConnectionsPerHost = kMaxInFlight; diff --git a/packages/flutter_tools/lib/src/protocol_discovery.dart b/packages/flutter_tools/lib/src/protocol_discovery.dart index a94000db23..803c957eee 100644 --- a/packages/flutter_tools/lib/src/protocol_discovery.dart +++ b/packages/flutter_tools/lib/src/protocol_discovery.dart @@ -17,23 +17,16 @@ class ProtocolDiscovery { this.logReader, this.serviceName, { this.portForwarder, - this.throttleDuration, this.hostPort, this.devicePort, this.ipv6, - }) : assert(logReader != null) - { - _deviceLogSubscription = logReader.logLines.listen( - _handleLine, - onDone: _stopScrapingLogs, - ); - _uriStreamController = StreamController.broadcast(); + }) : assert(logReader != null) { + _deviceLogSubscription = logReader.logLines.listen(_handleLine); } factory ProtocolDiscovery.observatory( DeviceLogReader logReader, { DevicePortForwarder portForwarder, - Duration throttleDuration = const Duration(milliseconds: 200), @required int hostPort, @required int devicePort, @required bool ipv6, @@ -43,7 +36,6 @@ class ProtocolDiscovery { logReader, kObservatoryService, portForwarder: portForwarder, - throttleDuration: throttleDuration, hostPort: hostPort, devicePort: devicePort, ipv6: ipv6, @@ -57,70 +49,50 @@ class ProtocolDiscovery { final int devicePort; final bool ipv6; - /// The time to wait before forwarding a new observatory URIs from [logReader]. - final Duration throttleDuration; + final Completer _completer = Completer(); StreamSubscription _deviceLogSubscription; - StreamController _uriStreamController; /// The discovered service URI. - /// Use [uris] instead. - // TODO(egarciad): replace `uri` for `uris`. - Future get uri { - return uris.first; - } - - /// The discovered service URIs. /// - /// When a new observatory URI is available in [logReader], - /// the URIs are forwarded at most once every [throttleDuration]. - /// - /// Port forwarding is only attempted when this is invoked, - /// for each observatory URI in the stream. - Stream get uris { - return _uriStreamController.stream - .transform(_throttle( - waitDuration: throttleDuration, - )) - .asyncMap(_forwardPort); + /// Port forwarding is only attempted when this is invoked, in case we never + /// need to port forward. + Future get uri async { + final Uri rawUri = await _completer.future; + return await _forwardPort(rawUri); } Future cancel() => _stopScrapingLogs(); Future _stopScrapingLogs() async { - await _uriStreamController?.close(); await _deviceLogSubscription?.cancel(); _deviceLogSubscription = null; } - Match _getPatternMatch(String line) { - final RegExp r = RegExp('${RegExp.escape(serviceName)} listening on ((http|\/\/)[a-zA-Z0-9:/=_\\-\.\\[\\]]+)'); - return r.firstMatch(line); - } - - Uri _getObservatoryUri(String line) { - final Match match = _getPatternMatch(line); - if (match != null) { - return Uri.parse(match[1]); - } - return null; - } - void _handleLine(String line) { Uri uri; - try { - uri = _getObservatoryUri(line); - } on FormatException catch(error, stackTrace) { - _uriStreamController.addError(error, stackTrace); + final RegExp r = RegExp('${RegExp.escape(serviceName)} listening on ((http|\/\/)[a-zA-Z0-9:/=_\\-\.\\[\\]]+)'); + final Match match = r.firstMatch(line); + + if (match != null) { + try { + uri = Uri.parse(match[1]); + } on FormatException catch (error, stackTrace) { + _stopScrapingLogs(); + _completer.completeError(error, stackTrace); + } } if (uri == null) { return; } - if (devicePort != null && uri.port != devicePort) { + if (devicePort != null && uri.port != devicePort) { printTrace('skipping potential observatory $uri due to device port mismatch'); return; } - _uriStreamController.add(uri); + + assert(!_completer.isCompleted); + _stopScrapingLogs(); + _completer.complete(uri); } Future _forwardPort(Uri deviceUri) async { @@ -138,43 +110,7 @@ class ProtocolDiscovery { if (ipv6) { hostUri = hostUri.replace(host: InternetAddress.loopbackIPv6.host); } + return hostUri; } } - -/// This transformer will produce an event at most once every [waitDuration]. -/// -/// For example, consider a `waitDuration` of `10ms`, and list of event names -/// and arrival times: `a (0ms), b (5ms), c (11ms), d (21ms)`. -/// The events `c` and `d` will be produced as a result. -StreamTransformer _throttle({ - @required Duration waitDuration, -}) { - assert(waitDuration != null); - - S latestLine; - int lastExecution; - Future throttleFuture; - - return StreamTransformer - .fromHandlers( - handleData: (S value, EventSink sink) { - latestLine = value; - - final int currentTime = DateTime.now().millisecondsSinceEpoch; - lastExecution ??= currentTime; - final int remainingTime = currentTime - lastExecution; - final int nextExecutionTime = remainingTime > waitDuration.inMilliseconds - ? 0 - : waitDuration.inMilliseconds - remainingTime; - - throttleFuture ??= Future - .delayed(Duration(milliseconds: nextExecutionTime)) - .whenComplete(() { - sink.add(latestLine); - throttleFuture = null; - lastExecution = DateTime.now().millisecondsSinceEpoch; - }); - } - ); -} diff --git a/packages/flutter_tools/lib/src/resident_runner.dart b/packages/flutter_tools/lib/src/resident_runner.dart index ea34ae10d6..9b3ebb6dc1 100644 --- a/packages/flutter_tools/lib/src/resident_runner.dart +++ b/packages/flutter_tools/lib/src/resident_runner.dart @@ -131,20 +131,16 @@ class FlutterDevice { final Device device; final ResidentCompiler generator; - Stream observatoryUris; + List observatoryUris; List vmServices; DevFS devFS; ApplicationPackage package; List fileSystemRoots; String fileSystemScheme; StreamSubscription _loggingSubscription; - bool _isListeningForObservatoryUri; final String viewFilter; final bool trackWidgetCreation; - /// Whether the stream [observatoryUris] is still open. - bool get isWaitingForObservatory => _isListeningForObservatoryUri ?? false; - /// If the [reloadSources] parameter is not null the 'reloadSources' service /// will be registered. /// The 'reloadSources' service can be used by other Service Protocol clients @@ -158,50 +154,23 @@ class FlutterDevice { ReloadSources reloadSources, Restart restart, CompileExpression compileExpression, - }) { - final Completer completer = Completer(); - StreamSubscription subscription; - bool isWaitingForVm = false; - - subscription = observatoryUris.listen((Uri observatoryUri) async { - // FYI, this message is used as a sentinel in tests. - printTrace('Connecting to service protocol: $observatoryUri'); - isWaitingForVm = true; - VMService service; - - try { - service = await VMService.connect( - observatoryUri, - reloadSources: reloadSources, - restart: restart, - compileExpression: compileExpression, - ); - } on Exception catch (exception) { - printTrace('Fail to connect to service protocol: $observatoryUri: $exception'); - if (!completer.isCompleted && !_isListeningForObservatoryUri) { - completer.completeError('failed to connect to $observatoryUri'); - } - return; - } - if (completer.isCompleted) { - return; - } - printTrace('Successfully connected to service protocol: $observatoryUri'); - - vmServices = [service]; - device.getLogReader(app: package).connectedVMServices = vmServices; - completer.complete(); - await subscription.cancel(); - }, onError: (dynamic error) { - printTrace('Fail to handle observatory URI: $error'); - }, onDone: () { - _isListeningForObservatoryUri = false; - if (!completer.isCompleted && !isWaitingForVm) { - completer.completeError('connection to device ended too early'); - } - }); - _isListeningForObservatoryUri = true; - return completer.future; + }) async { + if (vmServices != null) { + return; + } + final List localVmServices = List(observatoryUris.length); + for (int i = 0; i < observatoryUris.length; i += 1) { + printTrace('Connecting to service protocol: ${observatoryUris[i]}'); + localVmServices[i] = await VMService.connect( + observatoryUris[i], + reloadSources: reloadSources, + restart: restart, + compileExpression: compileExpression, + ); + printTrace('Successfully connected to service protocol: ${observatoryUris[i]}'); + } + vmServices = localVmServices; + device.getLogReader(app: package).connectedVMServices = vmServices; } Future refreshViews() async { @@ -252,7 +221,6 @@ class FlutterDevice { if (flutterViews.any((FlutterView view) { return view != null && view.uiIsolate != null && - view.uiIsolate.pauseEvent != null && view.uiIsolate.pauseEvent.isPauseEvent; } )) { @@ -463,13 +431,9 @@ class FlutterDevice { return 2; } if (result.hasObservatory) { - observatoryUris = Stream - .value(result.observatoryUri) - .asBroadcastStream(); + observatoryUris = [result.observatoryUri]; } else { - observatoryUris = const Stream - .empty() - .asBroadcastStream(); + observatoryUris = []; } return 0; } @@ -527,13 +491,9 @@ class FlutterDevice { return 2; } if (result.hasObservatory) { - observatoryUris = Stream - .value(result.observatoryUri) - .asBroadcastStream(); + observatoryUris = [result.observatoryUri]; } else { - observatoryUris = const Stream - .empty() - .asBroadcastStream(); + observatoryUris = []; } return 0; } @@ -653,21 +613,14 @@ abstract class ResidentRunner { /// The parent location of the incremental artifacts. @visibleForTesting final Directory artifactDirectory; + final Completer _finished = Completer(); final String packagesFilePath; final String projectRootPath; final String mainPath; final AssetBundle assetBundle; bool _exited = false; - Completer _finished = Completer(); - bool hotMode; - - /// Returns true if every device is streaming observatory URIs. - bool get isWaitingForObservatory { - return flutterDevices.every((FlutterDevice device) { - return device.isWaitingForObservatory; - }); - } + bool hotMode ; String get dillOutputPath => _dillOutputPath ?? fs.path.join(artifactDirectory.path, 'app.dill'); String getReloadPath({ bool fullRestart }) => mainPath + (fullRestart ? '' : '.incremental') + '.dill'; @@ -678,9 +631,6 @@ abstract class ResidentRunner { bool get isRunningRelease => debuggingOptions.buildInfo.isRelease; bool get supportsServiceProtocol => isRunningDebug || isRunningProfile; - /// Returns [true] if the resident runner exited after invoking [exit()]. - bool get exited => _exited; - /// Whether this runner can hot restart. /// /// To prevent scenarios where only a subset of devices are hot restarted, @@ -912,8 +862,6 @@ abstract class ResidentRunner { throw 'The service protocol is not enabled.'; } - _finished = Completer(); - bool viewFound = false; for (FlutterDevice device in flutterDevices) { await device.connect( @@ -1097,33 +1045,15 @@ class TerminalHandler { subscription = terminal.keystrokes.listen(processTerminalInput); } - - final Map _signalTokens = {}; - - void _addSignalHandler(io.ProcessSignal signal, SignalHandler handler) { - _signalTokens[signal] = signals.addHandler(signal, handler); - } - void registerSignalHandlers() { assert(residentRunner.stayResident); - - _addSignalHandler(io.ProcessSignal.SIGINT, _cleanUp); - _addSignalHandler(io.ProcessSignal.SIGTERM, _cleanUp); + signals.addHandler(io.ProcessSignal.SIGINT, _cleanUp); + signals.addHandler(io.ProcessSignal.SIGTERM, _cleanUp); if (!residentRunner.supportsServiceProtocol || !residentRunner.supportsRestart) { return; } - _addSignalHandler(io.ProcessSignal.SIGUSR1, _handleSignal); - _addSignalHandler(io.ProcessSignal.SIGUSR2, _handleSignal); - } - - /// Unregisters terminal signal and keystroke handlers. - void stop() { - assert(residentRunner.stayResident); - for (MapEntry entry in _signalTokens.entries) { - signals.removeHandler(entry.key, entry.value); - } - _signalTokens.clear(); - subscription.cancel(); + signals.addHandler(io.ProcessSignal.SIGUSR1, _handleSignal); + signals.addHandler(io.ProcessSignal.SIGUSR2, _handleSignal); } /// Returns [true] if the input has been handled by this function. diff --git a/packages/flutter_tools/lib/src/run_cold.dart b/packages/flutter_tools/lib/src/run_cold.dart index 1d81a93d0c..21f077653c 100644 --- a/packages/flutter_tools/lib/src/run_cold.dart +++ b/packages/flutter_tools/lib/src/run_cold.dart @@ -83,7 +83,7 @@ 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, + httpUri: flutterDevices.first.observatoryUris.first, wsUri: flutterDevices.first.vmServices.first.wsAddress, )); } @@ -183,9 +183,10 @@ 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.wsAddress}'); + if (device.observatoryUris != null) { + for (Uri uri in device.observatoryUris) { + printStatus('An Observatory debugger and profiler on $dname is available at $uri'); + haveAnything = true; } } } diff --git a/packages/flutter_tools/lib/src/run_hot.dart b/packages/flutter_tools/lib/src/run_hot.dart index ccdcfe0b21..1886aa1f8e 100644 --- a/packages/flutter_tools/lib/src/run_hot.dart +++ b/packages/flutter_tools/lib/src/run_hot.dart @@ -180,7 +180,7 @@ class HotRunner extends ResidentRunner { // Only handle one debugger connection. connectionInfoCompleter.complete( DebugConnectionInfo( - httpUri: flutterDevices.first.vmServices.first.httpAddress, + httpUri: flutterDevices.first.observatoryUris.first, wsUri: flutterDevices.first.vmServices.first.wsAddress, baseUri: baseUris.first.toString(), ), @@ -987,8 +987,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.wsAddress}'); + for (Uri uri in device.observatoryUris) { + printStatus('An Observatory debugger and profiler on $dname is available at: $uri'); } } final String quitMessage = _didAttach diff --git a/packages/flutter_tools/test/commands.shard/hermetic/attach_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/attach_test.dart index e100bc6b85..7a99e5b8ec 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/attach_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/attach_test.dart @@ -7,31 +7,24 @@ import 'dart:async'; import 'package:file/memory.dart'; import 'package:flutter_tools/src/base/common.dart'; import 'package:flutter_tools/src/base/file_system.dart'; -import 'package:flutter_tools/src/base/io.dart'; import 'package:flutter_tools/src/base/logger.dart'; -import 'package:flutter_tools/src/base/net.dart'; import 'package:flutter_tools/src/base/platform.dart'; import 'package:flutter_tools/src/base/terminal.dart'; import 'package:flutter_tools/src/cache.dart'; import 'package:flutter_tools/src/commands/attach.dart'; -import 'package:flutter_tools/src/convert.dart'; import 'package:flutter_tools/src/device.dart'; import 'package:flutter_tools/src/ios/devices.dart'; import 'package:flutter_tools/src/mdns_discovery.dart'; -import 'package:flutter_tools/src/project.dart'; import 'package:flutter_tools/src/resident_runner.dart'; import 'package:flutter_tools/src/run_hot.dart'; -import 'package:flutter_tools/src/vmservice.dart'; import 'package:meta/meta.dart'; import 'package:mockito/mockito.dart'; import 'package:process/process.dart'; -import 'package:quiver/testing/async.dart'; import '../../src/common.dart'; import '../../src/context.dart'; import '../../src/mocks.dart'; - void main() { group('attach', () { StreamLogger logger; @@ -56,16 +49,11 @@ void main() { MockDeviceLogReader mockLogReader; MockPortForwarder portForwarder; MockAndroidDevice device; - MockProcessManager mockProcessManager; - MockHttpClient httpClient; - Completer vmServiceDoneCompleter; setUp(() { - mockProcessManager = MockProcessManager(); mockLogReader = MockDeviceLogReader(); portForwarder = MockPortForwarder(); device = MockAndroidDevice(); - vmServiceDoneCompleter = Completer(); when(device.portForwarder) .thenReturn(portForwarder); when(portForwarder.forward(devicePort, hostPort: anyNamed('hostPort'))) @@ -75,14 +63,6 @@ void main() { when(portForwarder.unforward(any)) .thenAnswer((_) async => null); - final HttpClientRequest httpClientRequest = MockHttpClientRequest(); - httpClient = MockHttpClient(); - when(httpClient.putUrl(any)) - .thenAnswer((_) => Future.value(httpClientRequest)); - when(httpClientRequest.headers).thenReturn(MockHttpHeaders()); - when(httpClientRequest.close()) - .thenAnswer((_) => Future.value(MockHttpClientResponse())); - // We cannot add the device to a device manager because that is // only enabled by the context of each testUsingContext call. // @@ -97,15 +77,17 @@ void main() { testUsingContext('finds observatory port and forwards', () async { when(device.getLogReader()).thenAnswer((_) { // Now that the reader is used, start writing messages to it. - mockLogReader.addLine('Foo'); - mockLogReader.addLine('Observatory listening on http://127.0.0.1:$devicePort'); + Timer.run(() { + mockLogReader.addLine('Foo'); + mockLogReader.addLine('Observatory listening on http://127.0.0.1:$devicePort'); + }); return mockLogReader; }); testDeviceManager.addDevice(device); final Completer completer = Completer(); final StreamSubscription loggerSubscription = logger.stream.listen((String message) { - if (message == '[verbose] Observatory URL on device: http://127.0.0.1:$devicePort') { - // The "Observatory URL on device" message is output by the ProtocolDiscovery when it found the observatory. + if (message == '[stdout] Done.') { + // The "Done." message is output by the AttachCommand when it's done. completer.complete(); } }); @@ -114,7 +96,6 @@ void main() { verify( portForwarder.forward(devicePort, hostPort: anyNamed('hostPort')), ).called(1); - await mockLogReader.dispose(); await expectLoggerInterruptEndsTask(task, logger); await loggerSubscription.cancel(); }, overrides: { @@ -123,113 +104,13 @@ void main() { Logger: () => logger, }); - testUsingContext('finds all observatory ports and forwards them', () async { - testFileSystem.file(testFileSystem.path.join('.packages')).createSync(); - testFileSystem.file(testFileSystem.path.join('lib', 'main.dart')).createSync(); - testFileSystem - .file(testFileSystem.path.join('build', 'flutter_assets', 'AssetManifest.json')) - ..createSync(recursive: true) - ..writeAsStringSync('{}'); - - when(device.name).thenReturn('MockAndroidDevice'); - when(device.getLogReader()).thenReturn(mockLogReader); - - final Process dartProcess = MockProcess(); - final StreamController> compilerStdoutController = StreamController>(); - - when(dartProcess.stdout).thenAnswer((_) => compilerStdoutController.stream); - when(dartProcess.stderr) - .thenAnswer((_) => Stream>.fromFuture(Future>.value(const []))); - - when(dartProcess.stdin).thenAnswer((_) => MockStdIn()); - - final Completer dartProcessExitCode = Completer(); - when(dartProcess.exitCode).thenAnswer((_) => dartProcessExitCode.future); - when(mockProcessManager.start(any)).thenAnswer((_) => Future.value(dartProcess)); - - testDeviceManager.addDevice(device); - - final List observatoryLogs = []; - - await FakeAsync().run((FakeAsync time) { - unawaited(runZoned(() async { - final StreamSubscription loggerSubscription = logger.stream.listen((String message) { - // The "Observatory URL on device" message is output by the ProtocolDiscovery when it found the observatory. - if (message.startsWith('[verbose] Observatory URL on device')) { - observatoryLogs.add(message); - } - if (message == '[stdout] Waiting for a connection from Flutter on MockAndroidDevice...') { - observatoryLogs.add(message); - } - if (message == '[stdout] Lost connection to device.') { - observatoryLogs.add(message); - } - if (message.contains('To hot reload changes while running, press "r". To hot restart (and rebuild state), press "R".')) { - observatoryLogs.add(message); - } - }); - - final TestHotRunnerFactory testHotRunnerFactory = TestHotRunnerFactory(); - final Future task = createTestCommandRunner( - AttachCommand(hotRunnerFactory: testHotRunnerFactory) - ).run(['attach']); - - // First iteration of the attach loop. - mockLogReader.addLine('Observatory listening on http://127.0.0.1:0001'); - mockLogReader.addLine('Observatory listening on http://127.0.0.1:1234'); - - time.elapse(const Duration(milliseconds: 200)); - - compilerStdoutController - .add(utf8.encode('result abc\nline1\nline2\nabc\nabc /path/to/main.dart.dill 0\n')); - time.flushMicrotasks(); - - // Second iteration of the attach loop. - mockLogReader.addLine('Observatory listening on http://127.0.0.1:0002'); - mockLogReader.addLine('Observatory listening on http://127.0.0.1:1235'); - - time.elapse(const Duration(milliseconds: 200)); - - compilerStdoutController - .add(utf8.encode('result abc\nline1\nline2\nabc\nabc /path/to/main.dart.dill 0\n')); - time.flushMicrotasks(); - - dartProcessExitCode.complete(0); - - await loggerSubscription.cancel(); - await testHotRunnerFactory.exitApp(); - await task; - })); - }); - - expect(observatoryLogs.length, 7); - expect(observatoryLogs[0], '[stdout] Waiting for a connection from Flutter on MockAndroidDevice...'); - expect(observatoryLogs[1], '[verbose] Observatory URL on device: http://127.0.0.1:1234'); - expect(observatoryLogs[2], '[stdout] Lost connection to device.'); - expect(observatoryLogs[3].contains('To hot reload changes while running, press "r". To hot restart (and rebuild state), press "R"'), isTrue); - expect(observatoryLogs[4], '[verbose] Observatory URL on device: http://127.0.0.1:1235'); - expect(observatoryLogs[5], '[stdout] Lost connection to device.'); - expect(observatoryLogs[6].contains('To hot reload changes while running, press "r". To hot restart (and rebuild state), press "R"'), isTrue); - - verify(portForwarder.forward(1234, hostPort: anyNamed('hostPort'))).called(1); - verify(portForwarder.forward(1235, hostPort: anyNamed('hostPort'))).called(1); - - }, overrides: { - FileSystem: () => testFileSystem, - HttpClientFactory: () => () => httpClient, - ProcessManager: () => mockProcessManager, - Logger: () => logger, - VMServiceConnector: () => getFakeVmServiceFactory( - vmServiceDoneCompleter: vmServiceDoneCompleter, - ), - }); - testUsingContext('Fails with tool exit on bad Observatory uri', () async { when(device.getLogReader()).thenAnswer((_) { // Now that the reader is used, start writing messages to it. - mockLogReader.addLine('Foo'); - mockLogReader.addLine('Observatory listening on http:/:/127.0.0.1:$devicePort'); - mockLogReader.dispose(); + Timer.run(() { + mockLogReader.addLine('Foo'); + mockLogReader.addLine('Observatory listening on http:/:/127.0.0.1:$devicePort'); + }); return mockLogReader; }); testDeviceManager.addDevice(device); @@ -244,8 +125,10 @@ void main() { testUsingContext('accepts filesystem parameters', () async { when(device.getLogReader()).thenAnswer((_) { // Now that the reader is used, start writing messages to it. - mockLogReader.addLine('Foo'); - mockLogReader.addLine('Observatory listening on http://127.0.0.1:$devicePort'); + Timer.run(() { + mockLogReader.addLine('Foo'); + mockLogReader.addLine('Observatory listening on http://127.0.0.1:$devicePort'); + }); return mockLogReader; }); testDeviceManager.addDevice(device); @@ -258,8 +141,6 @@ void main() { final MockHotRunner mockHotRunner = MockHotRunner(); when(mockHotRunner.attach(appStartedCompleter: anyNamed('appStartedCompleter'))) .thenAnswer((_) async => 0); - when(mockHotRunner.exited).thenReturn(false); - when(mockHotRunner.isWaitingForObservatory).thenReturn(false); final MockHotRunnerFactory mockHotRunnerFactory = MockHotRunnerFactory(); when( @@ -323,8 +204,10 @@ void main() { testUsingContext('exits when ipv6 is specified and debug-port is not', () async { when(device.getLogReader()).thenAnswer((_) { // Now that the reader is used, start writing messages to it. - mockLogReader.addLine('Foo'); - mockLogReader.addLine('Observatory listening on http://127.0.0.1:$devicePort'); + Timer.run(() { + mockLogReader.addLine('Foo'); + mockLogReader.addLine('Observatory listening on http://127.0.0.1:$devicePort'); + }); return mockLogReader; }); testDeviceManager.addDevice(device); @@ -345,8 +228,10 @@ void main() { testUsingContext('exits when observatory-port is specified and debug-port is not', () async { when(device.getLogReader()).thenAnswer((_) { // Now that the reader is used, start writing messages to it. - mockLogReader.addLine('Foo'); - mockLogReader.addLine('Observatory listening on http://127.0.0.1:$devicePort'); + Timer.run(() { + mockLogReader.addLine('Foo'); + mockLogReader.addLine('Observatory listening on http://127.0.0.1:$devicePort'); + }); return mockLogReader; }); testDeviceManager.addDevice(device); @@ -365,6 +250,7 @@ void main() { },); }); + testUsingContext('selects specified target', () async { const int devicePort = 499; const int hostPort = 42; @@ -391,16 +277,16 @@ void main() { flutterProject: anyNamed('flutterProject'), ipv6: false, )).thenReturn(mockHotRunner); - when(mockHotRunner.exited).thenReturn(false); - when(mockHotRunner.isWaitingForObservatory).thenReturn(false); testDeviceManager.addDevice(device); when(device.getLogReader()) .thenAnswer((_) { // Now that the reader is used, start writing messages to it. - mockLogReader.addLine('Foo'); - mockLogReader.addLine( - 'Observatory listening on http://127.0.0.1:$devicePort'); + Timer.run(() { + mockLogReader.addLine('Foo'); + mockLogReader.addLine( + 'Observatory listening on http://127.0.0.1:$devicePort'); + }); return mockLogReader; }); final File foo = fs.file('lib/foo.dart') @@ -684,84 +570,3 @@ Future expectLoggerInterruptEndsTask(Future task, StreamLogger logge expect(error.exitCode, 2); // ...with exit code 2. } } - -VMServiceConnector getFakeVmServiceFactory({ - @required Completer vmServiceDoneCompleter, -}) { - assert(vmServiceDoneCompleter != null); - - return ( - Uri httpUri, { - ReloadSources reloadSources, - Restart restart, - CompileExpression compileExpression, - CompressionOptions compression, - }) async { - final VMService vmService = VMServiceMock(); - final VM vm = VMMock(); - - when(vmService.vm).thenReturn(vm); - when(vmService.isClosed).thenReturn(false); - when(vmService.done).thenAnswer((_) { - return Future.value(null); - }); - - when(vm.refreshViews(waitForViews: anyNamed('waitForViews'))) - .thenAnswer((_) => Future.value(null)); - when(vm.views) - .thenReturn([FlutterViewMock()]); - when(vm.createDevFS(any)) - .thenAnswer((_) => Future>.value({'uri': '/',})); - - return vmService; - }; -} - -class TestHotRunnerFactory extends HotRunnerFactory { - HotRunner _runner; - - @override - HotRunner build( - List devices, { - String target, - DebuggingOptions debuggingOptions, - bool benchmarkMode = false, - File applicationBinary, - bool hostIsIde = false, - String projectRootPath, - String packagesFilePath, - String dillOutputPath, - bool stayResident = true, - bool ipv6 = false, - FlutterProject flutterProject, - }) { - _runner ??= HotRunner( - devices, - target: target, - debuggingOptions: debuggingOptions, - benchmarkMode: benchmarkMode, - applicationBinary: applicationBinary, - hostIsIde: hostIsIde, - projectRootPath: projectRootPath, - packagesFilePath: packagesFilePath, - dillOutputPath: dillOutputPath, - stayResident: stayResident, - ipv6: ipv6, - ); - return _runner; - } - - Future exitApp() async { - assert(_runner != null); - await _runner.exit(); - } -} - -class VMMock extends Mock implements VM {} -class VMServiceMock extends Mock implements VMService {} -class FlutterViewMock extends Mock implements FlutterView {} -class MockProcessManager extends Mock implements ProcessManager {} -class MockProcess extends Mock implements Process {} -class MockHttpClientRequest extends Mock implements HttpClientRequest {} -class MockHttpClientResponse extends Mock implements HttpClientResponse {} -class MockHttpHeaders extends Mock implements HttpHeaders {} \ No newline at end of file diff --git a/packages/flutter_tools/test/general.shard/devfs_test.dart b/packages/flutter_tools/test/general.shard/devfs_test.dart index 0298b16a44..bd5a5a69a0 100644 --- a/packages/flutter_tools/test/general.shard/devfs_test.dart +++ b/packages/flutter_tools/test/general.shard/devfs_test.dart @@ -10,7 +10,6 @@ import 'package:file/file.dart'; import 'package:file/memory.dart'; import 'package:flutter_tools/src/base/file_system.dart'; import 'package:flutter_tools/src/base/io.dart'; -import 'package:flutter_tools/src/base/net.dart'; import 'package:flutter_tools/src/compile.dart'; import 'package:flutter_tools/src/devfs.dart'; import 'package:flutter_tools/src/vmservice.dart'; @@ -160,7 +159,6 @@ void main() { verify(httpRequest.close()).called(kFailedAttempts + 1); }, overrides: { FileSystem: () => fs, - HttpClientFactory: () => () => httpClient, ProcessManager: () => FakeProcessManager.any(), }); }); @@ -210,7 +208,6 @@ void main() { expect(report.success, true); }, overrides: { FileSystem: () => fs, - HttpClient: () => () => HttpClient(), ProcessManager: () => FakeProcessManager.any(), }); @@ -313,7 +310,6 @@ void main() { expect(devFS.lastCompiled, isNot(previousCompile)); }, overrides: { FileSystem: () => fs, - HttpClient: () => () => HttpClient(), ProcessManager: () => FakeProcessManager.any(), }); }); diff --git a/packages/flutter_tools/test/general.shard/protocol_discovery_test.dart b/packages/flutter_tools/test/general.shard/protocol_discovery_test.dart index c1b9369763..db074a0980 100644 --- a/packages/flutter_tools/test/general.shard/protocol_discovery_test.dart +++ b/packages/flutter_tools/test/general.shard/protocol_discovery_test.dart @@ -6,7 +6,6 @@ import 'dart:async'; import 'package:flutter_tools/src/device.dart'; import 'package:flutter_tools/src/protocol_discovery.dart'; -import 'package:quiver/testing/async.dart'; import '../src/common.dart'; import '../src/context.dart'; @@ -17,47 +16,40 @@ void main() { MockDeviceLogReader logReader; ProtocolDiscovery discoverer; - /// Performs test set-up functionality that must be performed as part of - /// the `test()` pass and not part of the `setUp()` pass. - /// - /// This exists to make sure we're not creating an error that tries to - /// cross an error-zone boundary. Our use of `testUsingContext()` runs the - /// test code inside an error zone, but the `setUp()` code is not run in - /// any zone. This creates the potential for errors that try to cross - /// error-zone boundaries, which are considered uncaught. - /// - /// This also exists for cases where our initialization requires access to - /// a `Context` object, which is only set up inside the zone. - /// - /// These issues do not pertain to real code and are a test-only concern, - /// because in real code, the zone is set up in `main()`. - /// - /// See also: [runZoned] - void initialize({ - int devicePort, - Duration throttleDuration = const Duration(milliseconds: 200), - }) { - logReader = MockDeviceLogReader(); - discoverer = ProtocolDiscovery.observatory( - logReader, - ipv6: false, - hostPort: null, - devicePort: devicePort, - throttleDuration: throttleDuration, - ); - } - - testUsingContext('returns non-null uri future', () async { - initialize(); - expect(discoverer.uri, isNotNull); - }); - group('no port forwarding', () { + int devicePort; + + /// Performs test set-up functionality that must be performed as part of + /// the `test()` pass and not part of the `setUp()` pass. + /// + /// This exists to make sure we're not creating an error that tries to + /// cross an error-zone boundary. Our use of `testUsingContext()` runs the + /// test code inside an error zone, but the `setUp()` code is not run in + /// any zone. This creates the potential for errors that try to cross + /// error-zone boundaries, which are considered uncaught. + /// + /// This also exists for cases where our initialization requires access to + /// a `Context` object, which is only set up inside the zone. + /// + /// These issues do not pertain to real code and are a test-only concern, + /// because in real code, the zone is set up in `main()`. + /// + /// See also: [runZoned] + void initialize() { + logReader = MockDeviceLogReader(); + discoverer = ProtocolDiscovery.observatory(logReader, ipv6: false, hostPort: null, devicePort: devicePort); + } + tearDown(() { discoverer.cancel(); logReader.dispose(); }); + testUsingContext('returns non-null uri future', () async { + initialize(); + expect(discoverer.uri, isNotNull); + }); + testUsingContext('discovers uri if logs already produced output', () async { initialize(); logReader.addLine('HELLO WORLD'); @@ -86,7 +78,9 @@ void main() { testUsingContext('uri throws if logs produce bad line', () async { initialize(); - logReader.addLine('Observatory listening on http://127.0.0.1:apple'); + Timer.run(() { + logReader.addLine('Observatory listening on http://127.0.0.1:apple'); + }); expect(discoverer.uri, throwsA(isFormatException)); }); @@ -129,7 +123,8 @@ void main() { }); testUsingContext('skips uri if port does not match the requested vmservice - requested last', () async { - initialize(devicePort: 12346); + devicePort = 12346; + initialize(); final Future uriFuture = discoverer.uri; logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:12345/PTwjm8Ii8qg=/'); logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:12346/PTwjm8Ii8qg=/'); @@ -139,7 +134,8 @@ void main() { }); testUsingContext('skips uri if port does not match the requested vmservice - requested first', () async { - initialize(devicePort: 12346); + devicePort = 12346; + initialize(); final Future uriFuture = discoverer.uri; logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:12346/PTwjm8Ii8qg=/'); logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:12345/PTwjm8Ii8qg=/'); @@ -147,86 +143,6 @@ void main() { expect(uri.port, 12346); expect('$uri', 'http://127.0.0.1:12346/PTwjm8Ii8qg=/'); }); - - testUsingContext('first uri in the stream is the last one from the log', () async { - initialize(); - logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:12346/PTwjm8Ii8qg=/'); - logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:12345/PTwjm8Ii8qg=/'); - final Uri uri = await discoverer.uris.first; - expect(uri.port, 12345); - expect('$uri', 'http://127.0.0.1:12345/PTwjm8Ii8qg=/'); - }); - - testUsingContext('first uri in the stream is the last one from the log that matches the port', () async { - initialize(devicePort: 12345); - logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:12346/PTwjm8Ii8qg=/'); - logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:12345/PTwjm8Ii8qg=/'); - logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:12344/PTwjm8Ii8qg=/'); - final Uri uri = await discoverer.uris.first; - expect(uri.port, 12345); - expect('$uri', 'http://127.0.0.1:12345/PTwjm8Ii8qg=/'); - }); - - testUsingContext('uris in the stream are throttled', () async { - const Duration kThrottleDuration = Duration(milliseconds: 10); - - FakeAsync().run((FakeAsync time) { - initialize(throttleDuration: kThrottleDuration); - - final List discoveredUris = []; - discoverer.uris.listen((Uri uri) { - discoveredUris.add(uri); - }); - - logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:12346/PTwjm8Ii8qg=/'); - logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:12345/PTwjm8Ii8qg=/'); - - time.elapse(kThrottleDuration); - - logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:12344/PTwjm8Ii8qg=/'); - logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:12343/PTwjm8Ii8qg=/'); - - time.elapse(kThrottleDuration); - - expect(discoveredUris.length, 2); - expect(discoveredUris[0].port, 12345); - expect('${discoveredUris[0]}', 'http://127.0.0.1:12345/PTwjm8Ii8qg=/'); - expect(discoveredUris[1].port, 12343); - expect('${discoveredUris[1]}', 'http://127.0.0.1:12343/PTwjm8Ii8qg=/'); - }); - }); - - testUsingContext('uris in the stream are throttled when they match the port', () async { - const Duration kThrottleTimeInMilliseconds = Duration(milliseconds: 10); - - FakeAsync().run((FakeAsync time) { - initialize( - devicePort: 12345, - throttleDuration: kThrottleTimeInMilliseconds, - ); - - final List discoveredUris = []; - discoverer.uris.listen((Uri uri) { - discoveredUris.add(uri); - }); - - logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:12346/PTwjm8Ii8qg=/'); - logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:12345/PTwjm8Ii8qg=/'); - - time.elapse(kThrottleTimeInMilliseconds); - - logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:12345/PTwjm8Ii8qc=/'); - logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:12344/PTwjm8Ii8qf=/'); - - time.elapse(kThrottleTimeInMilliseconds); - - expect(discoveredUris.length, 2); - expect(discoveredUris[0].port, 12345); - expect('${discoveredUris[0]}', 'http://127.0.0.1:12345/PTwjm8Ii8qg=/'); - expect(discoveredUris[1].port, 12345); - expect('${discoveredUris[1]}', 'http://127.0.0.1:12345/PTwjm8Ii8qc=/'); - }); - }); }); group('port forwarding', () { @@ -248,7 +164,7 @@ void main() { expect('$uri', 'http://127.0.0.1:99/PTwjm8Ii8qg=/'); await discoverer.cancel(); - await logReader.dispose(); + logReader.dispose(); }); testUsingContext('specified port', () async { @@ -269,7 +185,7 @@ void main() { expect('$uri', 'http://127.0.0.1:1243/PTwjm8Ii8qg=/'); await discoverer.cancel(); - await logReader.dispose(); + logReader.dispose(); }); testUsingContext('specified port zero', () async { @@ -290,7 +206,7 @@ void main() { expect('$uri', 'http://127.0.0.1:99/PTwjm8Ii8qg=/'); await discoverer.cancel(); - await logReader.dispose(); + logReader.dispose(); }); testUsingContext('ipv6', () async { @@ -311,7 +227,7 @@ void main() { expect('$uri', 'http://[::1]:54777/PTwjm8Ii8qg=/'); await discoverer.cancel(); - await logReader.dispose(); + logReader.dispose(); }); testUsingContext('ipv6 with Ascii Escape code', () async { @@ -332,7 +248,7 @@ void main() { expect('$uri', 'http://[::1]:54777/PTwjm8Ii8qg=/'); await discoverer.cancel(); - await logReader.dispose(); + logReader.dispose(); }); }); }); 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 00d0e28385..ceeb55d0fe 100644 --- a/packages/flutter_tools/test/general.shard/resident_runner_test.dart +++ b/packages/flutter_tools/test/general.shard/resident_runner_test.dart @@ -95,7 +95,9 @@ void main() { when(mockFlutterView.uiIsolate).thenReturn(mockIsolate); when(mockFlutterView.runFromSource(any, any, any)).thenAnswer((Invocation invocation) async {}); when(mockFlutterDevice.stopEchoingDeviceLog()).thenAnswer((Invocation invocation) async { }); - when(mockFlutterDevice.observatoryUris).thenAnswer((_) => Stream.value(testUri)); + when(mockFlutterDevice.observatoryUris).thenReturn([ + testUri, + ]); when(mockFlutterDevice.connect( reloadSources: anyNamed('reloadSources'), restart: anyNamed('restart'), @@ -634,7 +636,7 @@ void main() { final TestFlutterDevice flutterDevice = TestFlutterDevice( mockDevice, [], - observatoryUris: Stream.value(testUri), + observatoryUris: [ testUri ] ); await flutterDevice.connect(); @@ -655,7 +657,7 @@ class MockUsage extends Mock implements Usage {} class MockProcessManager extends Mock implements ProcessManager {} class MockServiceEvent extends Mock implements ServiceEvent {} class TestFlutterDevice extends FlutterDevice { - TestFlutterDevice(Device device, this.views, { Stream observatoryUris }) + TestFlutterDevice(Device device, this.views, { List observatoryUris }) : super(device, buildMode: BuildMode.debug, trackWidgetCreation: false) { _observatoryUris = observatoryUris; } @@ -664,8 +666,8 @@ class TestFlutterDevice extends FlutterDevice { final List views; @override - Stream get observatoryUris => _observatoryUris; - Stream _observatoryUris; + List get observatoryUris => _observatoryUris; + List _observatoryUris; } class ThrowingForwardingFileSystem extends ForwardingFileSystem { diff --git a/packages/flutter_tools/test/src/mocks.dart b/packages/flutter_tools/test/src/mocks.dart index cde2e80211..9f6c9f1b16 100644 --- a/packages/flutter_tools/test/src/mocks.dart +++ b/packages/flutter_tools/test/src/mocks.dart @@ -533,12 +533,6 @@ class MockAndroidDevice extends Mock implements AndroidDevice { @override bool isSupported() => true; - @override - bool get supportsHotRestart => true; - - @override - bool get supportsFlutterExit => false; - @override bool isSupportedForProject(FlutterProject flutterProject) => true; } @@ -569,33 +563,16 @@ class MockDeviceLogReader extends DeviceLogReader { @override String get name => 'MockLogReader'; - StreamController _cachedLinesController; - - final List _lineQueue = []; - StreamController get _linesController { - _cachedLinesController ??= StreamController - .broadcast(onListen: () { - _lineQueue.forEach(_linesController.add); - _lineQueue.clear(); - }); - return _cachedLinesController; - } + final StreamController _linesController = StreamController.broadcast(); @override Stream get logLines => _linesController.stream; - void addLine(String line) { - if (_linesController.hasListener) { - _linesController.add(line); - } else { - _lineQueue.add(line); - } - } + void addLine(String line) => _linesController.add(line); @override - Future dispose() async { - _lineQueue.clear(); - await _linesController.close(); + void dispose() { + _linesController.close(); } }