Skip to content

Commit

Permalink
Wait a random amount of time before selecting a port (#92)
Browse files Browse the repository at this point in the history
To try to avoid port conflicts when multiple test runs start at the same time.
  • Loading branch information
ahuth committed Mar 20, 2024
1 parent 2fc9022 commit 8408ddf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- [fix] Wait a random amount of time before selecting a port, to try to avoid port conflicts when multiple test runs start at the same time [#92](https://github.com/chanzuckerberg/axe-storybook-testing/pull/92)

# 8.0.0 (2024-03-05)

- [breaking] Support Node >= 18 [#91](https://github.com/chanzuckerberg/axe-storybook-testing/pull/91)
Expand Down
9 changes: 9 additions & 0 deletions src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ async function getServer(options: Options): Promise<Server> {
};
}

// Try to prevent port conflicts when multiple test runs start at the exact same time (such as in
// a monorepo).
await waitRandomTime(500);

const localPath = getStaticStorybookPath(options);
const port = await portfinder.getPortPromise();
const host = '127.0.0.1';
Expand Down Expand Up @@ -78,3 +82,8 @@ function getStaticStorybookPath(options: Options): string {

return storybookStaticPath;
}

function waitRandomTime(maxWaitTime: number) {
const waitTime = Math.floor(Math.random() * maxWaitTime);
return new Promise((resolve) => setTimeout(resolve, waitTime));
}

0 comments on commit 8408ddf

Please sign in to comment.