diff --git a/packages/flutter_tools/test/integration/test_driver.dart b/packages/flutter_tools/test/integration/test_driver.dart index b45ce7e8f6..a95f833ae7 100644 --- a/packages/flutter_tools/test/integration/test_driver.dart +++ b/packages/flutter_tools/test/integration/test_driver.dart @@ -19,20 +19,14 @@ import '../src/common.dart'; // Set this to true for debugging to get JSON written to stdout. const bool _printJsonAndStderr = false; const Duration defaultTimeout = const Duration(seconds: 20); - -String debugPrint(String msg) { - const int maxLength = 200; - if (_printJsonAndStderr) { - print(msg.length > maxLength ? msg.substring(0, maxLength) + '...' : msg); - } - return msg; -} +const Duration appStartTimeout = const Duration(seconds: 60); class FlutterTestDriver { Directory _projectFolder; Process _proc; final StreamController _stdout = new StreamController.broadcast(); final StreamController _stderr = new StreamController.broadcast(); + final StreamController _allMessages = new StreamController.broadcast(); final StringBuffer _errorBuffer = new StringBuffer(); String _lastResponse; String _currentRunningAppId; @@ -42,6 +36,17 @@ class FlutterTestDriver { VMServiceClient vmService; String get lastErrorInfo => _errorBuffer.toString(); + String _debugPrint(String msg) { + const int maxLength = 500; + final String truncatedMsg = + msg.length > maxLength ? msg.substring(0, maxLength) + '...' : msg; + _allMessages.add(truncatedMsg); + if (_printJsonAndStderr) { + print(truncatedMsg); + } + return msg; +} + // TODO(dantup): Is there a better way than spawning a proc? This breaks debugging.. // However, there's a lot of logic inside RunCommand that wouldn't be good // to duplicate here. @@ -54,24 +59,28 @@ class FlutterTestDriver { _stderr.stream.listen(_errorBuffer.writeln); // This is just debug printing to aid running/debugging tests locally. - _stdout.stream.listen(debugPrint); - _stderr.stream.listen(debugPrint); + _stdout.stream.listen(_debugPrint); + _stderr.stream.listen(_debugPrint); // Set this up now, but we don't wait it yet. We want to make sure we don't // miss it while waiting for debugPort below. - final Future> started = _waitFor(event: 'app.started'); + final Future> started = _waitFor(event: 'app.started', + timeout: appStartTimeout); if (withDebugger) { - final Future> debugPort = _waitFor(event: 'app.debugPort'); + final Future> debugPort = _waitFor(event: 'app.debugPort', + timeout: appStartTimeout); final String wsUriString = (await debugPort)['params']['wsUri']; + // Ensure the app is started before we try to connect to it. + await started; final Uri uri = Uri.parse(wsUriString); // Proxy the stream/sink for the VM Client so we can debugPrint it. final StreamChannel channel = new IOWebSocketChannel.connect(uri) .cast() - .changeStream((Stream stream) => stream.map(debugPrint)) + .changeStream((Stream stream) => stream.map(_debugPrint)) .changeSink((StreamSink sink) => new StreamController() - ..stream.listen((String s) => sink.add(debugPrint(s)))); + ..stream.listen((String s) => sink.add(_debugPrint(s)))); vmService = new VMServiceClient(channel); // Because we start paused, resume so the app is in a "running" state as @@ -126,7 +135,7 @@ class FlutterTestDriver { 'flutter-tester', '--start-paused', ]; - debugPrint('Spawning $command in ${projectDir.path}'); + _debugPrint('Spawning $command in ${projectDir.path}'); const ProcessManager _processManager = const LocalProcessManager(); return _processManager.start( @@ -139,16 +148,16 @@ class FlutterTestDriver { Future addBreakpoint(String path, int line) async { final VM vm = await vmService.getVM(); final VMIsolate isolate = await vm.isolates.first.load(); - debugPrint('Sending breakpoint for $path:$line'); + _debugPrint('Sending breakpoint for $path:$line'); await isolate.addBreakpoint(path, line); } Future waitForPause() async { final VM vm = await vmService.getVM(); final VMIsolate isolate = await vm.isolates.first.load(); - debugPrint('Waiting for isolate to pause'); - await isolate.waitUntilPaused() - .timeout(defaultTimeout, onTimeout: () => throw 'Isolate did not pause'); + _debugPrint('Waiting for isolate to pause'); + await _timeoutWithMessages(isolate.waitUntilPaused, + message: 'Isolate did not pause'); return isolate.load(); } @@ -160,9 +169,9 @@ class FlutterTestDriver { Future _resume({VMStep step, bool wait = true}) async { final VM vm = await vmService.getVM(); final VMIsolate isolate = await vm.isolates.first.load(); - debugPrint('Sending resume ($step)'); - await isolate.resume(step: step) - .timeout(defaultTimeout, onTimeout: () => throw 'Isolate did not respond to resume ($step)'); + _debugPrint('Sending resume ($step)'); + await _timeoutWithMessages(() => isolate.resume(step: step), + message: 'Isolate did not respond to resume ($step)'); return wait ? waitForPause() : null; } @@ -182,8 +191,8 @@ class FlutterTestDriver { Future evaluateExpression(String expression) async { final VMFrame topFrame = await getTopStackFrame(); - return topFrame.evaluate(expression) - .timeout(defaultTimeout, onTimeout: () => throw 'Timed out evaluating expression ($expression)'); + return _timeoutWithMessages(() => topFrame.evaluate(expression), + message: 'Timed out evaluating expression ($expression)'); } Future getTopStackFrame() async { @@ -202,13 +211,7 @@ class FlutterTestDriver { return script.sourceLocation(frame.location.token); } - Future> _waitFor({String event, int id}) async { - // Capture output to a buffer so if we don't get the repsonse we want we can show - // the output that did arrive in the timeout error. - final StringBuffer messages = new StringBuffer(); - _stdout.stream.listen(messages.writeln); - _stderr.stream.listen(messages.writeln); - + Future> _waitFor({String event, int id, Duration timeout}) async { final Completer> response = new Completer>(); final StreamSubscription sub = _stdout.stream.listen((String line) { final dynamic json = _parseFlutterResponse(line); @@ -221,12 +224,29 @@ class FlutterTestDriver { } }); - return response.future.timeout(defaultTimeout, onTimeout: () { - if (event != null) - throw 'Did not receive expected $event event.\nDid get:\n${messages.toString()}'; - else if (id != null) - throw 'Did not receive response to request "$id".\nDid get:\n${messages.toString()}'; - }).whenComplete(() => sub.cancel()); + return _timeoutWithMessages(() => response.future, + timeout: timeout, + message: event != null + ? 'Did not receive expected $event event.' + : 'Did not receive response to request "$id".') + .whenComplete(() => sub.cancel()); + } + + Future _timeoutWithMessages(Future Function() f, {Duration timeout, String message}) { + // Capture output to a buffer so if we don't get the response we want we can show + // the output that did arrive in the timeout error. + final StringBuffer messages = new StringBuffer(); + final DateTime start = new DateTime.now(); + void logMessage(String m) { + final int ms = new DateTime.now().difference(start).inMilliseconds; + messages.writeln('[+ ${ms.toString().padLeft(5)}] $m'); + } + final StreamSubscription sub = _allMessages.stream.listen(logMessage); + + return f().timeout(timeout ?? defaultTimeout, onTimeout: () { + logMessage(''); + throw '$message\nReceived:\n${messages.toString()}'; + }).whenComplete(() => sub.cancel()); } Map _parseFlutterResponse(String line) { @@ -252,7 +272,7 @@ class FlutterTestDriver { 'params': params }; final String jsonEncoded = json.encode(>[req]); - debugPrint(jsonEncoded); + _debugPrint(jsonEncoded); // Set up the response future before we send the request to avoid any // races.