From d88e69289562d11402e14bc879a2bb69e8796100 Mon Sep 17 00:00:00 2001 From: Andrew Kolos Date: Tue, 10 Sep 2024 10:23:56 -0700 Subject: [PATCH] fix test `chrome.close can recover if getTab throws a StateError` (#154889) Fixes https://github.com/flutter/flutter/issues/154857. Does so by: * adding `await chromiumLauncher.connect(chrome, false);` before the `close` call to make sure we enter[ the block ](https://github.com/flutter/flutter/blob/9cd2fc90af406a5ae798a6e099f3fca3eebd0da0/packages/flutter_tools/lib/src/web/chrome.dart#L521-L535)that actually tries to close chromium * adding an `onGetTab` callback to `FakeChromeConnectionWithTab`, which the test now uses to throw a StateError upon `getTab` getting called. ## How I verified this change 1. Change `Chromium.close` from using the safer `getChromeTabGuarded` function to using the previous method of calling `ChromeConnection.getTab` directly. Do so by applying this diff: ```diff diff --git a/packages/flutter_tools/lib/src/web/chrome.dart b/packages/flutter_tools/lib/src/web/chrome.dart index c9a5fdab81..81bc246ff9 100644 --- a/packages/flutter_tools/lib/src/web/chrome.dart +++ b/packages/flutter_tools/lib/src/web/chrome.dart @@ -520,7 +520,7 @@ class Chromium { Duration sigtermDelay = Duration.zero; if (_hasValidChromeConnection) { try { - final ChromeTab? tab = await getChromeTabGuarded(chromeConnection, + final ChromeTab? tab = await chromeConnection.getTab( (_) => true, retryFor: const Duration(seconds: 1)); if (tab != null) { final WipConnection wipConnection = await tab.connect(); ``` 2. Then, run the test, which should correctly fail: ``` dart test test/web.shard/chrome_test.dart --name="chrome.close can recover if getTab throws a StateError"` ``` 3. Revert the change from step 1 and run again. The test should now pass. --- .../test/web.shard/chrome_test.dart | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/packages/flutter_tools/test/web.shard/chrome_test.dart b/packages/flutter_tools/test/web.shard/chrome_test.dart index 3fa87afcc8..c56ad58c9a 100644 --- a/packages/flutter_tools/test/web.shard/chrome_test.dart +++ b/packages/flutter_tools/test/web.shard/chrome_test.dart @@ -800,9 +800,10 @@ void main() { testWithoutContext('chrome.close can recover if getTab throws a StateError', () async { final BufferLogger logger = BufferLogger.test(); - final FakeChromeConnection chromeConnection = FakeChromeConnection( - maxRetries: 4, - error: StateError('Client is closed.'), + final FakeChromeConnectionWithTab chromeConnection = FakeChromeConnectionWithTab( + onGetTab: () { + throw StateError('Client is closed.'); + }, ); final ChromiumLauncher chromiumLauncher = ChromiumLauncher( fileSystem: fileSystem, @@ -813,7 +814,14 @@ void main() { logger: logger, ); final FakeProcess process = FakeProcess(); - final Chromium chrome = Chromium(0, chromeConnection, chromiumLauncher: chromiumLauncher, process: process, logger: logger,); + final Chromium chrome = Chromium( + 0, + chromeConnection, + chromiumLauncher: chromiumLauncher, + process: process, + logger: logger, + ); + await chromiumLauncher.connect(chrome, false); await chrome.close(); expect(logger.errorText, isEmpty); }); @@ -944,14 +952,19 @@ typedef OnSendCommand = void Function(String); /// Fake chrome connection that returns a tab. class FakeChromeConnectionWithTab extends Fake implements ChromeConnection { - FakeChromeConnectionWithTab({OnSendCommand? onSendCommand, bool throwWebSocketException = false}) - : _tab = FakeChromeTab(onSendCommand, throwWebSocketException); + FakeChromeConnectionWithTab({ + OnSendCommand? onSendCommand, + this.onGetTab, + bool throwWebSocketException = false, + }) : _tab = FakeChromeTab(onSendCommand, throwWebSocketException); final FakeChromeTab _tab; + void Function()? onGetTab; bool throwSocketExceptions = false; @override Future getTab(bool Function(ChromeTab tab) accept, {Duration? retryFor}) async { + onGetTab?.call(); if (throwSocketExceptions) { throw const io.SocketException('test'); }