-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix concurrent issues brought up in #1
- Loading branch information
Showing
2 changed files
with
153 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
using System.Collections.Concurrent; | ||
using FastCloner.Code; | ||
|
||
namespace FastCloner.Tests; | ||
|
||
[TestFixture] | ||
public class ConcurrentTests | ||
{ | ||
private class TestClass | ||
{ | ||
public int Value { get; set; } | ||
} | ||
|
||
[Test] | ||
public void GenerateCloner_IsCalledOnlyOnce() | ||
{ | ||
// Arrange | ||
CountHolder generatorCallCount = new CountHolder(); | ||
Type testType = typeof(TestClassForSingleCallTest); | ||
|
||
// Act | ||
Task<object>[] tasks = Enumerable.Range(0, 10) | ||
.Select(_ => Task.Run(() => | ||
FastClonerCache.GetOrAddClass(testType, ValueFactory))) | ||
.ToArray(); | ||
|
||
Task.WaitAll(tasks); | ||
|
||
// Assert | ||
Assert.Multiple(() => | ||
{ | ||
Assert.That(generatorCallCount.Count, Is.EqualTo(1)); | ||
|
||
object firstResult = tasks[0].Result; | ||
foreach (Task<object> task in tasks) | ||
{ | ||
Assert.That(task.Result, Is.SameAs(firstResult)); | ||
} | ||
}); | ||
|
||
return; | ||
|
||
object ValueFactory(Type t) | ||
{ | ||
Thread.Sleep(100); | ||
generatorCallCount.Increment(); | ||
return new Func<object, FastCloneState, object>((obj, state) => obj); | ||
} | ||
} | ||
|
||
private class TestClassForSingleCallTest | ||
{ | ||
public int Value { get; set; } | ||
} | ||
|
||
private class CountHolder | ||
{ | ||
private int _count; | ||
public int Count => _count; | ||
|
||
public void Increment() | ||
{ | ||
Interlocked.Increment(ref _count); | ||
} | ||
} | ||
|
||
[Test] | ||
public void CloneObject_WithConcurrentAccess_GeneratesOnlyOneCloner() | ||
{ | ||
// Arrange | ||
TestClass obj = new TestClass { Value = 42 }; | ||
|
||
// Act | ||
Task<TestClass>[] tasks = Enumerable.Range(0, 10).Select(_ => Task.Run(() => | ||
{ | ||
return FastClonerGenerator.CloneObject(obj); | ||
})).ToArray(); | ||
|
||
Task.WaitAll(tasks); | ||
|
||
// Assert | ||
Assert.Multiple(() => | ||
{ | ||
foreach (Task<TestClass> task in tasks) | ||
{ | ||
TestClass? clone = task.Result; | ||
Assert.That(clone.Value, Is.EqualTo(42)); | ||
} | ||
}); | ||
} | ||
|
||
[Test] | ||
public void GetOrAdd_CanCallValueFactoryMultipleTimes() | ||
{ | ||
// Arrange | ||
ConcurrentDictionary<int, string> dictionary = new ConcurrentDictionary<int, string>(); | ||
int callCount = 0; | ||
const int key = 1; | ||
|
||
// Act | ||
Task<string>[] tasks = Enumerable.Range(0, 10) | ||
.Select(_ => Task.Run(() => dictionary.GetOrAdd(key, ValueFactory))) | ||
.ToArray(); | ||
|
||
Task.WaitAll(tasks); | ||
|
||
// Assert | ||
Assert.Multiple(() => | ||
{ | ||
Assert.That(dictionary, Has.Count.EqualTo(1)); | ||
Assert.That(callCount, Is.GreaterThan(1)); | ||
Assert.That(tasks.Select(t => t.Result).Distinct().Count(), Is.EqualTo(1)); | ||
}); | ||
return; | ||
|
||
string ValueFactory(int k) | ||
{ | ||
Thread.Sleep(100); | ||
Interlocked.Increment(ref callCount); | ||
return $"Value{k}"; | ||
} | ||
} | ||
|
||
} |
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