From 9b4ec6ec713d3c3bedc3ac5622c38ef5b4666192 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Sat, 10 Aug 2019 17:51:02 -0700 Subject: [PATCH] Add semaphore to selenium tests This change prevents thread pool starvation when running a bunch of selenium-based tests, by turning the blocking wait for a WebDriver to start into an async wait. This also seems to help with speed, and reliability since we're not running too many browsers at once. I was experencing timeouts, and seeing them in the debugger while running tests locally, this no longer happens. --- src/Shared/E2ETesting/BrowserTestBase.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Shared/E2ETesting/BrowserTestBase.cs b/src/Shared/E2ETesting/BrowserTestBase.cs index 6299b4b020..8e94a4bfb2 100644 --- a/src/Shared/E2ETesting/BrowserTestBase.cs +++ b/src/Shared/E2ETesting/BrowserTestBase.cs @@ -21,6 +21,11 @@ namespace Microsoft.AspNetCore.E2ETesting private static readonly AsyncLocal _logs = new AsyncLocal(); private static readonly AsyncLocal _output = new AsyncLocal(); + // Limit the number of concurrent browser tests. + private readonly static int MaxConcurrentBrowsers = Environment.ProcessorCount * 2; + private static readonly SemaphoreSlim _semaphore = new SemaphoreSlim(MaxConcurrentBrowsers); + private bool _semaphoreHeld; + public BrowserTestBase(BrowserFixture browserFixture, ITestOutputHelper output) { BrowserFixture = browserFixture; @@ -39,6 +44,11 @@ namespace Microsoft.AspNetCore.E2ETesting public Task DisposeAsync() { + if (_semaphoreHeld) + { + _semaphore.Release(); + } + return Task.CompletedTask; } @@ -49,6 +59,9 @@ namespace Microsoft.AspNetCore.E2ETesting public virtual async Task InitializeAsync(string isolationContext) { + await _semaphore.WaitAsync(TimeSpan.FromMinutes(30)); + _semaphoreHeld = true; + var (browser, logs) = await BrowserFixture.GetOrCreateBrowserAsync(Output, isolationContext); _asyncBrowser.Value = browser; _logs.Value = logs;