Skip to content

Commit

Permalink
tests: static-server can continue if already running (#14307)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed Aug 24, 2022
1 parent 2f77ba5 commit 02b72d2
Showing 1 changed file with 17 additions and 8 deletions.
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

0 comments on commit 02b72d2

Please sign in to comment.