Sha256: a3c743f87ba29a55bc75be480d861327cc64c466a51757636c21391517ae9a7f

Contents?: true

Size: 1.46 KB

Versions: 14

Compression:

Stored size: 1.46 KB

Contents

require "more/spec_helper"

describe "Synchronize" do
  it "synchronize_method" do
    class SAccount
      attr_reader :from, :to
      
      def initialize
        super
        @from, @to = 0, 0
      end
      
      def transfer
        @from -= 1
        @to += 1
      end
      synchronize_method :transfer
    end
    
    a, threads = SAccount.new, []
    100.times do 
      t = Thread.new do
        100.times{a.transfer}
      end
      threads << t
    end        
    threads.each{|t| t.join}
    
    a.from.should == -10_000
    a.to.should == 10_000
  end
  
  it "synchronize_all_methods" do
    class SAccount2
      attr_reader :from, :to
      
      def initialize
        super
        @from, @to = 0, 0
      end
      
      def transfer
        @from -= 1
        @to += 1
      end
      synchronize_all_methods
    end
    
    a, threads = SAccount2.new, []
    100.times do 
      t = Thread.new do
        100.times{a.transfer}
      end
      threads << t
    end        
    threads.each{|t| t.join}
    
    a.from.should == -10_000
    a.to.should == 10_000
  end
  
  it "singleton" do
    class SAccount3                
      class << self
        def a; end
        synchronize_method :a
      end        
    end
    SAccount3.a
  end
  
  it "shouldn't allow to synchronize twice" do
    class SAccount4
      def a; end        
    end
    SAccount4.synchronize_method :a
    -> {SAccount4.synchronize_method :a}.should raise_error(/twice/)
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
ruby_ext-0.4.25 spec/more/synchronize_spec.rb
ruby_ext-0.4.24 spec/more/synchronize_spec.rb
ruby_ext-0.4.23 spec/more/synchronize_spec.rb
ruby_ext-0.4.22 spec/more/synchronize_spec.rb
ruby_ext-0.4.21 spec/more/synchronize_spec.rb
ruby_ext-0.4.20 spec/more/synchronize_spec.rb
ruby_ext-0.4.19 spec/more/synchronize_spec.rb
ruby_ext-0.4.18 spec/more/synchronize_spec.rb
ruby_ext-0.4.17 spec/more/synchronize_spec.rb
ruby_ext-0.4.16 spec/more/synchronize_spec.rb
ruby_ext-0.4.15 spec/more/synchronize_spec.rb
ruby_ext-0.4.14 spec/more/synchronize_spec.rb
ruby_ext-0.4.13 spec/more/synchronize_spec.rb
ruby_ext-0.4.12 spec/more/synchronize_spec.rb