Skip to content

Commit a28e501

Browse files
[#255] changed ThreadLocal to AsyncLocal to be able to use async/await in the tests (#119)
* [#255] changed ThreadLocal to AsyncLocal to be able to use async/await in the tests --------- Co-authored-by: dmitry.bogatko
1 parent 4987a5d commit a28e501

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Applications/AqualityServices.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ namespace Aquality.Selenium.Core.Applications
77
public abstract class AqualityServices<TApplication>
88
where TApplication : class, IApplication
99
{
10-
private static readonly ThreadLocal<TApplication> AppContainer = new ThreadLocal<TApplication>();
11-
private static readonly ThreadLocal<IServiceProvider> ServiceProviderContainer = new ThreadLocal<IServiceProvider>();
10+
private static readonly AsyncLocal<TApplication> AppContainer = new AsyncLocal<TApplication>();
11+
private static readonly AsyncLocal<IServiceProvider> ServiceProviderContainer = new AsyncLocal<IServiceProvider>();
1212

1313
protected AqualityServices()
1414
{
1515
}
1616

1717
protected static bool IsApplicationStarted()
1818
{
19-
return AppContainer.IsValueCreated && AppContainer.Value.IsStarted;
19+
return AppContainer.Value != null && AppContainer.Value.IsStarted;
2020
}
2121

2222
protected static TApplication GetApplication(Func<IServiceProvider, TApplication> startApplicationFunction, Func<IServiceCollection> serviceCollectionProvider = null)
@@ -36,7 +36,7 @@ protected static void SetApplication(TApplication application)
3636

3737
protected static IServiceProvider GetServiceProvider(Func<IServiceProvider, TApplication> applicationSupplier, Func<IServiceCollection> serviceCollectionProvider = null)
3838
{
39-
if (!ServiceProviderContainer.IsValueCreated)
39+
if (ServiceProviderContainer.Value == null)
4040
{
4141
IServiceCollection services;
4242
if (serviceCollectionProvider == null)

Aquality.Selenium.Core/tests/Aquality.Selenium.Core.Tests/Applications/Browser/FindElementsTests.cs

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using OpenQA.Selenium;
77
using System.Collections.Generic;
88
using System.Linq;
9+
using System.Threading.Tasks;
910
using Aquality.Selenium.Core.Tests.Applications.Browser.Elements;
1011

1112
namespace Aquality.Selenium.Core.Tests.Applications.Browser
@@ -103,5 +104,15 @@ public void Should_BePossibleTo_WorkWithElements_FoundByDottedLocator()
103104
() => foundElements.Select(element => element.GetElement()).ToList(),
104105
$"Failed to find elements using dotted locator [{DottedLoc}]");
105106
}
107+
108+
[Test]
109+
public async Task Should_BePossibleTo_To_Have_Async_Tests()
110+
{
111+
await Task.Delay(2000);
112+
var foundElements = FindElements<Label>(DottedLoc, expectedCount: ElementsCount.MoreThenZero);
113+
Assert.DoesNotThrow(
114+
() => foundElements.Select(element => element.GetElement()).ToList(),
115+
$"Failed to find elements using dotted locator [{DottedLoc}]");
116+
}
106117
}
107118
}

0 commit comments

Comments
 (0)