Author: kclark
Date: Tue Jun 17 18:17:30 2008
New Revision: 669010

URL: http://svn.apache.org/viewvc?rev=669010&view=rev
Log:
rb: Enhance non-blocking read in Socket

Also prevent errors when trying to close a @handle twice

Modified:
    incubator/thrift/trunk/lib/rb/lib/thrift/transport/socket.rb
    incubator/thrift/trunk/lib/rb/spec/socket_spec.rb

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/transport/socket.rb
URL: 
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/transport/socket.rb?rev=669010&r1=669009&r2=669010&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/transport/socket.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/transport/socket.rb Tue Jun 17 
18:17:30 2008
@@ -48,8 +48,11 @@
         else
           data = @handle.read(sz)
         end
+      rescue Errno::EAGAIN => e
+        # let our parent know that the nonblock read failed
+        raise e
       rescue StandardError => e
-        @handle.close
+        @handle.close unless @handle.closed?
         @handle = nil
         raise TransportException.new(TransportException::NOT_OPEN, e.message)
       end
@@ -59,10 +62,18 @@
       data
     end
 
+    def read_nonblock(sz)
+      read(sz, true)
+    end
+
     def close
-      @handle.close unless @handle.nil?
+      @handle.close unless @handle.nil? or @handle.closed?
       @handle = nil
     end
+
+    def to_io
+      @handle
+    end
   end
   deprecate_class! :TSocket => Socket
 
@@ -95,7 +106,7 @@
     end
 
     def close
-     @handle.close unless @handle.nil?
+     @handle.close unless @handle.nil? or @handle.closed?
      @handle = nil
     end
   end

Modified: incubator/thrift/trunk/lib/rb/spec/socket_spec.rb
URL: 
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/socket_spec.rb?rev=669010&r1=669009&r2=669010&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/socket_spec.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/socket_spec.rb Tue Jun 17 18:17:30 2008
@@ -5,7 +5,7 @@
 
   before(:each) do
     @socket = Socket.new
-    @handle = mock("Handle")
+    @handle = mock("Handle", :closed? => false)
     @handle.stub!(:close)
   end
 
@@ -107,7 +107,7 @@
     end
 
     it "should close the handle when closed" do
-      handle = mock("TCPServer")
+      handle = mock("TCPServer", :closed? => false)
       TCPServer.should_receive(:new).with(nil, 1234).and_return(handle)
       @socket.listen
       handle.should_receive(:close)


Reply via email to