Skip to content

Commit df74da0

Browse files
authored
Merge pull request rails#38602 from fanktom/patch-1
Testing Guides: Added section on Parallell Testing of Transactions
2 parents c86750c + 926365b commit df74da0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

guides/source/testing.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,32 @@ to be able to easily change the number of workers a test run should use:
545545
PARALLEL_WORKERS=15 rails test
546546
```
547547

548+
### Testing Parallel Transactions
549+
550+
Rails automatically wraps any test case in a database transaction that is rolled
551+
back after the test completes. This makes test cases independent of each other
552+
and changes to the database are only visible within a single test.
553+
554+
When you want to test code that runs parallel transactions in threads,
555+
transactions can block each other because they are already nested under the test
556+
transaction.
557+
558+
You can disable transactions in a test case class by setting
559+
`self.use_transactional_tests = false`:
560+
561+
```ruby
562+
class WorkerTest < ActiveSupport::TestCase
563+
self.use_transactional_tests = false
564+
565+
test "parallel transactions" do
566+
# start some threads that create transactions
567+
end
568+
end
569+
```
570+
571+
NOTE: With disabled transactional tests, you have to clean up any data tests
572+
create as changes are not automatically rolled back after the test completes.
573+
548574
The Test Database
549575
-----------------
550576

0 commit comments

Comments
 (0)