blob: d5d4ceedba2e7b482e3e6f794da0d53ef0afa258 [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
James E. King III27247072018-03-22 20:50:23 -040029 @prot = double("MockProtocol")
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000030 @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
James E. King III27247072018-03-22 20:50:23 -040035 expect(@client.instance_variable_get(:'@iprot')).to eql(@prot)
36 expect(@client.instance_variable_get(:'@oprot')).to eql(@prot)
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000037 end
38
39 it "should send a test message" do
James E. King III27247072018-03-22 20:50:23 -040040 expect(@prot).to receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::CALL, 0)
41 mock_args = double('#<TestMessage_args:mock>')
42 expect(mock_args).to receive(:foo=).with('foo')
43 expect(mock_args).to receive(:bar=).with(42)
44 expect(mock_args).to receive(:write).with(@prot)
45 expect(@prot).to receive(:write_message_end)
46 expect(@prot).to receive(:trans) do
47 double('trans').tap do |trans|
48 expect(trans).to receive(:flush)
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000049 end
50 end
James E. King III27247072018-03-22 20:50:23 -040051 klass = double("TestMessage_args", :new => mock_args)
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000052 @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
James E. King III27247072018-03-22 20:50:23 -040056 pending "it seems sequence ids are completely ignored right now"
57 @prot.expect(:write_message_begin).with('testMessage', Thrift::MessageTypes::CALL, 0).ordered
58 @prot.expect(:write_message_begin).with('testMessage2', Thrift::MessageTypes::CALL, 1).ordered
59 @prot.expect(:write_message_begin).with('testMessage3', Thrift::MessageTypes::CALL, 2).ordered
60 @prot.stub!(:write_message_end)
61 @prot.stub!(:trans).and_return double("trans").as_null_object
62 @client.send_message('testMessage', double("args class").as_null_object)
63 @client.send_message('testMessage2', double("args class").as_null_object)
64 @client.send_message('testMessage3', double("args class").as_null_object)
Kevin Clarkb397bbb2008-06-18 01:05:32 +000065 end
66
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000067 it "should receive a test message" do
James E. King III27247072018-03-22 20:50:23 -040068 expect(@prot).to receive(:read_message_begin).and_return [nil, Thrift::MessageTypes::CALL, 0]
69 expect(@prot).to receive(:read_message_end)
70 mock_klass = double("#<MockClass:mock>")
71 expect(mock_klass).to receive(:read).with(@prot)
72 @client.receive_message(double("MockClass", :new => mock_klass))
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000073 end
74
75 it "should handle received exceptions" do
James E. King III27247072018-03-22 20:50:23 -040076 expect(@prot).to receive(:read_message_begin).and_return [nil, Thrift::MessageTypes::EXCEPTION, 0]
77 expect(@prot).to receive(:read_message_end)
78 expect(Thrift::ApplicationException).to receive(:new) do
Jake Farrell5d6bd5a2012-10-01 18:42:23 +000079 StandardError.new.tap do |mock_exc|
James E. King III27247072018-03-22 20:50:23 -040080 expect(mock_exc).to receive(:read).with(@prot)
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000081 end
82 end
James E. King III27247072018-03-22 20:50:23 -040083 expect { @client.receive_message(nil) }.to raise_error(StandardError)
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000084 end
Kevin Clark5ebb23b2008-07-28 22:16:28 +000085
86 it "should close the transport if an error occurs while sending a message" do
James E. King III27247072018-03-22 20:50:23 -040087 allow(@prot).to receive(:write_message_begin)
88 expect(@prot).not_to receive(:write_message_end)
89 mock_args = double("#<TestMessage_args:mock>")
90 expect(mock_args).to receive(:write).with(@prot).and_raise(StandardError)
91 trans = double("MockTransport")
92 allow(@prot).to receive(:trans).and_return(trans)
93 expect(trans).to receive(:close)
94 klass = double("TestMessage_args", :new => mock_args)
95 expect { @client.send_message("testMessage", klass) }.to raise_error(StandardError)
Kevin Clark5ebb23b2008-07-28 22:16:28 +000096 end
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000097 end
98end