aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/arel/visitors/dispatch_contamination_test.rb
diff options
context:
space:
mode:
authorMatthew Draper <matthew@trebex.net>2018-02-24 17:04:15 +1030
committerMatthew Draper <matthew@trebex.net>2018-02-24 17:15:32 +1030
commit17ca17072dcdff11b3702a6b45f2fb0c8f8fe9a4 (patch)
tree38afd3ed74f8afda1c2959fefbc13f70b2e448e2 /activerecord/test/cases/arel/visitors/dispatch_contamination_test.rb
parent5ecbeda0e225e4961977b5c516088cf12d92319f (diff)
parenteb3f968b5ffdd3b343e7d190f1aa0b36864f56a2 (diff)
downloadrails-17ca17072dcdff11b3702a6b45f2fb0c8f8fe9a4.tar.gz
rails-17ca17072dcdff11b3702a6b45f2fb0c8f8fe9a4.tar.bz2
rails-17ca17072dcdff11b3702a6b45f2fb0c8f8fe9a4.zip
Merge Arel into Active Record
Diffstat (limited to 'activerecord/test/cases/arel/visitors/dispatch_contamination_test.rb')
-rw-r--r--activerecord/test/cases/arel/visitors/dispatch_contamination_test.rb72
1 files changed, 72 insertions, 0 deletions
diff --git a/activerecord/test/cases/arel/visitors/dispatch_contamination_test.rb b/activerecord/test/cases/arel/visitors/dispatch_contamination_test.rb
new file mode 100644
index 0000000000..eb278cde4c
--- /dev/null
+++ b/activerecord/test/cases/arel/visitors/dispatch_contamination_test.rb
@@ -0,0 +1,72 @@
+# frozen_string_literal: true
+require_relative '../helper'
+require 'concurrent'
+
+module Arel
+ module Visitors
+ class DummyVisitor < Visitor
+ def initialize
+ super
+ @barrier = Concurrent::CyclicBarrier.new(2)
+ end
+
+ def visit_Arel_Visitors_DummySuperNode node
+ 42
+ end
+
+ # This is terrible, but it's the only way to reliably reproduce
+ # the possible race where two threads attempt to correct the
+ # dispatch hash at the same time.
+ def send *args
+ super
+ rescue
+ # Both threads try (and fail) to dispatch to the subclass's name
+ @barrier.wait
+ raise
+ ensure
+ # Then one thread successfully completes (updating the dispatch
+ # table in the process) before the other finishes raising its
+ # exception.
+ Thread.current[:delay].wait if Thread.current[:delay]
+ end
+ end
+
+ class DummySuperNode
+ end
+
+ class DummySubNode < DummySuperNode
+ end
+
+ class DispatchContaminationTest < Arel::Spec
+ before do
+ @connection = Table.engine.connection
+ @table = Table.new(:users)
+ end
+
+ it 'dispatches properly after failing upwards' do
+ node = Nodes::Union.new(Nodes::True.new, Nodes::False.new)
+ assert_equal "( TRUE UNION FALSE )", node.to_sql
+
+ node.first # from Nodes::Node's Enumerable mixin
+
+ assert_equal "( TRUE UNION FALSE )", node.to_sql
+ end
+
+ it 'is threadsafe when implementing superclass fallback' do
+ visitor = DummyVisitor.new
+ main_thread_finished = Concurrent::Event.new
+
+ racing_thread = Thread.new do
+ Thread.current[:delay] = main_thread_finished
+ visitor.accept DummySubNode.new
+ end
+
+ assert_equal 42, visitor.accept(DummySubNode.new)
+ main_thread_finished.set
+
+ assert_equal 42, racing_thread.value
+ end
+ end
+ end
+end
+