blob: f8ffe8a8dedbcf62a48fd65def7511612d7f2542 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001#
2# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18#
19
Jake Farrella87810f2012-09-28 01:59:04 +000020require 'spec_helper'
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000021
Jake Farrella87810f2012-09-28 01:59:04 +000022describe 'Client' do
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000023
24 class ClientSpec
25 include Thrift::Client
26 end
27
28 before(:each) do
29 @prot = mock("MockProtocol")
30 @client = ClientSpec.new(@prot)
31 end
32
Jake Farrella87810f2012-09-28 01:59:04 +000033 describe Thrift::Client do
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000034 it "should re-use iprot for oprot if not otherwise specified" do
35 @client.instance_variable_get(:'@iprot').should eql(@prot)
36 @client.instance_variable_get(:'@oprot').should eql(@prot)
37 end
38
39 it "should send a test message" do
Jake Farrella87810f2012-09-28 01:59:04 +000040 @prot.should_receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::CALL, 0)
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000041 mock_args = mock('#<TestMessage_args:mock>')
42 mock_args.should_receive(:foo=).with('foo')
43 mock_args.should_receive(:bar=).with(42)
44 mock_args.should_receive(:write).with(@prot)
45 @prot.should_receive(:write_message_end)
46 @prot.should_receive(:trans) do
Jake Farrell5d6bd5a2012-10-01 18:42:23 +000047 mock('trans').tap do |trans|
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000048 trans.should_receive(:flush)
49 end
50 end
51 klass = stub("TestMessage_args", :new => mock_args)
52 @client.send_message('testMessage', klass, :foo => 'foo', :bar => 42)
53 end
54
Kevin Clarkb397bbb2008-06-18 01:05:32 +000055 it "should increment the sequence id when sending messages" do
56 pending "it seems sequence ids are completely ignored right now" do
Jake Farrella87810f2012-09-28 01:59:04 +000057 @prot.should_receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::CALL, 0).ordered
58 @prot.should_receive(:write_message_begin).with('testMessage2', Thrift::MessageTypes::CALL, 1).ordered
59 @prot.should_receive(:write_message_begin).with('testMessage3', Thrift::MessageTypes::CALL, 2).ordered
Kevin Clarkb397bbb2008-06-18 01:05:32 +000060 @prot.stub!(:write_message_end)
Bryan Duxburye3ab50d2009-03-25 21:06:53 +000061 @prot.stub!(:trans).and_return mock("trans").as_null_object
62 @client.send_message('testMessage', mock("args class").as_null_object)
63 @client.send_message('testMessage2', mock("args class").as_null_object)
Jake Farrella87810f2012-09-28 01:59:04 +000064 @client.send_message('testMessage3', mock("args class").as_null_object)
Kevin Clarkb397bbb2008-06-18 01:05:32 +000065 end
66 end
67
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000068 it "should receive a test message" do
Jake Farrella87810f2012-09-28 01:59:04 +000069 @prot.should_receive(:read_message_begin).and_return [nil, Thrift::MessageTypes::CALL, 0]
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000070 @prot.should_receive(:read_message_end)
71 mock_klass = mock("#<MockClass:mock>")
72 mock_klass.should_receive(:read).with(@prot)
73 @client.receive_message(stub("MockClass", :new => mock_klass))
74 end
75
76 it "should handle received exceptions" do
Jake Farrella87810f2012-09-28 01:59:04 +000077 @prot.should_receive(:read_message_begin).and_return [nil, Thrift::MessageTypes::EXCEPTION, 0]
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000078 @prot.should_receive(:read_message_end)
Jake Farrella87810f2012-09-28 01:59:04 +000079 Thrift::ApplicationException.should_receive(:new).and_return do
Jake Farrell5d6bd5a2012-10-01 18:42:23 +000080 StandardError.new.tap do |mock_exc|
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000081 mock_exc.should_receive(:read).with(@prot)
82 end
83 end
84 lambda { @client.receive_message(nil) }.should raise_error(StandardError)
85 end
Kevin Clark5ebb23b2008-07-28 22:16:28 +000086
87 it "should close the transport if an error occurs while sending a message" do
88 @prot.stub!(:write_message_begin)
89 @prot.should_not_receive(:write_message_end)
90 mock_args = mock("#<TestMessage_args:mock>")
91 mock_args.should_receive(:write).with(@prot).and_raise(StandardError)
92 trans = mock("MockTransport")
93 @prot.stub!(:trans).and_return(trans)
94 trans.should_receive(:close)
95 klass = mock("TestMessage_args", :new => mock_args)
96 lambda { @client.send_message("testMessage", klass) }.should raise_error(StandardError)
97 end
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000098 end
99end