class PessimisticLockingTest
Public Instance Methods
duel(zzz = 5) { || ... }
click to toggle source
# File activerecord/test/cases/locking_test.rb, line 622 def duel(zzz = 5) t0, t1, t2, t3 = nil, nil, nil, nil a = Thread.new do t0 = Time.now Person.transaction do yield sleep zzz # block thread 2 for zzz seconds end t1 = Time.now end b = Thread.new do sleep zzz / 2.0 # ensure thread 1 tx starts first t2 = Time.now Person.transaction { yield } t3 = Time.now end a.join b.join assert t1 > t0 + zzz assert t2 > t0 assert t3 > t2 [t0.to_f..t1.to_f, t2.to_f..t3.to_f] end
setup()
click to toggle source
# File activerecord/test/cases/locking_test.rb, line 541 def setup Person.connection_pool.clear_reloadable_connections! # Avoid introspection queries during tests. Person.columns; Reader.columns end
test_eager_find_with_lock()
click to toggle source
Test locked eager find.
# File activerecord/test/cases/locking_test.rb, line 559 def test_eager_find_with_lock assert_nothing_raised do Person.transaction do Person.includes(:readers).lock.find(1) end end end
test_lock_sending_custom_lock_statement()
click to toggle source
# File activerecord/test/cases/locking_test.rb, line 604 def test_lock_sending_custom_lock_statement Person.transaction do person = Person.find(1) assert_sql(/LIMIT \$?\d FOR SHARE NOWAIT/) do person.lock!("FOR SHARE NOWAIT") end end end
test_no_locks_no_wait()
click to toggle source
# File activerecord/test/cases/locking_test.rb, line 615 def test_no_locks_no_wait first, second = duel { Person.find 1 } assert first.end > second.end end
test_sane_find_with_lock()
click to toggle source
Test typical find.
# File activerecord/test/cases/locking_test.rb, line 548 def test_sane_find_with_lock assert_nothing_raised do Person.transaction do Person.lock.find(1) end end end
test_sane_lock_method()
click to toggle source
Locking a record reloads it.
# File activerecord/test/cases/locking_test.rb, line 569 def test_sane_lock_method assert_nothing_raised do Person.transaction do person = Person.find 1 old, person.first_name = person.first_name, "fooman" # Locking a dirty record is deprecated assert_deprecated do person.lock! end assert_equal old, person.first_name end end end
test_with_lock_commits_transaction()
click to toggle source
# File activerecord/test/cases/locking_test.rb, line 583 def test_with_lock_commits_transaction person = Person.find 1 person.with_lock do person.first_name = "fooman" person.save! end assert_equal "fooman", person.reload.first_name end
test_with_lock_rolls_back_transaction()
click to toggle source
# File activerecord/test/cases/locking_test.rb, line 592 def test_with_lock_rolls_back_transaction person = Person.find 1 old = person.first_name person.with_lock do person.first_name = "fooman" person.save! raise "oops" end rescue nil assert_equal old, person.reload.first_name end