observatory_port_test.dart 3.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Copyright 2014 The Flutter 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 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/convert.dart';

import '../src/common.dart';
import 'test_data/basic_project.dart';
import 'test_utils.dart';

Future<int> getFreePort() async {
  int port = 0;
  final ServerSocket serverSocket = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);
  port = serverSocket.port;
  await serverSocket.close();
  return port;
}

23
Future<void> waitForVmServiceMessage(Process process, int port) async {
24 25 26 27
  final Completer<void> completer = Completer<void>();
  process.stdout
    .transform(utf8.decoder)
    .listen((String line) {
28
      printOnFailure(line);
29
      if (line.contains('A Dart VM Service on Flutter test device is available at')) {
30 31 32 33 34 35 36 37 38
        if (line.contains('http://127.0.0.1:$port')) {
          completer.complete();
        } else {
          completer.completeError(Exception('Did not forward to provided port $port, instead found $line'));
        }
      }
    });
  process.stderr
    .transform(utf8.decoder)
39
    .listen(printOnFailure);
40 41 42 43
  return completer.future;
}

void main() {
44
  late Directory tempDir;
45
  final BasicProject project = BasicProject();
46 47 48

  setUp(() async {
    tempDir = createResolvedTempDirectorySync('run_test.');
49
    await project.setUpIn(tempDir);
50 51 52 53 54 55
  });

  tearDown(() async {
    tryToDelete(tempDir);
  });

56
  testWithoutContext('flutter run --vm-service-port', () async {
57 58
    final String flutterBin = fileSystem.path.join(getFlutterRoot(), 'bin', 'flutter');
    final int port = await getFreePort();
59
    // If only --vm-service-port is provided, --vm-service-port will be used by DDS
60 61 62 63 64
    // and the VM service will bind to a random port.
    final Process process = await processManager.start(<String>[
      flutterBin,
      'run',
      '--show-test-device',
65
      '--vm-service-port=$port',
66 67 68
      '-d',
      'flutter-tester',
    ], workingDirectory: tempDir.path);
69
    await waitForVmServiceMessage(process, port);
70 71 72 73
    process.kill();
    await process.exitCode;
  });

74
  testWithoutContext('flutter run --dds-port --vm-service-port', () async {
75
    final String flutterBin = fileSystem.path.join(getFlutterRoot(), 'bin', 'flutter');
76
    final int vmServicePort = await getFreePort();
77
    int ddsPort = await getFreePort();
78
    while (ddsPort == vmServicePort) {
79 80
      ddsPort = await getFreePort();
    }
81 82
    // If both --dds-port and --vm-service-port are provided, --dds-port will be used by
    // DDS and --vm-service-port will be used by the VM service.
83 84 85 86
    final Process process = await processManager.start(<String>[
      flutterBin,
      'run',
      '--show-test-device',
87
      '--vm-service-port=$vmServicePort',
88 89 90 91
      '--dds-port=$ddsPort',
      '-d',
      'flutter-tester',
    ], workingDirectory: tempDir.path);
92
    await waitForVmServiceMessage(process, ddsPort);
93 94 95 96
    process.kill();
    await process.exitCode;
  });

97
  testWithoutContext('flutter run --dds-port', () async {
98 99 100 101 102 103 104 105 106 107 108 109
    final String flutterBin = fileSystem.path.join(getFlutterRoot(), 'bin', 'flutter');
    final int ddsPort = await getFreePort();
    // If only --dds-port is provided, --dds-port will be used by DDS and the VM service
    // will bind to a random port.
    final Process process = await processManager.start(<String>[
      flutterBin,
      'run',
      '--show-test-device',
      '--dds-port=$ddsPort',
      '-d',
      'flutter-tester',
    ], workingDirectory: tempDir.path);
110
    await waitForVmServiceMessage(process, ddsPort);
111 112 113 114 115
    process.kill();
    await process.exitCode;
  });

}