-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathWorkerAwareTest.cs
More file actions
85 lines (73 loc) · 2.4 KB
/
WorkerAwareTest.cs
File metadata and controls
85 lines (73 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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 virtual 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();
}
new Program().Run(["install", "--with-deps", PlaywrightSettingsProvider.BrowserName], throwOnError: true);
}
[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();
}