From d53115ab2e9f1f5e9b82bd1923bddf9c74e77c05 Mon Sep 17 00:00:00 2001 From: done Date: Sat, 4 May 2019 22:13:25 +0900 Subject: [PATCH] fix FlutterDriver timeout (#31824) --- .../flutter_driver/lib/src/driver/driver.dart | 23 ++++++------------- .../test/flutter_driver_test.dart | 21 +++++++++++++++-- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/packages/flutter_driver/lib/src/driver/driver.dart b/packages/flutter_driver/lib/src/driver/driver.dart index 8929118dcc..bf58352d0d 100644 --- a/packages/flutter_driver/lib/src/driver/driver.dart +++ b/packages/flutter_driver/lib/src/driver/driver.dart @@ -63,7 +63,8 @@ const List _defaultStreams = [TimelineStream.all /// How long to wait before showing a message saying that /// things seem to be taking a long time. -const Duration _kUnusuallyLongTimeout = Duration(seconds: 5); +@visibleForTesting +const Duration kUnusuallyLongTimeout = Duration(seconds: 5); /// The amount of time we wait prior to making the next attempt to connect to /// the VM service. @@ -102,16 +103,6 @@ Future _warnIfSlow({ return future..timeout(timeout, onTimeout: () { _log.warning(message); }); } -Duration _maxDuration(Duration a, Duration b) { - if (a == null) - return b; - if (b == null) - return a; - if (a > b) - return a; - return b; -} - /// A convenient accessor to frequently used finders. /// /// Examples: @@ -334,7 +325,7 @@ class FlutterDriver { // register it. If that happens, show a message but continue waiting. await _warnIfSlow( future: whenServiceExtensionReady, - timeout: _kUnusuallyLongTimeout, + timeout: kUnusuallyLongTimeout, message: 'Flutter Driver extension is taking a long time to become available. ' 'Ensure your test app (often "lib/main.dart") imports ' '"package:flutter_driver/driver_extension.dart" and ' @@ -416,7 +407,7 @@ class FlutterDriver { ).then>((Object value) => value); response = await _warnIfSlow>( future: future, - timeout: _maxDuration(command.timeout, _kUnusuallyLongTimeout), + timeout: command.timeout ?? kUnusuallyLongTimeout, message: '${command.kind} message is taking a long time to complete...', ); _logCommunication('<<< $response'); @@ -736,7 +727,7 @@ class FlutterDriver { /// operation. Future startTracing({ List streams = _defaultStreams, - Duration timeout = _kUnusuallyLongTimeout, + Duration timeout = kUnusuallyLongTimeout, }) async { assert(streams != null && streams.isNotEmpty); assert(timeout != null); @@ -763,7 +754,7 @@ class FlutterDriver { /// operation exceeds the specified timeout; it does not actually cancel the /// operation. Future stopTracingAndDownloadTimeline({ - Duration timeout = _kUnusuallyLongTimeout, + Duration timeout = kUnusuallyLongTimeout, }) async { assert(timeout != null); try { @@ -832,7 +823,7 @@ class FlutterDriver { /// operation exceeds the specified timeout; it does not actually cancel the /// operation. Future clearTimeline({ - Duration timeout = _kUnusuallyLongTimeout, + Duration timeout = kUnusuallyLongTimeout, }) async { assert(timeout != null); try { diff --git a/packages/flutter_driver/test/flutter_driver_test.dart b/packages/flutter_driver/test/flutter_driver_test.dart index c532f515ca..c143354f3d 100644 --- a/packages/flutter_driver/test/flutter_driver_test.dart +++ b/packages/flutter_driver/test/flutter_driver_test.dart @@ -386,7 +386,7 @@ void main() { }); group('sendCommand error conditions', () { - test('local timeout', () async { + test('local default timeout', () async { final List log = []; final StreamSubscription logSub = flutterDriverLog.listen((LogRecord s) => log.add(s.toString())); when(mockIsolate.invokeExtension(any, any)).thenAnswer((Invocation i) { @@ -396,7 +396,24 @@ void main() { FakeAsync().run((FakeAsync time) { driver.waitFor(find.byTooltip('foo')); expect(log, []); - time.elapse(const Duration(hours: 1)); + time.elapse(kUnusuallyLongTimeout); + }); + expect(log, ['[warning] FlutterDriver: waitFor message is taking a long time to complete...']); + await logSub.cancel(); + }); + + test('local custom timeout', () async { + final List log = []; + final StreamSubscription logSub = flutterDriverLog.listen((LogRecord s) => log.add(s.toString())); + when(mockIsolate.invokeExtension(any, any)).thenAnswer((Invocation i) { + // completer never completed to trigger timeout + return Completer>().future; + }); + FakeAsync().run((FakeAsync time) { + final Duration customTimeout = kUnusuallyLongTimeout - const Duration(seconds: 1); + driver.waitFor(find.byTooltip('foo'), timeout: customTimeout); + expect(log, []); + time.elapse(customTimeout); }); expect(log, ['[warning] FlutterDriver: waitFor message is taking a long time to complete...']); await logSub.cancel();