Remove race conditions involving finding available ports (#18698)

This is an attempt to reland #18488 with less breakage on macOS.
This commit is contained in:
Ian Hickson
2018-06-27 16:44:28 -07:00
committed by GitHub
parent ed2376a7fc
commit 35ad2a786d
25 changed files with 258 additions and 273 deletions

View File

@@ -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, '', ''));
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
when(xcode.cc(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
when(xcode.clang(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
@@ -427,7 +427,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, '', ''));
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
when(xcode.cc(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
when(xcode.clang(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
@@ -474,7 +474,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, '', ''));
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
when(xcode.cc(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
when(xcode.clang(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
@@ -525,7 +525,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, '', ''));
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
when(xcode.cc(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
when(xcode.clang(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
@@ -571,7 +571,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, '', ''));
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
when(xcode.cc(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
when(xcode.clang(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
@@ -617,7 +617,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, '', ''));
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
when(xcode.cc(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
when(xcode.clang(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
@@ -683,7 +683,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, '', ''));
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
when(xcode.cc(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
when(xcode.clang(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
@@ -734,7 +734,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, '', ''));
final RunResult successResult = new RunResult(new ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
when(xcode.cc(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));
when(xcode.clang(any)).thenAnswer((_) => new Future<RunResult>.value(successResult));

View File

@@ -42,10 +42,13 @@ void main() {
});
Future<LaunchResult> start(String mainPath) async {
return await device.startApp(null,
mainPath: mainPath,
debuggingOptions: new DebuggingOptions.enabled(
const BuildInfo(BuildMode.debug, null)));
return await device.startApp(
null,
mainPath: mainPath,
debuggingOptions: new DebuggingOptions.enabled(
const BuildInfo(BuildMode.debug, null),
),
);
}
testUsingContext('start', () async {

View File

@@ -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),
hostPort: 54777);
logReader,
portForwarder: new MockPortForwarder(99),
);
// 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, 54777);
expect('$uri', 'http://127.0.0.1:54777/PTwjm8Ii8qg=/');
expect(uri.port, 99);
expect('$uri', 'http://127.0.0.1:99/PTwjm8Ii8qg=/');
discoverer.cancel();
logReader.dispose();
@@ -133,9 +133,10 @@ 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;
@@ -148,13 +149,33 @@ 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;
@@ -175,7 +196,12 @@ class MockPortForwarder extends DevicePortForwarder {
MockPortForwarder([this.availablePort]);
@override
Future<int> forward(int devicePort, {int hostPort}) async => hostPort ?? availablePort;
Future<int> forward(int devicePort, {int hostPort}) async {
hostPort ??= 0;
if (hostPort == 0)
return availablePort;
return hostPort;
}
@override
List<ForwardedPort> get forwardedPorts => throw 'not implemented';

View File

@@ -12,7 +12,6 @@ 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';
@@ -77,7 +76,6 @@ void testUsingContext(String description, dynamic testMethod(), {
},
Logger: () => new BufferLogger(),
OperatingSystemUtils: () => new MockOperatingSystemUtils(),
PortScanner: () => new MockPortScanner(),
SimControl: () => new MockSimControl(),
Usage: () => new MockUsage(),
XcodeProjectInterpreter: () => new MockXcodeProjectInterpreter(),
@@ -127,16 +125,6 @@ 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>[];

View File

@@ -4,7 +4,6 @@
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';
@@ -13,9 +12,8 @@ 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://localhost:$port')),
VMService.connect(Uri.parse('http://host.invalid:9999/')),
throwsToolExit(),
);
});