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

Feature Request: add a driver chokepoint #2623

Open
casperisfine opened this issue Dec 7, 2022 · 0 comments
Open

Feature Request: add a driver chokepoint #2623

casperisfine opened this issue Dec 7, 2022 · 0 comments

Comments

@casperisfine
Copy link

👋 Hi, I hope it's ok to open feature requests here, I'm happy to work on it, I'd just like to ask for feedback first.

Problem

We're having a problem in Rails system tests (full details here rails/rails#45994) that may require some help from capybara to properly fix.

Because of transactional fixtures (tests are ran inside a transaction), we need all threads to use the same database connection. Which mean both the main thread that runs the test case and the various puma threads use the same databse connection.

So to avoid race conditions we have to put a lock around all DB access. The problem arise when applications have multiple database, threads sometimes need to lock all connections to invalidate the cache and it can cause deadlock.

Ultimately, locking around the database connection isn't ideal, it would be much cleaner to acquire a single global lock when starting the test, and have a Rack middleware acquire that lock too so that we ensure Puma can only process requests when the test is doing Capybara assertions.

Example:

require "application_system_test_case"

class HomePagesTest < ApplicationSystemTestCase
  test "visiting the index" do
    Post.create!(title: 'my title')

    visit 'home_page/index'

    Animal.create!(name: 'cat')

    assert_selector "h1", text: "HomePage"
  end
end

In the above example we'd like to be able to implicitly lock and unlock as follow:

require "application_system_test_case"

class HomePagesTest < ApplicationSystemTestCase
  test "visiting the index" do # lock when starting the test
    
    Post.create!(title: 'my title')

    # unlock when calling the capybara driver
    visit 'home_page/index'
    # relock when done

    Animal.create!(name: 'cat')

    # unlock when calling the capybara driver
    assert_selector "h1", text: "HomePage"
    # relock when done 
  end
end

Problem

This is relatively simple on paper, however in practice it is hard to ensure that all the possible driver methods are covered.

It would be very helpful if Capybara had a "chokepoint" method we could redefine, e.g.

e.g. if Session instead of:

driver.visit(visit_uri.to_s)

Would do something like:

call_driver(:visit, visit_uri.to_s)

It would be even better if Capybara provided a first class hook API around these calls.

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

1 participant