Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: static-server can continue if already running #14307

Merged
merged 5 commits into from
Aug 24, 2022
Merged
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
25 changes: 17 additions & 8 deletions cli/test/fixtures/static-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import esMain from 'es-main';
import {LH_ROOT} from '../../../root.js';

const HEADER_SAFELIST = new Set(['x-robots-tag', 'link', 'content-security-policy']);
const wasInvokedDirectly = esMain(import.meta);

class Server {
baseDir = `${LH_ROOT}/cli/test/fixtures`;
Expand Down Expand Up @@ -249,20 +250,28 @@ class Server {
async function createServers() {
const servers = [10200, 10503, 10420].map(port => {
const server = new Server(port);
server._server.on('error', e => console.error(e.code, e));
server._server.on('error', e => console.error(e.message));
if (wasInvokedDirectly) {
server._server.on('listening', _ => console.log(`listening on http://localhost:${port}`));
}
return server;
});
await Promise.all(servers.map(s => s.listen(s._port, 'localhost')));

const outcomes = await Promise.allSettled(servers.map(s => s.listen(s._port, 'localhost')));
if (outcomes.some(o => o.status === 'rejected')) {
if (outcomes.every(o => o.reason.message.includes('already'))) {
console.warn('😧 Server already up. Continuing…');
} else {
console.error(outcomes.map(o => o.reason));
throw new Error('One or more servers did not start correctly');
}
}
return servers;
}

// If called directly (such as via `yarn static-server`) then start all of the servers.
if (esMain(import.meta)) {
createServers().then(servers => {
for (const server of servers) {
console.log(`listening on http://localhost:${server._port}`);
}
});
if (wasInvokedDirectly) {
createServers();
}

export {
Expand Down