Skip to content
This repository
tag: 1.5.0.RC3
Fetching contributors…

Cannot retrieve contributors at this time

file 65 lines (56 sloc) 1.481 kb
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
require 'test/unit'

# JRUBY-4548
class TestRespondToConcurrency < Test::Unit::TestCase
  module Hashable
    def to_h
      hash = {}
      self.class.hashable_methods.each do |method_entry|
        val = self.__send__(method_entry[:method_name])
        #Respond to returns true sometimes and false other times
        hash[method_entry[:key]] = (val.respond_to?(:to_h) ? val.to_h : val ) if val
        #is_a?(Hashable) always seems to work
        #hash[method_entry[:key]] = (val.is_a?(Hashable) ? val.to_h : val ) if val
      end
      hash
    end

    def self.included(klass)
      klass.extend(ClassMethods)
    end

    module ClassMethods
      attr_reader :hashable_methods
      def method_added(method_name)
        @hashable_methods ||= []
        @hashable_methods << {:method_name => method_name, :key => method_name.to_s }
      end
    end
  end

  class Foo
    include Hashable
    def bar
      @bar ||= Bar.new
    end

    def dofoo
      return 1
    end
  end

  class Bar
    include Hashable
    def dobar
      return 1
    end
  end

  EXPECTED = {"bar"=>{"dobar"=>1}, "dofoo"=>1}

  def test_respond_to_heavy_concurrency
    assert_nothing_raised do
      threads = (0..100).map do
        Thread.new do
          1000.times do
            actual = Foo.new.to_h
            assert_equal EXPECTED, actual, "respond_to? concurrency produced an invalid result"
          end
        end
      end

      threads.each { |t| t.join }
    end
  end
end
Something went wrong with that request. Please try again.