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

This commit is contained in:
Dmitry Gozman 2021-09-22 17:08:49 -07:00 committed by GitHub
parent 568ec05a97
commit e6f5404621
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 12 deletions

View File

@ -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');
});

View File

@ -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;