From 4d508df5e824c7e92fe6ad65a65ae4ae7134cd31 Mon Sep 17 00:00:00 2001 From: Marco Regueira Date: Wed, 19 Dec 2018 18:13:41 +0100 Subject: [PATCH 1/2] Fixes new race conditions in FillerManager --- src/GenFu/FillerManager.cs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/GenFu/FillerManager.cs b/src/GenFu/FillerManager.cs index 2c4dc7c..4b7bbeb 100644 --- a/src/GenFu/FillerManager.cs +++ b/src/GenFu/FillerManager.cs @@ -328,12 +328,27 @@ private static IPropertyFiller GetMatchingMethodFiller(MethodInfo methodInfo, ID public Result GetGenericFiller() { var type = typeof(Input); - return (Result)_genericPropertyFillersByPropertyType[type]; + try + { + rwl.EnterReadLock(); + return (Result) _genericPropertyFillersByPropertyType[type]; + } + finally + { + rwl.ExitReadLock(); + } } public IPropertyFiller GetGenericFillerForType(Type t) { - return _genericPropertyFillersByPropertyType[t]; + try + { + rwl.EnterReadLock(); + return _genericPropertyFillersByPropertyType[t]; + } + finally + { + rwl.ExitReadLock(); + } } - } } From 4245535af704975dc788ae4cce39536aa70d60a2 Mon Sep 17 00:00:00 2001 From: Marco Regueira Date: Wed, 19 Dec 2018 18:48:58 +0100 Subject: [PATCH 2/2] Remove proof of concept --- tests/GenFu.Tests/When_running_in_parallel.cs | 45 +------------------ 1 file changed, 2 insertions(+), 43 deletions(-) diff --git a/tests/GenFu.Tests/When_running_in_parallel.cs b/tests/GenFu.Tests/When_running_in_parallel.cs index ba1067c..f76c0f6 100644 --- a/tests/GenFu.Tests/When_running_in_parallel.cs +++ b/tests/GenFu.Tests/When_running_in_parallel.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using GenFu.Tests.TestEntities; using Xunit; @@ -59,47 +60,5 @@ public void registrations_are_configurable() Assert.True(person.Age >= 40 && person.Age <= 50); }); } - - - - [Fact] - public void registration_changes_are_non_deterministic() - { - /* - * this is not exactly a test - * we demosntrate here that, while the FillerManager is thread safe, - * it is not deterministic for multithreading - * because we have only one shared storage - */ - /* - * THIS TEST CAN BE SAFELY DELETED - */ - - var testOneGreaterThan100 = false; - var testOneSmallerThan100 = false; - var testTwoGreaterThan100 = false; - var testTwoSmallerThan100 = false; - - Parallel.For(0, 1000, i => - { - //conf #1 - A.Configure().Fill(x => x.Id).WithinRange(200, 300); - - var one = A.New(); - testOneGreaterThan100 = testOneGreaterThan100 || one.Id > 100; //comes from conf#1 in any thread - testOneSmallerThan100 = testOneSmallerThan100 || one.Id < 100; //comes from conf#2 in another thread - - //conf #2 - A.Configure().Fill(x => x.Id).WithinRange(20, 30); - - var two = A.New(); - testTwoGreaterThan100 = testTwoGreaterThan100 || two.Id > 100; //comes from conf#1 in any thread - testTwoSmallerThan100 = testTwoSmallerThan100 || two.Id < 100; //comes from conf#2 in another thread - }); - - //if it were ran in a single thread both assertions would fail - Assert.True(testOneGreaterThan100 && testOneSmallerThan100); //result is not deterministic - Assert.True(testTwoGreaterThan100 && testTwoSmallerThan100); //result is not deterministic - } } } \ No newline at end of file