Skip to content

Commit

Permalink
add thread safety test
Browse files Browse the repository at this point in the history
  • Loading branch information
lofcz committed Jan 8, 2025
1 parent 396f016 commit 15d994f
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions FastCloner.Tests/SpecialCaseTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
Expand Down Expand Up @@ -347,6 +348,39 @@ public void Test_Clone_Auto_Properties()
Assert.That(clone, Is.Not.SameAs(original));
});
}

[Test]
public void ParallelCloning_WithReadOnlyFields_ShouldBeThreadSafe()
{
// Arrange
ClassWithReadOnlyField testObject = new ClassWithReadOnlyField();
const int iterations = 1000;
ConcurrentBag<Exception> exceptions = [];

// Act
Parallel.For(0, iterations, i =>
{
try
{
ClassWithReadOnlyField clone = testObject.DeepClone();
Assert.That(clone, Is.Not.SameAs(testObject));
}
catch (Exception ex)
{
exceptions.Add(ex);
}
});

// Assert
Assert.That(exceptions, Is.Empty, "Parallel cloning should not throw any exceptions");
}

private class ClassWithReadOnlyField
{
private readonly string _readOnlyField = "test";
public string ReadOnlyValue => _readOnlyField;
}


private class TestAutoPropsWithIgnored
{
Expand Down

0 comments on commit 15d994f

Please sign in to comment.