Revision: 3422 http://trac.macosforge.org/projects/ruby/changeset/3422 Author: ernest.prabhakar@gmail.com Date: 2010-02-03 17:15:53 -0800 (Wed, 03 Feb 2010) Log Message: ----------- Add/pass Dispatch :group and :queue_for specs Modified Paths: -------------- MacRuby/trunk/lib/dispatch/dispatch.rb MacRuby/trunk/spec/macruby/core/gcd/queue_spec.rb MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb Modified: MacRuby/trunk/lib/dispatch/dispatch.rb =================================================================== --- MacRuby/trunk/lib/dispatch/dispatch.rb 2010-02-04 01:15:46 UTC (rev 3421) +++ MacRuby/trunk/lib/dispatch/dispatch.rb 2010-02-04 01:15:53 UTC (rev 3422) @@ -6,7 +6,7 @@ # the ancestor chain and ID of +obj+ def queue_for(obj) label = obj.class.ancestors.reverse.join(".").downcase - Dispatch::Queue.new("#{label}.%x" % obj.object_id) + Dispatch::Queue.new("#{label}.0x%x[#{obj}]" % obj.object_id) end # Run the +&block+ synchronously on a concurrent queue @@ -24,7 +24,8 @@ # Run the +&block+ asynchronously on a concurrent queue # of the given (optional) +priority+ as part of the specified +grp+ def group(grp, priority=nil, &block) - Dispatch::Queue.concurrent(priority).async(grp) &block + Dispatch::Queue.concurrent(priority).async(grp) { block.call } + # Can't pass block directly for some reason end # Wrap the passed +obj+ (or its instance) inside an Actor to serialize access Modified: MacRuby/trunk/spec/macruby/core/gcd/queue_spec.rb =================================================================== --- MacRuby/trunk/spec/macruby/core/gcd/queue_spec.rb 2010-02-04 01:15:46 UTC (rev 3421) +++ MacRuby/trunk/spec/macruby/core/gcd/queue_spec.rb 2010-02-04 01:15:53 UTC (rev 3422) @@ -84,6 +84,14 @@ @i.should == 42 end + it "accepts a group which tracks the asynchronous execution" do + @i = 0 + g = Dispatch::Group.new + @q.async(g) { sleep 0.01; @i = 42 } + @i.should == 0 + g.wait + @i.should == 42 + end it "raises an ArgumentError if no block is given" do lambda { @q.async }.should raise_error(ArgumentError) Modified: MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb =================================================================== --- MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb 2010-02-04 01:15:46 UTC (rev 3421) +++ MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb 2010-02-04 01:15:53 UTC (rev 3422) @@ -17,10 +17,20 @@ @actee = Actee.new("my_actee") end + describe :queue_for do + it "should return a unique label per actee" do + s1 = Dispatch.queue_for(@actee).to_s + s2 = Dispatch.queue_for(@actee).to_s + s3 = Dispatch.queue_for(Actee.new("your_actee")).to_s + s1.should == s2 + s1.should_not == s3 + end + end + describe :sync do it "should execute the block Synchronously" do $global = 0 - Dispatch::sync { @actee.delay_set(42) } + Dispatch.sync { @actee.delay_set(42) } $global.should == 42 end end @@ -28,12 +38,23 @@ describe :async do it "should execute the block Asynchronously" do $global = 0 - Dispatch::async { @actee.delay_set(42) } + Dispatch.async(:default) { @actee.delay_set(42) } $global.should == 0 while $global == 0 do; end $global.should == 42 end end + describe :group do + it "should execute the block with the specified group" do + $global = 0 + g = Dispatch::Group.new + Dispatch.group(g) { @actee.delay_set(42) } + $global.should == 0 + g.wait + $global.should == 42 + end + end + end end \ No newline at end of file
participants (1)
-
source_changes@macosforge.org