diff --git a/packages/flutter_tools/lib/src/ios/ios_deploy.dart b/packages/flutter_tools/lib/src/ios/ios_deploy.dart index 7e6e79fe7f..553d8b388b 100644 --- a/packages/flutter_tools/lib/src/ios/ios_deploy.dart +++ b/packages/flutter_tools/lib/src/ios/ios_deploy.dart @@ -395,11 +395,11 @@ class IOSDeployDebugger { _monitorIOSDeployFailure(line, _logger); _logger.printTrace(line); }); - unawaited(_iosDeployProcess!.exitCode.then((int status) { + unawaited(_iosDeployProcess!.exitCode.then((int status) async { _logger.printTrace('ios-deploy exited with code $exitCode'); _debuggerState = _IOSDeployDebuggerState.detached; - unawaited(stdoutSubscription.cancel()); - unawaited(stderrSubscription.cancel()); + await stdoutSubscription.cancel(); + await stderrSubscription.cancel(); }).whenComplete(() async { if (_debuggerOutput.hasListener) { // Tell listeners the process died. diff --git a/packages/flutter_tools/test/general.shard/ios/ios_deploy_test.dart b/packages/flutter_tools/test/general.shard/ios/ios_deploy_test.dart index e9c9c2c61b..3da7d21b95 100644 --- a/packages/flutter_tools/test/general.shard/ios/ios_deploy_test.dart +++ b/packages/flutter_tools/test/general.shard/ios/ios_deploy_test.dart @@ -75,8 +75,9 @@ void main () { interfaceType: IOSDeviceConnectionInterface.network, ); + expect(iosDeployDebugger.logLines, emits('Did finish launching.')); expect(await iosDeployDebugger.launchAndAttach(), isTrue); - expect(await iosDeployDebugger.logLines.toList(), ['Did finish launching.']); + await iosDeployDebugger.logLines.drain(); expect(processManager, hasNoRemainingExpectations); expect(appDeltaDirectory, exists); }); @@ -92,7 +93,6 @@ void main () { testWithoutContext('debugger attached and stopped', () async { final StreamController> stdin = StreamController>(); - final Stream stdinStream = stdin.stream.transform(const Utf8Decoder()); final FakeProcessManager processManager = FakeProcessManager.list([ FakeCommand( command: const ['ios-deploy'], @@ -108,24 +108,47 @@ void main () { final Stream logLines = iosDeployDebugger.logLines ..listen(receivedLogLines.add); - expect(await iosDeployDebugger.launchAndAttach(), isTrue); - await logLines.toList(); - expect(receivedLogLines, [ + expect(iosDeployDebugger.logLines, emitsInOrder([ 'success', // ignore first "success" from lldb, but log subsequent ones from real logging. 'Log on attach1', 'Log on attach2', '', '', 'Log after process stop' - ]); - expect(logger.traceText, contains('PROCESS_STOPPED')); - expect(logger.traceText, contains('thread backtrace all')); - expect(logger.traceText, contains('* thread #1')); - expect(await stdinStream.take(3).toList(), [ + ])); + expect(stdin.stream.transform(const Utf8Decoder()), emitsInOrder([ 'thread backtrace all', '\n', 'process detach', + ])); + expect(await iosDeployDebugger.launchAndAttach(), isTrue); + await logLines.drain(); + + expect(logger.traceText, contains('PROCESS_STOPPED')); + expect(logger.traceText, contains('thread backtrace all')); + expect(logger.traceText, contains('* thread #1')); + }); + + testWithoutContext('handle processing logging after process exit', () async { + final StreamController> stdin = StreamController>(); + // Make sure we don't hit a race where logging processed after the process exits + // causes listeners to receive logging on the closed logLines stream. + final FakeProcessManager processManager = FakeProcessManager.list([ + FakeCommand( + command: const ['ios-deploy'], + stdout: 'stdout: "(lldb) run\r\nsuccess\r\n', + stdin: IOSink(stdin.sink), + outputFollowsExit: true, + ), ]); + final IOSDeployDebugger iosDeployDebugger = IOSDeployDebugger.test( + processManager: processManager, + logger: logger, + ); + + expect(iosDeployDebugger.logLines, emitsDone); + expect(await iosDeployDebugger.launchAndAttach(), isFalse); + await iosDeployDebugger.logLines.drain(); }); testWithoutContext('app exit', () async { @@ -139,21 +162,17 @@ void main () { processManager: processManager, logger: logger, ); - final List receivedLogLines = []; - final Stream logLines = iosDeployDebugger.logLines - ..listen(receivedLogLines.add); - - expect(await iosDeployDebugger.launchAndAttach(), isTrue); - await logLines.toList(); - expect(receivedLogLines, [ + expect(iosDeployDebugger.logLines, emitsInOrder([ 'Log on attach', 'Log after process exit', - ]); + ])); + + expect(await iosDeployDebugger.launchAndAttach(), isTrue); + await iosDeployDebugger.logLines.drain(); }); testWithoutContext('app crash', () async { final StreamController> stdin = StreamController>(); - final Stream stdinStream = stdin.stream.transform(const Utf8Decoder()); final FakeProcessManager processManager = FakeProcessManager.list([ FakeCommand( command: const ['ios-deploy'], @@ -166,24 +185,24 @@ void main () { processManager: processManager, logger: logger, ); - final List receivedLogLines = []; - final Stream logLines = iosDeployDebugger.logLines - ..listen(receivedLogLines.add); - expect(await iosDeployDebugger.launchAndAttach(), isTrue); - await logLines.toList(); - expect(receivedLogLines, [ + expect(iosDeployDebugger.logLines, emitsInOrder([ 'Log on attach', '* thread #1, stop reason = Assertion failed:', - ]); - expect(logger.traceText, contains('Process 6156 stopped')); - expect(logger.traceText, contains('thread backtrace all')); - expect(logger.traceText, contains('* thread #1')); - expect(await stdinStream.take(3).toList(), [ + ])); + + expect(stdin.stream.transform(const Utf8Decoder()), emitsInOrder([ 'thread backtrace all', '\n', 'process detach', - ]); + ])); + + expect(await iosDeployDebugger.launchAndAttach(), isTrue); + await iosDeployDebugger.logLines.drain(); + + expect(logger.traceText, contains('Process 6156 stopped')); + expect(logger.traceText, contains('thread backtrace all')); + expect(logger.traceText, contains('* thread #1')); }); testWithoutContext('attach failed', () async { @@ -198,15 +217,12 @@ void main () { processManager: processManager, logger: logger, ); - final List receivedLogLines = []; - final Stream logLines = iosDeployDebugger.logLines - ..listen(receivedLogLines.add); - - expect(await iosDeployDebugger.launchAndAttach(), isFalse); - await logLines.toList(); // Debugger lines are double spaced, separated by an extra \r\n. Skip the extra lines. // Still include empty lines other than the extra added newlines. - expect(receivedLogLines, isEmpty); + expect(iosDeployDebugger.logLines, emitsDone); + + expect(await iosDeployDebugger.launchAndAttach(), isFalse); + await iosDeployDebugger.logLines.drain(); }); testWithoutContext('no provisioning profile 1, stdout', () async { @@ -273,7 +289,6 @@ void main () { testWithoutContext('detach', () async { final StreamController> stdin = StreamController>(); - final Stream stdinStream = stdin.stream.transform(const Utf8Decoder()); final FakeProcessManager processManager = FakeProcessManager.list([ FakeCommand( command: const [ @@ -286,9 +301,9 @@ void main () { final IOSDeployDebugger iosDeployDebugger = IOSDeployDebugger.test( processManager: processManager, ); + expect(stdin.stream.transform(const Utf8Decoder()), emits('process detach')); await iosDeployDebugger.launchAndAttach(); iosDeployDebugger.detach(); - expect(await stdinStream.first, 'process detach'); }); testWithoutContext('stop with backtrace', () async {