rb: Ensure the transport is closed if an exception is raised serializing data in...
authorKevin Clark <kclark@apache.org>
Mon, 28 Jul 2008 22:16:28 +0000 (22:16 +0000)
committerKevin Clark <kclark@apache.org>
Mon, 28 Jul 2008 22:16:28 +0000 (22:16 +0000)
Author: Kevin Ballard <kevin@rapleaf.com>

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

lib/rb/lib/thrift/client.rb
lib/rb/spec/client_spec.rb

index 8f9b96a..997923a 100644 (file)
@@ -12,7 +12,12 @@ module Thrift
       args.each do |k, v|
         data.send("#{k.to_s}=", v)
       end
-      data.write(@oprot)
+      begin
+        data.write(@oprot)
+      rescue StandardError => e
+        @oprot.trans.close
+        raise e
+      end
       @oprot.write_message_end
       @oprot.trans.flush
     end
index b92bbec..980552b 100644 (file)
@@ -65,5 +65,17 @@ class ThriftClientSpec < Spec::ExampleGroup
       end
       lambda { @client.receive_message(nil) }.should raise_error(StandardError)
     end
+
+    it "should close the transport if an error occurs while sending a message" do
+      @prot.stub!(:write_message_begin)
+      @prot.should_not_receive(:write_message_end)
+      mock_args = mock("#<TestMessage_args:mock>")
+      mock_args.should_receive(:write).with(@prot).and_raise(StandardError)
+      trans = mock("MockTransport")
+      @prot.stub!(:trans).and_return(trans)
+      trans.should_receive(:close)
+      klass = mock("TestMessage_args", :new => mock_args)
+      lambda { @client.send_message("testMessage", klass) }.should raise_error(StandardError)
+    end
   end
 end