Skip to content

Commit d88e692

Browse files
authored
fix test chrome.close can recover if getTab throws a StateError (flutter#154889)
Fixes flutter#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 c9a5fda..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.
1 parent 24d0b1d commit d88e692

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

packages/flutter_tools/test/web.shard/chrome_test.dart

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -800,9 +800,10 @@ void main() {
800800

801801
testWithoutContext('chrome.close can recover if getTab throws a StateError', () async {
802802
final BufferLogger logger = BufferLogger.test();
803-
final FakeChromeConnection chromeConnection = FakeChromeConnection(
804-
maxRetries: 4,
805-
error: StateError('Client is closed.'),
803+
final FakeChromeConnectionWithTab chromeConnection = FakeChromeConnectionWithTab(
804+
onGetTab: () {
805+
throw StateError('Client is closed.');
806+
},
806807
);
807808
final ChromiumLauncher chromiumLauncher = ChromiumLauncher(
808809
fileSystem: fileSystem,
@@ -813,7 +814,14 @@ void main() {
813814
logger: logger,
814815
);
815816
final FakeProcess process = FakeProcess();
816-
final Chromium chrome = Chromium(0, chromeConnection, chromiumLauncher: chromiumLauncher, process: process, logger: logger,);
817+
final Chromium chrome = Chromium(
818+
0,
819+
chromeConnection,
820+
chromiumLauncher: chromiumLauncher,
821+
process: process,
822+
logger: logger,
823+
);
824+
await chromiumLauncher.connect(chrome, false);
817825
await chrome.close();
818826
expect(logger.errorText, isEmpty);
819827
});
@@ -944,14 +952,19 @@ typedef OnSendCommand = void Function(String);
944952

945953
/// Fake chrome connection that returns a tab.
946954
class FakeChromeConnectionWithTab extends Fake implements ChromeConnection {
947-
FakeChromeConnectionWithTab({OnSendCommand? onSendCommand, bool throwWebSocketException = false})
948-
: _tab = FakeChromeTab(onSendCommand, throwWebSocketException);
955+
FakeChromeConnectionWithTab({
956+
OnSendCommand? onSendCommand,
957+
this.onGetTab,
958+
bool throwWebSocketException = false,
959+
}) : _tab = FakeChromeTab(onSendCommand, throwWebSocketException);
949960

950961
final FakeChromeTab _tab;
962+
void Function()? onGetTab;
951963
bool throwSocketExceptions = false;
952964

953965
@override
954966
Future<ChromeTab?> getTab(bool Function(ChromeTab tab) accept, {Duration? retryFor}) async {
967+
onGetTab?.call();
955968
if (throwSocketExceptions) {
956969
throw const io.SocketException('test');
957970
}

0 commit comments

Comments
 (0)