Skip to content
  • eileencodes's avatar
    d6466beb
    Ensure test threads share a DB connection · d6466beb
    eileencodes authored
    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 ]
    d6466beb
    Ensure test threads share a DB connection
    eileencodes authored
    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