From 210bdb2d603e5986c9dbb58fec90fd2a77b15b6c Mon Sep 17 00:00:00 2001 From: hulkhan22 Date: Sun, 1 Jun 2025 02:33:19 +0200 Subject: [PATCH] Fix for tests randomly failing when run altogether --- .../SPTarkov.Server.Core/Utils/JsonUtil.cs | 19 ++++++------------- UnitTests/DI.cs | 10 ++++++---- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Utils/JsonUtil.cs b/Libraries/SPTarkov.Server.Core/Utils/JsonUtil.cs index 94f16e6a..e015f6ed 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/JsonUtil.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/JsonUtil.cs @@ -9,15 +9,14 @@ namespace SPTarkov.Server.Core.Utils; [Injectable(InjectionType.Singleton)] public class JsonUtil { - private static JsonSerializerOptions? jsonSerializerOptionsNoIndent; private static JsonSerializerOptions? jsonSerializerOptionsIndented; - private static readonly Lock _lock = new(); + private static JsonSerializerOptions jsonSerializerOptionsNoIndent; public JsonUtil( IEnumerable registrators ) { - jsonSerializerOptionsNoIndent = new JsonSerializerOptions + jsonSerializerOptionsNoIndent = new JsonSerializerOptions() { WriteIndented = false, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, @@ -29,20 +28,14 @@ public class JsonUtil { foreach (var converter in registrator.GetJsonConverters()) { - lock (_lock) - { - jsonSerializerOptionsNoIndent.Converters.Add(converter); - } + jsonSerializerOptionsNoIndent.Converters.Add(converter); } } - lock (_lock) + jsonSerializerOptionsIndented = new JsonSerializerOptions(jsonSerializerOptionsNoIndent) { - jsonSerializerOptionsIndented = new JsonSerializerOptions(jsonSerializerOptionsNoIndent) - { - WriteIndented = true - }; - } + WriteIndented = true + }; } /// diff --git a/UnitTests/DI.cs b/UnitTests/DI.cs index 5c032824..911e29ad 100644 --- a/UnitTests/DI.cs +++ b/UnitTests/DI.cs @@ -7,15 +7,17 @@ using UnitTests.Mock; namespace UnitTests; +[TestClass] public class DI { private static IServiceProvider _serviceProvider; - private static IServiceProvider ConfigureServices() + [AssemblyInitialize] + public static void ConfigureServices(TestContext context) { if (_serviceProvider != null) { - return _serviceProvider; + return; } var services = new ServiceCollection(); @@ -29,11 +31,11 @@ public class DI services.AddSingleton(); services.AddSingleton(); - return _serviceProvider = services.BuildServiceProvider(); + _serviceProvider = services.BuildServiceProvider(); } public static T GetService() where T : notnull { - return ConfigureServices().GetRequiredService(); + return _serviceProvider.GetRequiredService(); } }