From ce5de1beea84785cef6c5218380ebc06b8c3fe03 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Sat, 25 Sep 2021 23:41:55 +0200 Subject: [PATCH 1/2] test: unflake selectors-register.spec.ts - should work --- src/remote/playwrightClient.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/remote/playwrightClient.ts b/src/remote/playwrightClient.ts index 5b30fe1d5f170..17480a31bf493 100644 --- a/src/remote/playwrightClient.ts +++ b/src/remote/playwrightClient.ts @@ -32,15 +32,23 @@ export class PlaywrightClient { const { wsEndpoint, timeout = 30000 } = options; const connection = new Connection(); const ws = new WebSocket(wsEndpoint); - connection.onmessage = message => ws.send(JSON.stringify(message)); + connection.onmessage = message => { + if (ws.readyState === 2 /** CLOSING */ || ws.readyState === 3 /** CLOSED */) + throw new Error('PlaywrightClient: writing to closed WebSocket connection'); + ws.send(JSON.stringify(message)); + }; ws.on('message', message => connection.dispatch(JSON.parse(message.toString()))); const errorPromise = new Promise((_, reject) => ws.on('error', error => reject(error))); const closePromise = new Promise((_, reject) => ws.on('close', () => reject(new Error('Connection closed')))); const playwrightClientPromise = new Promise((resolve, reject) => { + let playwright: Playwright; ws.on('open', async () => { - const playwright = await connection.initializePlaywright(); + playwright = await connection.initializePlaywright(); resolve(new PlaywrightClient(playwright, ws)); }); + ws.on('close', () => { + playwright?._cleanup(); + }); }); let timer: NodeJS.Timeout; try { From cf1b4f3ccdfcd2ba9ca0f247d34053bad6335730 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Wed, 29 Sep 2021 01:29:10 +0200 Subject: [PATCH 2/2] use makeWaitForNextTask --- src/remote/playwrightClient.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/remote/playwrightClient.ts b/src/remote/playwrightClient.ts index 17480a31bf493..bfa9b4cac68ab 100644 --- a/src/remote/playwrightClient.ts +++ b/src/remote/playwrightClient.ts @@ -17,6 +17,7 @@ import WebSocket from 'ws'; import { Connection } from '../client/connection'; import { Playwright } from '../client/playwright'; +import { makeWaitForNextTask } from '../utils/utils'; export type PlaywrightClientConnectOptions = { wsEndpoint: string; @@ -32,12 +33,13 @@ export class PlaywrightClient { const { wsEndpoint, timeout = 30000 } = options; const connection = new Connection(); const ws = new WebSocket(wsEndpoint); + const waitForNextTask = makeWaitForNextTask(); connection.onmessage = message => { if (ws.readyState === 2 /** CLOSING */ || ws.readyState === 3 /** CLOSED */) throw new Error('PlaywrightClient: writing to closed WebSocket connection'); ws.send(JSON.stringify(message)); }; - ws.on('message', message => connection.dispatch(JSON.parse(message.toString()))); + ws.on('message', message => waitForNextTask(() => connection.dispatch(JSON.parse(message.toString())))); const errorPromise = new Promise((_, reject) => ws.on('error', error => reject(error))); const closePromise = new Promise((_, reject) => ws.on('close', () => reject(new Error('Connection closed')))); const playwrightClientPromise = new Promise((resolve, reject) => {