-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
72 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
test/Soenneker.Utils.AutoBogus.Tests/AutoFakerParallelTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
test/Soenneker.Utils.AutoBogus.Tests/Dtos/Simple/TestClassWithSimpleProperties.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |