forked from firka/flutter
Fix race condition in protocol_discovery.dart (#10092)
For some reaosn, when we discovered our URI, we were re-instantiating the `Completer` instance variable whose future we listen to in `nextUri()`. This led to a race between a caller calling `nextUri()` and us discovering the URI. If we happened to discover our URI before a caller called `nextUri()`, then they would be left waiting on a future from the newly allocated `Completer` (which would never complete). Fixes #10064
This commit is contained in:
@@ -13,74 +13,113 @@ import 'src/mocks.dart';
|
||||
|
||||
void main() {
|
||||
group('service_protocol discovery', () {
|
||||
testUsingContext('no port forwarding', () async {
|
||||
final MockDeviceLogReader logReader = new MockDeviceLogReader();
|
||||
final ProtocolDiscovery discoverer =
|
||||
new ProtocolDiscovery(logReader, ProtocolDiscovery.kObservatoryService);
|
||||
MockDeviceLogReader logReader;
|
||||
ProtocolDiscovery discoverer;
|
||||
|
||||
// Get next port future.
|
||||
Future<Uri> nextUri = discoverer.nextUri();
|
||||
expect(nextUri, isNotNull);
|
||||
group('no port forwarding', () {
|
||||
/// Performs test set-up functionality that must be performed as part of
|
||||
/// the `test()` pass and not part of the `setUp()` pass.
|
||||
///
|
||||
/// This exists to make sure we're not creating an error that tries to
|
||||
/// cross an error-zone boundary. Our use of `testUsingContext()` runs the
|
||||
/// test code inside an error zone, but the `setUp()` code is not run in
|
||||
/// any zone. This creates the potential for errors that try to cross
|
||||
/// error-zone boundaries, which are considered uncaught.
|
||||
///
|
||||
/// This also exists for cases where our initialization requires access to
|
||||
/// a `Context` object, which is only set up inside the zone.
|
||||
///
|
||||
/// Note that these issues do not pertain to real code and are a test-only
|
||||
/// concern, since in real code, the zone is set-up in `main()`.
|
||||
///
|
||||
/// See also: [runZoned]
|
||||
void initialize() {
|
||||
logReader = new MockDeviceLogReader();
|
||||
discoverer = new ProtocolDiscovery.observatory(logReader);
|
||||
}
|
||||
|
||||
// Inject some lines.
|
||||
logReader.addLine('HELLO WORLD');
|
||||
logReader.addLine('Observatory listening on http://127.0.0.1:9999');
|
||||
// Await the port.
|
||||
Uri uri = await nextUri;
|
||||
expect(uri.port, 9999);
|
||||
expect('$uri', 'http://127.0.0.1:9999');
|
||||
tearDown(() {
|
||||
discoverer.cancel();
|
||||
logReader.dispose();
|
||||
});
|
||||
|
||||
// Get next port future.
|
||||
nextUri = discoverer.nextUri();
|
||||
logReader.addLine('Observatory listening on http://127.0.0.1:3333');
|
||||
uri = await nextUri;
|
||||
expect(uri.port, 3333);
|
||||
expect('$uri', 'http://127.0.0.1:3333');
|
||||
testUsingContext('returns non-null uri future', () async {
|
||||
initialize();
|
||||
expect(discoverer.uri, isNotNull);
|
||||
});
|
||||
|
||||
// Get next port future.
|
||||
nextUri = discoverer.nextUri();
|
||||
// Inject a bad line.
|
||||
logReader.addLine('Observatory listening on http://127.0.0.1:apple');
|
||||
final Uri timeoutUri = Uri.parse('http://timeout');
|
||||
final Uri actualUri = await nextUri.timeout(
|
||||
const Duration(milliseconds: 100), onTimeout: () => timeoutUri);
|
||||
expect(actualUri, timeoutUri);
|
||||
testUsingContext('discovers uri if logs already produced output', () async {
|
||||
initialize();
|
||||
logReader.addLine('HELLO WORLD');
|
||||
logReader.addLine('Observatory listening on http://127.0.0.1:9999');
|
||||
final Uri uri = await discoverer.uri;
|
||||
expect(uri.port, 9999);
|
||||
expect('$uri', 'http://127.0.0.1:9999');
|
||||
});
|
||||
|
||||
// Get next port future.
|
||||
nextUri = discoverer.nextUri();
|
||||
logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:52584');
|
||||
uri = await nextUri;
|
||||
expect(uri.port, 52584);
|
||||
expect('$uri', 'http://127.0.0.1:52584');
|
||||
testUsingContext('discovers uri if logs not yet produced output', () async {
|
||||
initialize();
|
||||
final Future<Uri> uriFuture = discoverer.uri;
|
||||
logReader.addLine('Observatory listening on http://127.0.0.1:3333');
|
||||
final Uri uri = await uriFuture;
|
||||
expect(uri.port, 3333);
|
||||
expect('$uri', 'http://127.0.0.1:3333');
|
||||
});
|
||||
|
||||
// Get next port future.
|
||||
nextUri = discoverer.nextUri();
|
||||
logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:54804/PTwjm8Ii8qg=/');
|
||||
uri = await nextUri;
|
||||
expect(uri.port, 54804);
|
||||
expect('$uri', 'http://127.0.0.1:54804/PTwjm8Ii8qg=/');
|
||||
testUsingContext('uri throws if logs produce bad line', () async {
|
||||
initialize();
|
||||
Timer.run(() {
|
||||
logReader.addLine('Observatory listening on http://127.0.0.1:apple');
|
||||
});
|
||||
expect(discoverer.uri, throwsA(isFormatException));
|
||||
});
|
||||
|
||||
// Get next port future.
|
||||
nextUri = discoverer.nextUri();
|
||||
logReader.addLine('I/flutter : Observatory listening on http://somehost:54804/PTwjm8Ii8qg=/');
|
||||
uri = await nextUri;
|
||||
expect(uri.port, 54804);
|
||||
expect('$uri', 'http://somehost:54804/PTwjm8Ii8qg=/');
|
||||
testUsingContext('uri waits for correct log line', () async {
|
||||
initialize();
|
||||
final Future<Uri> uriFuture = discoverer.uri;
|
||||
logReader.addLine('Observatory not listening...');
|
||||
final Uri timeoutUri = Uri.parse('http://timeout');
|
||||
final Uri actualUri = await uriFuture.timeout(
|
||||
const Duration(milliseconds: 100), onTimeout: () => timeoutUri);
|
||||
expect(actualUri, timeoutUri);
|
||||
});
|
||||
|
||||
discoverer.cancel();
|
||||
logReader.dispose();
|
||||
testUsingContext('discovers uri if log line contains Android prefix', () async {
|
||||
initialize();
|
||||
logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:52584');
|
||||
final Uri uri = await discoverer.uri;
|
||||
expect(uri.port, 52584);
|
||||
expect('$uri', 'http://127.0.0.1:52584');
|
||||
});
|
||||
|
||||
testUsingContext('discovers uri if log line contains auth key', () async {
|
||||
initialize();
|
||||
final Future<Uri> uriFuture = discoverer.uri;
|
||||
logReader.addLine('I/flutter : Observatory listening on http://127.0.0.1:54804/PTwjm8Ii8qg=/');
|
||||
final Uri uri = await uriFuture;
|
||||
expect(uri.port, 54804);
|
||||
expect('$uri', 'http://127.0.0.1:54804/PTwjm8Ii8qg=/');
|
||||
});
|
||||
|
||||
testUsingContext('discovers uri if log line contains non-localhost', () async {
|
||||
initialize();
|
||||
final Future<Uri> uriFuture = discoverer.uri;
|
||||
logReader.addLine('I/flutter : Observatory listening on http://somehost:54804/PTwjm8Ii8qg=/');
|
||||
final Uri uri = await uriFuture;
|
||||
expect(uri.port, 54804);
|
||||
expect('$uri', 'http://somehost:54804/PTwjm8Ii8qg=/');
|
||||
});
|
||||
});
|
||||
|
||||
testUsingContext('port forwarding - default port', () async {
|
||||
final MockDeviceLogReader logReader = new MockDeviceLogReader();
|
||||
final ProtocolDiscovery discoverer = new ProtocolDiscovery(
|
||||
final ProtocolDiscovery discoverer = new ProtocolDiscovery.observatory(
|
||||
logReader,
|
||||
ProtocolDiscovery.kObservatoryService,
|
||||
portForwarder: new MockPortForwarder(99),
|
||||
defaultHostPort: 54777);
|
||||
hostPort: 54777);
|
||||
|
||||
// Get next port future.
|
||||
final Future<Uri> nextUri = discoverer.nextUri();
|
||||
final Future<Uri> nextUri = discoverer.uri;
|
||||
logReader.addLine('I/flutter : Observatory listening on http://somehost:54804/PTwjm8Ii8qg=/');
|
||||
final Uri uri = await nextUri;
|
||||
expect(uri.port, 54777);
|
||||
@@ -92,15 +131,13 @@ void main() {
|
||||
|
||||
testUsingContext('port forwarding - specified port', () async {
|
||||
final MockDeviceLogReader logReader = new MockDeviceLogReader();
|
||||
final ProtocolDiscovery discoverer = new ProtocolDiscovery(
|
||||
final ProtocolDiscovery discoverer = new ProtocolDiscovery.observatory(
|
||||
logReader,
|
||||
ProtocolDiscovery.kObservatoryService,
|
||||
portForwarder: new MockPortForwarder(99),
|
||||
hostPort: 1243,
|
||||
defaultHostPort: 192);
|
||||
hostPort: 1243);
|
||||
|
||||
// Get next port future.
|
||||
final Future<Uri> nextUri = discoverer.nextUri();
|
||||
final Future<Uri> nextUri = discoverer.uri;
|
||||
logReader.addLine('I/flutter : Observatory listening on http://somehost:54804/PTwjm8Ii8qg=/');
|
||||
final Uri uri = await nextUri;
|
||||
expect(uri.port, 1243);
|
||||
|
||||
Reference in New Issue
Block a user