Sha256: e4d7b0504b4ee8ac4bd5b06985b0598b67aa0917cd52d2aa8542e003998c197b

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

require 'RubyExt/require'
require 'spec'

module RubyExt
	describe "Synchronize" do
		it "synchronize" do
			class Account
				inherit RubyExt::Synchronizer
				attr_reader :from, :to
				
				def initialize
					super
					@from, @to = 0, 0
				end
				
				def transfer
					@from -= 1
					@to += 1
				end
				synchronize :transfer
			end
			
			a, threads = Account.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" do
			class Account
				inherit RubyExt::Synchronizer
				attr_reader :from, :to
				
				def initialize
					super
					@from, @to = 0, 0
				end
				
				def transfer
					@from -= 1
					@to += 1
				end
				synchronize_all
			end
			
			a, threads = Account.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
	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
RubyExt-0.1.1 spec/synchronizer_spec.rb
RubyExt-0.1.2 spec/synchronizer_spec.rb