Skip to content

Commit

Permalink
chore(fake-server): handle server listen errors and retry
Browse files Browse the repository at this point in the history
Adds error handling and retry logic to the `listenPromise` function in the
`fake-server.ts` file. This ensures that the server can gracefully handle
cases where the address is already in use, and retry listening on the
specified port after a short delay.
This might aleviate our timeout flaky tests
  • Loading branch information
sandor-trombitas committed Feb 5, 2025
1 parent f33df13 commit 2452921
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions test/acceptance/fake-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -943,12 +943,28 @@ export const fakeServer = (basePath: string, snykToken: string): FakeServer => {
});

const listenPromise = (port: string | number) => {
return new Promise<void>((resolve) => {
return new Promise<void>((resolve, reject) => {
server = http.createServer(app).listen(Number(port), resolve);

server?.on('connection', (socket) => {
if (server === undefined) {
reject(new Error('Server not created'));
return;
}

server.on('connection', (socket) => {
sockets.add(socket);
});

server.on('error', (e) => {
console.log('Error in server.listen', e);
if ((e as NodeJS.ErrnoException).code === 'EADDRINUSE') {
console.error('Address in use, retrying...');
setTimeout(() => {
server?.close();
server?.listen(port, resolve);
}, 1000);
}
});
});
};

Expand Down

0 comments on commit 2452921

Please sign in to comment.