faker icon indicating copy to clipboard operation
faker copied to clipboard

Thread safety

Open kiskoza opened this issue 2 years ago • 1 comments

Issue#

https://github.com/faker-ruby/faker/issues/1700

Description:

In my project, I have a lot of specs, so I run them in parallel to get quicker results, usually in a few threads. I also have some special specs dealing with mocked data, where I need a random first name, but it can't be the same as the initial data. At this point, I started to use the unique generators, something like this:

before do
  generator = Faker::Name.unique
  generator.exclude(:first_name, [], %w[Adam]) }
  @first_name = generator.first_name
  generator.clear # have a lot of test, don't want to run out of names just because one is forbidden
end

It worked fine when I was running only one of the tests, but as soon as I started them in parallel, they became flaky and sometimes it even generated the excluded first name.

I saw that others also had problems with thread safety, so here I am, trying to fix it. I added two new test cases, one is checking the thread safety of the Config.random (code derived from the linked isssue), the other is covering my problem with unique generators having their own sets of exlusions.

I hope my PR helps and could be merged soon, just let me know if you need anything

kiskoza avatar Aug 08 '22 15:08 kiskoza

Looks good to me, but i'd like someone else from the faker team to have a look as well before merging.

Zeragamba avatar Aug 09 '22 13:08 Zeragamba