1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- module ThreadsHelper
-
-
- def ensure_threads_exited()
- initial_threads = Thread.list
- yield
- ensure
- superfluous_threads = -> { Thread.list - initial_threads }
-
- 3.times do
- superfluous_threads.call.each do |t|
- t.kill
- t.join
- end
- break if superfluous_threads.call.count.zero?
- sleep 1
- end
- if superfluous_threads.call.count.positive?
- superfluous_threads.each do |thread|
- warn "Error: found a superfluous thread after clean-up: #{thread}"
- warn "Backtrace: #{thread.backtrace.join("\n")}"
- end
- raise 'Superfluous threads found after clean-up.'
- end
-
-
- ActiveRecord::Base.connection_pool.reap
- end
-
- def ensure_block_keeps_running(timeout: 2.seconds, &block)
-
-
- Rails.logger.silence do
- Timeout.timeout(timeout, &block)
- end
- raise 'Process ended unexpectedly.'
- rescue SystemExit
-
- raise 'Process tried to shut down unexpectedly.'
- rescue Timeout::Error
-
- true
- end
- end
- RSpec.configure do |config|
- config.include ThreadsHelper
- config.around(:each, :ensure_threads_exited) do |example|
- ensure_threads_exited { example.run }
- end
- end
|