database_cleaner
database_cleaner copied to clipboard
DatabaseCleaner triggering `before` hook while threads are still running
This one is hard to reproduce, might not have anything to do with DC but I tracked the issue down to the DC configuration/behavior.
I have a code that runs a couple of operations on threads, using concurrent-ruby
's Future
. It goes along these lines:
Rails.application.executor.wrap do
future = Concurrent::Future.execute do
Rails.application.executor.wrap(executor: executor) do
operation_that_finds_a_user_on_db
end
end
values = ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
future.value!
end
end
when I'm running controller specs that invoke this part of code, they fail and the exception is that there are no users on the database anymore. I tracked the issue down to DC doing a clean-up.
It looks like DC is running even though Future
s are not finished yet. I can't say that this is an issue with DC but I figure that this could be a starting point
Here is my whole config file
RSpec.configure do |config|
DatabaseCleaner.allow_remote_database_url = true if Rails.env.test?
config.before(:suite) do
DatabaseCleaner.clean_with(:deletion)
end
config.before do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, js: true) do
DatabaseCleaner.strategy = :deletion
end
config.before do
DatabaseCleaner.start
end
config.after do
DatabaseCleaner.clean
end
end
Does this make any sense?
Edit: I noticed that changing the strategy from transaction
to deletion
or truncation
worked but the tests got SO much slower that it's not even an option
Same here. Were you able to resolve this?
@jlurena I didn't solve it. I'm no longer working at the company where I had this issue but back then, what I did was avoid database connections inside threads so I mocked anything that would trigger a connection. Later, I created a gem that handles the conversation between Threads and Rails and also added a helper for testing (ConcurrentRails::Testing.fake!
) that, when set up, does not run the given code on a separate thread