Skip to content

Commit

Permalink
test(e2e): check if port is available (web-infra-dev#992)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Dec 18, 2023
1 parent afcca09 commit d4664df
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
4 changes: 2 additions & 2 deletions e2e/cases/cli/reload-config/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test('should restart dev server and reload config when config file changed', asy
root: 'dist',
},
},
server: { port: ${getRandomPort()} }
server: { port: ${await getRandomPort()} }
};`,
);

Expand All @@ -45,7 +45,7 @@ test('should restart dev server and reload config when config file changed', asy
root: 'dist-2',
},
},
server: { port: ${getRandomPort()} }
server: { port: ${await getRandomPort()} }
};`,
);

Expand Down
2 changes: 1 addition & 1 deletion e2e/cases/cli/reload-env/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test.skip('should restart dev server when .env file is changed', async () => {
output: {
disableFilenameHash: true,
},
server: { port: ${getRandomPort()} }
server: { port: ${await getRandomPort()} }
};`,
);

Expand Down
2 changes: 1 addition & 1 deletion e2e/cases/preview/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test('should allow plugin to modify preview server config', async ({
page,
}) => {
const cwd = join(__dirname, 'basic');
const PORT = getRandomPort();
const PORT = await getRandomPort();

const plugin: RsbuildPlugin = {
name: 'test',
Expand Down
2 changes: 1 addition & 1 deletion e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
"@rsbuild/plugin-pug": "workspace:*",
"@rsbuild/plugin-react": "workspace:*",
"@rsbuild/plugin-rem": "workspace:*",
"@rsbuild/plugin-stylus": "workspace:*",
"@rsbuild/plugin-solid": "workspace:*",
"@rsbuild/plugin-source-build": "workspace:*",
"@rsbuild/plugin-styled-components": "workspace:*",
"@rsbuild/plugin-stylus": "workspace:*",
"@rsbuild/plugin-svelte": "workspace:*",
"@rsbuild/plugin-svgr": "workspace:*",
"@rsbuild/plugin-swc": "workspace:*",
Expand Down
25 changes: 22 additions & 3 deletions e2e/scripts/shared.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import net from 'net';
import { URL } from 'url';
import assert from 'assert';
import { join } from 'path';
Expand Down Expand Up @@ -73,16 +74,34 @@ export const createRsbuild = async (
return rsbuild;
};

function isPortAvailable(port: number) {
const server = net.createServer().listen(port);
return new Promise((resolve, reject) => {
server.on('listening', () => {
server.close();
resolve(true);
});

server.on('error', (err: { code: string }) => {
if (err.code === 'EADDRINUSE') {
resolve(false);
} else {
reject(err);
}
});
});
}

const portMap = new Map();

// Available port ranges: 1024 ~ 65535
// `10080` is not available in CI, so we start with `11000`.
export function getRandomPort(
export async function getRandomPort(
defaultPort = Math.ceil(Math.random() * 50000) + 11000,
) {
let port = defaultPort;
while (true) {
if (!portMap.get(port)) {
if (!portMap.get(port) && (await isPortAvailable(port))) {
portMap.set(port, 1);
return port;
} else {
Expand All @@ -105,7 +124,7 @@ const updateConfigForTest = async (
// make devPort random to avoid port conflict
config.server = {
...(config.server || {}),
port: getRandomPort(config.server?.port),
port: await getRandomPort(config.server?.port),
};

config.dev ??= {};
Expand Down

0 comments on commit d4664df

Please sign in to comment.