Fix for tests randomly failing when run altogether

This commit is contained in:
hulkhan22
2025-06-01 02:33:19 +02:00
parent e084d1889c
commit 210bdb2d60
2 changed files with 12 additions and 17 deletions
@@ -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<IJsonConverterRegistrator> 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
};
}
/// <summary>
+6 -4
View File
@@ -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<RandomUtil>();
services.AddSingleton<HashUtil>();
return _serviceProvider = services.BuildServiceProvider();
_serviceProvider = services.BuildServiceProvider();
}
public static T GetService<T>() where T : notnull
{
return ConfigureServices().GetRequiredService<T>();
return _serviceProvider.GetRequiredService<T>();
}
}