rb: Add some additional error handling to Thrift::Socket [THRIFT-53]
authorKevin Clark <kclark@apache.org>
Thu, 26 Jun 2008 18:35:15 +0000 (18:35 +0000)
committerKevin Clark <kclark@apache.org>
Thu, 26 Jun 2008 18:35:15 +0000 (18:35 +0000)
Author: Kevin Ballard <kevin@rapleaf.com>

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@671979 13f79535-47bb-0310-9956-ffa450edef68

lib/rb/lib/thrift/transport/socket.rb
lib/rb/spec/socket_spec_shared.rb

index ea3175c..e3981fe 100644 (file)
@@ -29,10 +29,11 @@ module Thrift
     end
 
     def open?
-      !@handle.nil?
+      !@handle.nil? and !@handle.closed?
     end
 
     def write(str)
+      raise IOError, "closed stream" unless open?
       begin
         @handle.write(str)
       rescue StandardError
@@ -43,6 +44,7 @@ module Thrift
     end
 
     def read(sz, partial=false)
+      raise IOError, "closed stream" unless open?
       begin
         if partial
           data = @handle.readpartial(sz)
index 448a516..a0092e1 100644 (file)
@@ -49,4 +49,12 @@ shared_examples_for "a socket" do
     lambda { @socket.write("fail") }.should raise_error
     @socket.should_not be_open
   end
+
+  it "should raise an error when the stream is closed" do
+    @socket.open
+    @handle.stub!(:closed?).and_return(true)
+    @socket.should_not be_open
+    lambda { @socket.write("fail") }.should raise_error(IOError, "closed stream")
+    lambda { @socket.read(10) }.should raise_error(IOError, "closed stream")
+  end
 end