blob: b92bbec4e324d18feb0d7f1ad6050d17d747fc71 [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
68 end
69end