Skip to content

Commit

Permalink
Simplify serializable test to avoid mystery deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewd committed Oct 6, 2016
1 parent 3a11c5b commit 3127e99
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions activerecord/test/cases/adapters/postgresql/transaction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,29 @@ class Sample < ActiveRecord::Base
end

test "raises SerializationFailure when a serialization failure occurs" do
with_warning_suppression do
assert_raises(ActiveRecord::SerializationFailure) do
thread = Thread.new do
Sample.transaction isolation: :serializable do
Sample.delete_all
assert_raises(ActiveRecord::SerializationFailure) do
before = Concurrent::CyclicBarrier.new(2)
after = Concurrent::CyclicBarrier.new(2)

10.times do |i|
sleep 0.1

Sample.create value: i
end
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

sleep 0.1

Sample.transaction isolation: :serializable do
Sample.delete_all

10.times do |i|
sleep 0.1

Sample.create value: i
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
Expand Down Expand Up @@ -91,10 +88,11 @@ class Sample < ActiveRecord::Base
protected

def with_warning_suppression
log_level = @connection.client_min_messages
@connection.client_min_messages = "error"
log_level = ActiveRecord::Base.connection.client_min_messages
ActiveRecord::Base.connection.client_min_messages = "error"
yield
@connection.client_min_messages = log_level
ensure
ActiveRecord::Base.connection.client_min_messages = log_level
end
end
end

0 comments on commit 3127e99

Please sign in to comment.