diff options
Diffstat (limited to 'activerecord/test/cases/adapters/postgresql/transaction_test.rb')
-rw-r--r-- | activerecord/test/cases/adapters/postgresql/transaction_test.rb | 176 |
1 files changed, 147 insertions, 29 deletions
diff --git a/activerecord/test/cases/adapters/postgresql/transaction_test.rb b/activerecord/test/cases/adapters/postgresql/transaction_test.rb index e76705a802..9821b103df 100644 --- a/activerecord/test/cases/adapters/postgresql/transaction_test.rb +++ b/activerecord/test/cases/adapters/postgresql/transaction_test.rb @@ -1,21 +1,27 @@ +# frozen_string_literal: true + require "cases/helper" -require 'support/connection_helper' +require "support/connection_helper" +require "concurrent/atomic/cyclic_barrier" module ActiveRecord class PostgresqlTransactionTest < ActiveRecord::PostgreSQLTestCase self.use_transactional_tests = false class Sample < ActiveRecord::Base - self.table_name = 'samples' + self.table_name = "samples" end setup do + @abort, Thread.abort_on_exception = Thread.abort_on_exception, false + Thread.report_on_exception, @original_report_on_exception = false, Thread.report_on_exception if Thread.respond_to?(:report_on_exception) + @connection = ActiveRecord::Base.connection @connection.transaction do - @connection.drop_table 'samples', if_exists: true - @connection.create_table('samples') do |t| - t.integer 'value' + @connection.drop_table "samples", if_exists: true + @connection.create_table("samples") do |t| + t.integer "value" end end @@ -23,50 +29,162 @@ module ActiveRecord end teardown do - @connection.drop_table 'samples', if_exists: true + @connection.drop_table "samples", if_exists: true + + Thread.abort_on_exception = @abort + Thread.report_on_exception = @original_report_on_exception if Thread.respond_to?(:report_on_exception) end - test "raises error when a serialization failure occurs" do - with_warning_suppression do - assert_raises(ActiveRecord::TransactionSerializationError) do - thread = Thread.new do - Sample.transaction isolation: :serializable do - Sample.delete_all + test "raises SerializationFailure when a serialization failure occurs" do + assert_raises(ActiveRecord::SerializationFailure) do + before = Concurrent::CyclicBarrier.new(2) + after = Concurrent::CyclicBarrier.new(2) - 10.times do |i| - sleep 0.1 + thread = Thread.new do + with_warning_suppression do + Sample.transaction isolation: :serializable do + before.wait + Sample.create value: Sample.sum(:value) + after.wait + end + end + end - Sample.create value: i - end + begin + with_warning_suppression do + Sample.transaction isolation: :serializable do + before.wait + Sample.create value: Sample.sum(:value) + after.wait end end + ensure + thread.join + end + end + end - sleep 0.1 + test "raises Deadlocked when a deadlock is encountered" do + with_warning_suppression do + assert_raises(ActiveRecord::Deadlocked) do + barrier = Concurrent::CyclicBarrier.new(2) - Sample.transaction isolation: :serializable do - Sample.delete_all + s1 = Sample.create value: 1 + s2 = Sample.create value: 2 - 10.times do |i| - sleep 0.1 + thread = Thread.new do + Sample.transaction do + s1.lock! + barrier.wait + s2.update_attributes value: 1 + end + end - Sample.create value: i + begin + Sample.transaction do + s2.lock! + barrier.wait + s1.update_attributes value: 2 end + ensure + thread.join + end + end + end + end - sleep 1 + test "raises LockWaitTimeout when lock wait timeout exceeded" do + skip unless ActiveRecord::Base.connection.postgresql_version >= 90300 + assert_raises(ActiveRecord::LockWaitTimeout) do + s = Sample.create!(value: 1) + latch1 = Concurrent::CountDownLatch.new + latch2 = Concurrent::CountDownLatch.new + + thread = Thread.new do + Sample.transaction do + Sample.lock.find(s.id) + latch1.count_down + latch2.wait end + end + begin + Sample.transaction do + latch1.wait + Sample.connection.execute("SET lock_timeout = 1") + Sample.lock.find(s.id) + end + ensure + Sample.connection.execute("SET lock_timeout = DEFAULT") + latch2.count_down thread.join end end end - protected + test "raises QueryCanceled when statement timeout exceeded" do + assert_raises(ActiveRecord::QueryCanceled) do + s = Sample.create!(value: 1) + latch1 = Concurrent::CountDownLatch.new + latch2 = Concurrent::CountDownLatch.new + + thread = Thread.new do + Sample.transaction do + Sample.lock.find(s.id) + latch1.count_down + latch2.wait + end + end - def with_warning_suppression - log_level = @connection.client_min_messages - @connection.client_min_messages = 'error' - yield - @connection.client_min_messages = log_level + begin + Sample.transaction do + latch1.wait + Sample.connection.execute("SET statement_timeout = 1") + Sample.lock.find(s.id) + end + ensure + Sample.connection.execute("SET statement_timeout = DEFAULT") + latch2.count_down + thread.join + end + end end + + test "raises QueryCanceled when canceling statement due to user request" do + assert_raises(ActiveRecord::QueryCanceled) do + s = Sample.create!(value: 1) + latch = Concurrent::CountDownLatch.new + + thread = Thread.new do + Sample.transaction do + Sample.lock.find(s.id) + latch.count_down + sleep(0.5) + conn = Sample.connection + pid = conn.query_value("SELECT pid FROM pg_stat_activity WHERE query LIKE '% FOR UPDATE'") + conn.execute("SELECT pg_cancel_backend(#{pid})") + end + end + + begin + Sample.transaction do + latch.wait + Sample.lock.find(s.id) + end + ensure + thread.join + end + end + end + + private + + def with_warning_suppression + log_level = ActiveRecord::Base.connection.client_min_messages + ActiveRecord::Base.connection.client_min_messages = "error" + yield + ensure + ActiveRecord::Base.connection.client_min_messages = log_level + end end end |