From 1d8c6d47607858376eb4b32b3ca7e10f7cbddd5c Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Mon, 8 Jan 2024 19:07:06 +0100 Subject: [PATCH] test: unflake 'android.launchServer' WS leak test (#28891) This test was flaky all the time. The problem was that we were sending messages on a WS connection which wasn't ready yet, so we lost some. --- tests/android/launch-server.spec.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/android/launch-server.spec.ts b/tests/android/launch-server.spec.ts index 4166e098a3..c90a25b0a0 100644 --- a/tests/android/launch-server.spec.ts +++ b/tests/android/launch-server.spec.ts @@ -118,14 +118,16 @@ test('android.launchServer should terminate WS connection when device gets disco const forwardingServer = new ws.Server({ port: 0, path: '/connect' }); let receivedConnection: ws.WebSocket | undefined; forwardingServer.on('connection', connection => { + // Pause the connection until we establish the actual connection to the browser server. + connection.pause(); receivedConnection = connection; const actualConnection = new ws.WebSocket(browserServer.wsEndpoint()); - actualConnection.on('open', () => { - actualConnection.on('message', message => connection.send(message)); - connection.on('message', message => actualConnection.send(message)); - connection.on('close', () => actualConnection.close()); - actualConnection.on('close', () => connection.close()); - }); + // We need to wait for the actual connection to be established before resuming + actualConnection.on('open', () => connection.resume()); + actualConnection.on('message', message => connection.send(message)); + connection.on('message', message => actualConnection.send(message)); + connection.on('close', () => actualConnection.close()); + actualConnection.on('close', () => connection.close()); }); try { const device = await playwright._android.connect(`ws://localhost:${(forwardingServer.address() as ws.AddressInfo).port}/connect`);