blob: 980552beb2ecf9788533bf5e7d555e9e587e1b45 [file] [log] [blame]
Kevin Clark0ff9e8c2008-06-18 01:05:03 +00001require File.dirname(__FILE__) + '/spec_helper'
2
Kevin Clark356f8612008-06-18 01:08:05 +00003class ThriftClientSpec < Spec::ExampleGroup
Kevin Clark0ff9e8c2008-06-18 01:05:03 +00004 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 Clark356f8612008-06-18 01:08:05 +000015 describe Client do
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000016 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 Clarkb397bbb2008-06-18 01:05:32 +000037 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 Clark0ff9e8c2008-06-18 01:05:03 +000050 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 Clark5ebb23b2008-07-28 22:16:28 +000068
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 Clark0ff9e8c2008-06-18 01:05:03 +000080 end
81end