aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases
diff options
context:
space:
mode:
authorMatthew Draper <matthew@trebex.net>2014-09-14 07:17:00 +0930
committerMatthew Draper <matthew@trebex.net>2014-09-14 07:32:53 +0930
commit1d4d15a48d0e422ae3a51920fbf055b0963917fa (patch)
treed9e32c07de1f2f46a9c0639b8f58725d30a75149 /activerecord/test/cases
parent457c876b15640d79f4398c3facb8652210d7c2e0 (diff)
parent997a62198c70c85f229bb05a4c1b63f496531cbd (diff)
downloadrails-1d4d15a48d0e422ae3a51920fbf055b0963917fa.tar.gz
rails-1d4d15a48d0e422ae3a51920fbf055b0963917fa.tar.bz2
rails-1d4d15a48d0e422ae3a51920fbf055b0963917fa.zip
Merge pull request #13656 from chanks/rollback_transactions_in_killed_threads
Data corruption risk: Roll back open transactions when the running thread is killed.
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r--activerecord/test/cases/transactions_test.rb31
1 files changed, 31 insertions, 0 deletions
diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb
index 9cfe041de2..5cccf2dda5 100644
--- a/activerecord/test/cases/transactions_test.rb
+++ b/activerecord/test/cases/transactions_test.rb
@@ -492,6 +492,37 @@ class TransactionTest < ActiveRecord::TestCase
assert topic.frozen?, 'not frozen'
end
+ # The behavior of killed threads having a status of "aborting" was changed
+ # in Ruby 2.0, so Thread#kill on 1.9 will prematurely commit the transaction
+ # and there's nothing we can do about it.
+ unless RUBY_VERSION.start_with? '1.9'
+ def test_rollback_when_thread_killed
+ queue = Queue.new
+ thread = Thread.new do
+ Topic.transaction do
+ @first.approved = true
+ @second.approved = false
+ @first.save
+
+ queue.push nil
+ sleep
+
+ @second.save
+ end
+ end
+
+ queue.pop
+ thread.kill
+ thread.join
+
+ assert @first.approved?, "First should still be changed in the objects"
+ assert !@second.approved?, "Second should still be changed in the objects"
+
+ assert !Topic.find(1).approved?, "First shouldn't have been approved"
+ assert Topic.find(2).approved?, "Second should still be approved"
+ end
+ end
+
def test_restore_active_record_state_for_all_records_in_a_transaction
topic_without_callbacks = Class.new(ActiveRecord::Base) do
self.table_name = 'topics'