From ff539c0a58857c1bb04f12a9d5a8fff54290b676 Mon Sep 17 00:00:00 2001 From: Jon Samwell Date: Wed, 8 Jan 2020 06:53:12 +1100 Subject: [PATCH] fix(driver): added retry logic to the Futter driver connect call to handle the seemingly random connection failures --- CHANGELOG.md | 3 ++ .../flutter/flutter_test_configuration.dart | 33 +++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2939cd1..f3ae176 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## [1.1.7+3] - 08/01/2019 +* Added retry logic to the Futter driver connect call to handle the seemingly random connection failures + ## [1.1.7+2] - 07/01/2019 * Increased the Flutter driver reconnection delay to try and overcome some driver to app connection issues on slower machines diff --git a/lib/src/flutter/flutter_test_configuration.dart b/lib/src/flutter/flutter_test_configuration.dart index 0453c59..2d03f8b 100644 --- a/lib/src/flutter/flutter_test_configuration.dart +++ b/lib/src/flutter/flutter_test_configuration.dart @@ -59,15 +59,17 @@ class FlutterTestConfiguration extends TestConfiguration { /// Defaults to 2 seconds Duration flutterDriverReconnectionDelay = const Duration(seconds: 2); + /// The maximum times the flutter driver can try and connect to the running app + /// Defaults to 3 + int flutterDriverMaxConnectionAttemps = 3; + void setObservatoryDebuggerUri(String uri) => _observatoryDebuggerUri = uri; Future createFlutterDriver([String dartVmServiceUrl]) async { dartVmServiceUrl = (dartVmServiceUrl ?? _observatoryDebuggerUri) ?? Platform.environment['VM_SERVICE_URL']; - return await FlutterDriver.connect( - dartVmServiceUrl: dartVmServiceUrl, - ); + return await _attemptDriverConnection(dartVmServiceUrl, 1, 3); } Future createFlutterWorld( @@ -101,4 +103,29 @@ class FlutterTestConfiguration extends TestConfiguration { RestartAppStep() ]); } + + Future _attemptDriverConnection( + String dartVmServiceUrl, + int attempt, + int maxAttemps, + ) async { + try { + return await FlutterDriver.connect( + dartVmServiceUrl: dartVmServiceUrl, + ); + } catch (e) { + if (attempt > maxAttemps) { + rethrow; + } else { + print(e); + await Future.delayed(flutterDriverReconnectionDelay); + + return _attemptDriverConnection( + dartVmServiceUrl, + attempt + 1, + maxAttemps, + ); + } + } + } }