From 5ccd18c8d9d1a46ee84da5800f6160d5bc17271b Mon Sep 17 00:00:00 2001 From: Andrew Gray Date: Tue, 21 Jan 2025 14:27:02 +1100 Subject: [PATCH] Use Concurrent Queue as access is multithreaded --- Saucery.Core/Dojo/BrowserVersion.cs | 46 ++++++++++--------- Saucery.Core/Saucery.Core.csproj | 2 +- .../Saucery.Playwright.NUnit.csproj | 2 +- Saucery.XUnit/Saucery.XUnit.csproj | 2 +- Saucery/Saucery.csproj | 2 +- 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/Saucery.Core/Dojo/BrowserVersion.cs b/Saucery.Core/Dojo/BrowserVersion.cs index 62a152f..99ae6d2 100644 --- a/Saucery.Core/Dojo/BrowserVersion.cs +++ b/Saucery.Core/Dojo/BrowserVersion.cs @@ -3,7 +3,7 @@ using Saucery.Core.OnDemand.Base; using Saucery.Core.RestAPI; using Saucery.Core.Util; -using System.Text; +using System.Collections.Concurrent; namespace Saucery.Core.Dojo; @@ -37,7 +37,7 @@ public class BrowserVersion public List ScreenResolutions { get; set; } - private StringBuilder TestNameBuilder { get; set; } + private ConcurrentQueue TestNameQueue { get; set; } public BrowserVersion(SupportedPlatform sp, BrowserBase b) { @@ -53,7 +53,7 @@ public BrowserVersion(SupportedPlatform sp, BrowserBase b) RecommendedAppiumVersion = sp.recommended_backend_version!; SupportedBackendVersions = sp.supported_backend_versions!; DeprecatedBackendVersions = sp.deprecated_backend_versions!; - TestNameBuilder = new StringBuilder(); + TestNameQueue = new ConcurrentQueue(); } public BrowserVersion(BrowserBase b, @@ -74,7 +74,7 @@ public BrowserVersion(BrowserBase b, SupportedBackendVersions = supportedBackendVersions!; DeprecatedBackendVersions = deprecatedBackendVersions!; ScreenResolutions = b.ScreenResolutions; - TestNameBuilder = new StringBuilder(); + TestNameQueue = new ConcurrentQueue(); } public BrowserVersion(SaucePlatform platform) { @@ -92,51 +92,53 @@ public BrowserVersion(SaucePlatform platform) { ScreenResolution = string.Empty; PlatformType = platform.IsAnAndroidDevice() ? PlatformType.Android : PlatformType.Apple; ScreenResolutions = []; - TestNameBuilder = new StringBuilder(); + TestNameQueue = new ConcurrentQueue(); } public void SetTestName(string testName) { - TestNameBuilder = new StringBuilder(); - TestNameBuilder.Append(testName.Contains(SauceryConstants.LEFT_SQUARE_BRACKET) - ? testName[..testName.IndexOf(SauceryConstants.LEFT_SQUARE_BRACKET, StringComparison.Ordinal)] - : testName); + TestNameQueue = new ConcurrentQueue(); + if(!TestNameQueue.Contains(testName)) + { + TestNameQueue.Enqueue(testName.Contains(SauceryConstants.LEFT_SQUARE_BRACKET) + ? testName[..testName.IndexOf(SauceryConstants.LEFT_SQUARE_BRACKET, StringComparison.Ordinal)] + : testName); + } if (this.IsAMobileDevice()) { - AppendPlatformField(DeviceName); + EnqueuePlatformField(DeviceName); if (!string.IsNullOrEmpty(DeviceOrientation)) { - AppendPlatformField(DeviceOrientation); + EnqueuePlatformField(DeviceOrientation); } } else { - AppendPlatformField(Os); - AppendPlatformField(BrowserName); - AppendPlatformField(Name!); + EnqueuePlatformField(Os); + EnqueuePlatformField(BrowserName); + EnqueuePlatformField(Name!); if (!string.IsNullOrEmpty(ScreenResolution)) { - AppendPlatformField(ScreenResolution); + EnqueuePlatformField(ScreenResolution); } } - if(TestNameBuilder.Length > 0) + if(!TestNameQueue.IsEmpty) { - TestName = TestNameBuilder?.ToString(); + TestName = string.Join("", TestNameQueue); } } - private void AppendPlatformField(string fieldToAdd) + private void EnqueuePlatformField(string fieldToAdd) { if(!string.IsNullOrEmpty(fieldToAdd) && - //TestNameBuilder != null && - TestNameBuilder?.Length > 0 && - !TestNameBuilder.ToString().Contains(fieldToAdd)) + !TestNameQueue.IsEmpty && + !TestNameQueue.Contains(fieldToAdd)) { - TestNameBuilder.Append($"{SauceryConstants.UNDERSCORE}{fieldToAdd}"); + TestNameQueue.Enqueue($"{SauceryConstants.UNDERSCORE}{fieldToAdd}"); } } } \ No newline at end of file diff --git a/Saucery.Core/Saucery.Core.csproj b/Saucery.Core/Saucery.Core.csproj index eda10c7..9aeffe1 100644 --- a/Saucery.Core/Saucery.Core.csproj +++ b/Saucery.Core/Saucery.Core.csproj @@ -9,7 +9,7 @@ true Saucery.Core - 4.5.12 + 4.5.13 Andrew Gray SauceForge Copyright Andrew Gray 2014 diff --git a/Saucery.Playwright.NUnit/Saucery.Playwright.NUnit.csproj b/Saucery.Playwright.NUnit/Saucery.Playwright.NUnit.csproj index d0c2cd7..a2400a3 100644 --- a/Saucery.Playwright.NUnit/Saucery.Playwright.NUnit.csproj +++ b/Saucery.Playwright.NUnit/Saucery.Playwright.NUnit.csproj @@ -12,7 +12,7 @@ true Saucery - 4.5.12 + 4.5.13 README.md Andrew Gray SauceForge diff --git a/Saucery.XUnit/Saucery.XUnit.csproj b/Saucery.XUnit/Saucery.XUnit.csproj index 3328ffd..4f6a21e 100644 --- a/Saucery.XUnit/Saucery.XUnit.csproj +++ b/Saucery.XUnit/Saucery.XUnit.csproj @@ -11,7 +11,7 @@ true Saucery.XUnit - 4.5.12 + 4.5.13 README.md Andrew Gray SauceForge diff --git a/Saucery/Saucery.csproj b/Saucery/Saucery.csproj index 51f42f0..ab06757 100644 --- a/Saucery/Saucery.csproj +++ b/Saucery/Saucery.csproj @@ -12,7 +12,7 @@ true Saucery - 4.5.12 + 4.5.13 README.md Andrew Gray SauceForge