summaryrefslogtreecommitdiff
path: root/lib/rb/spec/processor_spec.rb
blob: d35f652846ba6291ef806b2695746459137f3657 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

require File.dirname(__FILE__) + '/spec_helper'

class ThriftProcessorSpec < Spec::ExampleGroup
  include Thrift

  class ProcessorSpec
    include Thrift::Processor
  end

  describe "Processor" do
    before(:each) do
      @processor = ProcessorSpec.new(mock("MockHandler"))
      @prot = mock("MockProtocol")
    end

    def mock_trans(obj)
      obj.should_receive(:trans).ordered.and_return do
        mock("trans").tee do |trans|
          trans.should_receive(:flush).ordered
        end
      end
    end

    it "should call process_<message> when it receives that message" do
      @prot.should_receive(:read_message_begin).ordered.and_return ['testMessage', MessageTypes::CALL, 17]
      @processor.should_receive(:process_testMessage).with(17, @prot, @prot).ordered
      @processor.process(@prot, @prot).should == true
    end

    it "should raise an ApplicationException when the received message cannot be processed" do
      @prot.should_receive(:read_message_begin).ordered.and_return ['testMessage', MessageTypes::CALL, 4]
      @prot.should_receive(:skip).with(Types::STRUCT).ordered
      @prot.should_receive(:read_message_end).ordered
      @prot.should_receive(:write_message_begin).with('testMessage', MessageTypes::EXCEPTION, 4).ordered
      ApplicationException.should_receive(:new).with(ApplicationException::UNKNOWN_METHOD, "Unknown function testMessage").and_return do
        mock(ApplicationException).tee do |e|
          e.should_receive(:write).with(@prot).ordered
        end
      end
      @prot.should_receive(:write_message_end).ordered
      mock_trans(@prot)
      @processor.process(@prot, @prot)
    end

    it "should pass args off to the args class" do
      args_class = mock("MockArgsClass")
      args = mock("#<MockArgsClass:mock>").tee do |args|
        args.should_receive(:read).with(@prot).ordered
      end
      args_class.should_receive(:new).and_return args
      @prot.should_receive(:read_message_end).ordered
      @processor.read_args(@prot, args_class).should eql(args)
    end

    it "should write out a reply when asked" do
      @prot.should_receive(:write_message_begin).with('testMessage', MessageTypes::REPLY, 23).ordered
      result = mock("MockResult")
      result.should_receive(:write).with(@prot).ordered
      @prot.should_receive(:write_message_end).ordered
      mock_trans(@prot)
      @processor.write_result(result, @prot, 'testMessage', 23)
    end
  end
end