`AutoFaker<T>.Generate(null)` Fails on Ubuntu
See original GitHub issueI have a project that I’m building and I’m using Bogus + AutoBogus in my test suites. I personally develop on macOS and Windows and haven’t experienced the issue on either, but when I use Ubuntu on Azure Pipelines, calling AutoFaker<T>.Generate(null) (or Generate() for that matter, they amount to the same), Generate() throws:
Failed Heroku.NET.Tests.Apps.AppsClientTests.DisableACMSendsTheCorrectRequest
Error Message:
System.NullReferenceException : Object reference not set to an instance of an object.
Stack Trace:
at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
at Bogus.Faker`1.FinishWith(Action`2 action) in C:\projects\bogus\Source\Bogus\Faker[T].cs:line 376
at AutoBogus.AutoFaker`1.PrepareFinish(AutoGenerateContext context)
at AutoBogus.AutoFaker`1.Generate(String ruleSets)
at Heroku.NET.Tests.Apps.MockApp.Create(String ruleset) in /app/test/Heroku.NET.Tests/Apps/MockApp.cs:line 26
at Heroku.NET.Tests.Apps.AppsClientTests.DisableACMSendsTheCorrectRequest() in /app/test/Heroku.NET.Tests/Apps/AppsClientTests.cs:line 97
--- End of stack trace from previous location where exception was thrown ---
If, instead, I use Generate("default") to accomplish the same effet, everything works as expected and as they do on macOS and Windows. I’m not sure what the difference could be, unless MSCorlib has some weird implementation detail on Linux that makes the null string a problem.
You can see this in action on my current working branch where b3a5946 exhibits the issue and aa19cd0 fixes it.
I’m using .NET Core 2.2.102 on macOS 10.14.1, Windows 10 1809, and Ubuntu 16.04.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)

Top Related StackOverflow Question
Hey there,
I was able to reproduce the issue on Ubuntu 16.04 even at the latest commit:
As @nickdodd79 mentioned, the issue is likely the parallelism with xunit.
Multi-threaded concurrent access to a global static
Faker<T>in MockApp.cs is likely the cause of the issue. As Nick also mentioned, the dictionaries used under the hood are basicDictionary<T>and aren’t thread-safe. Threading issues are also usually a sign of the flaky behavior that you experienced with tests running okay on OSX and Windows, but not Linux probably due to differences in thread-scheduling.So, there are a couple of solutions for us.
Lazy<T>for thread-safe initialization of the global staticFaker<T>object.Faker<T>for each test-request of aFaker<T>I’d probably recommend the 2nd option because you might want to mutate the
Faker<T>more specifically later down the road for some specialized test and you don’t want those mutations leaking into other tests as they would with a mutated global static object.Details are below:
Lazy<T>to create the global static.Faker<T>. Might be better to just returnFaker<App>instead ofAppso you can mutateFaker<T>a little more for specialized unit tests.Also, check out the
.Clone()method onFaker<T>for some more interesting test scenarios. 😎And when you make the fix you should have some good success 🎉
Hope that helps!
Thanks,
Brian
🚗 🚙 “Let the good times roll…”
Thanks for that excellent breakdown @bchavez, this is my first foray into xunit and I hadn’t even thought about the concurrent access! Clearly this is a problem with my use of AutoBogus and not the library itself, so I’ll go ahead and close the issue.
Thanks both for taking the time to help out!