Skip to content

Commit

Permalink
Merge pull request #1824 from ably/improve-test-server-error-handling
Browse files Browse the repository at this point in the history
Improve test server error handling
  • Loading branch information
lawrence-forooghian authored Aug 1, 2024
2 parents d30826f + 3ce7032 commit 0977483
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion test/support/runPlaywrightTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const playwrightBrowsers = ['chromium', 'firefox', 'webkit'];
const mochaServer = new MochaServer(/* playwrightTest: */ true);

const runTests = async (browserType) => {
mochaServer.listen();
await mochaServer.listen();
const browser = await browserType.launch();
const context = await browser.newContext();
const page = await context.newPage();
Expand Down
23 changes: 20 additions & 3 deletions test/web_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var express = require('express'),
*/

class MochaServer {
servers = [];

constructor(playwrightTest) {
this.playwrightTest = playwrightTest;
}
Expand Down Expand Up @@ -46,13 +48,28 @@ class MochaServer {
app.use(express.static(__dirname));

const port = process.env.PORT || 3000;
this.server = app.listen(port);
// Explicitly listen on the IPv4 and IPv6 loopback interfaces. If you don’t
// pass an address to `app.listen`, then it will bind in a manner that
// succeeds even if the IPv4 socket address is already in use, as long as
// the IPv6 socket address is free. This can lead to confusion.
await this.startServer(app, '127.0.0.1', port);
await this.startServer(app, '::1', port);

console.log(`Mocha test server listening on http://localhost:${port}/`);
}

console.log('Mocha test server listening on http://localhost:3000/');
async startServer(app, address, port) {
await new Promise((resolve, reject) => {
const server = app.listen(port, address, resolve);
this.servers.push(server);
server.once('error', reject);
});
}

close() {
this.server.close();
for (const server of this.servers) {
server.close();
}
}
}

Expand Down

0 comments on commit 0977483

Please sign in to comment.