Unverified Commit 0fa5ba43 authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Revert "Remove race conditions involving finding available ports (#18488)" (#18521)

This reverts commit 77508722.
parent 77508722
......@@ -35,14 +35,12 @@ void main() {
.listen((String line) {
print('run:stdout: $line');
stdout.add(line);
if (vmServicePort == null) {
if (lineContainsServicePort(line)) {
vmServicePort = parseServicePort(line);
if (vmServicePort != null) {
print('service protocol connection available at port $vmServicePort');
print('run: ready!');
ready.complete();
ok ??= true;
}
print('service protocol connection available at port $vmServicePort');
print('run: ready!');
ready.complete();
ok ??= true;
}
});
run.stderr
......
......@@ -41,14 +41,12 @@ void main() {
.transform(const LineSplitter())
.listen((String line) {
print('run:stdout: $line');
if (vmServicePort == null) {
if (lineContainsServicePort(line)) {
vmServicePort = parseServicePort(line);
if (vmServicePort != null) {
print('service protocol connection available at port $vmServicePort');
print('run: ready!');
ready.complete();
ok ??= true;
}
print('service protocol connection available at port $vmServicePort');
print('run: ready!');
ready.complete();
ok ??= true;
}
});
run.stderr
......
......@@ -33,14 +33,12 @@ void main() {
.transform(const LineSplitter())
.listen((String line) {
print('run:stdout: $line');
if (vmServicePort == null) {
if (lineContainsServicePort(line)) {
vmServicePort = parseServicePort(line);
if (vmServicePort != null) {
print('service protocol connection available at port $vmServicePort');
print('run: ready!');
ready.complete();
ok ??= true;
}
print('service protocol connection available at port $vmServicePort');
print('run: ready!');
ready.complete();
ok ??= true;
}
});
run.stderr
......
......@@ -28,8 +28,9 @@ Future<Map<String, dynamic>> runTask(String taskName, { bool silent = false }) a
if (!file(taskExecutable).existsSync())
throw 'Executable Dart file not found: $taskExecutable';
final int vmServicePort = await findAvailablePort();
final Process runner = await startProcess(dartBin, <String>[
'--enable-vm-service=0',
'--enable-vm-service=$vmServicePort',
'--no-pause-isolates-on-exit',
taskExecutable,
]);
......@@ -40,17 +41,10 @@ Future<Map<String, dynamic>> runTask(String taskName, { bool silent = false }) a
runnerFinished = true;
});
final Completer<int> port = new Completer<int>();
final StreamSubscription<String> stdoutSub = runner.stdout
.transform(const Utf8Decoder())
.transform(const LineSplitter())
.listen((String line) {
if (!port.isCompleted) {
final int portValue = parseServicePort(line, prefix: 'Observatory listening on ');
if (portValue != null)
port.complete(portValue);
}
if (!silent) {
stdout.writeln('[$taskName] [STDOUT] $line');
}
......@@ -65,7 +59,7 @@ Future<Map<String, dynamic>> runTask(String taskName, { bool silent = false }) a
String waitingFor = 'connection';
try {
final VMIsolateRef isolate = await _connectToRunnerIsolate(await port.future);
final VMIsolateRef isolate = await _connectToRunnerIsolate(vmServicePort);
waitingFor = 'task completion';
final Map<String, dynamic> taskResult =
await isolate.invokeExtension('ext.cocoonRunTask').timeout(taskTimeoutWithGracePeriod);
......
......@@ -480,6 +480,21 @@ Future<Null> runAndCaptureAsyncStacks(Future<Null> callback()) {
return completer.future;
}
/// Return an unused TCP port number.
Future<int> findAvailablePort() async {
int port = 20000;
while (true) {
try {
final ServerSocket socket =
await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, port); // ignore: deprecated_member_use
await socket.close();
return port;
} catch (_) {
port++;
}
}
}
bool canRun(String path) => _processManager.canRun(path);
String extractCloudAuthTokenArg(List<String> rawArgs) {
......@@ -502,20 +517,13 @@ String extractCloudAuthTokenArg(List<String> rawArgs) {
return token;
}
/// Tries to extract a port from the string.
///
/// The `prefix`, if specified, is a regular expression pattern and must not contain groups.
///
/// The `multiLine` flag should be set to true if `line` is actually a buffer of many lines.
int parseServicePort(String line, {
String prefix = 'An Observatory debugger .* is available at: ',
bool multiLine = false,
}) {
// e.g. "An Observatory debugger and profiler on ... is available at: http://127.0.0.1:8100/"
final RegExp pattern = new RegExp('$prefix(\\S+:(\\d+)/\\S*)\$', multiLine: multiLine);
final Match match = pattern.firstMatch(line);
print(pattern);
print(match);
// "An Observatory debugger and profiler on ... is available at: http://127.0.0.1:8100/"
final RegExp _kObservatoryRegExp = new RegExp(r'An Observatory debugger .* is available at: (\S+:(\d+))');
bool lineContainsServicePort(String line) => line.contains(_kObservatoryRegExp);
int parseServicePort(String line) {
final Match match = _kObservatoryRegExp.firstMatch(line);
return match == null ? null : int.parse(match.group(2));
}
......
......@@ -364,6 +364,8 @@ class MemoryTest {
if (deviceOperatingSystem == DeviceOperatingSystem.ios)
await prepareProvisioningCertificates(testDirectory);
final int observatoryPort = await findAvailablePort();
final List<String> runOptions = <String>[
'-v',
'--profile',
......@@ -371,14 +373,11 @@ class MemoryTest {
'-d',
deviceId,
'--observatory-port',
'0',
observatoryPort.toString(),
];
if (testTarget != null)
runOptions.addAll(<String>['-t', testTarget]);
final String output = await evalFlutter('run', options: runOptions);
final int observatoryPort = parseServicePort(output, prefix: 'Successfully connected to service protocol: ', multiLine: true);
if (observatoryPort == null)
throw new Exception('Could not find observatory port in "flutter run" output.');
await flutter('run', options: runOptions);
final Map<String, dynamic> startData = await device.getMemoryStats(packageName);
......
......@@ -15,6 +15,7 @@ import '../base/common.dart' show throwToolExit;
import '../base/file_system.dart';
import '../base/io.dart';
import '../base/logger.dart';
import '../base/port_scanner.dart';
import '../base/process.dart';
import '../base/process_manager.dart';
import '../base/utils.dart';
......@@ -843,7 +844,7 @@ class _AndroidDevicePortForwarder extends DevicePortForwarder {
final int devicePort = _extractPort(splitLine[2]);
// Failed, skip.
if (hostPort == null || devicePort == null)
if ((hostPort == null) || (devicePort == null))
continue;
ports.add(new ForwardedPort(hostPort, devicePort));
......@@ -854,29 +855,15 @@ class _AndroidDevicePortForwarder extends DevicePortForwarder {
}
@override
Future<int> forward(int devicePort, {int hostPort}) async {
hostPort ??= 0;
final RunResult process = await runCheckedAsync(device.adbCommandForDevice(
<String>['forward', 'tcp:$hostPort', 'tcp:$devicePort']
));
if (process.stderr.isNotEmpty)
process.throwException('adb returned error:\n${process.stderr}');
if (process.exitCode != 0) {
if (process.stdout.isNotEmpty)
process.throwException('adb returned error:\n${process.stdout}');
process.throwException('adb failed without a message');
Future<int> forward(int devicePort, { int hostPort }) async {
if ((hostPort == null) || (hostPort == 0)) {
// Auto select host port.
hostPort = await portScanner.findAvailablePort();
}
if (hostPort == 0) {
if (process.stdout.isEmpty)
process.throwException('adb did not report forwarded port');
hostPort = int.tryParse(process.stdout) ?? (throw 'adb returned invalid port number:\n${process.stdout}');
} else {
if (process.stdout.isNotEmpty)
process.throwException('adb returned error:\n${process.stdout}');
}
await runCheckedAsync(device.adbCommandForDevice(
<String>['forward', 'tcp:$hostPort', 'tcp:$devicePort']
));
return hostPort;
}
......
......@@ -5,6 +5,8 @@
import 'file_system.dart';
import 'platform.dart';
const int kDefaultObservatoryPort = 8100;
/// Return the absolute path of the user's home directory
String get homeDirPath {
if (_homeDirPath == null) {
......
// Copyright 2017 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 'context.dart';
import 'io.dart';
const int _kMaxSearchIterations = 20;
PortScanner get portScanner => context[PortScanner];
abstract class PortScanner {
const PortScanner();
/// Returns true if the specified [port] is available to bind to.
Future<bool> isPortAvailable(int port);
/// Returns an available ephemeral port.
Future<int> findAvailablePort();
/// Returns an available port as close to [defaultPort] as possible.
///
/// If [defaultPort] is available, this will return it. Otherwise, it will
/// search for an available port close to [defaultPort]. If it cannot find one,
/// it will return any available port.
Future<int> findPreferredPort(int defaultPort) async {
int iterationCount = 0;
while (iterationCount < _kMaxSearchIterations) {
final int port = defaultPort + iterationCount;
if (await isPortAvailable(port))
return port;
iterationCount++;
}
return findAvailablePort();
}
}
class HostPortScanner extends PortScanner {
const HostPortScanner();
@override
Future<bool> isPortAvailable(int port) async {
try {
// TODO(ianh): This is super racy.
final ServerSocket socket = await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, port); // ignore: deprecated_member_use
await socket.close();
return true;
} catch (error) {
return false;
}
}
@override
Future<int> findAvailablePort() async {
ServerSocket socket;
try {
socket = await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0); // ignore: deprecated_member_use
} on SocketException {
socket = await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V6, 0, v6Only: true); // ignore: deprecated_member_use
}
final int port = socket.port;
await socket.close();
return port;
}
}
......@@ -229,7 +229,7 @@ Future<RunResult> runAsync(List<String> cmd, {
workingDirectory: workingDirectory,
environment: _environment(allowReentrantFlutter, environment),
);
final RunResult runResults = new RunResult(results, cmd);
final RunResult runResults = new RunResult(results);
printTrace(runResults.toString());
return runResults;
}
......@@ -240,10 +240,10 @@ Future<RunResult> runCheckedAsync(List<String> cmd, {
Map<String, String> environment
}) async {
final RunResult result = await runAsync(
cmd,
workingDirectory: workingDirectory,
allowReentrantFlutter: allowReentrantFlutter,
environment: environment,
cmd,
workingDirectory: workingDirectory,
allowReentrantFlutter: allowReentrantFlutter,
environment: environment
);
if (result.exitCode != 0)
throw 'Exit code ${result.exitCode} from: ${cmd.join(' ')}:\n$result';
......@@ -364,12 +364,10 @@ class ProcessExit implements Exception {
}
class RunResult {
RunResult(this.processResult, this._command) : assert(_command != null), assert(_command.isNotEmpty);
RunResult(this.processResult);
final ProcessResult processResult;
final List<String> _command;
int get exitCode => processResult.exitCode;
String get stdout => processResult.stdout;
String get stderr => processResult.stderr;
......@@ -383,14 +381,4 @@ class RunResult {
out.writeln(processResult.stderr);
return out.toString().trimRight();
}
/// Throws a [ProcessException] with the given `message`.
void throwException(String message) {
throw new ProcessException(
_command.first,
_command.skip(1).toList(),
message,
exitCode,
);
}
}
......@@ -55,7 +55,8 @@ abstract class RunCommandBase extends FlutterCommand {
void usesPortOptions() {
argParser.addOption('observatory-port',
help: 'Listen to the given port for an observatory debugger connection.\n'
'Specifying port 0 (the default) will find a random free port.'
'Specifying port 0 will find a random free port.\n'
'Defaults to the first available port after $kDefaultObservatoryPort.'
);
}
......
......@@ -16,18 +16,14 @@ import '../tracing.dart';
class TraceCommand extends FlutterCommand {
TraceCommand() {
requiresPubspecYaml();
argParser.addOption('debug-port',
help: 'Local port where the observatory is listening. Required.',
);
argParser.addFlag('start', negatable: false, help: 'Start tracing. Implied if --stop is also omitted.');
argParser.addFlag('stop', negatable: false, help: 'Stop tracing. Implied if --start is also omitted.');
argParser.addOption('duration',
abbr: 'd',
help: 'Time to wait after starting (if --start is specified or implied) and before\n'
'stopping (if --stop is specified or implied).\n'
'Defaults to ten seconds if --stop is specified or implied, zero otherwise.',
);
argParser.addFlag('start', negatable: false, help: 'Start tracing.');
argParser.addFlag('stop', negatable: false, help: 'Stop tracing.');
argParser.addOption('out', help: 'Specify the path of the saved trace file.');
argParser.addOption('duration',
defaultsTo: '10', abbr: 'd', help: 'Duration in seconds to trace.');
argParser.addOption('debug-port',
defaultsTo: kDefaultObservatoryPort.toString(),
help: 'Local port where the observatory is listening.');
}
@override
......@@ -38,39 +34,13 @@ class TraceCommand extends FlutterCommand {
@override
final String usageFooter =
'\`trace\` called without the --start or --stop flags will automatically start tracing,\n'
'delay a set amount of time (controlled by --duration), and stop tracing. To explicitly\n'
'control tracing, call trace with --start and later with --stop.\n'
'The --debug-port argument is required.';
'\`trace\` called with no arguments will automatically start tracing, delay a set amount of\n'
'time (controlled by --duration), and stop tracing. To explicitly control tracing, call trace\n'
'with --start and later with --stop.';
@override
Future<Null> runCommand() async {
int observatoryPort;
if (argResults.wasParsed('debug-port')) {
observatoryPort = int.tryParse(argResults['debug-port']);
}
if (observatoryPort == null) {
throwToolExit('The --debug-port argument must be specified.');
}
bool start = argResults['start'];
bool stop = argResults['stop'];
if (!start && !stop) {
start = true;
stop = true;
}
assert(start || stop);
Duration duration;
if (argResults.wasParsed('duration')) {
try {
duration = new Duration(seconds: int.parse(argResults['duration']));
} on FormatException {
throwToolExit('Invalid duration passed to --duration; it should be a positive number of seconds.');
}
} else {
duration = stop ? const Duration(seconds: 10) : Duration.zero;
}
final int observatoryPort = int.parse(argResults['debug-port']);
// TODO(danrubel): this will break if we move to the new observatory URL
// See https://github.com/flutter/flutter/issues/7038
......@@ -86,11 +56,20 @@ class TraceCommand extends FlutterCommand {
Cache.releaseLockEarly();
if (start)
if ((!argResults['start'] && !argResults['stop']) ||
(argResults['start'] && argResults['stop'])) {
// Setting neither flags or both flags means do both commands and wait
// duration seconds in between.
await tracing.startTracing();
await new Future<Null>.delayed(duration);
if (stop)
await new Future<Null>.delayed(
new Duration(seconds: int.parse(argResults['duration'])),
() => _stopTracing(tracing)
);
} else if (argResults['stop']) {
await _stopTracing(tracing);
} else {
await tracing.startTracing();
}
}
Future<Null> _stopTracing(Tracing tracing) async {
......
......@@ -19,6 +19,7 @@ import 'base/io.dart';
import 'base/logger.dart';
import 'base/os.dart';
import 'base/platform.dart';
import 'base/port_scanner.dart';
import 'base/utils.dart';
import 'cache.dart';
import 'compile.dart';
......@@ -69,6 +70,7 @@ Future<T> runInContext<T>(
KernelCompiler: () => const KernelCompiler(),
Logger: () => platform.isWindows ? new WindowsStdoutLogger() : new StdoutLogger(),
OperatingSystemUtils: () => new OperatingSystemUtils(),
PortScanner: () => const HostPortScanner(),
SimControl: () => new SimControl(),
Stdio: () => const Stdio(),
Usage: () => new Usage(),
......
......@@ -7,8 +7,10 @@ import 'dart:math' as math;
import 'android/android_device.dart';
import 'application_package.dart';
import 'base/common.dart';
import 'base/context.dart';
import 'base/file_system.dart';
import 'base/port_scanner.dart';
import 'base/utils.dart';
import 'build_info.dart';
import 'globals.dart';
......@@ -365,6 +367,14 @@ class DebuggingOptions {
final int observatoryPort;
bool get hasObservatoryPort => observatoryPort != null;
/// Return the user specified observatory port. If that isn't available,
/// return [kDefaultObservatoryPort], or a port close to that one.
Future<int> findBestObservatoryPort() {
if (hasObservatoryPort)
return new Future<int>.value(observatoryPort);
return portScanner.findPreferredPort(observatoryPort ?? kDefaultObservatoryPort);
}
}
class LaunchResult {
......@@ -404,9 +414,9 @@ abstract class DevicePortForwarder {
List<ForwardedPort> get forwardedPorts;
/// Forward [hostPort] on the host to [devicePort] on the device.
/// If [hostPort] is null or zero, will auto select a host port.
/// If [hostPort] is null, will auto select a host port.
/// Returns a Future that completes with the host port.
Future<int> forward(int devicePort, {int hostPort});
Future<int> forward(int devicePort, { int hostPort });
/// Stops forwarding [forwardedPort].
Future<Null> unforward(ForwardedPort forwardedPort);
......
......@@ -10,6 +10,7 @@ import '../base/file_system.dart';
import '../base/io.dart';
import '../base/logger.dart';
import '../base/platform.dart';
import '../base/port_scanner.dart';
import '../base/process.dart';
import '../base/process_manager.dart';
import '../build_info.dart';
......@@ -508,40 +509,26 @@ class _IOSDevicePortForwarder extends DevicePortForwarder {
@override
Future<int> forward(int devicePort, {int hostPort}) async {
final bool autoselect = hostPort == null || hostPort == 0;
if (autoselect)
hostPort = 1024;
Process process;
bool connected = false;
while (!connected) {
// Usage: iproxy LOCAL_TCP_PORT DEVICE_TCP_PORT UDID
process = await runCommand(<String>[
device._iproxyPath,
hostPort.toString(),
devicePort.toString(),
device.id,
]);
connected = !await process.stdout.isEmpty;
if (!connected) {
if (autoselect) {
hostPort += 1;
if (hostPort > 65535)
throw 'Could not find open port on host.';
} else {
throw 'Port $hostPort is not available.';
}
}
if ((hostPort == null) || (hostPort == 0)) {
// Auto select host port.
hostPort = await portScanner.findAvailablePort();
}
assert(connected);
assert(process != null);
final ForwardedPort forwardedPort = new ForwardedPort.withContext(
hostPort, devicePort, process,
);
// Usage: iproxy LOCAL_TCP_PORT DEVICE_TCP_PORT UDID
final Process process = await runCommand(<String>[
device._iproxyPath,
hostPort.toString(),
devicePort.toString(),
device.id,
]);
final ForwardedPort forwardedPort = new ForwardedPort.withContext(hostPort,
devicePort, process);
printTrace('Forwarded port $forwardedPort');
_forwardedPorts.add(forwardedPort);
return hostPort;
}
......
......@@ -305,7 +305,8 @@ class IOSSimulator extends Device {
args.add('--skia-deterministic-rendering');
if (debuggingOptions.useTestFonts)
args.add('--use-test-fonts');
final int observatoryPort = debuggingOptions.observatoryPort ?? 0;
final int observatoryPort = await debuggingOptions.findBestObservatoryPort();
args.add('--observatory-port=$observatoryPort');
}
......@@ -692,7 +693,7 @@ class _IOSSimulatorDevicePortForwarder extends DevicePortForwarder {
@override
Future<int> forward(int devicePort, {int hostPort}) async {
if (hostPort == null || hostPort == 0) {
if ((hostPort == null) || (hostPort == 0)) {
hostPort = devicePort;
}
assert(devicePort == hostPort);
......
......@@ -4,7 +4,9 @@
import 'dart:async';
import 'base/common.dart';
import 'base/io.dart';
import 'base/port_scanner.dart';
import 'device.dart';
import 'globals.dart';
......@@ -16,8 +18,10 @@ class ProtocolDiscovery {
this.serviceName, {
this.portForwarder,
this.hostPort,
this.defaultHostPort,
this.ipv6,
}) : assert(logReader != null),
assert(portForwarder == null || defaultHostPort != null),
_prefix = '$serviceName listening on ' {
_deviceLogSubscription = logReader.logLines.listen(_handleLine);
}
......@@ -33,6 +37,7 @@ class ProtocolDiscovery {
logReader, kObservatoryService,
portForwarder: portForwarder,
hostPort: hostPort,
defaultHostPort: kDefaultObservatoryPort,
ipv6: ipv6,
);
}
......@@ -41,6 +46,7 @@ class ProtocolDiscovery {
final String serviceName;
final DevicePortForwarder portForwarder;
final int hostPort;
final int defaultHostPort;
final bool ipv6;
final String _prefix;
......@@ -82,15 +88,16 @@ class ProtocolDiscovery {
Uri hostUri = deviceUri;
if (portForwarder != null) {
final int actualDevicePort = deviceUri.port;
final int actualHostPort = await portForwarder.forward(actualDevicePort, hostPort: hostPort);
printTrace('Forwarded host port $actualHostPort to device port $actualDevicePort for $serviceName');
hostUri = deviceUri.replace(port: actualHostPort);
final int devicePort = deviceUri.port;
int hostPort = this.hostPort ?? await portScanner.findPreferredPort(defaultHostPort);
hostPort = await portForwarder.forward(devicePort, hostPort: hostPort);
printTrace('Forwarded host port $hostPort to device port $devicePort for $serviceName');
hostUri = deviceUri.replace(port: hostPort);
}
assert(new InternetAddress(hostUri.host).isLoopback);
if (ipv6) {
hostUri = hostUri.replace(host: InternetAddress.loopbackIPv6.host);
hostUri = hostUri.replace(host: InternetAddress.LOOPBACK_IP_V6.host); // ignore: deprecated_member_use
}
return hostUri;
......
......@@ -122,7 +122,8 @@ class FlutterTesterDevice extends Device {
command.add('--start-paused');
}
if (debuggingOptions.hasObservatoryPort)
command.add('--observatory-port=${debuggingOptions.observatoryPort}');
command
.add('--observatory-port=${debuggingOptions.hasObservatoryPort}');
}
// Build assets and perform initial compilation.
......@@ -169,10 +170,9 @@ class FlutterTesterDevice extends Device {
if (!debuggingOptions.debuggingEnabled)
return new LaunchResult.succeeded();
final ProtocolDiscovery observatoryDiscovery = new ProtocolDiscovery.observatory(
getLogReader(),
hostPort: debuggingOptions.observatoryPort,
);
final ProtocolDiscovery observatoryDiscovery =
new ProtocolDiscovery.observatory(getLogReader(),
hostPort: debuggingOptions.observatoryPort);
final Uri observatoryUri = await observatoryDiscovery.uri;
return new LaunchResult.succeeded(observatoryUri: observatoryUri);
......
......@@ -381,7 +381,7 @@ void main() {
fs.path.join(outputPath, 'snapshot.d'): '${fs.path.join(outputPath, 'snapshot_assembly.S')} : ',
};
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''));
when(xcode.cc(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
when(xcode.clang(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
......@@ -428,7 +428,7 @@ void main() {
fs.path.join(outputPath, 'snapshot.d'): '${fs.path.join(outputPath, 'snapshot_assembly.S')} : ',
};
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''));
when(xcode.cc(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
when(xcode.clang(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
......@@ -476,7 +476,7 @@ void main() {
fs.path.join(outputPath, 'snapshot.d'): '${fs.path.join(outputPath, 'vm_snapshot_data')} : ',
};
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''));
when(xcode.cc(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
when(xcode.clang(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
......@@ -528,7 +528,7 @@ void main() {
fs.path.join(outputPath, 'snapshot.d'): '${fs.path.join(outputPath, 'vm_snapshot_data')} : ',
};
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''));
when(xcode.cc(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
when(xcode.clang(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
......@@ -575,7 +575,7 @@ void main() {
fs.path.join(outputPath, 'snapshot.d'): '${fs.path.join(outputPath, 'snapshot_assembly.S')} : ',
};
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''));
when(xcode.cc(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
when(xcode.clang(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
......@@ -622,7 +622,7 @@ void main() {
fs.path.join(outputPath, 'snapshot.d'): '${fs.path.join(outputPath, 'snapshot_assembly.S')} : ',
};
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''));
when(xcode.cc(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
when(xcode.clang(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
......@@ -689,7 +689,7 @@ void main() {
fs.path.join(outputPath, 'snapshot.d'): '${fs.path.join(outputPath, 'vm_snapshot_data')} : ',
};
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''));
when(xcode.cc(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
when(xcode.clang(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
......@@ -741,7 +741,7 @@ void main() {
fs.path.join(outputPath, 'snapshot.d'): '${fs.path.join(outputPath, 'vm_snapshot_data')} : ',
};
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''));
when(xcode.cc(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
when(xcode.clang(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
......
......@@ -115,16 +115,16 @@ void main() {
testUsingContext('default port', () async {
final MockDeviceLogReader logReader = new MockDeviceLogReader();
final ProtocolDiscovery discoverer = new ProtocolDiscovery.observatory(
logReader,
portForwarder: new MockPortForwarder(99),
);
logReader,
portForwarder: new MockPortForwarder(99),
hostPort: 54777);
// Get next port future.
final Future<Uri> nextUri = discoverer.uri;
logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:54804/PTwjm8Ii8qg=/');
final Uri uri = await nextUri;
expect(uri.port, 99);
expect('$uri', 'http://127.0.0.1:99/PTwjm8Ii8qg=/');
expect(uri.port, 54777);
expect('$uri', 'http://127.0.0.1:54777/PTwjm8Ii8qg=/');
discoverer.cancel();
logReader.dispose();
......@@ -133,10 +133,9 @@ void main() {
testUsingContext('specified port', () async {
final MockDeviceLogReader logReader = new MockDeviceLogReader();
final ProtocolDiscovery discoverer = new ProtocolDiscovery.observatory(
logReader,
portForwarder: new MockPortForwarder(99),
hostPort: 1243,
);
logReader,
portForwarder: new MockPortForwarder(99),
hostPort: 1243);
// Get next port future.
final Future<Uri> nextUri = discoverer.uri;
......@@ -149,33 +148,13 @@ void main() {
logReader.dispose();
});
testUsingContext('specified port zero', () async {
final MockDeviceLogReader logReader = new MockDeviceLogReader();
final ProtocolDiscovery discoverer = new ProtocolDiscovery.observatory(
logReader,
portForwarder: new MockPortForwarder(99),
hostPort: 0,
);
// Get next port future.
final Future<Uri> nextUri = discoverer.uri;
logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:54804/PTwjm8Ii8qg=/');
final Uri uri = await nextUri;
expect(uri.port, 99);
expect('$uri', 'http://127.0.0.1:99/PTwjm8Ii8qg=/');
discoverer.cancel();
logReader.dispose();
});
testUsingContext('ipv6', () async {
final MockDeviceLogReader logReader = new MockDeviceLogReader();
final ProtocolDiscovery discoverer = new ProtocolDiscovery.observatory(
logReader,
portForwarder: new MockPortForwarder(99),
hostPort: 54777,
ipv6: true,
);
logReader,
portForwarder: new MockPortForwarder(99),
hostPort: 54777,
ipv6: true);
// Get next port future.
final Future<Uri> nextUri = discoverer.uri;
......@@ -196,12 +175,7 @@ class MockPortForwarder extends DevicePortForwarder {
MockPortForwarder([this.availablePort]);
@override
Future<int> forward(int devicePort, {int hostPort}) async {
hostPort ??= 0;
if (hostPort == 0)
return availablePort;
return hostPort;
}
Future<int> forward(int devicePort, {int hostPort}) async => hostPort ?? availablePort;
@override
List<ForwardedPort> get forwardedPorts => throw 'not implemented';
......
......@@ -12,6 +12,7 @@ import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/os.dart';
import 'package:flutter_tools/src/base/port_scanner.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/context_runner.dart';
import 'package:flutter_tools/src/device.dart';
......@@ -76,6 +77,7 @@ void testUsingContext(String description, dynamic testMethod(), {
},
Logger: () => new BufferLogger(),
OperatingSystemUtils: () => new MockOperatingSystemUtils(),
PortScanner: () => new MockPortScanner(),
SimControl: () => new MockSimControl(),
Usage: () => new MockUsage(),
XcodeProjectInterpreter: () => new MockXcodeProjectInterpreter(),
......@@ -125,6 +127,16 @@ void _printBufferedErrors(AppContext testContext) {
}
}
class MockPortScanner extends PortScanner {
static int _nextAvailablePort = 12345;
@override
Future<bool> isPortAvailable(int port) async => true;
@override
Future<int> findAvailablePort() async => _nextAvailablePort++;
}
class MockDeviceManager implements DeviceManager {
List<Device> devices = <Device>[];
......
......@@ -4,6 +4,7 @@
import 'package:test/test.dart';
import 'package:flutter_tools/src/base/port_scanner.dart';
import 'package:flutter_tools/src/vmservice.dart';
import 'src/common.dart';
......@@ -12,8 +13,9 @@ import 'src/context.dart';
void main() {
group('VMService', () {
testUsingContext('fails connection eagerly in the connect() method', () async {
final int port = await const HostPortScanner().findAvailablePort();
expect(
VMService.connect(Uri.parse('http://host.invalid:9999/')),
VMService.connect(Uri.parse('http://localhost:$port')),
throwsToolExit(),
);
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment