blob: ef3bc85b31ac1ff7095db650bde87969e2ec6444 [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
30 @processor = ProcessorSpec.new(mock("MockHandler"))
31 @prot = mock("MockProtocol")
32 end
33
34 def mock_trans(obj)
35 obj.should_receive(:trans).ordered.and_return do
36 mock("trans").tee do |trans|
37 trans.should_receive(:flush).ordered
38 end
39 end
40 end
41
42 it "should call process_<message> when it receives that message" do
Jake Farrella87810f2012-09-28 01:59:04 +000043 @prot.should_receive(:read_message_begin).ordered.and_return ['testMessage', Thrift::MessageTypes::CALL, 17]
Kevin Clark4a2b8412008-06-18 01:05:47 +000044 @processor.should_receive(:process_testMessage).with(17, @prot, @prot).ordered
45 @processor.process(@prot, @prot).should == true
46 end
47
48 it "should raise an ApplicationException when the received message cannot be processed" do
Jake Farrella87810f2012-09-28 01:59:04 +000049 @prot.should_receive(:read_message_begin).ordered.and_return ['testMessage', Thrift::MessageTypes::CALL, 4]
50 @prot.should_receive(:skip).with(Thrift::Types::STRUCT).ordered
Kevin Clark4a2b8412008-06-18 01:05:47 +000051 @prot.should_receive(:read_message_end).ordered
Jake Farrella87810f2012-09-28 01:59:04 +000052 @prot.should_receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::EXCEPTION, 4).ordered
53 e = mock(Thrift::ApplicationException)
54 e.should_receive(:write).with(@prot).ordered
55 Thrift::ApplicationException.should_receive(:new).with(Thrift::ApplicationException::UNKNOWN_METHOD, "Unknown function testMessage").and_return(e)
Kevin Clark4a2b8412008-06-18 01:05:47 +000056 @prot.should_receive(:write_message_end).ordered
57 mock_trans(@prot)
58 @processor.process(@prot, @prot)
59 end
60
61 it "should pass args off to the args class" do
62 args_class = mock("MockArgsClass")
63 args = mock("#<MockArgsClass:mock>").tee do |args|
64 args.should_receive(:read).with(@prot).ordered
65 end
66 args_class.should_receive(:new).and_return args
67 @prot.should_receive(:read_message_end).ordered
68 @processor.read_args(@prot, args_class).should eql(args)
69 end
70
71 it "should write out a reply when asked" do
Jake Farrella87810f2012-09-28 01:59:04 +000072 @prot.should_receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::REPLY, 23).ordered
Kevin Clark4a2b8412008-06-18 01:05:47 +000073 result = mock("MockResult")
74 result.should_receive(:write).with(@prot).ordered
75 @prot.should_receive(:write_message_end).ordered
76 mock_trans(@prot)
77 @processor.write_result(result, @prot, 'testMessage', 23)
78 end
79 end
80end