[macruby-changes] [3467] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Tue Feb 9 15:24:49 PST 2010


Revision: 3467
          http://trac.macosforge.org/projects/ruby/changeset/3467
Author:   ernest.prabhakar at gmail.com
Date:     2010-02-09 15:24:49 -0800 (Tue, 09 Feb 2010)
Log Message:
-----------
Dispatch::Future now a subclass of Group

Modified Paths:
--------------
    MacRuby/trunk/lib/dispatch/dispatch.rb
    MacRuby/trunk/lib/dispatch/future.rb
    MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb
    MacRuby/trunk/spec/macruby/library/dispatch/future_spec.rb

Modified: MacRuby/trunk/lib/dispatch/dispatch.rb
===================================================================
--- MacRuby/trunk/lib/dispatch/dispatch.rb	2010-02-09 23:24:24 UTC (rev 3466)
+++ MacRuby/trunk/lib/dispatch/dispatch.rb	2010-02-09 23:24:49 UTC (rev 3467)
@@ -43,7 +43,7 @@
 
   # Run the +&block+ asynchronously on a concurrent queue of the given
   # (optional) +priority+ as part of a Future, which is returned for use with
-  # +join+ or +value+
+  # +join+ or +value+ -- or as a Group, of which it is a subclass
   
   def fork(priority=nil, &block)
     Dispatch::Future.new(priority) &block

Modified: MacRuby/trunk/lib/dispatch/future.rb
===================================================================
--- MacRuby/trunk/lib/dispatch/future.rb	2010-02-09 23:24:24 UTC (rev 3466)
+++ MacRuby/trunk/lib/dispatch/future.rb	2010-02-09 23:24:49 UTC (rev 3467)
@@ -4,7 +4,7 @@
   # Subclass of Dispatch::Group used to implement lazy Futures
   # By returning a value and duck-typing Thread +join+ and +value+
    
-  class Future < Group
+  class Future < Dispatch::Group
     # Create a future that asynchronously dispatches the block 
     # to a concurrent queue of the specified (optional) +priority+
     def initialize(priority=nil, &block)
@@ -14,7 +14,7 @@
     end
 
     # Waits for the computation to finish
-    alias :wait, :join
+    alias_method :join, :wait
 
     # Joins, then returns the value
     # If a block is passed, invoke that asynchronously with the final value
@@ -30,4 +30,14 @@
     end   
   end
 
+  # Run the +&block+ asynchronously on a concurrent queue of the given
+  # (optional) +priority+ as part of a Future, which is returned for use with
+  # +join+ or +value+ -- or as a Group, of which it is a subclass
+  
+  def fork(priority=nil, &block)
+    Dispatch::Future.new(priority) &block
+  end
+
+  module_function :fork
+
 end

Modified: MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb
===================================================================
--- MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb	2010-02-09 23:24:24 UTC (rev 3466)
+++ MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb	2010-02-09 23:24:49 UTC (rev 3467)
@@ -3,10 +3,10 @@
 
 if MACOSX_VERSION >= 10.6
   
-  $global = 0
+  $dispatch_gval = 0
   class Actee
     def initialize(s="default"); @s = s; end
-    def delay_set(n); sleep 0.01; $global = n; end
+    def delay_set(n); sleep 0.01; $dispatch_gval = n; end
     def to_s; @s; end
   end
   
@@ -33,30 +33,30 @@
 
     describe :sync do
       it "should execute the block Synchronously" do
-        $global = 0
+        $dispatch_gval = 0
         Dispatch.sync { @actee.delay_set(42) }
-        $global.should == 42
+        $dispatch_gval.should == 42
       end
     end
 
     describe :async do
       it "should execute the block Asynchronously" do
-        $global = 0
+        $dispatch_gval = 0
         Dispatch.async(:default) { @actee.delay_set(42) }
-        $global.should == 0
-        while $global == 0 do; end
-        $global.should == 42      
+        $dispatch_gval.should == 0
+        while $dispatch_gval == 0 do; end
+        $dispatch_gval.should == 42      
       end
     end
     
     describe :group do
       it "should execute the block with the specified group" do
-        $global = 0
+        $dispatch_gval = 0
         g = Dispatch::Group.new
         Dispatch.group(g) { @actee.delay_set(42) }
-        $global.should == 0
+        $dispatch_gval.should == 0
         g.wait
-        $global.should == 42      
+        $dispatch_gval.should == 42      
       end
     end
 
@@ -75,32 +75,23 @@
     end
     
     describe :fork do
-      it "should return an Group for tracking execution of the passed block" do
-        $global = 0
+      it "should return a Future for tracking execution of the passed block" do
+        $dispatch_gval = 0
         g = Dispatch.fork { @actee.delay_set(42) }
-        $global.should == 0
-        g.should be_kind_of Dispatch::Group
-        g.wait
-        $global.should == 42      
+        $dispatch_gval.should == 0
+        g.should be_kind_of Dispatch::Future
+        #g.join
+        $dispatch_gval.should == 42      
       end
-
-      it "should :join Synchronously to that group" do
-        $global = 0
+      
+      it "should return a Group for tracking execution of the passed block" do
+        $dispatch_gval = 0
         g = Dispatch.fork { @actee.delay_set(42) }
-        $global.should == 0
-        g.join
-        $global.should == 42      
+        $dispatch_gval.should == 0
+        g.should be_kind_of Dispatch::Group
+        #g.wait
+        $dispatch_gval.should == 42      
       end
-
-      it "should :join Asynchronously if passed another block" do
-        $global = 0
-        g = Dispatch.fork { @actee.delay_set(42) }
-        $global.should == 0
-        g.join { }
-        $global.should == 0
-        while $global == 0 do; end
-        $global.should == 42      
-      end
     end
     
   end

Modified: MacRuby/trunk/spec/macruby/library/dispatch/future_spec.rb
===================================================================
--- MacRuby/trunk/spec/macruby/library/dispatch/future_spec.rb	2010-02-09 23:24:24 UTC (rev 3466)
+++ MacRuby/trunk/spec/macruby/library/dispatch/future_spec.rb	2010-02-09 23:24:49 UTC (rev 3467)
@@ -5,28 +5,40 @@
   describe "Dispatch::Future" do
     
     before :each do
-      @future = Dispatch::Future.new { Math.sqrt(2**10) }
+      @result = 0
+      @future = Dispatch::Future.new { sleep 0.01; @result = Math.sqrt(2**10) }
     end
 
     describe :new do
-      it "should return an Future for tracking execution of the passed block" do
+      it "should return a Future for tracking execution of the passed block" do
         @future.should be_kind_of Dispatch::Future
-        future = Dispatch.future { Math.sqrt(2**10) }
-        future.should be_kind_of Dispatch::Future
       end
+
+      it "should return an instance of Dispatch::Group" do
+        @future.should be_kind_of Dispatch::Group
+      end
     end
-    
-    describe :call do
-      it "should wait Synchronously to return value" do
-        @future.call.should == 2**5
+
+    describe :join do
+      it "should wait until execution is complete"
+        @result.should == 0
+        @future.join
+        @result.should == 2**5
       end
+    end
 
-      it "should invoke passed block Asynchronously with returned value " do
-        $global = 0
-        @future.call {|v| $global = v}
-        while $global == 0 do; end
-        $global.should == 2**5
+    describe :value do
+      it "should return value when called Synchronously" do
+        @future.value.should == 2**5
       end
+
+      it "should invoke passed block Asynchronously with return value" do
+        @fval = 0
+        @future.value {|v| @fval = v}
+        while @fval == 0 do; end
+        @fval.should == 2**5
+      end
     end
+    
   end
 end
\ No newline at end of file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100209/319b0666/attachment.html>


More information about the macruby-changes mailing list