blob: c963b47ac7e9011247c626f2c449e16d274d04f2 [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 class ClientSpec
24 include Thrift::Client
25 end
26
Dmytro Shteflyuk9ffc3372026-03-11 21:02:28 -040027 class EmptyArgs
28 def write(_prot)
29 end
30 end
31
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000032 before(:each) do
James E. King III27247072018-03-22 20:50:23 -040033 @prot = double("MockProtocol")
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000034 @client = ClientSpec.new(@prot)
35 end
36
Jake Farrella87810f2012-09-28 01:59:04 +000037 describe Thrift::Client do
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000038 it "should re-use iprot for oprot if not otherwise specified" do
James E. King III27247072018-03-22 20:50:23 -040039 expect(@client.instance_variable_get(:'@iprot')).to eql(@prot)
40 expect(@client.instance_variable_get(:'@oprot')).to eql(@prot)
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000041 end
42
43 it "should send a test message" do
James E. King III27247072018-03-22 20:50:23 -040044 expect(@prot).to receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::CALL, 0)
45 mock_args = double('#<TestMessage_args:mock>')
46 expect(mock_args).to receive(:foo=).with('foo')
47 expect(mock_args).to receive(:bar=).with(42)
48 expect(mock_args).to receive(:write).with(@prot)
49 expect(@prot).to receive(:write_message_end)
50 expect(@prot).to receive(:trans) do
51 double('trans').tap do |trans|
52 expect(trans).to receive(:flush)
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000053 end
54 end
James E. King III27247072018-03-22 20:50:23 -040055 klass = double("TestMessage_args", :new => mock_args)
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000056 @client.send_message('testMessage', klass, :foo => 'foo', :bar => 42)
57 end
58
Kevin Clarkb397bbb2008-06-18 01:05:32 +000059 it "should increment the sequence id when sending messages" do
Dmytro Shteflyuk9ffc3372026-03-11 21:02:28 -040060 expect(@prot).to receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::CALL, 0).ordered
61 expect(@prot).to receive(:write_message_begin).with('testMessage2', Thrift::MessageTypes::CALL, 1).ordered
62 expect(@prot).to receive(:write_message_begin).with('testMessage3', Thrift::MessageTypes::CALL, 2).ordered
63 allow(@prot).to receive(:write_message_end)
64 allow(@prot).to receive(:trans).and_return(double("trans", :flush => nil))
65
66 args_class = double("ArgsClass", :new => EmptyArgs.new)
67 @client.send_message('testMessage', args_class)
68 @client.send_message('testMessage2', args_class)
69 @client.send_message('testMessage3', args_class)
70 end
71
72 it "should keep pending reply sequence ids in FIFO order" do
73 expect(@prot).to receive(:write_message_begin).with('first', Thrift::MessageTypes::CALL, 0).ordered
74 expect(@prot).to receive(:write_message_begin).with('second', Thrift::MessageTypes::CALL, 1).ordered
75 allow(@prot).to receive(:write_message_end)
76 allow(@prot).to receive(:trans).and_return(double("trans", :flush => nil))
77
78 args_class = double("ArgsClass", :new => EmptyArgs.new)
79 @client.send_message('first', args_class)
80 @client.send_message('second', args_class)
81
82 expect {
83 @client.validate_message_begin('first', Thrift::MessageTypes::REPLY, 0, 'first')
84 }.not_to raise_error
85 expect {
86 @client.validate_message_begin('second', Thrift::MessageTypes::REPLY, 1, 'second')
87 }.not_to raise_error
Kevin Clarkb397bbb2008-06-18 01:05:32 +000088 end
89
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000090 it "should receive a test message" do
James E. King III27247072018-03-22 20:50:23 -040091 expect(@prot).to receive(:read_message_begin).and_return [nil, Thrift::MessageTypes::CALL, 0]
92 expect(@prot).to receive(:read_message_end)
93 mock_klass = double("#<MockClass:mock>")
94 expect(mock_klass).to receive(:read).with(@prot)
zeshuai0075ef174b2020-11-07 15:45:11 +080095 @client.receive_message_begin()
James E. King III27247072018-03-22 20:50:23 -040096 @client.receive_message(double("MockClass", :new => mock_klass))
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000097 end
98
Dmytro Shteflyuk9ffc3372026-03-11 21:02:28 -040099 it "should raise BAD_SEQUENCE_ID for mismatched replies" do
100 @client.instance_variable_set(:@pending_seqids, [0])
101
102 expect {
103 @client.validate_message_begin('testMessage', Thrift::MessageTypes::REPLY, 1, 'testMessage')
104 }.to raise_error(Thrift::ApplicationException) { |error|
105 expect(error.type).to eq(Thrift::ApplicationException::BAD_SEQUENCE_ID)
106 }
107 end
108
109 it "should raise WRONG_METHOD_NAME for unexpected replies" do
110 @client.instance_variable_set(:@pending_seqids, [0])
111
112 expect {
113 @client.validate_message_begin('otherMessage', Thrift::MessageTypes::REPLY, 0, 'testMessage')
114 }.to raise_error(Thrift::ApplicationException) { |error|
115 expect(error.type).to eq(Thrift::ApplicationException::WRONG_METHOD_NAME)
116 }
117 end
118
119 it "should raise INVALID_MESSAGE_TYPE for non-reply messages" do
120 @client.instance_variable_set(:@pending_seqids, [0])
121
122 expect {
123 @client.validate_message_begin('testMessage', Thrift::MessageTypes::CALL, 0, 'testMessage')
124 }.to raise_error(Thrift::ApplicationException) { |error|
125 expect(error.type).to eq(Thrift::ApplicationException::INVALID_MESSAGE_TYPE)
126 }
127 end
128
129 it "should raise received application exceptions" do
James E. King III27247072018-03-22 20:50:23 -0400130 expect(@prot).to receive(:read_message_end)
Dmytro Shteflyuk9ffc3372026-03-11 21:02:28 -0400131 server_exception = Thrift::ApplicationException.new(Thrift::ApplicationException::UNKNOWN, "boom")
132 expect(server_exception).to receive(:read).with(@prot)
133 expect(Thrift::ApplicationException).to receive(:new).and_return(server_exception)
134 @client.instance_variable_set(:@pending_seqids, [0])
135
136 expect {
137 @client.validate_message_begin('testMessage', Thrift::MessageTypes::EXCEPTION, 0, 'testMessage')
138 }.to raise_error(Thrift::ApplicationException, "boom")
139 expect(@client.instance_variable_get(:@pending_seqids)).to be_empty
140 end
141
142 it "should roll sequence ids across the signed int32 boundary" do
143 expect(@prot).to receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::CALL, Thrift::Client::MAX_SEQUENCE_ID).ordered
144 expect(@prot).to receive(:write_message_begin).with('testMessage2', Thrift::MessageTypes::CALL, Thrift::Client::MIN_SEQUENCE_ID).ordered
145 allow(@prot).to receive(:write_message_end)
146 allow(@prot).to receive(:trans).and_return(double("trans", :flush => nil))
147
148 @client.instance_variable_set(:@seqid, Thrift::Client::MAX_SEQUENCE_ID)
149 args_class = double("ArgsClass", :new => EmptyArgs.new)
150 @client.send_message('testMessage', args_class)
151 @client.send_message('testMessage2', args_class)
Kevin Clark0ff9e8c2008-06-18 01:05:03 +0000152 end
Kevin Clark5ebb23b2008-07-28 22:16:28 +0000153
154 it "should close the transport if an error occurs while sending a message" do
James E. King III27247072018-03-22 20:50:23 -0400155 allow(@prot).to receive(:write_message_begin)
156 expect(@prot).not_to receive(:write_message_end)
157 mock_args = double("#<TestMessage_args:mock>")
158 expect(mock_args).to receive(:write).with(@prot).and_raise(StandardError)
159 trans = double("MockTransport")
160 allow(@prot).to receive(:trans).and_return(trans)
161 expect(trans).to receive(:close)
162 klass = double("TestMessage_args", :new => mock_args)
163 expect { @client.send_message("testMessage", klass) }.to raise_error(StandardError)
Kevin Clark5ebb23b2008-07-28 22:16:28 +0000164 end
Kevin Clark0ff9e8c2008-06-18 01:05:03 +0000165 end
166end