forked from adzap/validates_timeliness
-
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.
demonstrate threading issue adzap#187 in a spec
- Loading branch information
1 parent
3134072
commit bf418ab
Showing
1 changed file
with
39 additions
and
0 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,39 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe 'Validates Timeliness threadsafety' do | ||
before(:each) do | ||
ValidatesTimeliness.setup do |config| | ||
config.parser.remove_us_formats | ||
end | ||
end | ||
|
||
let(:us_date) { '06/30/2016' } | ||
let(:eu_date) { '30/06/2016' } | ||
|
||
# (fails with Timeliness >= 0.4 but should be fixed with ValidatesTimeliness?) | ||
it "doesn't need re-configuration per thread" do | ||
expect(Timeliness.parse(eu_date)).not_to be_nil | ||
expect(Timeliness.parse(us_date)).to be_nil | ||
threads = [] | ||
threads << Thread.new { expect(Timeliness.parse(eu_date)).not_to be_nil } | ||
threads << Thread.new { expect(Timeliness.parse(us_date)).to be_nil } | ||
threads.each(&:join) | ||
end | ||
|
||
# (fails with Timeliness < 0.4, fixed with Timeliness >= 0.4) | ||
it 'is thread_safe' do | ||
threads = [] | ||
threads << Thread.new do | ||
Timeliness.use_euro_formats | ||
10_000.times { expect(Timeliness.parse(eu_date)).not_to be_nil } | ||
end | ||
threads << Thread.new do | ||
Timeliness.use_us_formats | ||
10_000.times { expect(Timeliness.parse(us_date)).not_to be_nil } | ||
end | ||
threads.each do |t| | ||
t.report_on_exception = false | ||
t.join | ||
end | ||
end | ||
end |