Skip to content

Commit

Permalink
Add thread safety check to rakefile
Browse files Browse the repository at this point in the history
  • Loading branch information
brasic committed Jun 29, 2023
1 parent 2d5b65a commit 99dd9e4
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,33 @@ Rake::RDocTask.new do |rdoc|
rdoc.rdoc_files.include('lib/**/*.rb')
rdoc.rdoc_files.include(Bundler::GemHelper.gemspec.extra_rdoc_files)
end

task :thread_safety_check, [:thread_count] do |_, args|
thread_count = Integer(args[:thread_count] || "1000", 10)
$LOAD_PATH.unshift(File.join(__dir__, 'lib'))

require 'http/cookie' or raise "already required"

# see https://github.com/sparklemotion/http-cookie/issues/27
puts "checking for thread safety bug with #{thread_count} threads on #{RUBY_DESCRIPTION}"

results = thread_count.times.map do
Thread.new do
begin
HTTP::CookieJar::HashStore.new
nil
rescue => e
e
end
end
end.map(&:value)

exceptions = results.reject(&:nil?)
if exceptions.any?
classes = exceptions.map { |e| e.class.name }.uniq
warn "#{exceptions.size} of #{thread_count} threads saw exceptions: #{classes.inspect}"
raise exceptions.first
else
puts "no issues detected"
end
end

0 comments on commit 99dd9e4

Please # to comment.