blob: 24440e42fb1b3dcd85c685ece01abbf393249e7d [file] [log] [blame]
Kevin Clark4a2b8412008-06-18 01:05:47 +00001require File.dirname(__FILE__) + '/spec_helper'
2
3class ThriftSpec < Spec::ExampleGroup
4 include Thrift
5
6 class ProcessorSpec
7 include Thrift::Processor
8 end
9
10 describe "Processor" do
11 before(:each) do
12 @processor = ProcessorSpec.new(mock("MockHandler"))
13 @prot = mock("MockProtocol")
14 end
15
16 def mock_trans(obj)
17 obj.should_receive(:trans).ordered.and_return do
18 mock("trans").tee do |trans|
19 trans.should_receive(:flush).ordered
20 end
21 end
22 end
23
24 it "should call process_<message> when it receives that message" do
25 @prot.should_receive(:read_message_begin).ordered.and_return ['testMessage', MessageTypes::CALL, 17]
26 @processor.should_receive(:process_testMessage).with(17, @prot, @prot).ordered
27 @processor.process(@prot, @prot).should == true
28 end
29
30 it "should raise an ApplicationException when the received message cannot be processed" do
31 @prot.should_receive(:read_message_begin).ordered.and_return ['testMessage', MessageTypes::CALL, 4]
32 @prot.should_receive(:skip).with(Types::STRUCT).ordered
33 @prot.should_receive(:read_message_end).ordered
34 @prot.should_receive(:write_message_begin).with('testMessage', MessageTypes::EXCEPTION, 4).ordered
35 ApplicationException.should_receive(:new).with(ApplicationException::UNKNOWN_METHOD, "Unknown function testMessage").and_return do
36 mock(ApplicationException).tee do |e|
37 e.should_receive(:write).with(@prot).ordered
38 end
39 end
40 @prot.should_receive(:write_message_end).ordered
41 mock_trans(@prot)
42 @processor.process(@prot, @prot)
43 end
44
45 it "should pass args off to the args class" do
46 args_class = mock("MockArgsClass")
47 args = mock("#<MockArgsClass:mock>").tee do |args|
48 args.should_receive(:read).with(@prot).ordered
49 end
50 args_class.should_receive(:new).and_return args
51 @prot.should_receive(:read_message_end).ordered
52 @processor.read_args(@prot, args_class).should eql(args)
53 end
54
55 it "should write out a reply when asked" do
56 @prot.should_receive(:write_message_begin).with('testMessage', MessageTypes::REPLY, 23).ordered
57 result = mock("MockResult")
58 result.should_receive(:write).with(@prot).ordered
59 @prot.should_receive(:write_message_end).ordered
60 mock_trans(@prot)
61 @processor.write_result(result, @prot, 'testMessage', 23)
62 end
63 end
64end