[macruby-changes] [3256] MacRuby/trunk/spec/macruby/core/gcd/source_spec.rb

source_changes at macosforge.org source_changes at macosforge.org
Tue Jan 12 16:47:25 PST 2010


Revision: 3256
          http://trac.macosforge.org/projects/ruby/changeset/3256
Author:   ernest.prabhakar at gmail.com
Date:     2010-01-12 16:47:24 -0800 (Tue, 12 Jan 2010)
Log Message:
-----------
Passes all Dispatch::Source file source specs

Modified Paths:
--------------
    MacRuby/trunk/spec/macruby/core/gcd/source_spec.rb

Modified: MacRuby/trunk/spec/macruby/core/gcd/source_spec.rb
===================================================================
--- MacRuby/trunk/spec/macruby/core/gcd/source_spec.rb	2010-01-13 00:47:23 UTC (rev 3255)
+++ MacRuby/trunk/spec/macruby/core/gcd/source_spec.rb	2010-01-13 00:47:24 UTC (rev 3256)
@@ -164,6 +164,7 @@
           src = Dispatch::Source.new(@type, $$, @mask, @q) { |s|  @i = s.data }
           Signal.trap(@signal, "IGNORE")
           Process.kill(@signal, $$)
+          sleep 0.01
           Signal.trap(@signal, "DEFAULT")
           @q.sync { }
           @i.should == @mask
@@ -197,22 +198,24 @@
       
       describe "file:" do
         before :each do
+          @msg = "#{$$}: #{Time.now}"
           @filename = "/var/tmp/gcd_spec_source-#{$$}"
-          @msg = "#{$$}: #{Time.now}"
+          File.delete(@filename) if File.exist?(@filename)
+          @file = nil
         end
+
+        after :each do
+          @q.sync { }
+          @file.close
+        end
       
         describe :READ do
           before :each do
             @type = Dispatch::Source::READ
-            File.delete(@filename) if File.exist?(@filename)
             File.open(@filename, "w") {|f| f.print @msg}
             @file = File.open(@filename, "r")
           end
           
-          after :each do
-            @file.close if not @file.closed?
-          end
-
           it "returns an instance of Dispatch::Source" do
             src = Dispatch::Source.new(@type, @file.to_i, 0, @q) { }
             src.should be_kind_of(Dispatch::Source)
@@ -229,9 +232,9 @@
               end
             end
             while (@result.size < @msg.size) do; end
-            src.cancel!            
             @q.sync { }
             @result.should == @msg
+            src.cancel!            
           end
           
           it "does not close file when cancelled" do
@@ -240,45 +243,80 @@
             @q.sync { }
             @file.closed?.should == false
           end
-          
         end    
 
         describe :WRITE do
           before :each do
             @type = Dispatch::Source::WRITE
+            @file = File.open(@filename, "w")
           end
 
           it "returns an instance of Dispatch::Source" do
-            src = Dispatch::Source.new(@type, $stdout.to_i, 0, @q) { }
+            src = Dispatch::Source.new(@type, @file.to_i, 0, @q) { }
             src.should be_kind_of(Dispatch::Source)
+            src.cancel!            
           end
+          
+          it "fires with data on estimated # of writeable bytes" do
+            @pos = 0
+            @message = @msg
+            src = Dispatch::Source.new(@type, @file.to_i, 0, @q) do |s|
+              begin
+                pos = s.data
+                if not @message.nil? then
+                  next_msg = @message[0..pos-1]
+                  @file.write(next_msg) # ideally should write_nonblock
+                  @message = @message[pos..-1]
+                end
+              rescue Exception => error
+                puts error
+              end
+            end
+            while (@message.size > 0) do; end
+            @q.sync { }
+            File.read(@filename).should == @msg
+            src.cancel!            
+          end
         end    
 
         describe :VNODE do
           before :each do
             @type = Dispatch::Source::VNODE
             @mask = Dispatch::Source::VNODE_WRITE
+            @file = File.open(@filename, "w")
           end
 
           it "returns an instance of Dispatch::Source" do
-            src = Dispatch::Source.new(@type, $stdout.to_i, @mask, @q) { }
+            src = Dispatch::Source.new(@type, @file.to_i, @mask, @q) { }
             src.should be_kind_of(Dispatch::Source)
+            src.cancel!            
           end
+          
+          it "fires with data showing mask of vnode events" do
+            @flag = 0
+            src = Dispatch::Source.new(@type, @file.to_i, @mask, @q) do |s|
+                @flag = s.data
+            end
+            @file.write(@msg)
+            @q.sync { }
+            @flag.should == @mask
+            src.cancel!            
+          end    
         end
-  
       end
     end
       
   end
   
-  
   describe "Dispatch::Timer" do
     before :each do
       @q = Dispatch::Queue.new('org.macruby.gcd_spec.sources')
     end
+    
     it "returns an instance of Dispatch::Source" do
-      src = Dispatch::Timer.new(nil, 0.1, nil, @q) { }
+      src = Dispatch::Timer.new(Time.now, 0.1, 0, @q) { }
       src.should be_kind_of(Dispatch::Source)
+      src.should be_kind_of(Dispatch::Timer)
     end
   end
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100112/7416108d/attachment-0001.html>


More information about the macruby-changes mailing list