-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: align MSTest & NUnit Workers implementation (#3042)
- Loading branch information
Showing
9 changed files
with
121 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Playwright.Core; | ||
using Microsoft.Playwright.TestAdapter; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
namespace Microsoft.Playwright.MSTest; | ||
|
||
public class WorkerAwareTest | ||
{ | ||
public TestContext TestContext { get; set; } = null!; | ||
|
||
private static readonly ConcurrentStack<Worker> _allWorkers = new(); | ||
private Worker _currentWorker = null!; | ||
|
||
private class Worker | ||
{ | ||
private static int _lastWorkedIndex = 0; | ||
public int WorkerIndex { get; } = Interlocked.Increment(ref _lastWorkedIndex); | ||
public Dictionary<string, IWorkerService> Services = []; | ||
} | ||
|
||
public int WorkerIndex => _currentWorker!.WorkerIndex; | ||
|
||
public async Task<T> RegisterService<T>(string name, Func<Task<T>> factory) where T : class, IWorkerService | ||
{ | ||
if (!_currentWorker.Services.ContainsKey(name)) | ||
{ | ||
_currentWorker.Services[name] = await factory().ConfigureAwait(false); | ||
} | ||
|
||
return (_currentWorker.Services[name] as T)!; | ||
} | ||
|
||
[TestInitialize] | ||
public void WorkerSetup() | ||
{ | ||
if (PlaywrightSettingsProvider.ExpectTimeout.HasValue) | ||
{ | ||
AssertionsBase.SetDefaultTimeout(PlaywrightSettingsProvider.ExpectTimeout.Value); | ||
} | ||
|
||
if (!_allWorkers.TryPop(out _currentWorker)) | ||
{ | ||
_currentWorker = new(); | ||
} | ||
} | ||
|
||
[TestCleanup] | ||
public async Task WorkerTeardown() | ||
{ | ||
if (TestOK()) | ||
{ | ||
foreach (var kv in _currentWorker.Services) | ||
{ | ||
await kv.Value.ResetAsync().ConfigureAwait(false); | ||
} | ||
_allWorkers.Push(_currentWorker); | ||
} | ||
else | ||
{ | ||
foreach (var kv in _currentWorker.Services) | ||
{ | ||
await kv.Value.DisposeAsync().ConfigureAwait(false); | ||
} | ||
_currentWorker.Services.Clear(); | ||
} | ||
} | ||
|
||
protected bool TestOK() | ||
{ | ||
return TestContext!.CurrentTestOutcome == UnitTestOutcome.Passed | ||
|| TestContext!.CurrentTestOutcome == UnitTestOutcome.NotRunnable; | ||
} | ||
} | ||
|
||
public interface IWorkerService | ||
{ | ||
public Task ResetAsync(); | ||
public Task DisposeAsync(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.