Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix new race condition #144

Merged
merged 2 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/GenFu/FillerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,27 @@ private static IPropertyFiller GetMatchingMethodFiller(MethodInfo methodInfo, ID
public Result GetGenericFiller<Input, Result>()
{
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();
}
}

}
}
45 changes: 2 additions & 43 deletions tests/GenFu.Tests/When_running_in_parallel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using GenFu.Tests.TestEntities;
using Xunit;

Expand Down Expand Up @@ -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<Person>().Fill(x => x.Id).WithinRange(200, 300);

var one = A.New<Person>();
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<Person>().Fill(x => x.Id).WithinRange(20, 30);

var two = A.New<Person>();
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
}
}
}