Revision: 3413 http://trac.macosforge.org/projects/ruby/changeset/3413 Author: ernest.prabhakar@gmail.com Date: 2010-02-02 12:58:45 -0800 (Tue, 02 Feb 2010) Log Message: ----------- Completed Dispatch::Actor spec, removed debug messages Modified Paths: -------------- MacRuby/trunk/lib/dispatch/actor.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 20:58:43 UTC (rev 3412) +++ MacRuby/trunk/lib/dispatch/actor.rb 2010-02-02 20:58:45 UTC (rev 3413) @@ -1,13 +1,12 @@ require 'delegate' module Dispatch - # Create an Actor that serializes or asynchronizes access to a delegate. + # Serialize or asynchronize access to a delegate object. # Forwards method invocations to the passed object via a private serial queue, # and optionally calls back asynchronously (if given a block or group). # # Note that this will NOT work for methods that themselves expect a block. # For those, use e.g., the Enumerable p_* methods insteads. - # class Actor < SimpleDelegator # Create an Actor to wrap the given +delegate+, @@ -19,40 +18,42 @@ end # Specify the +callback+ queue for async requests + # => self to allow chaining def _on_(callback) @callback = callback self end - # Specify the +group+ for async requests + # Specify the +group+ for both private async requests def _with_(group) @group = group self + # => self to allow chaining end - # Wait until the internal private queue has completed execution - # then returns the +delegate+ object + # Wait until the internal private queue has completed pending executions + # then return the +delegate+ object def _done_ @q.sync { } __getobj__ end - + + # Calls the +delegate+ object asychronously if there is a block or group, + # else synchronously 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? + callback.async { block.call(retval) } if not block.nil? end return nil else - #puts "\nSync #{symbol.inspect}" if symbol != :__ @retval = nil @q.sync { @retval = __getobj__.__send__(symbol, *args) } return @retval end end - + end end Modified: MacRuby/trunk/spec/macruby/library/dispatch/actor_spec.rb =================================================================== --- MacRuby/trunk/spec/macruby/library/dispatch/actor_spec.rb 2010-02-02 20:58:43 UTC (rev 3412) +++ MacRuby/trunk/spec/macruby/library/dispatch/actor_spec.rb 2010-02-02 20:58:45 UTC (rev 3413) @@ -39,9 +39,7 @@ it "should call actee Synchronously if block is NOT given" do $global = 0 - t0 = Time.now @actor.delay_set(42) - t1 = Time.now $global.should == 42 end @@ -49,6 +47,14 @@ @actor.increment(41).should == 42 end + it "should call actee Asynchronously if block IS given" do + $global = 0 + @actor.delay_set(42) { } + $global.should == 0 + while $global == 0 do; end + $global.should == 42 + end + it "should return nil when called Asynchronously" do @v = 0 v = @actor.increment(41) {|rv| @v = rv} @@ -62,15 +68,6 @@ @v.should == 42 end - it "should call actee Asynchronously if block IS given" do - $global = 0 - $global.should == 0 - @actor.delay_set(42) { } - $global.should == 0 - while $global == 0 do; end - $global.should == 42 - end - it "should use default callback queue when called Asynchronously" do @qs = "" @actor.increment(41) {|rv| @qs = Dispatch::Queue.current.to_s} @@ -87,16 +84,30 @@ @qs.should == qn.label end - it "should use callback queue specified by _on" do - true.should == true + it "should use callback queue specified by _on_" do + qn = Dispatch::Queue.new("custom") + @qs = "" + @actor._on_(qn).increment(41) {|rv| @qs = Dispatch::Queue.current.to_s} + while @qs == "" do; end + @qs.should == qn.label end - it "should invoke actee async with group specified by _with" do - true.should == true + it "should invoke actee async when group specified by _with_" do + $global = 0 + g = Dispatch::Group.new + @actor._with_(g).delay_set(42) + $global.should == 0 + g.wait + $global.should == 42 end - it "should return actee when called with _done" do - true.should == true + it "should complete work and return actee when called with _done" do + $global = 0 + @actor.delay_set(42) { } + $global.should == 0 + actee = @actor._done_ + $global.should == 42 + actee.should == @actee end end
participants (1)
-
source_changes@macosforge.org