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
|
require 'jruby'
require 'jruby/synchronized'
describe "JRuby::Synchronized" do
before(:each) do
@cls = Class.new do
include RSpec::Matchers
def call_wait
JRuby.reference(self).wait(1)
end
def expect_synchronized
expect { call_wait }.not_to raise_error
end
def expect_unsynchronized
expect { call_wait }.to raise_error(java.lang.IllegalMonitorStateException)
end
end
end
it "should make methods on instances of synchronized classes synchronized" do
@cls.new.expect_unsynchronized
@cls.class_eval { include JRuby::Synchronized }
@cls.new.expect_synchronized
end
it "should also affect subclasses" do
subcls = Class.new(@cls)
subcls.new.expect_unsynchronized
subcls.class_eval { include JRuby::Synchronized }
subcls.new.expect_synchronized
end
it "should not affect superclasses" do
@cls.new.expect_unsynchronized
subcls = Class.new(@cls) { include JRuby::Synchronized }
@cls.new.expect_unsynchronized
end
it "should affect existing instances" do
instance = @cls.new
instance.expect_unsynchronized
@cls.class_eval { include JRuby::Synchronized }
instance.expect_synchronized
end
it "should affect existing subclass instances" do
subcls = Class.new(@cls)
instance = subcls.new
instance.expect_unsynchronized
@cls.class_eval { include JRuby::Synchronized }
instance.expect_synchronized
end
it "should work for singleton classes" do
instance = @cls.new
instance.expect_unsynchronized
instance.extend JRuby::Synchronized
instance.expect_synchronized
end
it "should be includable only in classes" do
mod = Module.new
expect { mod.class_eval { include JRuby::Synchronized } }.to raise_error(TypeError)
end
end
|