2004-11-23 20:04:44 -05:00
|
|
|
require 'abstract_unit'
|
|
|
|
require 'fixtures/topic'
|
2006-04-27 18:39:45 -04:00
|
|
|
require 'fixtures/reply'
|
2005-09-29 23:39:15 -04:00
|
|
|
require 'fixtures/developer'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
class TransactionTest < Test::Unit::TestCase
|
2005-06-10 10:58:02 -04:00
|
|
|
self.use_transactional_fixtures = false
|
2005-09-29 23:39:15 -04:00
|
|
|
fixtures :topics, :developers
|
2005-06-10 10:58:02 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def setup
|
2005-06-10 10:58:02 -04:00
|
|
|
@first, @second = Topic.find(1, 2).sort_by { |t| t.id }
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_successful
|
|
|
|
Topic.transaction do
|
2005-09-29 23:39:15 -04:00
|
|
|
@first.approved = true
|
|
|
|
@second.approved = false
|
2004-11-23 20:04:44 -05:00
|
|
|
@first.save
|
|
|
|
@second.save
|
|
|
|
end
|
|
|
|
|
|
|
|
assert Topic.find(1).approved?, "First should have been approved"
|
|
|
|
assert !Topic.find(2).approved?, "Second should have been unapproved"
|
|
|
|
end
|
|
|
|
|
2005-09-28 14:53:22 -04:00
|
|
|
def transaction_with_return
|
|
|
|
Topic.transaction do
|
2005-09-29 23:39:15 -04:00
|
|
|
@first.approved = true
|
|
|
|
@second.approved = false
|
2005-09-28 14:53:22 -04:00
|
|
|
@first.save
|
|
|
|
@second.save
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_successful_with_return
|
|
|
|
class << Topic.connection
|
|
|
|
alias :real_commit_db_transaction :commit_db_transaction
|
|
|
|
def commit_db_transaction
|
|
|
|
$committed = true
|
2005-09-29 07:16:51 -04:00
|
|
|
real_commit_db_transaction
|
2005-09-28 14:53:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
$committed = false
|
|
|
|
transaction_with_return
|
|
|
|
assert $committed
|
|
|
|
|
|
|
|
assert Topic.find(1).approved?, "First should have been approved"
|
|
|
|
assert !Topic.find(2).approved?, "Second should have been unapproved"
|
|
|
|
ensure
|
|
|
|
class << Topic.connection
|
|
|
|
alias :commit_db_transaction :real_commit_db_transaction rescue nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_successful_with_instance_method
|
|
|
|
@first.transaction do
|
2005-09-29 23:39:15 -04:00
|
|
|
@first.approved = true
|
|
|
|
@second.approved = false
|
2004-11-23 20:04:44 -05:00
|
|
|
@first.save
|
|
|
|
@second.save
|
|
|
|
end
|
|
|
|
|
|
|
|
assert Topic.find(1).approved?, "First should have been approved"
|
|
|
|
assert !Topic.find(2).approved?, "Second should have been unapproved"
|
|
|
|
end
|
2005-09-28 14:53:22 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_failing_on_exception
|
|
|
|
begin
|
|
|
|
Topic.transaction do
|
|
|
|
@first.approved = true
|
|
|
|
@second.approved = false
|
|
|
|
@first.save
|
|
|
|
@second.save
|
|
|
|
raise "Bad things!"
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
# caught it
|
|
|
|
end
|
|
|
|
|
|
|
|
assert @first.approved?, "First should still be changed in the objects"
|
|
|
|
assert !@second.approved?, "Second should still be changed in the objects"
|
2007-06-22 20:54:51 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
assert !Topic.find(1).approved?, "First shouldn't have been approved"
|
|
|
|
assert Topic.find(2).approved?, "Second should still be approved"
|
|
|
|
end
|
2007-06-22 20:54:51 -04:00
|
|
|
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_callback_rollback_in_save
|
|
|
|
add_exception_raising_after_save_callback_to_topic
|
|
|
|
|
|
|
|
begin
|
|
|
|
@first.approved = true
|
|
|
|
@first.save
|
|
|
|
flunk
|
|
|
|
rescue => e
|
|
|
|
assert_equal "Make the transaction rollback", e.message
|
|
|
|
assert !Topic.find(1).approved?
|
|
|
|
ensure
|
|
|
|
remove_exception_raising_after_save_callback_to_topic
|
|
|
|
end
|
|
|
|
end
|
2007-06-22 20:54:51 -04:00
|
|
|
|
2007-01-02 00:36:30 -05:00
|
|
|
def test_callback_rollback_in_create
|
|
|
|
new_topic = Topic.new(
|
|
|
|
:title => "A new topic",
|
|
|
|
:author_name => "Ben",
|
|
|
|
:author_email_address => "ben@example.com",
|
|
|
|
:written_on => "2003-07-16t15:28:11.2233+01:00",
|
|
|
|
:last_read => "2004-04-15",
|
|
|
|
:bonus_time => "2005-01-30t15:28:00.00+01:00",
|
|
|
|
:content => "Have a nice day",
|
|
|
|
:approved => false)
|
|
|
|
new_record_snapshot = new_topic.new_record?
|
2007-01-12 00:10:06 -05:00
|
|
|
id_present = new_topic.has_attribute?(Topic.primary_key)
|
2007-01-02 00:36:30 -05:00
|
|
|
id_snapshot = new_topic.id
|
2007-01-12 00:10:06 -05:00
|
|
|
|
2007-01-02 00:36:30 -05:00
|
|
|
# Make sure the second save gets the after_create callback called.
|
|
|
|
2.times do
|
|
|
|
begin
|
|
|
|
add_exception_raising_after_create_callback_to_topic
|
|
|
|
new_topic.approved = true
|
|
|
|
new_topic.save
|
|
|
|
flunk
|
|
|
|
rescue => e
|
|
|
|
assert_equal "Make the transaction rollback", e.message
|
|
|
|
assert_equal new_record_snapshot, new_topic.new_record?, "The topic should have its old new_record value"
|
|
|
|
assert_equal id_snapshot, new_topic.id, "The topic should have its old id"
|
2007-01-12 00:10:06 -05:00
|
|
|
assert_equal id_present, new_topic.has_attribute?(Topic.primary_key)
|
2007-01-02 00:36:30 -05:00
|
|
|
ensure
|
|
|
|
remove_exception_raising_after_create_callback_to_topic
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2004-12-21 19:48:24 -05:00
|
|
|
def test_nested_explicit_transactions
|
2004-11-23 20:04:44 -05:00
|
|
|
Topic.transaction do
|
|
|
|
Topic.transaction do
|
2005-09-29 23:39:15 -04:00
|
|
|
@first.approved = true
|
|
|
|
@second.approved = false
|
2004-11-23 20:04:44 -05:00
|
|
|
@first.save
|
|
|
|
@second.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert Topic.find(1).approved?, "First should have been approved"
|
|
|
|
assert !Topic.find(2).approved?, "Second should have been unapproved"
|
|
|
|
end
|
2005-09-29 23:39:15 -04:00
|
|
|
|
2007-02-21 17:13:39 -05:00
|
|
|
def test_manually_rolling_back_a_transaction
|
2007-06-22 20:54:51 -04:00
|
|
|
Topic.transaction do
|
2007-02-21 17:13:39 -05:00
|
|
|
@first.approved = true
|
|
|
|
@second.approved = false
|
|
|
|
@first.save
|
|
|
|
@second.save
|
2007-06-22 20:54:51 -04:00
|
|
|
|
2007-05-17 21:02:08 -04:00
|
|
|
raise ActiveRecord::Rollback
|
2007-02-21 17:13:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
assert @first.approved?, "First should still be changed in the objects"
|
|
|
|
assert !@second.approved?, "Second should still be changed in the objects"
|
2007-06-22 20:54:51 -04:00
|
|
|
|
2007-02-21 17:13:39 -05:00
|
|
|
assert !Topic.find(1).approved?, "First shouldn't have been approved"
|
|
|
|
assert Topic.find(2).approved?, "Second should still be approved"
|
|
|
|
end
|
|
|
|
|
2007-06-22 20:54:51 -04:00
|
|
|
uses_mocha 'mocking connection.commit_db_transaction' do
|
|
|
|
def test_rollback_when_commit_raises
|
2007-06-22 21:58:41 -04:00
|
|
|
Topic.connection.expects(:begin_db_transaction)
|
2007-06-22 20:54:51 -04:00
|
|
|
Topic.connection.expects(:commit_db_transaction).raises('OH NOES')
|
|
|
|
Topic.connection.expects(:rollback_db_transaction)
|
|
|
|
|
|
|
|
assert_raise RuntimeError do
|
|
|
|
Topic.transaction do
|
|
|
|
# do nothing
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-02-21 17:13:39 -05:00
|
|
|
|
2006-06-19 18:48:51 -04:00
|
|
|
private
|
|
|
|
def add_exception_raising_after_save_callback_to_topic
|
|
|
|
Topic.class_eval { def after_save() raise "Make the transaction rollback" end }
|
|
|
|
end
|
2007-06-22 20:54:51 -04:00
|
|
|
|
2006-06-19 18:48:51 -04:00
|
|
|
def remove_exception_raising_after_save_callback_to_topic
|
|
|
|
Topic.class_eval { remove_method :after_save }
|
|
|
|
end
|
2007-06-22 20:54:51 -04:00
|
|
|
|
2007-01-02 00:36:30 -05:00
|
|
|
def add_exception_raising_after_create_callback_to_topic
|
|
|
|
Topic.class_eval { def after_create() raise "Make the transaction rollback" end }
|
|
|
|
end
|
2007-06-22 20:54:51 -04:00
|
|
|
|
2007-01-02 00:36:30 -05:00
|
|
|
def remove_exception_raising_after_create_callback_to_topic
|
|
|
|
Topic.class_eval { remove_method :after_create }
|
|
|
|
end
|
2006-06-19 18:48:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if current_adapter?(:PostgreSQLAdapter)
|
|
|
|
class ConcurrentTransactionTest < TransactionTest
|
|
|
|
def setup
|
|
|
|
@allow_concurrency = ActiveRecord::Base.allow_concurrency
|
|
|
|
ActiveRecord::Base.allow_concurrency = true
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
super
|
|
|
|
ActiveRecord::Base.allow_concurrency = @allow_concurrency
|
|
|
|
end
|
|
|
|
|
|
|
|
# This will cause transactions to overlap and fail unless they are performed on
|
|
|
|
# separate database connections.
|
|
|
|
def test_transaction_per_thread
|
|
|
|
assert_nothing_raised do
|
|
|
|
threads = (1..3).map do
|
|
|
|
Thread.new do
|
|
|
|
Topic.transaction do
|
|
|
|
topic = Topic.find(1)
|
|
|
|
topic.approved = !topic.approved?
|
|
|
|
topic.save!
|
|
|
|
topic.approved = !topic.approved?
|
|
|
|
topic.save!
|
|
|
|
end
|
2005-09-29 23:39:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-06-19 18:48:51 -04:00
|
|
|
threads.each { |t| t.join }
|
|
|
|
end
|
2005-09-29 23:39:15 -04:00
|
|
|
end
|
|
|
|
|
2006-06-19 18:48:51 -04:00
|
|
|
# Test for dirty reads among simultaneous transactions.
|
|
|
|
def test_transaction_isolation__read_committed
|
|
|
|
# Should be invariant.
|
|
|
|
original_salary = Developer.find(1).salary
|
|
|
|
temporary_salary = 200000
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
threads = (1..3).map do
|
|
|
|
Thread.new do
|
|
|
|
Developer.transaction do
|
|
|
|
# Expect original salary.
|
|
|
|
dev = Developer.find(1)
|
|
|
|
assert_equal original_salary, dev.salary
|
|
|
|
|
|
|
|
dev.salary = temporary_salary
|
|
|
|
dev.save!
|
|
|
|
|
|
|
|
# Expect temporary salary.
|
|
|
|
dev = Developer.find(1)
|
|
|
|
assert_equal temporary_salary, dev.salary
|
|
|
|
|
|
|
|
dev.salary = original_salary
|
|
|
|
dev.save!
|
|
|
|
|
|
|
|
# Expect original salary.
|
|
|
|
dev = Developer.find(1)
|
|
|
|
assert_equal original_salary, dev.salary
|
|
|
|
end
|
2005-09-29 23:39:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-06-19 18:48:51 -04:00
|
|
|
# Keep our eyes peeled.
|
|
|
|
threads << Thread.new do
|
|
|
|
10.times do
|
|
|
|
sleep 0.05
|
|
|
|
Developer.transaction do
|
|
|
|
# Always expect original salary.
|
|
|
|
assert_equal original_salary, Developer.find(1).salary
|
|
|
|
end
|
2005-09-29 23:39:15 -04:00
|
|
|
end
|
|
|
|
end
|
2006-06-19 18:48:51 -04:00
|
|
|
|
|
|
|
threads.each { |t| t.join }
|
2005-09-29 23:39:15 -04:00
|
|
|
end
|
|
|
|
|
2006-06-19 18:48:51 -04:00
|
|
|
assert_equal original_salary, Developer.find(1).salary
|
2005-09-29 23:39:15 -04:00
|
|
|
end
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|