blob: a07a1a050a54fa7dab35d7b96d38aec74122ddb5 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
|