File: GH-999_colon2_data_race_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (45 lines) | stat: -rw-r--r-- 1,094 bytes parent folder | download
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
describe "Colon2 lookup with changing receiver under concurrent load" do
  it "never fails to produce the correct value" do
    foo = Module.new do
      const_set :Bar, 1
    end
    
    baz = Module.new do
      const_set :Bar, 2
    end
    
    obj1 = Class.new do
      define_method :module do
        foo
      end
      def value
        1
      end
    end.new

    obj2 = Class.new do
      define_method :module do
        baz
      end
      def value
        2
      end
    end.new

    Thread.abort_on_exception = true

    obj3 = Object.new
    def obj3.casething(obj)
      raise "values did not match" unless obj.value == obj.module::Bar
    end

    # The logic here causes the same colon2 to flip between two different
    # target modules, which in JRuby 1.7.4 and lower could sometimes return
    # the wrong result due to a data race in the caching logic. This was fixed
    # in JRuby 1.7.5.
    ary = []
    50.times { ary << Thread.new { 10000.times { obj3.casething(obj1); obj3.casething(obj2) } } }

    lambda { ary.each(&:join) }.should_not raise_error
  end
end