blob: d30553f55c2a83275cc214e5920adb6855cacfd4 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001#
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 Farrella87810f2012-09-28 01:59:04 +000020require 'spec_helper'
Kevin Clark4a2b8412008-06-18 01:05:47 +000021
Jake Farrella87810f2012-09-28 01:59:04 +000022describe 'Processor' do
Kevin Clark4a2b8412008-06-18 01:05:47 +000023
24 class ProcessorSpec
25 include Thrift::Processor
26 end
27
Jake Farrella87810f2012-09-28 01:59:04 +000028 describe Thrift::Processor do
Kevin Clark4a2b8412008-06-18 01:05:47 +000029 before(:each) do
James E. King III27247072018-03-22 20:50:23 -040030 @processor = ProcessorSpec.new(double("MockHandler"))
31 @prot = double("MockProtocol")
Kevin Clark4a2b8412008-06-18 01:05:47 +000032 end
33
34 def mock_trans(obj)
James E. King III27247072018-03-22 20:50:23 -040035 expect(obj).to receive(:trans).ordered do
36 double("trans").tap do |trans|
37 expect(trans).to receive(:flush).ordered
Kevin Clark4a2b8412008-06-18 01:05:47 +000038 end
39 end
40 end
41
42 it "should call process_<message> when it receives that message" do
James E. King III27247072018-03-22 20:50:23 -040043 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 Clark4a2b8412008-06-18 01:05:47 +000046 end
47
48 it "should raise an ApplicationException when the received message cannot be processed" do
James E. King III27247072018-03-22 20:50:23 -040049 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 Clark4a2b8412008-06-18 01:05:47 +000057 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 III27247072018-03-22 20:50:23 -040062 args_class = double("MockArgsClass")
63 args = double("#<MockArgsClass:mock>").tap do |args|
64 expect(args).to receive(:read).with(@prot).ordered
Kevin Clark4a2b8412008-06-18 01:05:47 +000065 end
James E. King III27247072018-03-22 20:50:23 -040066 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 Clark4a2b8412008-06-18 01:05:47 +000069 end
70
71 it "should write out a reply when asked" do
James E. King III27247072018-03-22 20:50:23 -040072 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 Clark4a2b8412008-06-18 01:05:47 +000076 mock_trans(@prot)
77 @processor.write_result(result, @prot, 'testMessage', 23)
78 end
79 end
80end