Skip to content

Commit 0d02314

Browse files
authored
Fix race condition in flutter test when passing --serve-observatory (#123556)
`flutter test` wasn't awaiting the `_serveObservatory` request which was causing a race condition in the `flutter test should respect --serve-observatory` test in `test/integration.general/test_test.dart`. Related to flutter/flutter#123516
1 parent a573d1a commit 0d02314

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

packages/flutter_tools/lib/src/test/flutter_tester_device.dart

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
6-
75
import 'dart:async';
86
import 'dart:io' as io; // flutter_ignore: dart_io_import;
97

@@ -180,21 +178,19 @@ class FlutterTesterTestDevice extends TestDevice {
180178
}
181179

182180
logger.printTrace('Connecting to service protocol: $forwardingUri');
183-
final Future<FlutterVmService> localVmService = connectToVmService(
181+
final FlutterVmService vmService = await connectToVmServiceImpl(
184182
forwardingUri!,
185183
compileExpression: compileExpression,
186184
logger: logger,
187185
);
188-
unawaited(localVmService.then((FlutterVmService vmservice) async {
189-
logger.printTrace('test $id: Successfully connected to service protocol: $forwardingUri');
190-
if (debuggingOptions.serveObservatory) {
191-
try {
192-
await vmservice.callMethodWrapper('_serveObservatory');
193-
} on vm_service.RPCError {
194-
logger.printWarning('Unable to enable Observatory');
195-
}
186+
logger.printTrace('test $id: Successfully connected to service protocol: $forwardingUri');
187+
if (debuggingOptions.serveObservatory) {
188+
try {
189+
await vmService.callMethodWrapper('_serveObservatory');
190+
} on vm_service.RPCError {
191+
logger.printWarning('Unable to enable Observatory');
196192
}
197-
}));
193+
}
198194

199195
if (debuggingOptions.startPaused && !machine!) {
200196
logger.printStatus('The Dart VM service is listening on $forwardingUri');
@@ -268,6 +264,20 @@ class FlutterTesterTestDevice extends TestDevice {
268264
);
269265
}
270266

267+
@visibleForTesting
268+
@protected
269+
Future<FlutterVmService> connectToVmServiceImpl(
270+
Uri httpUri, {
271+
CompileExpression? compileExpression,
272+
required Logger logger,
273+
}) {
274+
return connectToVmService(
275+
httpUri,
276+
compileExpression: compileExpression,
277+
logger: logger,
278+
);
279+
}
280+
271281
Future<void> _startDevTools(Uri forwardingUri, DartDevelopmentService? dds) async {
272282
_devToolsLauncher = DevtoolsLauncher.instance;
273283
logger.printTrace('test $id: Serving DevTools...');

packages/flutter_tools/test/general.shard/flutter_tester_device_test.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ import 'package:flutter_tools/src/build_info.dart';
1414
import 'package:flutter_tools/src/device.dart';
1515
import 'package:flutter_tools/src/test/flutter_tester_device.dart';
1616
import 'package:flutter_tools/src/test/font_config_manager.dart';
17+
import 'package:flutter_tools/src/vmservice.dart';
1718
import 'package:stream_channel/stream_channel.dart';
1819
import 'package:test/fake.dart';
1920

20-
import '../src/common.dart';
2121
import '../src/context.dart';
2222
import '../src/fake_process_manager.dart';
23+
import '../src/fake_vm_services.dart';
2324

2425
void main() {
2526
late FakePlatform platform;
@@ -251,6 +252,17 @@ class TestFlutterTesterDevice extends FlutterTesterTestDevice {
251252
return dds;
252253
}
253254

255+
@override
256+
Future<FlutterVmService> connectToVmServiceImpl(
257+
Uri httpUri, {
258+
CompileExpression? compileExpression,
259+
required Logger logger,
260+
}) async {
261+
return FakeVmServiceHost(requests: <VmServiceExpectation>[
262+
const FakeVmServiceRequest(method: '_serveObservatory'),
263+
]).vmService;
264+
}
265+
254266
@override
255267
Future<HttpServer> bind(InternetAddress? host, int port) async => FakeHttpServer();
256268

0 commit comments

Comments
 (0)