|
3 | 3 | // found in the LICENSE file. |
4 | 4 |
|
5 | 5 | import 'package:file/file.dart'; |
| 6 | +import 'package:flutter_tools/src/base/io.dart'; |
6 | 7 | import 'package:vm_service/vm_service.dart'; |
7 | 8 |
|
8 | 9 | import '../src/common.dart'; |
9 | 10 | import 'test_data/basic_project.dart'; |
10 | 11 | import 'test_driver.dart'; |
11 | 12 | import 'test_utils.dart'; |
12 | 13 |
|
| 14 | +Future<int> getFreePort() async { |
| 15 | + int port = 0; |
| 16 | + final ServerSocket serverSocket = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0); |
| 17 | + port = serverSocket.port; |
| 18 | + await serverSocket.close(); |
| 19 | + return port; |
| 20 | +} |
| 21 | + |
13 | 22 | void main() { |
14 | | - late FlutterRunTestDriver flutterRun, flutterAttach; |
15 | 23 | final BasicProject project = BasicProject(); |
16 | 24 | late Directory tempDir; |
17 | 25 |
|
18 | 26 | setUp(() async { |
19 | 27 | tempDir = createResolvedTempDirectorySync('attach_test.'); |
20 | 28 | await project.setUpIn(tempDir); |
21 | | - flutterRun = FlutterRunTestDriver(tempDir, logPrefix: ' RUN '); |
22 | | - flutterAttach = FlutterRunTestDriver( |
23 | | - tempDir, |
24 | | - logPrefix: 'ATTACH ', |
25 | | - // Only one DDS instance can be connected to the VM service at a time. |
26 | | - // DDS can also only initialize if the VM service doesn't have any existing |
27 | | - // clients, so we'll just let _flutterRun be responsible for spawning DDS. |
28 | | - spawnDdsInstance: false, |
29 | | - ); |
30 | 29 | }); |
31 | 30 |
|
32 | | - tearDown(() async { |
33 | | - await flutterAttach.detach(); |
34 | | - await flutterRun.stop(); |
| 31 | + tearDown(() { |
35 | 32 | tryToDelete(tempDir); |
36 | 33 | }); |
37 | 34 |
|
38 | | - testWithoutContext('can hot reload', () async { |
39 | | - await flutterRun.run(withDebugger: true); |
40 | | - await flutterAttach.attach(flutterRun.vmServicePort!); |
41 | | - await flutterAttach.hotReload(); |
42 | | - }); |
| 35 | + group('DDS in flutter run', () { |
| 36 | + late FlutterRunTestDriver flutterRun, flutterAttach; |
| 37 | + setUp(() { |
| 38 | + flutterRun = FlutterRunTestDriver(tempDir, logPrefix: ' RUN '); |
| 39 | + flutterAttach = FlutterRunTestDriver( |
| 40 | + tempDir, |
| 41 | + logPrefix: 'ATTACH ', |
| 42 | + // Only one DDS instance can be connected to the VM service at a time. |
| 43 | + // DDS can also only initialize if the VM service doesn't have any existing |
| 44 | + // clients, so we'll just let _flutterRun be responsible for spawning DDS. |
| 45 | + spawnDdsInstance: false, |
| 46 | + ); |
| 47 | + }); |
43 | 48 |
|
44 | | - testWithoutContext('can detach, reattach, hot reload', () async { |
45 | | - await flutterRun.run(withDebugger: true); |
46 | | - await flutterAttach.attach(flutterRun.vmServicePort!); |
47 | | - await flutterAttach.detach(); |
48 | | - await flutterAttach.attach(flutterRun.vmServicePort!); |
49 | | - await flutterAttach.hotReload(); |
50 | | - }); |
| 49 | + tearDown(() async { |
| 50 | + await flutterAttach.detach(); |
| 51 | + await flutterRun.stop(); |
| 52 | + }); |
| 53 | + |
| 54 | + testWithoutContext('can hot reload', () async { |
| 55 | + await flutterRun.run(withDebugger: true); |
| 56 | + await flutterAttach.attach(flutterRun.vmServicePort!); |
| 57 | + await flutterAttach.hotReload(); |
| 58 | + }); |
| 59 | + |
| 60 | + testWithoutContext('can detach, reattach, hot reload', () async { |
| 61 | + await flutterRun.run(withDebugger: true); |
| 62 | + await flutterAttach.attach(flutterRun.vmServicePort!); |
| 63 | + await flutterAttach.detach(); |
| 64 | + await flutterAttach.attach(flutterRun.vmServicePort!); |
| 65 | + await flutterAttach.hotReload(); |
| 66 | + }); |
| 67 | + |
| 68 | + testWithoutContext('killing process behaves the same as detach ', () async { |
| 69 | + await flutterRun.run(withDebugger: true); |
| 70 | + await flutterAttach.attach(flutterRun.vmServicePort!); |
| 71 | + await flutterAttach.quit(); |
| 72 | + flutterAttach = FlutterRunTestDriver( |
| 73 | + tempDir, |
| 74 | + logPrefix: 'ATTACH-2', |
| 75 | + spawnDdsInstance: false, |
| 76 | + ); |
| 77 | + await flutterAttach.attach(flutterRun.vmServicePort!); |
| 78 | + await flutterAttach.hotReload(); |
| 79 | + }); |
51 | 80 |
|
52 | | - testWithoutContext('killing process behaves the same as detach ', () async { |
53 | | - await flutterRun.run(withDebugger: true); |
54 | | - await flutterAttach.attach(flutterRun.vmServicePort!); |
55 | | - await flutterAttach.quit(); |
56 | | - flutterAttach = FlutterRunTestDriver( |
57 | | - tempDir, |
58 | | - logPrefix: 'ATTACH-2', |
59 | | - spawnDdsInstance: false, |
60 | | - ); |
61 | | - await flutterAttach.attach(flutterRun.vmServicePort!); |
62 | | - await flutterAttach.hotReload(); |
| 81 | + testWithoutContext('sets activeDevToolsServerAddress extension', () async { |
| 82 | + await flutterRun.run( |
| 83 | + startPaused: true, |
| 84 | + withDebugger: true, |
| 85 | + additionalCommandArgs: <String>['--devtools-server-address', 'http://127.0.0.1:9105'], |
| 86 | + ); |
| 87 | + await flutterRun.resume(); |
| 88 | + await pollForServiceExtensionValue<String>( |
| 89 | + testDriver: flutterRun, |
| 90 | + extension: 'ext.flutter.activeDevToolsServerAddress', |
| 91 | + continuePollingValue: '', |
| 92 | + matches: equals('http://127.0.0.1:9105'), |
| 93 | + ); |
| 94 | + await pollForServiceExtensionValue<String>( |
| 95 | + testDriver: flutterRun, |
| 96 | + extension: 'ext.flutter.connectedVmServiceUri', |
| 97 | + continuePollingValue: '', |
| 98 | + matches: isNotEmpty, |
| 99 | + ); |
| 100 | + |
| 101 | + final Response response = await flutterRun.callServiceExtension('ext.flutter.connectedVmServiceUri'); |
| 102 | + final String vmServiceUri = response.json!['value'] as String; |
| 103 | + |
| 104 | + // Attach with a different DevTools server address. |
| 105 | + await flutterAttach.attach( |
| 106 | + flutterRun.vmServicePort!, |
| 107 | + additionalCommandArgs: <String>['--devtools-server-address', 'http://127.0.0.1:9110'], |
| 108 | + ); |
| 109 | + await pollForServiceExtensionValue<String>( |
| 110 | + testDriver: flutterAttach, |
| 111 | + extension: 'ext.flutter.activeDevToolsServerAddress', |
| 112 | + continuePollingValue: '', |
| 113 | + matches: equals('http://127.0.0.1:9110'), |
| 114 | + ); |
| 115 | + await pollForServiceExtensionValue<String>( |
| 116 | + testDriver: flutterRun, |
| 117 | + extension: 'ext.flutter.connectedVmServiceUri', |
| 118 | + continuePollingValue: '', |
| 119 | + matches: equals(vmServiceUri), |
| 120 | + ); |
| 121 | + }); |
63 | 122 | }); |
64 | 123 |
|
65 | | - testWithoutContext('sets activeDevToolsServerAddress extension', () async { |
66 | | - await flutterRun.run( |
67 | | - startPaused: true, |
68 | | - withDebugger: true, |
69 | | - additionalCommandArgs: <String>['--devtools-server-address', 'http://127.0.0.1:9105'], |
70 | | - ); |
71 | | - await flutterRun.resume(); |
72 | | - await pollForServiceExtensionValue<String>( |
73 | | - testDriver: flutterRun, |
74 | | - extension: 'ext.flutter.activeDevToolsServerAddress', |
75 | | - continuePollingValue: '', |
76 | | - matches: equals('http://127.0.0.1:9105'), |
77 | | - ); |
78 | | - await pollForServiceExtensionValue<String>( |
79 | | - testDriver: flutterRun, |
80 | | - extension: 'ext.flutter.connectedVmServiceUri', |
81 | | - continuePollingValue: '', |
82 | | - matches: isNotEmpty, |
83 | | - ); |
84 | | - |
85 | | - final Response response = await flutterRun.callServiceExtension('ext.flutter.connectedVmServiceUri'); |
86 | | - final String vmServiceUri = response.json!['value'] as String; |
87 | | - |
88 | | - // Attach with a different DevTools server address. |
89 | | - await flutterAttach.attach( |
90 | | - flutterRun.vmServicePort!, |
91 | | - additionalCommandArgs: <String>['--devtools-server-address', 'http://127.0.0.1:9110'], |
92 | | - ); |
93 | | - await pollForServiceExtensionValue<String>( |
94 | | - testDriver: flutterAttach, |
95 | | - extension: 'ext.flutter.activeDevToolsServerAddress', |
96 | | - continuePollingValue: '', |
97 | | - matches: equals('http://127.0.0.1:9110'), |
98 | | - ); |
99 | | - await pollForServiceExtensionValue<String>( |
100 | | - testDriver: flutterRun, |
101 | | - extension: 'ext.flutter.connectedVmServiceUri', |
102 | | - continuePollingValue: '', |
103 | | - matches: equals(vmServiceUri), |
104 | | - ); |
| 124 | + group('DDS in flutter attach', () { |
| 125 | + late FlutterRunTestDriver flutterRun, flutterAttach; |
| 126 | + setUp(() { |
| 127 | + flutterRun = FlutterRunTestDriver( |
| 128 | + tempDir, |
| 129 | + logPrefix: ' RUN ', |
| 130 | + spawnDdsInstance: false, |
| 131 | + ); |
| 132 | + flutterAttach = FlutterRunTestDriver( |
| 133 | + tempDir, |
| 134 | + logPrefix: 'ATTACH ', |
| 135 | + ); |
| 136 | + }); |
| 137 | + |
| 138 | + tearDown(() async { |
| 139 | + await flutterAttach.detach(); |
| 140 | + await flutterRun.stop(); |
| 141 | + }); |
| 142 | + |
| 143 | + testWithoutContext('uses the designated dds port', () async { |
| 144 | + final int ddsPort = await getFreePort(); |
| 145 | + |
| 146 | + await flutterRun.run(withDebugger: true); |
| 147 | + await flutterAttach.attach( |
| 148 | + flutterRun.vmServicePort!, |
| 149 | + additionalCommandArgs: <String>[ |
| 150 | + '--dds-port=$ddsPort', |
| 151 | + ], |
| 152 | + ); |
| 153 | + |
| 154 | + final Response response = await flutterAttach.callServiceExtension('ext.flutter.connectedVmServiceUri'); |
| 155 | + final String vmServiceUriString = response.json!['value'] as String; |
| 156 | + final Uri vmServiceUri = Uri.parse(vmServiceUriString); |
| 157 | + expect(vmServiceUri.port, equals(ddsPort)); |
| 158 | + }); |
105 | 159 | }); |
106 | 160 | } |
0 commit comments