run_machine_concurrent_hot_reload.dart 5.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Copyright (c) 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:path/path.dart' as path;
import 'package:vm_service_client/vm_service_client.dart';

import 'package:flutter_devicelab/framework/adb.dart';
import 'package:flutter_devicelab/framework/framework.dart';
import 'package:flutter_devicelab/framework/utils.dart';

void main() {
  Map<String, dynamic> parseFlutterResponse(String line) {
    if (line.startsWith('[') && line.endsWith(']')) {
      try {
20
        return json.decode(line)[0];
21 22 23 24 25 26 27 28 29
      } catch (e) {
        // Not valid JSON, so likely some other output that was surrounded by [brackets]
        return null;
      }
    }
    return null;
  }

  Stream<String> transformToLines(Stream<List<int>> byteStream) {
30
    return byteStream.transform<String>(utf8.decoder).transform<String>(const LineSplitter());
31 32 33
  }

  task(() async {
34
    Uri vmServiceUri;
35 36 37 38 39 40 41
    String appId;

    final Device device = await devices.workingDevice;
    await device.unlock();
    final Directory appDir =
        dir(path.join(flutterDirectory.path, 'dev/integration_tests/ui'));
    await inDirectory(appDir, () async {
42
      final Completer<void> ready = Completer<void>();
43 44 45 46 47 48 49 50 51 52
      bool ok;
      print('run: starting...');
      final Process run = await startProcess(
        path.join(flutterDirectory.path, 'bin', 'flutter'),
        <String>[
          'run',
          '--machine',
          '--verbose',
          '-d',
          device.deviceId,
53
          'lib/commands.dart',
54 55
        ],
      );
56
      final StreamController<String> stdout = StreamController<String>.broadcast();
57 58 59 60
      transformToLines(run.stdout).listen((String line) {
        print('run:stdout: $line');
        stdout.add(line);
        final dynamic json = parseFlutterResponse(line);
61 62
        if (json != null) {
          if (json['event'] == 'app.debugPort') {
63 64
            vmServiceUri = Uri.parse(json['params']['wsUri']);
            print('service protocol connection available at $vmServiceUri');
65 66 67 68
          } else if (json['event'] == 'app.started') {
            appId = json['params']['appId'];
            print('application identifier is $appId');
          }
69
        }
70
        if (vmServiceUri != null && appId != null && !ready.isCompleted) {
71 72 73 74 75 76 77
          print('run: ready!');
          ready.complete();
          ok ??= true;
        }
      });
      transformToLines(run.stderr).listen((String line) {
        stderr.writeln('run:stderr: $line');
78
        ok = false;
79
      });
80
      run.exitCode.then<void>((int exitCode) {
81 82 83 84 85 86
        ok = false;
      });
      await Future.any<dynamic>(<Future<dynamic>>[ready.future, run.exitCode]);
      if (!ok)
        throw 'Failed to run test app.';

87
      final VMServiceClient client = VMServiceClient.connect(vmServiceUri);
88 89

      int id = 1;
90
      Future<Map<String, dynamic>> sendRequest(String method, dynamic params) async {
91
        final int requestId = id++;
92 93
        final Completer<Map<String, dynamic>> response = Completer<Map<String, dynamic>>();
        final StreamSubscription<String> responseSubscription = stdout.stream.listen((String line) {
94 95 96 97 98 99 100
          final Map<String, dynamic> json = parseFlutterResponse(line);
          if (json != null && json['id'] == requestId)
            response.complete(json);
        });
        final Map<String, dynamic> req = <String, dynamic>{
          'id': requestId,
          'method': method,
101
          'params': params,
102
        };
103 104 105
        final String jsonEncoded = json.encode(<Map<String, dynamic>>[req]);
        print('run:stdin: $jsonEncoded');
        run.stdin.writeln(jsonEncoded);
106 107 108 109 110 111
        final Map<String, dynamic> result = await response.future;
        responseSubscription.cancel();
        return result;
      }

      print('test: sending two hot reloads...');
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
      final Future<dynamic> hotReload1 = sendRequest(
        'app.restart',
        <String, dynamic>{'appId': appId, 'fullRestart': false},
      );
      final Future<dynamic> hotReload2 = sendRequest(
        'app.restart',
        <String, dynamic>{'appId': appId, 'fullRestart': false},
      );
      final Future<List<dynamic>> reloadRequests = Future.wait<dynamic>(<Future<dynamic>>[
        hotReload1,
        hotReload2,
      ]);
      final dynamic results = await Future.any<dynamic>(<Future<dynamic>>[
        run.exitCode,
        reloadRequests,
      ]);
128 129

      if (!ok)
130
        throw 'App failed or crashed during hot reloads.';
131 132

      final List<dynamic> responses = results;
133 134 135 136 137 138 139 140
      final List<dynamic> errorResponses = responses.where(
        (dynamic r) => r['error'] != null
      ).toList();
      final List<dynamic> successResponses = responses.where(
        (dynamic r) => r['error'] == null &&
                       r['result'] != null &&
                       r['result']['code'] == 0
      ).toList();
141 142 143 144 145 146 147 148 149

      if (errorResponses.length != 1)
        throw 'Did not receive the expected (exactly one) hot reload error response.';
      final String errorMessage = errorResponses.first['error'];
      if (!errorMessage.contains('in progress'))
        throw 'Error response was not that hot reload was in progress.';
      if (successResponses.length != 1)
        throw 'Did not receive the expected (exactly one) successful hot reload response.';

150 151 152 153
      final dynamic hotReload3 = await sendRequest(
        'app.restart',
        <String, dynamic>{'appId': appId, 'fullRestart': false},
      );
154 155 156 157 158 159 160 161
      if (hotReload3['error'] != null)
        throw 'Received an error response from a hot reload after all other hot reloads had completed.';

      sendRequest('app.stop', <String, dynamic>{'appId': appId});
      final int result = await run.exitCode;
      if (result != 0)
        throw 'Received unexpected exit code $result from run process.';
      print('test: validating that the app has in fact closed...');
162
      await client.done;
163
    });
164
    return TaskResult.success(null);
165 166
  });
}