diff --git a/src/AutoFakerBinder.cs b/src/AutoFakerBinder.cs index 94a366d..659d672 100644 --- a/src/AutoFakerBinder.cs +++ b/src/AutoFakerBinder.cs @@ -1,7 +1,7 @@ using System; using System.Collections; +using System.Collections.Concurrent; using System.Collections.Generic; -using System.Runtime.InteropServices; using Soenneker.Extensions.FieldInfo; using Soenneker.Reflection.Cache.Constructors; using Soenneker.Reflection.Cache.Extensions; @@ -26,8 +26,8 @@ public class AutoFakerBinder : IAutoFakerBinder { internal readonly GeneratorService GeneratorService; - private readonly Dictionary> _autoMembersCache = []; - private readonly Dictionary _constructorsCache = []; + private readonly ConcurrentDictionary> _autoMembersCache = []; + private readonly ConcurrentDictionary _constructorsCache = []; public AutoFakerBinder() { diff --git a/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerParallelTests.cs b/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerParallelTests.cs new file mode 100644 index 0000000..14dce7f --- /dev/null +++ b/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerParallelTests.cs @@ -0,0 +1,56 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Soenneker.Utils.AutoBogus.Tests.Dtos.Simple; +using Xunit; + +namespace Soenneker.Utils.AutoBogus.Tests; + +public class AutoFakerParallelTests +{ + [Fact] + public void Generate_with_ParallelExecution() + { + // Arrange + const int numberOfTasks = 1000; + var results = new ConcurrentBag(); + + var autoFaker = new AutoFaker(); + + // Act + Parallel.For(0, numberOfTasks, _ => + { + var fakeModel = autoFaker.Generate(); + results.Add(fakeModel); + }); + + // Assert + Assert.Equal(numberOfTasks, results.Count); + + List ids = results.Select(x => x.Id).ToList(); + Assert.Equal(ids.Count, ids.Distinct().Count()); // Ensure IDs are unique + } + + [Fact] + public async Task Generate_with_ParallelExecutionTasks() + { + // Arrange + const int numberOfTasks = 1000; + + var autoFaker = new AutoFaker(); + + // Act + Task[] tasks = Enumerable.Range(0, numberOfTasks) + .Select(_ => Task.Run(() => autoFaker.Generate())) + .ToArray(); + + TestClassWithSimpleProperties[] results = await Task.WhenAll(tasks); + + // Assert + Assert.Equal(numberOfTasks, results.Length); + + List ids = results.Select(x => x.Id).ToList(); + Assert.Equal(ids.Count, ids.Distinct().Count()); // Ensure IDs are unique + } +} \ No newline at end of file diff --git a/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/TestClassWithSimpleProperties.cs b/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/TestClassWithSimpleProperties.cs new file mode 100644 index 0000000..2be0c2c --- /dev/null +++ b/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/TestClassWithSimpleProperties.cs @@ -0,0 +1,13 @@ +using System; + +namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Simple +{ + internal class TestClassWithSimpleProperties + { + public int Id { get; set; } + + public string Name { get; set; } + + public DateTime CreatedAt { get; set; } + } +} \ No newline at end of file