Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feat(test runner): use happy eyeballs in webServer port check (#40458)

Simon Knott39109eb29ea8
diff --git a/packages/playwright/src/plugins/webServerPlugin.ts b/packages/playwright/src/plugins/webServerPlugin.ts
index 7c83dadad8..2b8b9ca628 100644
--- a/packages/playwright/src/plugins/webServerPlugin.ts
+++ b/packages/playwright/src/plugins/webServerPlugin.ts
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-import net from 'net';
import path from 'path';
import colors from 'colors/safe';
@@ -21,6 +20,7 @@ import debug from 'debug';
import { ManualPromise } from '@isomorphic/manualPromise';
import { monotonicTime } from '@isomorphic/time';
import { raceAgainstDeadline } from '@isomorphic/timeoutRunner';
+import { createSocket } from '@utils/happyEyeballs';
import { isURLAvailable } from '@utils/network';
import { launchProcess } from '@utils/processLauncher';
@@ -210,18 +210,13 @@ export class WebServerPlugin implements TestRunnerPlugin {
}
async function isPortUsed(port: number): Promise<boolean> {
- const innerIsPortUsed = (host: string) => new Promise<boolean>(resolve => {
- const conn = net
- .connect(port, host)
- .on('error', () => {
- resolve(false);
- })
- .on('connect', () => {
- conn.end();
- resolve(true);
- });
- });
- return await innerIsPortUsed('127.0.0.1') || await innerIsPortUsed('::1');
+ try {
+ const socket = await createSocket('localhost', port);
+ socket.end();
+ return true;
+ } catch {
+ return false;
+ }
}
async function waitFor(waitFn: () => Promise<boolean>, cancellationToken: { canceled: boolean }) {
diff --git a/packages/utils/index.ts b/packages/utils/index.ts
index 00fd5ca902..1e319ecd9a 100644
--- a/packages/utils/index.ts
+++ b/packages/utils/index.ts
@@ -23,6 +23,7 @@ export * from './debugLogger';
export * from './env';
export * from './eventsHelper';
export * from './fileUtils';
+export * from './happyEyeballs';
export * from './hostPlatform';
export * from './httpServer';
export * from './network';
diff --git a/tests/playwright-test/web-server.spec.ts b/tests/playwright-test/web-server.spec.ts
index c171637bad..992bfca875 100644
--- a/tests/playwright-test/web-server.spec.ts
+++ b/tests/playwright-test/web-server.spec.ts
@@ -15,6 +15,7 @@
*/
import type http from 'http';
+import type { AddressInfo } from 'net';
import path from 'path';
import { test, expect, parseTestRunnerOutput } from './playwright-test-fixtures';
import type { RunResult } from './playwright-test-fixtures';
@@ -312,12 +313,12 @@ test('should time out waiting for a server with url', async ({ runInlineTest },
expect(result.output).toContain(`Timed out waiting 300ms from config.webServer.`);
});
-test('should be able to specify the baseURL without the server', async ({ runInlineTest }, { workerIndex }) => {
- const port = workerIndex * 2 + 10500;
+test('should be able to specify the baseURL without the server', async ({ runInlineTest }) => {
const server = createHttpServer((req: http.IncomingMessage, res: http.ServerResponse) => {
res.end('<html><body>hello</body></html>');
});
- await new Promise<void>(resolve => server.listen(port, resolve));
+ await new Promise<void>(resolve => server.listen(0, resolve));
+ const port = (server.address() as AddressInfo).port;
const result = await runInlineTest({
'test.spec.ts': `
import { test, expect } from '@playwright/test';
@@ -375,12 +376,12 @@ test('should be able to specify a custom baseURL with the server', async ({ runI
await new Promise(resolve => server.close(resolve));
});
-test('should be able to use an existing server when reuseExistingServer:true', async ({ runInlineTest }, { workerIndex }) => {
- const port = workerIndex * 2 + 10500;
+test('should be able to use an existing server when reuseExistingServer:true', async ({ runInlineTest }) => {
const server = createHttpServer((req: http.IncomingMessage, res: http.ServerResponse) => {
res.end('<html><body>hello</body></html>');
});
- await new Promise<void>(resolve => server.listen(port, resolve));
+ await new Promise<void>(resolve => server.listen(0, resolve));
+ const port = (server.address() as AddressInfo).port;
const result = await runInlineTest({
'test.spec.ts': `
import { test, expect } from '@playwright/test';
@@ -408,12 +409,12 @@ test('should be able to use an existing server when reuseExistingServer:true', a
await new Promise(resolve => server.close(resolve));
});
-test('should throw when a server is already running on the given port and strict is true', async ({ runInlineTest }, { workerIndex }) => {
- const port = workerIndex * 2 + 10500;
+test('should throw when a server is already running on the given port and strict is true', async ({ runInlineTest }) => {
const server = createHttpServer((req: http.IncomingMessage, res: http.ServerResponse) => {
res.end('<html><body>hello</body></html>');
});
- await new Promise<void>(resolve => server.listen(port, resolve));
+ await new Promise<void>(resolve => server.listen(0, resolve));
+ const port = (server.address() as AddressInfo).port;
const result = await runInlineTest({
'test.spec.ts': `
import { test, expect } from '@playwright/test';
@@ -439,13 +440,13 @@ test('should throw when a server is already running on the given port and strict
await new Promise(resolve => server.close(resolve));
});
-for (const host of ['localhost', '127.0.0.1', '0.0.0.0']) {
- test(`should detect the server if a web-server is already running on ${host}`, async ({ runInlineTest }, { workerIndex }) => {
- const port = workerIndex * 2 + 10500;
+for (const host of ['localhost', '127.0.0.1', '0.0.0.0', '::1']) {
+ test(`should detect the server if a web-server is already running on ${host}`, async ({ runInlineTest }) => {
const server = createHttpServer((req: http.IncomingMessage, res: http.ServerResponse) => {
res.end('<html><body>hello</body></html>');
});
- await new Promise<void>(resolve => server.listen(port, host, resolve));
+ await new Promise<void>(resolve => server.listen(0, host, resolve));
+ const port = (server.address() as AddressInfo).port;
try {
const result = await runInlineTest({
'test.spec.ts': `