Revision: 3405 http://trac.macosforge.org/projects/ruby/changeset/3405 Author: ernest.prabhakar@gmail.com Date: 2010-02-01 17:35:49 -0800 (Mon, 01 Feb 2010) Log Message: ----------- Tested asynchronous execution via 'sleep' Modified Paths: -------------- MacRuby/trunk/lib/dispatch/actor.rb MacRuby/trunk/spec/macruby/core/gcd/queue_spec.rb MacRuby/trunk/spec/macruby/library/dispatch/actor_spec.rb Modified: MacRuby/trunk/lib/dispatch/actor.rb =================================================================== --- MacRuby/trunk/lib/dispatch/actor.rb 2010-02-02 01:35:46 UTC (rev 3404) +++ MacRuby/trunk/lib/dispatch/actor.rb 2010-02-02 01:35:49 UTC (rev 3405) @@ -15,15 +15,10 @@ def initialize(delegate, callback=nil) super(delegate) @default_callback = callback || Dispatch::Queue.concurrent + @callback = @default_callback @q = Dispatch::Queue.new("dispatch.actor.#{delegate}.#{object_id}") - __reset!__ end - - def __reset!__ - @callback = @default_callback - @group = nil - end - + # Specify the +callback+ queue for the next async request def _on_(callback) @callback = callback @@ -43,13 +38,17 @@ def method_missing(symbol, *args, &block) if block_given? or not @group.nil? + puts "\nAsync #{symbol.inspect}" callback = @callback @q.async(@group) do retval = __getobj__.__send__(symbol, *args) callback.async { block.call(retval) } if not callback.nil? end - return __reset!__ + @callback = @default_callback if not @callback == @default_callback + @group = nil if not @group.nil? + return nil else + puts "\nSync #{symbol.inspect}" if symbol != :__ @retval = nil @q.sync { @retval = __getobj__.__send__(symbol, *args) } return @retval Modified: MacRuby/trunk/spec/macruby/core/gcd/queue_spec.rb =================================================================== --- MacRuby/trunk/spec/macruby/core/gcd/queue_spec.rb 2010-02-02 01:35:46 UTC (rev 3404) +++ MacRuby/trunk/spec/macruby/core/gcd/queue_spec.rb 2010-02-02 01:35:49 UTC (rev 3405) @@ -78,7 +78,8 @@ describe :async do it "accepts a block and yields it asynchronously" do @i = 0 - @q.async { @i = 42 } + @q.async { sleep 0.01; @i = 42 } + @i.should == 0 while @i == 0 do; end @i.should == 42 end @@ -92,7 +93,7 @@ describe :sync do it "accepts a block and yields it synchronously" do @i = 0 - @q.sync { @i = 42 } + @q.sync { sleep 0.01; @i = 42 } @i.should == 42 end Modified: MacRuby/trunk/spec/macruby/library/dispatch/actor_spec.rb =================================================================== --- MacRuby/trunk/spec/macruby/library/dispatch/actor_spec.rb 2010-02-02 01:35:46 UTC (rev 3404) +++ MacRuby/trunk/spec/macruby/library/dispatch/actor_spec.rb 2010-02-02 01:35:49 UTC (rev 3405) @@ -3,10 +3,12 @@ if MACOSX_VERSION >= 10.6 + $global = 0 class Actee def initialize(s); @s = s; end def current_queue; Dispatch::Queue.current; end - def wait(n); sleep n; end + def delay_set(n); sleep 0.01; $global = n; end + def increment(v); v+1; end def to_s; @s; end end @@ -36,14 +38,41 @@ end it "should call actee Synchronously if block is NOT given" do - true.should == true + $global = 0 + t0 = Time.now + @actor.delay_set(42) + t1 = Time.now + puts "Elapsed: #{(t1-t0)}" + $global.should == 42 end + it "should return value when called Synchronously" do + @actor.increment(41).should == 42 + end + + it "should return nil when called Asynchronously" do + @v = 0 + v = @actor.increment(41) {|rv| @v = rv} + v.should.nil? + end + + it "should provide return value to block when called Asynchronously" do + @v = 0 + @actor.increment(41) {|rv| @v = rv} + while @v == 0 do; end + @v.should == 42 + end + it "should call actee Asynchronously if block IS given" do - true.should == true + $global = 0 + $global.should == 0 + @actor.delay_set(42) { } + $global.should == 0 + while $global == 0 do; end + $global.should == 42 end - it "should used default callback when called Asynchronously" do + it "should used default callback queue when called Asynchronously" do true.should == true end
participants (1)
-
source_changes@macosforge.org