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

Update testing guide with multithreaded transactional tests #2173

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading