Retry getting tabs in chrome launcher on ConnectionException (#104218)
This commit is contained in:
@@ -95,6 +95,8 @@ bool androidManifestHasNameVariable(final Directory projectDir) {
|
||||
document = XmlDocument.parse(manifestFile.readAsStringSync());
|
||||
} on XmlParserException {
|
||||
return false;
|
||||
} on XmlTagException {
|
||||
return false;
|
||||
} on FileSystemException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -226,7 +226,7 @@ class ChromiumLauncher {
|
||||
_cacheUserSessionInformation(userDataDir, cacheDir);
|
||||
}));
|
||||
}
|
||||
return _connect(Chromium(
|
||||
return connect(Chromium(
|
||||
port,
|
||||
ChromeConnection('localhost', port),
|
||||
url: url,
|
||||
@@ -393,13 +393,15 @@ class ChromiumLauncher {
|
||||
!directory.path.endsWith('GPUCache');
|
||||
}
|
||||
|
||||
Future<Chromium> _connect(Chromium chrome, bool skipCheck) async {
|
||||
/// Connect to the [chrome] instance, testing the connection if
|
||||
/// [skipCheck] is set to false.
|
||||
@visibleForTesting
|
||||
Future<Chromium> connect(Chromium chrome, bool skipCheck) async {
|
||||
// The connection is lazy. Try a simple call to make sure the provided
|
||||
// connection is valid.
|
||||
if (!skipCheck) {
|
||||
try {
|
||||
await chrome.chromeConnection.getTab(
|
||||
(ChromeTab tab) => true, retryFor: const Duration(seconds: 2));
|
||||
await _getFirstTab(chrome);
|
||||
} on Exception catch (error, stackTrace) {
|
||||
_logger.printError('$error', stackTrace: stackTrace);
|
||||
await chrome.close();
|
||||
@@ -411,6 +413,36 @@ class ChromiumLauncher {
|
||||
return chrome;
|
||||
}
|
||||
|
||||
/// Gets the first [chrome] tab.
|
||||
///
|
||||
/// Note: Retry getting tabs from Chrome for a few seconds and retry finding
|
||||
/// the tab a few times. This prevents flakes caused by Chrome not returning
|
||||
/// correct output if the call was too close to the start.
|
||||
Future<ChromeTab?> _getFirstTab(Chromium chrome) async {
|
||||
const Duration retryFor = Duration(seconds: 2);
|
||||
const int attempts = 5;
|
||||
|
||||
for (int i = 1; i <= attempts; i++) {
|
||||
try {
|
||||
final List<ChromeTab> tabs =
|
||||
await chrome.chromeConnection.getTabs(retryFor: retryFor);
|
||||
|
||||
if (tabs.isNotEmpty) {
|
||||
return tabs.first;
|
||||
}
|
||||
if (i == attempts) {
|
||||
return null;
|
||||
}
|
||||
} on ConnectionException catch (_) {
|
||||
if (i == attempts) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
await Future<void>.delayed(const Duration(milliseconds: 25));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<Chromium> get connectedInstance => currentCompleter.future;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user