Skip to content

Commit

Permalink
Fix for parallel generation
Browse files Browse the repository at this point in the history
  • Loading branch information
soenneker committed Nov 29, 2024
1 parent 0ea1232 commit 70d0d02
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/AutoFakerBinder.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,8 +26,8 @@ public class AutoFakerBinder : IAutoFakerBinder
{
internal readonly GeneratorService GeneratorService;

private readonly Dictionary<CachedType, List<AutoMember>> _autoMembersCache = [];
private readonly Dictionary<CachedType, CachedConstructor> _constructorsCache = [];
private readonly ConcurrentDictionary<CachedType, List<AutoMember>> _autoMembersCache = [];
private readonly ConcurrentDictionary<CachedType, CachedConstructor> _constructorsCache = [];

public AutoFakerBinder()
{
Expand Down
56 changes: 56 additions & 0 deletions test/Soenneker.Utils.AutoBogus.Tests/AutoFakerParallelTests.cs
Original file line number Diff line number Diff line change
@@ -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<TestClassWithSimpleProperties>();

var autoFaker = new AutoFaker();

// Act
Parallel.For(0, numberOfTasks, _ =>
{
var fakeModel = autoFaker.Generate<TestClassWithSimpleProperties>();
results.Add(fakeModel);
});

// Assert
Assert.Equal(numberOfTasks, results.Count);

List<int> 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<TestClassWithSimpleProperties>[] tasks = Enumerable.Range(0, numberOfTasks)
.Select(_ => Task.Run(() => autoFaker.Generate<TestClassWithSimpleProperties>()))
.ToArray();

TestClassWithSimpleProperties[] results = await Task.WhenAll(tasks);

// Assert
Assert.Equal(numberOfTasks, results.Length);

List<int> ids = results.Select(x => x.Id).ToList();
Assert.Equal(ids.Count, ids.Distinct().Count()); // Ensure IDs are unique
}
}
Original file line number Diff line number Diff line change
@@ -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; }
}
}

0 comments on commit 70d0d02

Please sign in to comment.