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.
This commit is contained in:
Ryan Nowak 2019-08-10 17:51:02 -07:00
parent 18f5ec71ed
commit 9b4ec6ec71
1 changed files with 13 additions and 0 deletions

View File

@ -21,6 +21,11 @@ namespace Microsoft.AspNetCore.E2ETesting
private static readonly AsyncLocal<ILogs> _logs = new AsyncLocal<ILogs>();
private static readonly AsyncLocal<ITestOutputHelper> _output = new AsyncLocal<ITestOutputHelper>();
// 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;