Unverified Commit 3d3b5349 authored by Helin Shiah's avatar Helin Shiah Committed by GitHub

Return host and port from devtools launcher (#63795)

parent 8a4ada25
...@@ -256,7 +256,11 @@ The returned `params` will contain: ...@@ -256,7 +256,11 @@ The returned `params` will contain:
#### devtools.serve #### devtools.serve
The `serve()` command starts a DevTools server if one isn't already running and returns the host and port of the server. The `serve()` command starts a DevTools server if one isn't already running. The return value will contain:
- `success` - whether the server started.
- `host` - the address host if the server successfully started.
- `port` - the port if the server successfully started.
## 'flutter run --machine' and 'flutter attach --machine' ## 'flutter run --machine' and 'flutter attach --machine'
......
...@@ -897,11 +897,11 @@ class DevToolsDomain extends Domain { ...@@ -897,11 +897,11 @@ class DevToolsDomain extends Domain {
Future<Map<String, dynamic>> serve([ Map<String, dynamic> args ]) async { Future<Map<String, dynamic>> serve([ Map<String, dynamic> args ]) async {
_devtoolsLauncher ??= DevtoolsLauncher.instance; _devtoolsLauncher ??= DevtoolsLauncher.instance;
final HttpServer server = await _devtoolsLauncher.serve(); final DevToolsServerAddress server = await _devtoolsLauncher.serve();
return<String, dynamic>{ return<String, dynamic>{
'host': server.address.host, 'host': server?.host,
'port': server.port, 'port': server?.port,
}; };
} }
......
...@@ -1723,15 +1723,16 @@ class DevtoolsLauncher { ...@@ -1723,15 +1723,16 @@ class DevtoolsLauncher {
} }
} }
Future<io.HttpServer> serve() async { Future<DevToolsServerAddress> serve() async {
try { try {
_devtoolsServer ??= await devtools_server.serveDevTools( _devtoolsServer ??= await devtools_server.serveDevTools(
enableStdinCommands: false, enableStdinCommands: false,
); );
return DevToolsServerAddress(_devtoolsServer.address.host, _devtoolsServer.port);
} on Exception catch (e, st) { } on Exception catch (e, st) {
globals.printTrace('Failed to serve DevTools: $e\n$st'); globals.printTrace('Failed to serve DevTools: $e\n$st');
return null;
} }
return _devtoolsServer;
} }
Future<void> close() async { Future<void> close() async {
...@@ -1741,3 +1742,10 @@ class DevtoolsLauncher { ...@@ -1741,3 +1742,10 @@ class DevtoolsLauncher {
static DevtoolsLauncher get instance => context.get<DevtoolsLauncher>() ?? DevtoolsLauncher(); static DevtoolsLauncher get instance => context.get<DevtoolsLauncher>() ?? DevtoolsLauncher();
} }
class DevToolsServerAddress {
DevToolsServerAddress(this.host, this.port);
final String host;
final int port;
}
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:async'; import 'dart:async';
import 'dart:io';
import 'package:flutter_tools/src/android/android_workflow.dart'; import 'package:flutter_tools/src/android/android_workflow.dart';
import 'package:flutter_tools/src/base/common.dart'; import 'package:flutter_tools/src/base/common.dart';
...@@ -304,7 +303,7 @@ void main() { ...@@ -304,7 +303,7 @@ void main() {
await input.close(); await input.close();
}); });
testUsingContext('devtools.serve command should return host and port', () async { testUsingContext('devtools.serve command should return host and port on success', () async {
final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>(); final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>();
final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>(); final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>();
daemon = Daemon( daemon = Daemon(
...@@ -312,19 +311,34 @@ void main() { ...@@ -312,19 +311,34 @@ void main() {
responses.add, responses.add,
notifyingLogger: notifyingLogger, notifyingLogger: notifyingLogger,
); );
final HttpServer mockDevToolsServer = MockDevToolsServer(); when(mockDevToolsLauncher.serve()).thenAnswer((_) async => DevToolsServerAddress('127.0.0.1', 1234));
final InternetAddress mockInternetAddress = MockInternetAddress();
when(mockDevToolsServer.address).thenReturn(mockInternetAddress);
when(mockInternetAddress.host).thenReturn('127.0.0.1');
when(mockDevToolsServer.port).thenReturn(1234);
when(mockDevToolsLauncher.serve()).thenAnswer((_) async => mockDevToolsServer); commands.add(<String, dynamic>{'id': 0, 'method': 'devtools.serve'});
final Map<String, dynamic> response = await responses.stream.firstWhere((Map<String, dynamic> response) => response['id'] == 0);
expect(response['result'], isNotEmpty);
expect(response['result']['host'], '127.0.0.1');
expect(response['result']['port'], 1234);
await responses.close();
await commands.close();
}, overrides: <Type, Generator>{
DevtoolsLauncher: () => mockDevToolsLauncher,
});
testUsingContext('devtools.serve command should return null fields if null returned', () async {
final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>();
final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>();
daemon = Daemon(
commands.stream,
responses.add,
notifyingLogger: notifyingLogger,
);
when(mockDevToolsLauncher.serve()).thenAnswer((_) async => null);
commands.add(<String, dynamic>{'id': 0, 'method': 'devtools.serve'}); commands.add(<String, dynamic>{'id': 0, 'method': 'devtools.serve'});
final Map<String, dynamic> response = await responses.stream.firstWhere((Map<String, dynamic> response) => response['id'] == 0); final Map<String, dynamic> response = await responses.stream.firstWhere((Map<String, dynamic> response) => response['id'] == 0);
expect(response['result'], isNotEmpty); expect(response['result'], isNotEmpty);
expect(response['result']['host'], equals('127.0.0.1')); expect(response['result']['host'], null);
expect(response['result']['port'], equals(1234)); expect(response['result']['port'], null);
await responses.close(); await responses.close();
await commands.close(); await commands.close();
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
......
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