Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test after_commit not called after raise in callback #40461

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 49 additions & 0 deletions activerecord/test/cases/transaction_callbacks_test.rb
Expand Up @@ -44,6 +44,9 @@ class TopicWithCallbacks < ActiveRecord::Base
before_destroy { self.class.find(id).touch if persisted? }

before_commit { |record| record.do_before_commit(nil) }

around_save :do_around_save

after_commit { |record| record.do_after_commit(nil) }
after_save_commit { |record| record.do_after_commit(:save) }
after_create_commit { |record| record.do_after_commit(:create) }
Expand All @@ -64,6 +67,11 @@ def before_commit_block(on = nil, &block)
@before_commit[on] << block
end

def around_save_block(&block)
@around_save ||= []
@around_save << block
end

def after_commit_block(on = nil, &block)
@after_commit ||= {}
@after_commit[on] ||= []
Expand All @@ -86,6 +94,12 @@ def do_after_commit(on)
blocks.each { |b| b.call(self) } if blocks
end

def do_around_save
yield
blocks = @around_save if defined?(@around_save)
blocks.each { |b| b.call(self) } if blocks
end

def do_after_rollback(on)
blocks = @after_rollback[on] if defined?(@after_rollback)
blocks.each { |b| b.call(self) } if blocks
Expand Down Expand Up @@ -486,6 +500,41 @@ def test_after_commit_chain_not_called_on_errors
assert_equal [], callbacks
end

def test_after_commit_not_called_on_errors_in_around_save
record_1 = TopicWithCallbacks.create!
callbacks = []
record_1.around_save_block { raise }
record_1.after_commit_block { callbacks << record_1.id }
begin
TopicWithCallbacks.transaction do
record_1.save!
end
rescue
# From record_1.after_commit
end
assert_equal [], callbacks
end

def test_after_commit_chain_not_called_on_errors_in_around_save
record_1 = TopicWithCallbacks.create!
record_2 = TopicWithCallbacks.create!
record_3 = TopicWithCallbacks.create!
callbacks = []
record_1.around_save_block { raise }
record_2.after_commit_block { callbacks << record_2.id }
record_3.after_commit_block { callbacks << record_3.id }
begin
TopicWithCallbacks.transaction do
record_1.save!
record_2.save!
record_3.save!
end
rescue
# From record_1.after_commit
end
assert_equal [], callbacks
end

def test_saving_a_record_with_a_belongs_to_that_specifies_touching_the_parent_should_call_callbacks_on_the_parent_object
pet = Pet.first
owner = pet.owner
Expand Down