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 | |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 20 | require File.dirname(__FILE__) + '/spec_helper' |
| 21 | |
Kevin Clark | 356f861 | 2008-06-18 01:08:05 +0000 | [diff] [blame] | 22 | class ThriftClientSpec < Spec::ExampleGroup |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 23 | include Thrift |
| 24 | |
| 25 | class ClientSpec |
| 26 | include Thrift::Client |
| 27 | end |
| 28 | |
| 29 | before(:each) do |
| 30 | @prot = mock("MockProtocol") |
| 31 | @client = ClientSpec.new(@prot) |
| 32 | end |
| 33 | |
Kevin Clark | 356f861 | 2008-06-18 01:08:05 +0000 | [diff] [blame] | 34 | describe Client do |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 35 | it "should re-use iprot for oprot if not otherwise specified" do |
| 36 | @client.instance_variable_get(:'@iprot').should eql(@prot) |
| 37 | @client.instance_variable_get(:'@oprot').should eql(@prot) |
| 38 | end |
| 39 | |
| 40 | it "should send a test message" do |
| 41 | @prot.should_receive(:write_message_begin).with('testMessage', MessageTypes::CALL, 0) |
| 42 | mock_args = mock('#<TestMessage_args:mock>') |
| 43 | mock_args.should_receive(:foo=).with('foo') |
| 44 | mock_args.should_receive(:bar=).with(42) |
| 45 | mock_args.should_receive(:write).with(@prot) |
| 46 | @prot.should_receive(:write_message_end) |
| 47 | @prot.should_receive(:trans) do |
| 48 | mock('trans').tee do |trans| |
| 49 | trans.should_receive(:flush) |
| 50 | end |
| 51 | end |
| 52 | klass = stub("TestMessage_args", :new => mock_args) |
| 53 | @client.send_message('testMessage', klass, :foo => 'foo', :bar => 42) |
| 54 | end |
| 55 | |
Kevin Clark | b397bbb | 2008-06-18 01:05:32 +0000 | [diff] [blame] | 56 | it "should increment the sequence id when sending messages" do |
| 57 | pending "it seems sequence ids are completely ignored right now" do |
| 58 | @prot.should_receive(:write_message_begin).with('testMessage', MessageTypes::CALL, 0).ordered |
| 59 | @prot.should_receive(:write_message_begin).with('testMessage2', MessageTypes::CALL, 1).ordered |
| 60 | @prot.should_receive(:write_message_begin).with('testMessage3', MessageTypes::CALL, 2).ordered |
| 61 | @prot.stub!(:write_message_end) |
Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 62 | @prot.stub!(:trans).and_return mock("trans").as_null_object |
| 63 | @client.send_message('testMessage', mock("args class").as_null_object) |
| 64 | @client.send_message('testMessage2', mock("args class").as_null_object) |
| 65 | @client.send_message('testMessage3', mock("args class").as_null_object) |
Kevin Clark | b397bbb | 2008-06-18 01:05:32 +0000 | [diff] [blame] | 66 | end |
| 67 | end |
| 68 | |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 69 | it "should receive a test message" do |
| 70 | @prot.should_receive(:read_message_begin).and_return [nil, MessageTypes::CALL, 0] |
| 71 | @prot.should_receive(:read_message_end) |
| 72 | mock_klass = mock("#<MockClass:mock>") |
| 73 | mock_klass.should_receive(:read).with(@prot) |
| 74 | @client.receive_message(stub("MockClass", :new => mock_klass)) |
| 75 | end |
| 76 | |
| 77 | it "should handle received exceptions" do |
| 78 | @prot.should_receive(:read_message_begin).and_return [nil, MessageTypes::EXCEPTION, 0] |
| 79 | @prot.should_receive(:read_message_end) |
| 80 | ApplicationException.should_receive(:new).and_return do |
| 81 | StandardError.new.tee do |mock_exc| |
| 82 | mock_exc.should_receive(:read).with(@prot) |
| 83 | end |
| 84 | end |
| 85 | lambda { @client.receive_message(nil) }.should raise_error(StandardError) |
| 86 | end |
Kevin Clark | 5ebb23b | 2008-07-28 22:16:28 +0000 | [diff] [blame] | 87 | |
| 88 | it "should close the transport if an error occurs while sending a message" do |
| 89 | @prot.stub!(:write_message_begin) |
| 90 | @prot.should_not_receive(:write_message_end) |
| 91 | mock_args = mock("#<TestMessage_args:mock>") |
| 92 | mock_args.should_receive(:write).with(@prot).and_raise(StandardError) |
| 93 | trans = mock("MockTransport") |
| 94 | @prot.stub!(:trans).and_return(trans) |
| 95 | trans.should_receive(:close) |
| 96 | klass = mock("TestMessage_args", :new => mock_args) |
| 97 | lambda { @client.send_message("testMessage", klass) }.should raise_error(StandardError) |
| 98 | end |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 99 | end |
| 100 | end |