Skip to content

Commit

Permalink
Document multithreaded transactional tests
Browse files Browse the repository at this point in the history
People searching for testing advice will likely look at the testing
guide, so add a section on browser tests there showing the new
temporarily_release_connection extension.
  • Loading branch information
janko committed Jun 1, 2024
1 parent dfa946e commit da46557
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions doc/testing.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ These run each test in its own transaction, the recommended way to test.
end
end

== Transactional testing with multiple threads

Some tests may require executing code across multiple threads. The most common example are browser tests with Capybara, where the web server is running in a separate thread. For transactional tests to work in this case, the main thread needs to allow other threads to use its database connection while the transaction is in progress. This can be achieved with the temporarily_release_connection extension:

DB.extension :temporarily_release_connection
DB.transaction(rollback: :always, auto_savepoint: true) do |conn|
DB.temporarily_release_connection(conn) do
# Other threads can operate on connection safely inside the transaction
yield
end
end

This requires maximum connection pool size to be 1, so make sure to set the Database +:max_connections+ option to 1 in tests.

== Transactional testing with multiple databases

You can use the Sequel.transaction method to run a transaction on multiple databases, rolling all of them back. Instead of:
Expand Down

0 comments on commit da46557

Please sign in to comment.