Module: ArMultiThreadedTransactionalTests
- Defined in:
- lib/ar_multi_threaded_transactional_tests.rb,
lib/ar_multi_threaded_transactional_tests/version.rb
Overview
Multithreaded code uses the same connection or we would escape our surrounding transactional fixtures. To make multiple threads not step on each other, we wrap each transaction and and each execution outside of a transaction in a lock, being careful not to lock multiple times when nesting.
Defined Under Namespace
Modules: ConnectionSyncer, ExecutionSyncer, TransactionSyncer
Constant Summary collapse
- MUTEX =
Mutex.new
- VERSION =
"0.2.0"
Class Attribute Summary collapse
-
.active ⇒ Object
readonly
Returns the value of attribute active.
-
.connection ⇒ Object
readonly
Returns the value of attribute connection.
Class Method Summary collapse
Class Attribute Details
.active ⇒ Object (readonly)
Returns the value of attribute active.
10 11 12 |
# File 'lib/ar_multi_threaded_transactional_tests.rb', line 10 def active @active end |
.connection ⇒ Object (readonly)
Returns the value of attribute connection.
10 11 12 |
# File 'lib/ar_multi_threaded_transactional_tests.rb', line 10 def connection @connection end |
Class Method Details
.activate ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/ar_multi_threaded_transactional_tests.rb', line 12 def activate if block_given? begin activate yield ensure deactivate end else @active = true @connection = ActiveRecord::Base.connection end end |
.deactivate ⇒ Object
26 27 28 29 |
# File 'lib/ar_multi_threaded_transactional_tests.rb', line 26 def deactivate @active = false @connection = nil end |
.synchronize ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ar_multi_threaded_transactional_tests.rb', line 31 def synchronize if !active || Thread.current[:MultiThreadDbSyncerLocked] yield else MUTEX.synchronize do begin Thread.current[:MultiThreadDbSyncerLocked] = true yield ensure Thread.current[:MultiThreadDbSyncerLocked] = false end end end end |