diff --git a/Libraries/SPTarkov.Server.Core/Utils/JsonUtil.cs b/Libraries/SPTarkov.Server.Core/Utils/JsonUtil.cs index 4f3aefb0..94f16e6a 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/JsonUtil.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/JsonUtil.cs @@ -11,6 +11,7 @@ public class JsonUtil { private static JsonSerializerOptions? jsonSerializerOptionsNoIndent; private static JsonSerializerOptions? jsonSerializerOptionsIndented; + private static readonly Lock _lock = new(); public JsonUtil( IEnumerable registrators @@ -28,14 +29,20 @@ public class JsonUtil { foreach (var converter in registrator.GetJsonConverters()) { - jsonSerializerOptionsNoIndent.Converters.Add(converter); + lock (_lock) + { + jsonSerializerOptionsNoIndent.Converters.Add(converter); + } } } - jsonSerializerOptionsIndented = new JsonSerializerOptions(jsonSerializerOptionsNoIndent) + lock (_lock) { - WriteIndented = true - }; + jsonSerializerOptionsIndented = new JsonSerializerOptions(jsonSerializerOptionsNoIndent) + { + WriteIndented = true + }; + } } /// diff --git a/Libraries/SPTarkov.Server.Core/Utils/RandomUtil.cs b/Libraries/SPTarkov.Server.Core/Utils/RandomUtil.cs index d9e4f112..4643ba43 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/RandomUtil.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/RandomUtil.cs @@ -400,7 +400,7 @@ public class RandomUtil(ISptLogger _logger, ICloner _cloner) while (currentIndex != 0) { - var randomIndex = GetInt(0, currentIndex); + var randomIndex = GetInt(0, currentIndex, true); currentIndex--; // Swap it with the current element. diff --git a/UnitTests/DI.cs b/UnitTests/DI.cs new file mode 100644 index 00000000..5c032824 --- /dev/null +++ b/UnitTests/DI.cs @@ -0,0 +1,39 @@ +using Microsoft.Extensions.DependencyInjection; +using SPTarkov.Server.Core.Models.Utils; +using SPTarkov.Server.Core.Utils; +using SPTarkov.Server.Core.Utils.Cloners; +using SPTarkov.Server.Core.Utils.Json.Converters; +using UnitTests.Mock; + +namespace UnitTests; + +public class DI +{ + private static IServiceProvider _serviceProvider; + + private static IServiceProvider ConfigureServices() + { + if (_serviceProvider != null) + { + return _serviceProvider; + } + + var services = new ServiceCollection(); + var jsonUtil = new JsonUtil([ new SptJsonConverterRegistrator() ]); + var mathUtil = new MathUtil(); + + services.AddSingleton(jsonUtil); + services.AddSingleton(mathUtil); + services.AddSingleton(); + services.AddSingleton,MockLogger>(); + services.AddSingleton(); + services.AddSingleton(); + + return _serviceProvider = services.BuildServiceProvider(); + } + + public static T GetService() where T : notnull + { + return ConfigureServices().GetRequiredService(); + } +} diff --git a/UnitTests/Tests/Utils/HashUtilTests.cs b/UnitTests/Tests/Utils/HashUtilTests.cs index 9d15bc49..de9c21ad 100644 --- a/UnitTests/Tests/Utils/HashUtilTests.cs +++ b/UnitTests/Tests/Utils/HashUtilTests.cs @@ -1,16 +1,19 @@ using System.Collections.Concurrent; using System.Diagnostics; using SPTarkov.Server.Core.Utils; -using SPTarkov.Server.Core.Utils.Cloners; -using SPTarkov.Server.Core.Utils.Json.Converters; -using UnitTests.Mock; namespace UnitTests.Tests.Utils; [TestClass] public class HashUtilTests { - protected HashUtil _hashUtil = new(new RandomUtil(new MockLogger(), new JsonCloner(new JsonUtil([ new SptJsonConverterRegistrator() ])))); + private HashUtil _hashUtil; + + [TestInitialize] + public void Initialize() + { + _hashUtil = DI.GetService(); + } [TestMethod] public void GenerateTest() diff --git a/UnitTests/Tests/Utils/JsonUtilTests.cs b/UnitTests/Tests/Utils/JsonUtilTests.cs index 8df9406a..d74cc86b 100644 --- a/UnitTests/Tests/Utils/JsonUtilTests.cs +++ b/UnitTests/Tests/Utils/JsonUtilTests.cs @@ -1,13 +1,18 @@ using SPTarkov.Server.Core.Models.Enums; using SPTarkov.Server.Core.Utils; -using SPTarkov.Server.Core.Utils.Json.Converters; namespace UnitTests.Tests.Utils; [TestClass] public class JsonUtilTests { - protected JsonUtil _jsonUtil = new([ new SptJsonConverterRegistrator() ]); + private JsonUtil _jsonUtil; + + [TestInitialize] + public void Initialize() + { + _jsonUtil = DI.GetService(); + } [TestMethod] public void SerializeAndDeserialize_WithDictionaryOfETFEnum_ExpectCorrectParsing() diff --git a/UnitTests/Tests/Utils/MathUtilTests.cs b/UnitTests/Tests/Utils/MathUtilTests.cs index f9f99c07..41ebeb52 100644 --- a/UnitTests/Tests/Utils/MathUtilTests.cs +++ b/UnitTests/Tests/Utils/MathUtilTests.cs @@ -5,7 +5,13 @@ namespace UnitTests.Tests.Utils; [TestClass] public class MathUtilTests { - protected MathUtil _mathUtil = new(); + private MathUtil _mathUtil; + + [TestInitialize] + public void Initialize() + { + _mathUtil = DI.GetService(); + } [TestMethod] public void ListSumTest() diff --git a/UnitTests/Tests/Utils/RandomUtilTests.cs b/UnitTests/Tests/Utils/RandomUtilTests.cs index 1442e199..89985d3d 100644 --- a/UnitTests/Tests/Utils/RandomUtilTests.cs +++ b/UnitTests/Tests/Utils/RandomUtilTests.cs @@ -1,14 +1,17 @@ using SPTarkov.Server.Core.Utils; -using SPTarkov.Server.Core.Utils.Cloners; -using SPTarkov.Server.Core.Utils.Json.Converters; -using UnitTests.Mock; namespace UnitTests.Tests.Utils; [TestClass] public sealed class RandomUtilTests { - private readonly RandomUtil _randomUtil = new(new MockLogger(), new JsonCloner(new JsonUtil([ new SptJsonConverterRegistrator() ]))); + private RandomUtil _randomUtil; + + [TestInitialize] + public void Initialize() + { + _randomUtil = DI.GetService(); + } [TestMethod] public void GetIntTest()