Gavin McDonald | 0b75e1a | 2010-10-28 02:12:01 +0000 | [diff] [blame^] | 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 | require File.dirname(__FILE__) + '/gen-rb/thrift_spec_types' |
| 22 | |
| 23 | class ThriftStructSpec < Spec::ExampleGroup |
| 24 | include Thrift |
| 25 | include SpecNamespace |
| 26 | |
| 27 | describe Struct do |
| 28 | it "should iterate over all fields properly" do |
| 29 | fields = {} |
| 30 | Foo.new.each_field { |fid,field_info| fields[fid] = field_info } |
| 31 | fields.should == Foo::FIELDS |
| 32 | end |
| 33 | |
| 34 | it "should initialize all fields to defaults" do |
| 35 | struct = Foo.new |
| 36 | struct.simple.should == 53 |
| 37 | struct.words.should == "words" |
| 38 | struct.hello.should == Hello.new(:greeting => 'hello, world!') |
| 39 | struct.ints.should == [1, 2, 2, 3] |
| 40 | struct.complex.should be_nil |
| 41 | struct.shorts.should == Set.new([5, 17, 239]) |
| 42 | end |
| 43 | |
| 44 | it "should not share default values between instances" do |
| 45 | begin |
| 46 | struct = Foo.new |
| 47 | struct.ints << 17 |
| 48 | Foo.new.ints.should == [1,2,2,3] |
| 49 | ensure |
| 50 | # ensure no leakage to other tests |
| 51 | Foo::FIELDS[4][:default] = [1,2,2,3] |
| 52 | end |
| 53 | end |
| 54 | |
| 55 | it "should properly initialize boolean values" do |
| 56 | struct = BoolStruct.new(:yesno => false) |
| 57 | struct.yesno.should be_false |
| 58 | end |
| 59 | |
| 60 | it "should have proper == semantics" do |
| 61 | Foo.new.should_not == Hello.new |
| 62 | Foo.new.should == Foo.new |
| 63 | Foo.new(:simple => 52).should_not == Foo.new |
| 64 | end |
| 65 | |
| 66 | it "should read itself off the wire" do |
| 67 | struct = Foo.new |
| 68 | prot = BaseProtocol.new(mock("transport")) |
| 69 | prot.should_receive(:read_struct_begin).twice |
| 70 | prot.should_receive(:read_struct_end).twice |
| 71 | prot.should_receive(:read_field_begin).and_return( |
| 72 | ['complex', Types::MAP, 5], # Foo |
| 73 | ['words', Types::STRING, 2], # Foo |
| 74 | ['hello', Types::STRUCT, 3], # Foo |
| 75 | ['greeting', Types::STRING, 1], # Hello |
| 76 | [nil, Types::STOP, 0], # Hello |
| 77 | ['simple', Types::I32, 1], # Foo |
| 78 | ['ints', Types::LIST, 4], # Foo |
| 79 | ['shorts', Types::SET, 6], # Foo |
| 80 | [nil, Types::STOP, 0] # Hello |
| 81 | ) |
| 82 | prot.should_receive(:read_field_end).exactly(7).times |
| 83 | prot.should_receive(:read_map_begin).and_return( |
| 84 | [Types::I32, Types::MAP, 2], # complex |
| 85 | [Types::STRING, Types::DOUBLE, 2], # complex/1/value |
| 86 | [Types::STRING, Types::DOUBLE, 1] # complex/2/value |
| 87 | ) |
| 88 | prot.should_receive(:read_map_end).exactly(3).times |
| 89 | prot.should_receive(:read_list_begin).and_return([Types::I32, 4]) |
| 90 | prot.should_receive(:read_list_end) |
| 91 | prot.should_receive(:read_set_begin).and_return([Types::I16, 2]) |
| 92 | prot.should_receive(:read_set_end) |
| 93 | prot.should_receive(:read_i32).and_return( |
| 94 | 1, 14, # complex keys |
| 95 | 42, # simple |
| 96 | 4, 23, 4, 29 # ints |
| 97 | ) |
| 98 | prot.should_receive(:read_string).and_return("pi", "e", "feigenbaum", "apple banana", "what's up?") |
| 99 | prot.should_receive(:read_double).and_return(Math::PI, Math::E, 4.669201609) |
| 100 | prot.should_receive(:read_i16).and_return(2, 3) |
| 101 | prot.should_not_receive(:skip) |
| 102 | struct.read(prot) |
| 103 | |
| 104 | struct.simple.should == 42 |
| 105 | struct.complex.should == {1 => {"pi" => Math::PI, "e" => Math::E}, 14 => {"feigenbaum" => 4.669201609}} |
| 106 | struct.hello.should == Hello.new(:greeting => "what's up?") |
| 107 | struct.words.should == "apple banana" |
| 108 | struct.ints.should == [4, 23, 4, 29] |
| 109 | struct.shorts.should == Set.new([3, 2]) |
| 110 | end |
| 111 | |
| 112 | it "should skip unexpected fields in structs and use default values" do |
| 113 | struct = Foo.new |
| 114 | prot = BaseProtocol.new(mock("transport")) |
| 115 | prot.should_receive(:read_struct_begin) |
| 116 | prot.should_receive(:read_struct_end) |
| 117 | prot.should_receive(:read_field_begin).and_return( |
| 118 | ['simple', Types::I32, 1], |
| 119 | ['complex', Types::STRUCT, 5], |
| 120 | ['thinz', Types::MAP, 7], |
| 121 | ['foobar', Types::I32, 3], |
| 122 | ['words', Types::STRING, 2], |
| 123 | [nil, Types::STOP, 0] |
| 124 | ) |
| 125 | prot.should_receive(:read_field_end).exactly(5).times |
| 126 | prot.should_receive(:read_i32).and_return(42) |
| 127 | prot.should_receive(:read_string).and_return("foobar") |
| 128 | prot.should_receive(:skip).with(Types::STRUCT) |
| 129 | prot.should_receive(:skip).with(Types::MAP) |
| 130 | # prot.should_receive(:read_map_begin).and_return([Types::I32, Types::I32, 0]) |
| 131 | # prot.should_receive(:read_map_end) |
| 132 | prot.should_receive(:skip).with(Types::I32) |
| 133 | struct.read(prot) |
| 134 | |
| 135 | struct.simple.should == 42 |
| 136 | struct.complex.should be_nil |
| 137 | struct.words.should == "foobar" |
| 138 | struct.hello.should == Hello.new(:greeting => 'hello, world!') |
| 139 | struct.ints.should == [1, 2, 2, 3] |
| 140 | struct.shorts.should == Set.new([5, 17, 239]) |
| 141 | end |
| 142 | |
| 143 | it "should write itself to the wire" do |
| 144 | prot = BaseProtocol.new(mock("transport")) #mock("Protocol") |
| 145 | prot.should_receive(:write_struct_begin).with("SpecNamespace::Foo") |
| 146 | prot.should_receive(:write_struct_begin).with("SpecNamespace::Hello") |
| 147 | prot.should_receive(:write_struct_end).twice |
| 148 | prot.should_receive(:write_field_begin).with('ints', Types::LIST, 4) |
| 149 | prot.should_receive(:write_i32).with(1) |
| 150 | prot.should_receive(:write_i32).with(2).twice |
| 151 | prot.should_receive(:write_i32).with(3) |
| 152 | prot.should_receive(:write_field_begin).with('complex', Types::MAP, 5) |
| 153 | prot.should_receive(:write_i32).with(5) |
| 154 | prot.should_receive(:write_string).with('foo') |
| 155 | prot.should_receive(:write_double).with(1.23) |
| 156 | prot.should_receive(:write_field_begin).with('shorts', Types::SET, 6) |
| 157 | prot.should_receive(:write_i16).with(5) |
| 158 | prot.should_receive(:write_i16).with(17) |
| 159 | prot.should_receive(:write_i16).with(239) |
| 160 | prot.should_receive(:write_field_stop).twice |
| 161 | prot.should_receive(:write_field_end).exactly(6).times |
| 162 | prot.should_receive(:write_field_begin).with('simple', Types::I32, 1) |
| 163 | prot.should_receive(:write_i32).with(53) |
| 164 | prot.should_receive(:write_field_begin).with('hello', Types::STRUCT, 3) |
| 165 | prot.should_receive(:write_field_begin).with('greeting', Types::STRING, 1) |
| 166 | prot.should_receive(:write_string).with('hello, world!') |
| 167 | prot.should_receive(:write_map_begin).with(Types::I32, Types::MAP, 1) |
| 168 | prot.should_receive(:write_map_begin).with(Types::STRING, Types::DOUBLE, 1) |
| 169 | prot.should_receive(:write_map_end).twice |
| 170 | prot.should_receive(:write_list_begin).with(Types::I32, 4) |
| 171 | prot.should_receive(:write_list_end) |
| 172 | prot.should_receive(:write_set_begin).with(Types::I16, 3) |
| 173 | prot.should_receive(:write_set_end) |
| 174 | |
| 175 | struct = Foo.new |
| 176 | struct.words = nil |
| 177 | struct.complex = {5 => {"foo" => 1.23}} |
| 178 | struct.write(prot) |
| 179 | end |
| 180 | |
| 181 | it "should raise an exception if presented with an unknown container" do |
| 182 | # yeah this is silly, but I'm going for code coverage here |
| 183 | struct = Foo.new |
| 184 | lambda { struct.send :write_container, nil, nil, {:type => "foo"} }.should raise_error(StandardError, "Not a container type: foo") |
| 185 | end |
| 186 | |
| 187 | it "should support optional type-checking in Thrift::Struct.new" do |
| 188 | Thrift.type_checking = true |
| 189 | begin |
| 190 | lambda { Hello.new(:greeting => 3) }.should raise_error(TypeError, "Expected Types::STRING, received Fixnum for field greeting") |
| 191 | ensure |
| 192 | Thrift.type_checking = false |
| 193 | end |
| 194 | lambda { Hello.new(:greeting => 3) }.should_not raise_error(TypeError) |
| 195 | end |
| 196 | |
| 197 | it "should support optional type-checking in field accessors" do |
| 198 | Thrift.type_checking = true |
| 199 | begin |
| 200 | hello = Hello.new |
| 201 | lambda { hello.greeting = 3 }.should raise_error(TypeError, "Expected Types::STRING, received Fixnum for field greeting") |
| 202 | ensure |
| 203 | Thrift.type_checking = false |
| 204 | end |
| 205 | lambda { hello.greeting = 3 }.should_not raise_error(TypeError) |
| 206 | end |
| 207 | |
| 208 | it "should raise an exception when unknown types are given to Thrift::Struct.new" do |
| 209 | lambda { Hello.new(:fish => 'salmon') }.should raise_error(Exception, "Unknown key given to SpecNamespace::Hello.new: fish") |
| 210 | end |
| 211 | |
| 212 | it "should support `raise Xception, 'message'` for Exception structs" do |
| 213 | begin |
| 214 | raise Xception, "something happened" |
| 215 | rescue Thrift::Exception => e |
| 216 | e.message.should == "something happened" |
| 217 | e.code.should == 1 |
| 218 | # ensure it gets serialized properly, this is the really important part |
| 219 | prot = BaseProtocol.new(mock("trans")) |
| 220 | prot.should_receive(:write_struct_begin).with("SpecNamespace::Xception") |
| 221 | prot.should_receive(:write_struct_end) |
| 222 | prot.should_receive(:write_field_begin).with('message', Types::STRING, 1)#, "something happened") |
| 223 | prot.should_receive(:write_string).with("something happened") |
| 224 | prot.should_receive(:write_field_begin).with('code', Types::I32, 2)#, 1) |
| 225 | prot.should_receive(:write_i32).with(1) |
| 226 | prot.should_receive(:write_field_stop) |
| 227 | prot.should_receive(:write_field_end).twice |
| 228 | |
| 229 | e.write(prot) |
| 230 | end |
| 231 | end |
| 232 | |
| 233 | it "should support the regular initializer for exception structs" do |
| 234 | begin |
| 235 | raise Xception, :message => "something happened", :code => 5 |
| 236 | rescue Thrift::Exception => e |
| 237 | e.message.should == "something happened" |
| 238 | e.code.should == 5 |
| 239 | prot = BaseProtocol.new(mock("trans")) |
| 240 | prot.should_receive(:write_struct_begin).with("SpecNamespace::Xception") |
| 241 | prot.should_receive(:write_struct_end) |
| 242 | prot.should_receive(:write_field_begin).with('message', Types::STRING, 1) |
| 243 | prot.should_receive(:write_string).with("something happened") |
| 244 | prot.should_receive(:write_field_begin).with('code', Types::I32, 2) |
| 245 | prot.should_receive(:write_i32).with(5) |
| 246 | prot.should_receive(:write_field_stop) |
| 247 | prot.should_receive(:write_field_end).twice |
| 248 | |
| 249 | e.write(prot) |
| 250 | end |
| 251 | end |
| 252 | end |
| 253 | end |