55import 'dart:async' ;
66import 'dart:convert' ;
77import 'dart:io' ;
8- import 'dart:math' ;
98
109import 'package:path/path.dart' as p;
1110import 'package:retry/retry.dart' ;
1211
13- final _random = Random .secure ();
14-
1512/// The timeout factor that should be used in integration tests.
1613final testTimeoutFactor = 6 ;
1714
1815/// Wrapper and helper methods around the fake server process.
1916class FakePubServerProcess {
2017 final int port;
18+ final int storagePort;
2119 final String _tmpDir;
2220 final Process _process;
2321 final _CoverageConfig ? _coverageConfig;
@@ -28,19 +26,19 @@ class FakePubServerProcess {
2826
2927 FakePubServerProcess ._(
3028 this .port,
29+ this .storagePort,
3130 this ._tmpDir,
3231 this ._process,
3332 this ._coverageConfig,
3433 );
3534
36- static Future <FakePubServerProcess > start ({String ? appDir, int ? port }) async {
35+ static Future <FakePubServerProcess > start ({String ? appDir}) async {
3736 appDir ?? = p.join (Directory .current.path, '../../app' );
38- // TODO: check for free port
39- port ?? = 20000 + _random.nextInt (990 );
40- final storagePort = port + 1 ;
41- final searchPort = port + 2 ;
42- final analyzerPort = port + 3 ;
43- final vmPort = port + 5 ;
37+ final port = await _getFreePort ();
38+ final storagePort = await _getFreePort ();
39+ final searchPort = await _getFreePort ();
40+ final analyzerPort = await _getFreePort ();
41+ final vmPort = await _getFreePort ();
4442 final coverageConfig = await _CoverageConfig .detect (vmPort);
4543
4644 await _runPubGet (appDir);
@@ -70,6 +68,7 @@ class FakePubServerProcess {
7068 );
7169 final instance = FakePubServerProcess ._(
7270 port,
71+ storagePort,
7372 tmpDir.path,
7473 process,
7574 coverageConfig,
@@ -316,3 +315,10 @@ void _writeLogs(Stream<List<int>> stream, String prefix) {
316315 },
317316 );
318317}
318+
319+ Future <int > _getFreePort () async {
320+ final server = await HttpServer .bind ('localhost' , 0 );
321+ final port = server.port;
322+ await server.close ();
323+ return port;
324+ }
0 commit comments