| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | # |
| 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 Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 20 | require 'spec_helper' |
| Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 21 | |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 22 | describe 'Client' do |
| Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 23 | class ClientSpec |
| 24 | include Thrift::Client |
| 25 | end |
| 26 | |
| Dmytro Shteflyuk | 9ffc337 | 2026-03-11 21:02:28 -0400 | [diff] [blame^] | 27 | class EmptyArgs |
| 28 | def write(_prot) |
| 29 | end |
| 30 | end |
| 31 | |
| Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 32 | before(:each) do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 33 | @prot = double("MockProtocol") |
| Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 34 | @client = ClientSpec.new(@prot) |
| 35 | end |
| 36 | |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 37 | describe Thrift::Client do |
| Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 38 | it "should re-use iprot for oprot if not otherwise specified" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 39 | expect(@client.instance_variable_get(:'@iprot')).to eql(@prot) |
| 40 | expect(@client.instance_variable_get(:'@oprot')).to eql(@prot) |
| Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 41 | end |
| 42 | |
| 43 | it "should send a test message" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 44 | 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 Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 53 | end |
| 54 | end |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 55 | klass = double("TestMessage_args", :new => mock_args) |
| Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 56 | @client.send_message('testMessage', klass, :foo => 'foo', :bar => 42) |
| 57 | end |
| 58 | |
| Kevin Clark | b397bbb | 2008-06-18 01:05:32 +0000 | [diff] [blame] | 59 | it "should increment the sequence id when sending messages" do |
| Dmytro Shteflyuk | 9ffc337 | 2026-03-11 21:02:28 -0400 | [diff] [blame^] | 60 | 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 Clark | b397bbb | 2008-06-18 01:05:32 +0000 | [diff] [blame] | 88 | end |
| 89 | |
| Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 90 | it "should receive a test message" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 91 | 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) |
| zeshuai007 | 5ef174b | 2020-11-07 15:45:11 +0800 | [diff] [blame] | 95 | @client.receive_message_begin() |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 96 | @client.receive_message(double("MockClass", :new => mock_klass)) |
| Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 97 | end |
| 98 | |
| Dmytro Shteflyuk | 9ffc337 | 2026-03-11 21:02:28 -0400 | [diff] [blame^] | 99 | 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 III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 130 | expect(@prot).to receive(:read_message_end) |
| Dmytro Shteflyuk | 9ffc337 | 2026-03-11 21:02:28 -0400 | [diff] [blame^] | 131 | 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 Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 152 | end |
| Kevin Clark | 5ebb23b | 2008-07-28 22:16:28 +0000 | [diff] [blame] | 153 | |
| 154 | it "should close the transport if an error occurs while sending a message" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 155 | 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 Clark | 5ebb23b | 2008-07-28 22:16:28 +0000 | [diff] [blame] | 164 | end |
| Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 165 | end |
| 166 | end |