Revision: 3421 http://trac.macosforge.org/projects/ruby/changeset/3421 Author: ernest.prabhakar@gmail.com Date: 2010-02-03 17:15:46 -0800 (Wed, 03 Feb 2010) Log Message: ----------- Add/pass spec for Dispatch methods Modified Paths: -------------- MacRuby/trunk/lib/dispatch/dispatch.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-03 20:51:32 UTC (rev 3420) +++ MacRuby/trunk/lib/dispatch/dispatch.rb 2010-02-04 01:15:46 UTC (rev 3421) @@ -9,12 +9,6 @@ Dispatch::Queue.new("#{label}.%x" % obj.object_id) end - # Run the +&block+ asynchronously on a concurrent queue - # of the given (optional) +priority+ - def async(priority=nil, &block) - Dispatch::Queue.concurrent(priority).async &block - end - # Run the +&block+ synchronously on a concurrent queue # of the given (optional) +priority+ def sync(priority=nil, &block) @@ -22,6 +16,12 @@ end # Run the +&block+ asynchronously on a concurrent queue + # of the given (optional) +priority+ + def async(priority=nil, &block) + Dispatch::Queue.concurrent(priority).async &block + end + + # 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 @@ -52,5 +52,7 @@ block_given? ? notify(&block) : wait end end + + module_function :queue_for, :async, :sync, :group, :wrap, :fork end \ No newline at end of file Modified: MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb =================================================================== --- MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb 2010-02-03 20:51:32 UTC (rev 3420) +++ MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb 2010-02-04 01:15:46 UTC (rev 3421) @@ -2,9 +2,38 @@ require 'dispatch' if MACOSX_VERSION >= 10.6 - describe "Dispatch methods" do - it "should do something" do - true.should == true + + $global = 0 + class Actee + def initialize(s); @s = s; end + def current_queue; Dispatch::Queue.current; end + def delay_set(n); sleep 0.01; $global = n; end + def increment(v); v+1; end + def to_s; @s; end + end + + describe "Dispatch method" do + before :each do + @actee = Actee.new("my_actee") end + + describe :sync do + it "should execute the block Synchronously" do + $global = 0 + Dispatch::sync { @actee.delay_set(42) } + $global.should == 42 + end + end + + describe :async do + it "should execute the block Asynchronously" do + $global = 0 + Dispatch::async { @actee.delay_set(42) } + $global.should == 0 + while $global == 0 do; end + $global.should == 42 + end + end + end end \ No newline at end of file