From 1067425ef7bb4fa81a28a39adc1a3153efaff306 Mon Sep 17 00:00:00 2001 From: Kevin Clark Date: Wed, 18 Jun 2008 01:17:30 +0000 Subject: [PATCH] rb: Enhance non-blocking read in Socket Also prevent errors when trying to close a @handle twice git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@669010 13f79535-47bb-0310-9956-ffa450edef68 --- lib/rb/lib/thrift/transport/socket.rb | 17 ++++++++++++++--- lib/rb/spec/socket_spec.rb | 4 ++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/rb/lib/thrift/transport/socket.rb b/lib/rb/lib/thrift/transport/socket.rb index 6b648cfc..7cf2f451 100644 --- a/lib/rb/lib/thrift/transport/socket.rb +++ b/lib/rb/lib/thrift/transport/socket.rb @@ -48,8 +48,11 @@ module Thrift 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 @@ module Thrift 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 @@ module Thrift end def close - @handle.close unless @handle.nil? + @handle.close unless @handle.nil? or @handle.closed? @handle = nil end end diff --git a/lib/rb/spec/socket_spec.rb b/lib/rb/spec/socket_spec.rb index 4f5868b6..4b6dae53 100644 --- a/lib/rb/spec/socket_spec.rb +++ b/lib/rb/spec/socket_spec.rb @@ -5,7 +5,7 @@ class ThriftSocketSpec < Spec::ExampleGroup before(:each) do @socket = Socket.new - @handle = mock("Handle") + @handle = mock("Handle", :closed? => false) @handle.stub!(:close) end @@ -107,7 +107,7 @@ class ThriftSocketSpec < Spec::ExampleGroup 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) -- 2.17.1