Skip to content

Commit

Permalink
Avoid port race condition (finally pls)
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst committed Sep 12, 2022
1 parent b86bdb7 commit dd5fe3f
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 23 deletions.
3 changes: 1 addition & 2 deletions packages/node-integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
"mongodb-memory-server-global": "^7.6.3",
"mysql": "^2.18.1",
"nock": "^13.1.0",
"pg": "^8.7.3",
"portfinder": "^1.0.28"
"pg": "^8.7.3"
},
"config": {
"mongodbMemoryServer": {
Expand Down
19 changes: 4 additions & 15 deletions packages/node-integration-tests/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { logger, parseSemver } from '@sentry/utils';
import axios, { AxiosRequestConfig } from 'axios';
import { Express } from 'express';
import * as http from 'http';
import { AddressInfo } from 'net';
import nock from 'nock';
import * as path from 'path';
import { getPorts } from 'portfinder';

export type TestServerConfig = {
url: string;
Expand Down Expand Up @@ -151,20 +151,9 @@ export class TestEnv {
}
});

getPorts(50, {}, (err, ports) => {
if (err) {
throw err;
}

const port = ports.find(
// Only allow ports that do not overlap with other workers - this is done to avoid race-conditions
p => p % Number(process.env.TEST_WORKERS_AMOUNT) === Number(process.env.TEST_PORT_MODULO),
);

const url = `http://localhost:${port}/test`;
const server = app.listen(port, () => {
resolve([server, url]);
});
const server = app.listen(0, () => {
const url = `http://localhost:${(server.address() as AddressInfo).port}/test`;
resolve([server, url]);
});
});

Expand Down
6 changes: 1 addition & 5 deletions packages/node-integration-tests/utils/run-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ const threads = os.cpus().map(async (_, i) => {
while (testPath !== undefined) {
console.log(`(Worker ${i}) Running test "${testPath}"`);
await new Promise(resolve => {
const p = childProcess.spawn('jest', ['--runTestsByPath', testPath as string, '--forceExit'], {
// These env vars are used to give workers a limited, non-overlapping set of ports to occupy.
// This is done to avoid race-conditions during port reservation.
env: { ...process.env, TEST_WORKERS_AMOUNT: String(os.cpus().length), TEST_PORT_MODULO: String(i) },
});
const p = childProcess.spawn('jest', ['--runTestsByPath', testPath as string, '--forceExit']);

let output = '';

Expand Down
3 changes: 2 additions & 1 deletion packages/remix/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
},
"devDependencies": {
"@remix-run/node": "^1.4.3",
"@remix-run/react": "^1.4.3"
"@remix-run/react": "^1.4.3",
"portfinder": "^1.0.28"
},
"peerDependencies": {
"@remix-run/node": "1.x",
Expand Down

0 comments on commit dd5fe3f

Please sign in to comment.