Retry getting tabs in chrome launcher on ConnectionException (#104218)

This commit is contained in:
Anna Gringauze
2022-05-26 15:20:11 -07:00
committed by GitHub
parent b2eafd735d
commit 5cd979e441
70 changed files with 537 additions and 447 deletions

View File

@@ -1261,6 +1261,11 @@ class FakeChromeConnection extends Fake implements ChromeConnection {
Future<ChromeTab> getTab(bool Function(ChromeTab tab) accept, {Duration retryFor}) async {
return tabs.firstWhere(accept);
}
@override
Future<List<ChromeTab>> getTabs({Duration retryFor}) async {
return tabs;
}
}
class FakeChromeTab extends Fake implements ChromeTab {
@@ -1314,6 +1319,11 @@ class TestChromiumLauncher implements ChromiumLauncher {
Future<Chromium> launch(String url, {bool headless = false, int debugPort, bool skipCheck = false, Directory cacheDir}) async {
return currentCompleter.future;
}
@override
Future<Chromium> connect(Chromium chrome, bool skipCheck) {
return currentCompleter.future;
}
}
class FakeFlutterDevice extends Fake implements FlutterDevice {

View File

@@ -392,4 +392,9 @@ class TestChromiumLauncher implements ChromiumLauncher {
Future<Chromium> launch(String url, {bool headless = false, int? debugPort, bool skipCheck = false, Directory? cacheDir}) async {
return currentCompleter.future;
}
@override
Future<Chromium> connect(Chromium chrome, bool skipCheck) {
return currentCompleter.future;
}
}

View File

@@ -11,6 +11,8 @@ import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/os.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/web/chrome.dart';
import 'package:test/fake.dart';
import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart';
import '../src/common.dart';
import '../src/fake_process_manager.dart';
@@ -556,6 +558,48 @@ void main() {
);
expect(logger.errorText, contains('SocketException'));
});
test('can recover if getTabs throws a connection exception', () async {
final BufferLogger logger = BufferLogger.test();
final FakeChromeConnection chromeConnection = FakeChromeConnection(maxRetries: 4);
final ChromiumLauncher chromiumLauncher = ChromiumLauncher(
fileSystem: fileSystem,
platform: platform,
processManager: processManager,
operatingSystemUtils: operatingSystemUtils,
browserFinder: findChromeExecutable,
logger: logger,
);
final Chromium chrome = Chromium(0, chromeConnection, chromiumLauncher: chromiumLauncher);
expect(await chromiumLauncher.connect(chrome, false), equals(chrome));
expect(logger.errorText, isEmpty);
});
test('exits if getTabs throws a connection exception consistently', () async {
final BufferLogger logger = BufferLogger.test();
final FakeChromeConnection chromeConnection = FakeChromeConnection();
final ChromiumLauncher chromiumLauncher = ChromiumLauncher(
fileSystem: fileSystem,
platform: platform,
processManager: processManager,
operatingSystemUtils: operatingSystemUtils,
browserFinder: findChromeExecutable,
logger: logger,
);
final Chromium chrome = Chromium(0, chromeConnection, chromiumLauncher: chromiumLauncher);
await expectToolExitLater(
chromiumLauncher.connect(chrome, false),
allOf(
contains('Unable to connect to Chrome debug port'),
contains('incorrect format'),
));
expect(logger.errorText,
allOf(
contains('incorrect format'),
contains('OK'),
contains('<html> ...'),
));
});
}
Future<Chromium> _testLaunchChrome(String userDataDir, FakeProcessManager processManager, ChromiumLauncher chromeLauncher) {
@@ -575,3 +619,36 @@ Future<Chromium> _testLaunchChrome(String userDataDir, FakeProcessManager proces
skipCheck: true,
);
}
/// Fake chrome connection that fails to get tabs a few times.
class FakeChromeConnection extends Fake implements ChromeConnection {
/// Create a connection that throws a connection exception on first
/// [maxRetries] calls to [getTabs].
/// If [maxRetries] is `null`, [getTabs] calls never succeed.
FakeChromeConnection({this.maxRetries}): _retries = 0;
final List<ChromeTab> tabs = <ChromeTab>[];
final int? maxRetries;
int _retries;
@override
Future<ChromeTab?> getTab(bool Function(ChromeTab tab) accept, {Duration? retryFor}) async {
return tabs.firstWhere(accept);
}
@override
Future<List<ChromeTab>> getTabs({Duration? retryFor}) async {
_retries ++;
if (maxRetries == null || _retries < maxRetries!) {
throw ConnectionException(
formatException: const FormatException('incorrect format'),
responseStatus: 'OK,',
responseBody: '<html> ...');
}
return tabs;
}
@override
void close() {}
}