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 |
| 29 | @prot = mock("MockProtocol") |
| 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 |
| 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 Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 40 | @prot.should_receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::CALL, 0) |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 41 | 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 Farrell | 5d6bd5a | 2012-10-01 18:42:23 +0000 | [diff] [blame] | 47 | mock('trans').tap do |trans| |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 48 | 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 Clark | b397bbb | 2008-06-18 01:05:32 +0000 | [diff] [blame] | 55 | it "should increment the sequence id when sending messages" do |
| 56 | pending "it seems sequence ids are completely ignored right now" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 57 | @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 Clark | b397bbb | 2008-06-18 01:05:32 +0000 | [diff] [blame] | 60 | @prot.stub!(:write_message_end) |
Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 61 | @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 Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 64 | @client.send_message('testMessage3', mock("args class").as_null_object) |
Kevin Clark | b397bbb | 2008-06-18 01:05:32 +0000 | [diff] [blame] | 65 | end |
| 66 | end |
| 67 | |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 68 | it "should receive a test message" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 69 | @prot.should_receive(:read_message_begin).and_return [nil, Thrift::MessageTypes::CALL, 0] |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 70 | @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 Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 77 | @prot.should_receive(:read_message_begin).and_return [nil, Thrift::MessageTypes::EXCEPTION, 0] |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 78 | @prot.should_receive(:read_message_end) |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 79 | Thrift::ApplicationException.should_receive(:new).and_return do |
Jake Farrell | 5d6bd5a | 2012-10-01 18:42:23 +0000 | [diff] [blame] | 80 | StandardError.new.tap do |mock_exc| |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 81 | mock_exc.should_receive(:read).with(@prot) |
| 82 | end |
| 83 | end |
| 84 | lambda { @client.receive_message(nil) }.should raise_error(StandardError) |
| 85 | end |
Kevin Clark | 5ebb23b | 2008-07-28 22:16:28 +0000 | [diff] [blame] | 86 | |
| 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 Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 98 | end |
| 99 | end |