Skip to content

Commit e4b7631

Browse files
author
Luca Forstner
committed
Avoid port race condition
1 parent b0b65c7 commit e4b7631

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

packages/node-integration-tests/utils/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Express } from 'express';
77
import * as http from 'http';
88
import nock from 'nock';
99
import * as path from 'path';
10-
import { getPortPromise } from 'portfinder';
10+
import { getPorts } from 'portfinder';
1111

1212
export type TestServerConfig = {
1313
url: string;
@@ -151,7 +151,16 @@ export class TestEnv {
151151
}
152152
});
153153

154-
void getPortPromise().then(port => {
154+
getPorts(50, {}, (err, ports) => {
155+
if (err) {
156+
throw err;
157+
}
158+
159+
const port = ports.find(
160+
// Only allow ports that do not overlap with other workers - this is done to avoid race-conditions
161+
p => p % Number(process.env.TEST_WORKERS_AMOUNT) === Number(process.env.TEST_PORT_MODULO),
162+
);
163+
155164
const url = `http://localhost:${port}/test`;
156165
const server = app.listen(port, () => {
157166
resolve([server, url]);

packages/node-integration-tests/utils/run-tests.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ const threads = os.cpus().map(async (_, i) => {
1313
while (testPath !== undefined) {
1414
console.log(`(Worker ${i}) Running test "${testPath}"`);
1515
await new Promise(resolve => {
16-
const p = childProcess.spawn('jest', ['--runTestsByPath', testPath as string, '--forceExit']);
16+
const p = childProcess.spawn('jest', ['--runTestsByPath', testPath as string, '--forceExit'], {
17+
// These env vars are used to give workers a limited, non-overlapping set of ports to occupy.
18+
// This is done to avoid race-conditions during port reservation.
19+
env: { ...process.env, TEST_WORKERS_AMOUNT: String(os.cpus().length), TEST_PORT_MODULO: String(i) },
20+
});
1721

1822
let output = '';
1923

0 commit comments

Comments
 (0)