rb: BufferedTransport should flush on close [THRIFT-49]

This also adds code and spec so nothing will be written to the transport
if there's nothing in the write buffer.


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@671967 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/rb/lib/thrift/transport.rb b/lib/rb/lib/thrift/transport.rb
index 3ea6ee0..36492dd 100644
--- a/lib/rb/lib/thrift/transport.rb
+++ b/lib/rb/lib/thrift/transport.rb
@@ -90,6 +90,7 @@
     end
 
     def close
+      flush
       @transport.close
     end
 
@@ -102,9 +103,12 @@
     end
 
     def flush
-      @transport.write(@wbuf)
+      if @wbuf != ''
+        @transport.write(@wbuf)
+        @wbuf = ''
+      end
+      
       @transport.flush
-      @wbuf = ''
     end
   end
   deprecate_class! :TBufferedTransport => BufferedTransport
diff --git a/lib/rb/spec/transport_spec.rb b/lib/rb/spec/transport_spec.rb
index 5d5c92f..44d0508 100644
--- a/lib/rb/spec/transport_spec.rb
+++ b/lib/rb/spec/transport_spec.rb
@@ -52,6 +52,7 @@
       trans = mock("Transport")
       trans.should_receive(:open?).ordered.and_return("+ open?")
       trans.should_receive(:open).ordered.and_return("+ open")
+      trans.should_receive(:flush).ordered # from the close
       trans.should_receive(:close).ordered.and_return("+ close")
       trans.should_receive(:read).with(217).ordered.and_return("+ read")
       btrans = BufferedTransport.new(trans)
@@ -81,7 +82,22 @@
       trans.should_receive(:write).with("one/two/three/")
       trans.stub!(:flush)
       btrans.flush
-      trans.should_receive(:write).with("")
+      # Nothing to flush with no data
+      btrans.flush
+    end
+    
+    it "should flush on close" do
+      trans = mock("Transport")
+      trans.should_receive(:close)
+      btrans = BufferedTransport.new(trans)
+      btrans.should_receive(:flush)
+      btrans.close
+    end
+    
+    it "should not write to socket if there's no data" do
+      trans = mock("Transport")
+      trans.should_receive(:flush)
+      btrans = BufferedTransport.new(trans)
       btrans.flush
     end
   end