Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DatabaseCleaner triggering before hook while threads are still running #683

Open
luizkowalski opened this issue Apr 11, 2021 · 2 comments
Open

Comments

@luizkowalski
Copy link

luizkowalski commented Apr 11, 2021

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 Futures 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

@jlurena
Copy link

jlurena commented Dec 31, 2021

Same here. Were you able to resolve this?

@luizkowalski
Copy link
Author

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants