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 | |
| 24 | class ClientSpec |
| 25 | include Thrift::Client |
| 26 | end |
| 27 | |
| 28 | before(:each) do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame^] | 29 | @prot = double("MockProtocol") |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 30 | @client = ClientSpec.new(@prot) |
| 31 | end |
| 32 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 33 | describe Thrift::Client do |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 34 | 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^] | 35 | expect(@client.instance_variable_get(:'@iprot')).to eql(@prot) |
| 36 | expect(@client.instance_variable_get(:'@oprot')).to eql(@prot) |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 37 | end |
| 38 | |
| 39 | it "should send a test message" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame^] | 40 | 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 Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 49 | end |
| 50 | end |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame^] | 51 | klass = double("TestMessage_args", :new => mock_args) |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 52 | @client.send_message('testMessage', klass, :foo => 'foo', :bar => 42) |
| 53 | end |
| 54 | |
Kevin Clark | b397bbb | 2008-06-18 01:05:32 +0000 | [diff] [blame] | 55 | it "should increment the sequence id when sending messages" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame^] | 56 | 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 Clark | b397bbb | 2008-06-18 01:05:32 +0000 | [diff] [blame] | 65 | end |
| 66 | |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 67 | it "should receive a test message" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame^] | 68 | 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 Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 73 | end |
| 74 | |
| 75 | it "should handle received exceptions" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame^] | 76 | 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 Farrell | 5d6bd5a | 2012-10-01 18:42:23 +0000 | [diff] [blame] | 79 | StandardError.new.tap do |mock_exc| |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame^] | 80 | expect(mock_exc).to receive(:read).with(@prot) |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 81 | end |
| 82 | end |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame^] | 83 | expect { @client.receive_message(nil) }.to raise_error(StandardError) |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 84 | end |
Kevin Clark | 5ebb23b | 2008-07-28 22:16:28 +0000 | [diff] [blame] | 85 | |
| 86 | 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^] | 87 | 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 Clark | 5ebb23b | 2008-07-28 22:16:28 +0000 | [diff] [blame] | 96 | end |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 97 | end |
| 98 | end |