rb: Improve IOStreamTransport to behave more like a real transport [THRIFT-76]
Author: Kevin Ballard <kevin@rapleaf.com>
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@678065 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/rb/lib/thrift/transport.rb b/lib/rb/lib/thrift/transport.rb
index 6b1152e..e4a70cd 100644
--- a/lib/rb/lib/thrift/transport.rb
+++ b/lib/rb/lib/thrift/transport.rb
@@ -298,9 +298,11 @@
@output = output
end
- def open?; true end
+ def open?; not @input.closed? or not @output.closed? end
def read(sz); @input.read(sz) end
def write(buf); @output.write(buf) end
+ def close; @input.close; @output.close end
+ def to_io; @input end # we're assuming this is used in a IO.select for reading
end
deprecate_class! :TIOStreamTransport => IOStreamTransport
end
diff --git a/lib/rb/spec/transport_spec.rb b/lib/rb/spec/transport_spec.rb
index 972e984..98408ab 100644
--- a/lib/rb/spec/transport_spec.rb
+++ b/lib/rb/spec/transport_spec.rb
@@ -293,15 +293,20 @@
describe IOStreamTransport do
before(:each) do
- @input = mock("Input")
- @output = mock("Output")
+ @input = mock("Input", :closed? => false)
+ @output = mock("Output", :closed? => false)
@trans = IOStreamTransport.new(@input, @output)
end
- it "should always be open" do
+ it "should be open as long as both input or output are open" do
@trans.should be_open
- @trans.close
+ @input.stub!(:closed?).and_return(true)
@trans.should be_open
+ @input.stub!(:closed?).and_return(false)
+ @output.stub!(:closed?).and_return(true)
+ @trans.should be_open
+ @input.stub!(:closed?).and_return(true)
+ @trans.should_not be_open
end
it "should pass through read/write to input/output" do
@@ -310,5 +315,11 @@
@trans.read(17).should == "+ read"
@trans.write("foobar").should == "+ write"
end
+
+ it "should close both input and output when closed" do
+ @input.should_receive(:close)
+ @output.should_receive(:close)
+ @trans.close
+ end
end
end