Please sign in to comment.
Ensure test threads share a DB connection
This ensures multiple threads inside a transactional test to see consistent database state. When a system test starts Puma spins up one thread and Capybara spins up another thread. Because of this when tests are run the database cannot see what was inserted into the database on teardown. This is because there are two threads using two different connections. This change uses the statement cache to lock the threads to using a single connection ID instead of each not being able to see each other. This code only runs in the fixture setup and teardown so it does not affect real production databases. When a transaction is opened we set `lock_thread` to `Thread.current` so we can keep track of which connection the thread is using. When we rollback the transaction we unlock the thread and then there will be no left-over data in the database because the transaction will roll back the correct connections. [ Eileen M. Uchitelle, Matthew Draper ]
- Loading branch information...
Showing with 80 additions and 32 deletions.
- +11 −1 activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
- +20 −16 activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb
- +6 −1 activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
- +20 −14 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
- +3 −0 activerecord/lib/active_record/fixtures.rb
- +6 −0 activerecord/test/cases/fixtures_test.rb
- +12 −0 activerecord/test/cases/query_cache_test.rb
- +2 −0 activerecord/test/cases/scoping/default_scoping_test.rb