From 926365b355b2793c34ba65c82331982b4e9aa10b Mon Sep 17 00:00:00 2001 From: Thomas Fankhauser Date: Fri, 28 Feb 2020 10:09:25 +0100 Subject: [PATCH] Added parallell testing of transactions section Based on https://github.com/rails/rails/issues/38578, it would be nice to have some information on transactions in test cases --- guides/source/testing.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/guides/source/testing.md b/guides/source/testing.md index 1b9af789c9..682f9bc9ca 100644 --- a/guides/source/testing.md +++ b/guides/source/testing.md @@ -545,6 +545,32 @@ to be able to easily change the number of workers a test run should use: PARALLEL_WORKERS=15 rails test ``` +### Testing Parallel Transactions + +Rails automatically wraps any test case in a database transaction that is rolled +back after the test completes. This makes test cases independent of each other +and changes to the database are only visible within a single test. + +When you want to test code that runs parallel transactions in threads, +transactions can block each other because they are already nested under the test +transaction. + +You can disable transactions in a test case class by setting +`self.use_transactional_tests = false`: + +```ruby +class WorkerTest < ActiveSupport::TestCase + self.use_transactional_tests = false + + test "parallel transactions" do + # start some threads that create transactions + end +end +``` + +NOTE: With disabled transactional tests, you have to clean up any data tests +create as changes are not automatically rolled back after the test completes. + The Test Database -----------------