rb: Add some additional error handling to Thrift::Socket [THRIFT-53]

Author: Kevin Ballard <kevin@rapleaf.com>


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@671979 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/rb/lib/thrift/transport/socket.rb b/lib/rb/lib/thrift/transport/socket.rb
index ea3175c..e3981fe 100644
--- a/lib/rb/lib/thrift/transport/socket.rb
+++ b/lib/rb/lib/thrift/transport/socket.rb
@@ -29,10 +29,11 @@
     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 @@
     end
 
     def read(sz, partial=false)
+      raise IOError, "closed stream" unless open?
       begin
         if partial
           data = @handle.readpartial(sz)
diff --git a/lib/rb/spec/socket_spec_shared.rb b/lib/rb/spec/socket_spec_shared.rb
index 448a516..a0092e1 100644
--- a/lib/rb/spec/socket_spec_shared.rb
+++ b/lib/rb/spec/socket_spec_shared.rb
@@ -49,4 +49,12 @@
     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