| 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 | 4a2b841 | 2008-06-18 01:05:47 +0000 | [diff] [blame] | 21 |  | 
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 22 | describe 'Processor' do | 
| Kevin Clark | 4a2b841 | 2008-06-18 01:05:47 +0000 | [diff] [blame] | 23 |  | 
 | 24 |   class ProcessorSpec | 
 | 25 |     include Thrift::Processor | 
 | 26 |   end | 
 | 27 |  | 
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 28 |   describe Thrift::Processor do | 
| Kevin Clark | 4a2b841 | 2008-06-18 01:05:47 +0000 | [diff] [blame] | 29 |     before(:each) do | 
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 30 |       @processor = ProcessorSpec.new(double("MockHandler")) | 
 | 31 |       @prot = double("MockProtocol") | 
| Kevin Clark | 4a2b841 | 2008-06-18 01:05:47 +0000 | [diff] [blame] | 32 |     end | 
 | 33 |  | 
 | 34 |     def mock_trans(obj) | 
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 35 |       expect(obj).to receive(:trans).ordered do | 
 | 36 |         double("trans").tap do |trans| | 
 | 37 |           expect(trans).to receive(:flush).ordered | 
| Kevin Clark | 4a2b841 | 2008-06-18 01:05:47 +0000 | [diff] [blame] | 38 |         end | 
 | 39 |       end | 
 | 40 |     end | 
 | 41 |  | 
 | 42 |     it "should call process_<message> when it receives that message" do | 
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 43 |       expect(@prot).to receive(:read_message_begin).ordered.and_return ['testMessage', Thrift::MessageTypes::CALL, 17] | 
 | 44 |       expect(@processor).to receive(:process_testMessage).with(17, @prot, @prot).ordered | 
 | 45 |       expect(@processor.process(@prot, @prot)).to eq(true) | 
| Kevin Clark | 4a2b841 | 2008-06-18 01:05:47 +0000 | [diff] [blame] | 46 |     end | 
 | 47 |  | 
 | 48 |     it "should raise an ApplicationException when the received message cannot be processed" do | 
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 49 |       expect(@prot).to receive(:read_message_begin).ordered.and_return ['testMessage', Thrift::MessageTypes::CALL, 4] | 
 | 50 |       expect(@prot).to receive(:skip).with(Thrift::Types::STRUCT).ordered | 
 | 51 |       expect(@prot).to receive(:read_message_end).ordered | 
 | 52 |       expect(@prot).to receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::EXCEPTION, 4).ordered | 
 | 53 |       e = double(Thrift::ApplicationException) | 
 | 54 |       expect(e).to receive(:write).with(@prot).ordered | 
 | 55 |       expect(Thrift::ApplicationException).to receive(:new).with(Thrift::ApplicationException::UNKNOWN_METHOD, "Unknown function testMessage").and_return(e) | 
 | 56 |       expect(@prot).to receive(:write_message_end).ordered | 
| Kevin Clark | 4a2b841 | 2008-06-18 01:05:47 +0000 | [diff] [blame] | 57 |       mock_trans(@prot) | 
 | 58 |       @processor.process(@prot, @prot) | 
 | 59 |     end | 
 | 60 |  | 
 | 61 |     it "should pass args off to the args class" do | 
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 62 |       args_class = double("MockArgsClass") | 
 | 63 |       args = double("#<MockArgsClass:mock>").tap do |args| | 
 | 64 |         expect(args).to receive(:read).with(@prot).ordered | 
| Kevin Clark | 4a2b841 | 2008-06-18 01:05:47 +0000 | [diff] [blame] | 65 |       end | 
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 66 |       expect(args_class).to receive(:new).and_return args | 
 | 67 |       expect(@prot).to receive(:read_message_end).ordered | 
 | 68 |       expect(@processor.read_args(@prot, args_class)).to eql(args) | 
| Kevin Clark | 4a2b841 | 2008-06-18 01:05:47 +0000 | [diff] [blame] | 69 |     end | 
 | 70 |  | 
 | 71 |     it "should write out a reply when asked" do | 
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 72 |       expect(@prot).to receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::REPLY, 23).ordered | 
 | 73 |       result = double("MockResult") | 
 | 74 |       expect(result).to receive(:write).with(@prot).ordered | 
 | 75 |       expect(@prot).to receive(:write_message_end).ordered | 
| Kevin Clark | 4a2b841 | 2008-06-18 01:05:47 +0000 | [diff] [blame] | 76 |       mock_trans(@prot) | 
 | 77 |       @processor.write_result(result, @prot, 'testMessage', 23) | 
 | 78 |     end | 
 | 79 |   end | 
 | 80 | end |