Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions pkg/pub_integration/lib/src/fake_pub_server_process.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math';

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

final _random = Random.secure();

/// The timeout factor that should be used in integration tests.
final testTimeoutFactor = 6;

/// Wrapper and helper methods around the fake server process.
class FakePubServerProcess {
final int port;
final int storagePort;
final String _tmpDir;
final Process _process;
final _CoverageConfig? _coverageConfig;
Expand All @@ -28,19 +26,19 @@ class FakePubServerProcess {

FakePubServerProcess._(
this.port,
this.storagePort,
this._tmpDir,
this._process,
this._coverageConfig,
);

static Future<FakePubServerProcess> start({String? appDir, int? port}) async {
static Future<FakePubServerProcess> start({String? appDir}) async {
appDir ??= p.join(Directory.current.path, '../../app');
// TODO: check for free port
port ??= 20000 + _random.nextInt(990);
final storagePort = port + 1;
final searchPort = port + 2;
final analyzerPort = port + 3;
final vmPort = port + 5;
final port = await _getFreePort();
final storagePort = await _getFreePort();
final searchPort = await _getFreePort();
final analyzerPort = await _getFreePort();
final vmPort = await _getFreePort();
final coverageConfig = await _CoverageConfig.detect(vmPort);

await _runPubGet(appDir);
Expand Down Expand Up @@ -70,6 +68,7 @@ class FakePubServerProcess {
);
final instance = FakePubServerProcess._(
port,
storagePort,
tmpDir.path,
process,
coverageConfig,
Expand Down Expand Up @@ -316,3 +315,10 @@ void _writeLogs(Stream<List<int>> stream, String prefix) {
},
);
}

Future<int> _getFreePort() async {
final server = await HttpServer.bind('localhost', 0);
final port = server.port;
await server.close();
return port;
}
2 changes: 2 additions & 0 deletions pkg/pub_integration/lib/src/fake_test_context_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class TestContextProvider {
return TestContextProvider._(origin, fakePubServerProcess, testBrowser);
}

int get storagePort => _fakePubServerProcess.storagePort;

Future<void> close() async {
await _testBrowser.close();
await _fakePubServerProcess.kill();
Expand Down
2 changes: 1 addition & 1 deletion pkg/pub_integration/test/exported_bucket_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void main() {

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

Expand Down