Subversion Repositories SmartDukaan

Rev

Rev 30 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 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
 
20
require File.dirname(__FILE__) + '/spec_helper'
21
 
22
class ThriftClientSpec < Spec::ExampleGroup
23
  include Thrift
24
 
25
  class ClientSpec
26
    include Thrift::Client
27
  end
28
 
29
  before(:each) do
30
    @prot = mock("MockProtocol")
31
    @client = ClientSpec.new(@prot)
32
  end
33
 
34
  describe Client do
35
    it "should re-use iprot for oprot if not otherwise specified" do
36
      @client.instance_variable_get(:'@iprot').should eql(@prot)
37
      @client.instance_variable_get(:'@oprot').should eql(@prot)
38
    end
39
 
40
    it "should send a test message" do
41
      @prot.should_receive(:write_message_begin).with('testMessage', MessageTypes::CALL, 0)
42
      mock_args = mock('#<TestMessage_args:mock>')
43
      mock_args.should_receive(:foo=).with('foo')
44
      mock_args.should_receive(:bar=).with(42)
45
      mock_args.should_receive(:write).with(@prot)
46
      @prot.should_receive(:write_message_end)
47
      @prot.should_receive(:trans) do
48
        mock('trans').tee do |trans|
49
          trans.should_receive(:flush)
50
        end
51
      end
52
      klass = stub("TestMessage_args", :new => mock_args)
53
      @client.send_message('testMessage', klass, :foo => 'foo', :bar => 42)
54
    end
55
 
56
    it "should increment the sequence id when sending messages" do
57
      pending "it seems sequence ids are completely ignored right now" do
58
        @prot.should_receive(:write_message_begin).with('testMessage',  MessageTypes::CALL, 0).ordered
59
        @prot.should_receive(:write_message_begin).with('testMessage2', MessageTypes::CALL, 1).ordered
60
        @prot.should_receive(:write_message_begin).with('testMessage3', MessageTypes::CALL, 2).ordered
61
        @prot.stub!(:write_message_end)
62
        @prot.stub!(:trans).and_return mock("trans").as_null_object
63
        @client.send_message('testMessage', mock("args class").as_null_object)
64
        @client.send_message('testMessage2', mock("args class").as_null_object)
65
        @client.send_message('testMessage3', mock("args class").as_null_object)        
66
      end
67
    end
68
 
69
    it "should receive a test message" do
70
      @prot.should_receive(:read_message_begin).and_return [nil, MessageTypes::CALL, 0]
71
      @prot.should_receive(:read_message_end)
72
      mock_klass = mock("#<MockClass:mock>")
73
      mock_klass.should_receive(:read).with(@prot)
74
      @client.receive_message(stub("MockClass", :new => mock_klass))
75
    end
76
 
77
    it "should handle received exceptions" do
78
      @prot.should_receive(:read_message_begin).and_return [nil, MessageTypes::EXCEPTION, 0]
79
      @prot.should_receive(:read_message_end)
80
      ApplicationException.should_receive(:new).and_return do
81
        StandardError.new.tee do |mock_exc|
82
          mock_exc.should_receive(:read).with(@prot)
83
        end
84
      end
85
      lambda { @client.receive_message(nil) }.should raise_error(StandardError)
86
    end
87
 
88
    it "should close the transport if an error occurs while sending a message" do
89
      @prot.stub!(:write_message_begin)
90
      @prot.should_not_receive(:write_message_end)
91
      mock_args = mock("#<TestMessage_args:mock>")
92
      mock_args.should_receive(:write).with(@prot).and_raise(StandardError)
93
      trans = mock("MockTransport")
94
      @prot.stub!(:trans).and_return(trans)
95
      trans.should_receive(:close)
96
      klass = mock("TestMessage_args", :new => mock_args)
97
      lambda { @client.send_message("testMessage", klass) }.should raise_error(StandardError)
98
    end
99
  end
100
end