Unverified Commit 6db3d61e authored by Wyte Krongapiradee's avatar Wyte Krongapiradee Committed by GitHub

fix(flutter_driver): Properly declare socket as nullable (#75769)

parent e7953b3b
...@@ -561,7 +561,7 @@ String _getWebSocketUrl(String url) { ...@@ -561,7 +561,7 @@ String _getWebSocketUrl(String url) {
Future<vms.VmService> _waitAndConnect(String url, Map<String, dynamic>? headers) async { Future<vms.VmService> _waitAndConnect(String url, Map<String, dynamic>? headers) async {
final String webSocketUrl = _getWebSocketUrl(url); final String webSocketUrl = _getWebSocketUrl(url);
int attempts = 0; int attempts = 0;
late WebSocket socket; WebSocket? socket;
while (true) { while (true) {
try { try {
socket = await WebSocket.connect(webSocketUrl, headers: headers); socket = await WebSocket.connect(webSocketUrl, headers: headers);
...@@ -575,7 +575,7 @@ Future<vms.VmService> _waitAndConnect(String url, Map<String, dynamic>? headers) ...@@ -575,7 +575,7 @@ Future<vms.VmService> _waitAndConnect(String url, Map<String, dynamic>? headers)
controller.stream, controller.stream,
socket.add, socket.add,
log: null, log: null,
disposeHandler: () => socket.close(), disposeHandler: () => socket!.close(),
streamClosed: streamClosedCompleter.future streamClosed: streamClosedCompleter.future
); );
// This call is to ensure we are able to establish a connection instead of // This call is to ensure we are able to establish a connection instead of
...@@ -583,7 +583,7 @@ Future<vms.VmService> _waitAndConnect(String url, Map<String, dynamic>? headers) ...@@ -583,7 +583,7 @@ Future<vms.VmService> _waitAndConnect(String url, Map<String, dynamic>? headers)
await service.getVersion(); await service.getVersion();
return service; return service;
} catch (e) { } catch (e) {
await socket.close(); await socket?.close();
if (attempts > 5) { if (attempts > 5) {
_log('It is taking an unusually long time to connect to the VM...'); _log('It is taking an unusually long time to connect to the VM...');
} }
......
...@@ -52,6 +52,19 @@ void main() { ...@@ -52,6 +52,19 @@ void main() {
restoreVmServiceConnectFunction(); restoreVmServiceConnectFunction();
}); });
test('Retries while Dart VM service is not available', () async {
// This test case will test the real implementation of `_waitAndConnect`.
restoreVmServiceConnectFunction();
// The actual behavior is to retry indefinitely until the Dart VM service
// becomes available. `.timeout` is used here to exit the infinite loop,
// expecting that no other types of error are thrown during the process.
expect(
vmServiceConnectFunction('http://foo.bar', <String, dynamic>{})
.timeout(const Duration(seconds: 1)),
throwsA(isA<TimeoutException>()),
);
});
test('throws after retries if no isolate', () async { test('throws after retries if no isolate', () async {
fakeVM.numberOfTriesBeforeResolvingIsolate = 10000; fakeVM.numberOfTriesBeforeResolvingIsolate = 10000;
......
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