File: synchronized_class_spec.rb

package info (click to toggle)
jruby 1.5.1-1%2Bdeb6u1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze-lts
  • size: 47,024 kB
  • ctags: 74,144
  • sloc: ruby: 398,155; java: 169,506; yacc: 3,782; xml: 2,469; ansic: 415; sh: 279; makefile: 78; tcl: 40
file content (68 lines) | stat: -rw-r--r-- 1,829 bytes parent folder | download | duplicates (4)
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 Spec::Matchers

      def call_wait
        JRuby.reference(self).wait(1)
      end

      def expect_synchronized
        lambda { call_wait }.should_not raise_error
      end

      def expect_unsynchronized
        lambda { call_wait }.should 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
    lambda { mod.class_eval { include JRuby::Synchronized } }.should raise_error(TypeError)
  end
end