Skip to content

Commit 062e63a

Browse files
authored
Better port allocation to reduce port conflicts in integration tests on CI. (#9087)
1 parent 7f9e0eb commit 062e63a

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

pkg/pub_integration/lib/src/fake_pub_server_process.dart

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@
55
import 'dart:async';
66
import 'dart:convert';
77
import 'dart:io';
8-
import 'dart:math';
98

109
import 'package:path/path.dart' as p;
1110
import 'package:retry/retry.dart';
1211

13-
final _random = Random.secure();
14-
1512
/// The timeout factor that should be used in integration tests.
1613
final testTimeoutFactor = 6;
1714

1815
/// Wrapper and helper methods around the fake server process.
1916
class 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+
}

pkg/pub_integration/lib/src/fake_test_context_provider.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class TestContextProvider {
5353
return TestContextProvider._(origin, fakePubServerProcess, testBrowser);
5454
}
5555

56+
int get storagePort => _fakePubServerProcess.storagePort;
57+
5658
Future<void> close() async {
5759
await _testBrowser.close();
5860
await _fakePubServerProcess.kill();

pkg/pub_integration/test/exported_bucket_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void main() {
2828

2929
final pubUri = Uri.parse(fakeTestScenario.pubHostedUrl);
3030
final storageUri = pubUri.replace(
31-
port: pubUri.port + 1,
31+
port: fakeTestScenario.storagePort,
3232
path: '/fake-exported-apis/latest',
3333
);
3434

0 commit comments

Comments
 (0)