From f0443e4326599d22aadd8591fc586805ba0575b9 Mon Sep 17 00:00:00 2001 From: Ilona Tomkowicz <32700855+ilonatommy@users.noreply.github.com> Date: Fri, 5 Jul 2024 17:10:29 +0000 Subject: [PATCH] Use 3 attempts in case of timeout. --- .../wasm/Wasm.Build.Tests/BrowserRunner.cs | 41 ++++++++++++++----- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/mono/wasm/Wasm.Build.Tests/BrowserRunner.cs b/src/mono/wasm/Wasm.Build.Tests/BrowserRunner.cs index b37deb5781b29..e632ab472791a 100644 --- a/src/mono/wasm/Wasm.Build.Tests/BrowserRunner.cs +++ b/src/mono/wasm/Wasm.Build.Tests/BrowserRunner.cs @@ -102,24 +102,43 @@ public async Task StartServerAndGetUrlAsync( public async Task SpawnBrowserAsync( string browserUrl, - bool headless = true + bool headless = true, + int timeout = 10000, + int maxRetries = 3 ) { var url = new Uri(browserUrl); Playwright = await Microsoft.Playwright.Playwright.CreateAsync(); // codespaces: ignore certificate error -> Microsoft.Playwright.PlaywrightException : net::ERR_CERT_AUTHORITY_INVALID string[] chromeArgs = new[] { $"--explicitly-allowed-ports={url.Port}", "--ignore-certificate-errors" }; _testOutput.WriteLine($"Launching chrome ('{s_chromePath.Value}') via playwright with args = {string.Join(',', chromeArgs)}"); - Browser = await Playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions{ - ExecutablePath = s_chromePath.Value, - Headless = headless, - Args = chromeArgs - }); - Browser.Disconnected += (sender, e) => + + int attempt = 0; + while (attempt < maxRetries) { - Browser = null; - _testOutput.WriteLine("Browser has been disconnected"); - }; - return Browser; + try + { + Browser = await Playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { + ExecutablePath = s_chromePath.Value, + Headless = headless, + Args = chromeArgs, + Timeout = timeout + }); + Browser.Disconnected += (sender, e) => + { + Browser = null; + _testOutput.WriteLine("Browser has been disconnected"); + }; + break; + } + catch (System.TimeoutException ex) + { + attempt++; + _testOutput.WriteLine($"Attempt {attempt} failed with TimeoutException: {ex.Message}"); + } + } + if (attempt == maxRetries) + throw new Exception($"Failed to launch browser after {maxRetries} attempts"); + return Browser!; } // FIXME: options