Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 1 | require File.dirname(__FILE__) + '/spec_helper' |
| 2 | |
Kevin Clark | 356f861 | 2008-06-18 01:08:05 +0000 | [diff] [blame] | 3 | class ThriftClientSpec < Spec::ExampleGroup |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 4 | include Thrift |
| 5 | |
| 6 | class ClientSpec |
| 7 | include Thrift::Client |
| 8 | end |
| 9 | |
| 10 | before(:each) do |
| 11 | @prot = mock("MockProtocol") |
| 12 | @client = ClientSpec.new(@prot) |
| 13 | end |
| 14 | |
Kevin Clark | 356f861 | 2008-06-18 01:08:05 +0000 | [diff] [blame] | 15 | describe Client do |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 16 | it "should re-use iprot for oprot if not otherwise specified" do |
| 17 | @client.instance_variable_get(:'@iprot').should eql(@prot) |
| 18 | @client.instance_variable_get(:'@oprot').should eql(@prot) |
| 19 | end |
| 20 | |
| 21 | it "should send a test message" do |
| 22 | @prot.should_receive(:write_message_begin).with('testMessage', MessageTypes::CALL, 0) |
| 23 | mock_args = mock('#<TestMessage_args:mock>') |
| 24 | mock_args.should_receive(:foo=).with('foo') |
| 25 | mock_args.should_receive(:bar=).with(42) |
| 26 | mock_args.should_receive(:write).with(@prot) |
| 27 | @prot.should_receive(:write_message_end) |
| 28 | @prot.should_receive(:trans) do |
| 29 | mock('trans').tee do |trans| |
| 30 | trans.should_receive(:flush) |
| 31 | end |
| 32 | end |
| 33 | klass = stub("TestMessage_args", :new => mock_args) |
| 34 | @client.send_message('testMessage', klass, :foo => 'foo', :bar => 42) |
| 35 | end |
| 36 | |
Kevin Clark | b397bbb | 2008-06-18 01:05:32 +0000 | [diff] [blame] | 37 | it "should increment the sequence id when sending messages" do |
| 38 | pending "it seems sequence ids are completely ignored right now" do |
| 39 | @prot.should_receive(:write_message_begin).with('testMessage', MessageTypes::CALL, 0).ordered |
| 40 | @prot.should_receive(:write_message_begin).with('testMessage2', MessageTypes::CALL, 1).ordered |
| 41 | @prot.should_receive(:write_message_begin).with('testMessage3', MessageTypes::CALL, 2).ordered |
| 42 | @prot.stub!(:write_message_end) |
| 43 | @prot.stub!(:trans).and_return stub_everything("trans") |
| 44 | @client.send_message('testMessage', stub_everything("args class")) |
| 45 | @client.send_message('testMessage2', stub_everything("args class")) |
| 46 | @client.send_message('testMessage3', stub_everything("args class")) |
| 47 | end |
| 48 | end |
| 49 | |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 50 | it "should receive a test message" do |
| 51 | @prot.should_receive(:read_message_begin).and_return [nil, MessageTypes::CALL, 0] |
| 52 | @prot.should_receive(:read_message_end) |
| 53 | mock_klass = mock("#<MockClass:mock>") |
| 54 | mock_klass.should_receive(:read).with(@prot) |
| 55 | @client.receive_message(stub("MockClass", :new => mock_klass)) |
| 56 | end |
| 57 | |
| 58 | it "should handle received exceptions" do |
| 59 | @prot.should_receive(:read_message_begin).and_return [nil, MessageTypes::EXCEPTION, 0] |
| 60 | @prot.should_receive(:read_message_end) |
| 61 | ApplicationException.should_receive(:new).and_return do |
| 62 | StandardError.new.tee do |mock_exc| |
| 63 | mock_exc.should_receive(:read).with(@prot) |
| 64 | end |
| 65 | end |
| 66 | lambda { @client.receive_message(nil) }.should raise_error(StandardError) |
| 67 | end |
Kevin Clark | 5ebb23b | 2008-07-28 22:16:28 +0000 | [diff] [blame] | 68 | |
| 69 | it "should close the transport if an error occurs while sending a message" do |
| 70 | @prot.stub!(:write_message_begin) |
| 71 | @prot.should_not_receive(:write_message_end) |
| 72 | mock_args = mock("#<TestMessage_args:mock>") |
| 73 | mock_args.should_receive(:write).with(@prot).and_raise(StandardError) |
| 74 | trans = mock("MockTransport") |
| 75 | @prot.stub!(:trans).and_return(trans) |
| 76 | trans.should_receive(:close) |
| 77 | klass = mock("TestMessage_args", :new => mock_args) |
| 78 | lambda { @client.send_message("testMessage", klass) }.should raise_error(StandardError) |
| 79 | end |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 80 | end |
| 81 | end |