Skip to content

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

test: unflake "should timeout in socket while connecting" (#9093)

Dmitry Gozmane6f5404621ac
diff --git a/tests/browsertype-connect.spec.ts b/tests/browsertype-connect.spec.ts
index 66c1ddf0ae..2ac16b3593 100644
--- a/tests/browsertype-connect.spec.ts
+++ b/tests/browsertype-connect.spec.ts
@@ -104,8 +104,8 @@ test('should be able to connect two browsers at the same time', async ({browserT
test('should timeout in socket while connecting', async ({browserType, startRemoteServer, server}) => {
const e = await browserType.connect({
- wsEndpoint: `ws://localhost:${server.PORT}/ws`,
- timeout: 1,
+ wsEndpoint: `ws://localhost:${server.PORT}/ws-slow`,
+ timeout: 1000,
}).catch(e => e);
expect(e.message).toContain('browserType.connect: Opening handshake has timed out');
});
diff --git a/utils/testserver/index.js b/utils/testserver/index.js
index 8142206028..353e3aa763 100644
--- a/utils/testserver/index.js
+++ b/utils/testserver/index.js
@@ -70,17 +70,24 @@ class TestServer {
else
this._server = http.createServer(this._onRequest.bind(this));
this._server.on('connection', socket => this._onSocket(socket));
- this._wsServer = new WebSocketServer({server: this._server });
- this._wsServer.shouldHandle = (request) => {
+ this._wsServer = new WebSocketServer({ noServer: true });
+ this._server.on('upgrade', async (request, socket, head) => {
const pathname = url.parse(request.url).pathname;
- return ['/ws', '/ws-emit-and-close'].includes(pathname);
- };
- this._wsServer.on('connection', (ws, request) => {
- const pathname = url.parse(request.url).pathname;
- if (this._onWebSocketConnectionData !== undefined)
- ws.send(this._onWebSocketConnectionData);
- if (pathname === '/ws-emit-and-close')
- ws.close(1003, 'closed by Playwright test-server');
+ if (pathname === '/ws-slow')
+ await new Promise(f => setTimeout(f, 2000));
+ if (!['/ws', '/ws-slow', '/ws-emit-and-close'].includes(pathname)) {
+ socket.write('HTTP/1.1 400 Bad Request\r\n\r\n');
+ socket.destroy();
+ return;
+ }
+ this._wsServer.handleUpgrade(request, socket, head, ws => {
+ // Next emit is only for our internal 'connection' listeners.
+ this._wsServer.emit('connection', ws, request);
+ if (this._onWebSocketConnectionData !== undefined)
+ ws.send(this._onWebSocketConnectionData);
+ if (pathname === '/ws-emit-and-close')
+ ws.close(1003, 'closed by Playwright test-server');
+ });
});
this._server.listen(port);
this._dirPath = dirPath;