Sha256: 1b107264cfb8838f127d79659b625e58d75defceb394e3aefac8c6399fab052a

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

require "#{File.dirname __FILE__}/../ruby_ext_spec_helper"
require "ruby_ext/synchronize"

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
    lambda{SAccount4.synchronize_method :a}.should raise_error(/twice/)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby-ext-0.4.1 spec/ruby_ext/synchronize_spec.rb
ruby-ext-0.4.0 spec/ruby_ext/synchronize_spec.rb