public
Description: Cross language RPC
Homepage: http://developers.facebook.com/thrift/
Clone URL: git://github.com/kevinclark/thrift.git
kevinclark (author)
Sat Apr 26 14:27:19 -0700 2008
commit  993f8d3036b617d8db99a2bf1faf0709af0980e0
tree    75a0f0a18873c9d2395ff0160c43ea4268e831a8
parent  d2a247ac63fb786a3d2842fd4cd1c2030f7c4eca
thrift / test / rb / core / test_binary_protocol_accelerated.rb
100644 264 lines (233 sloc) 7.82 kb
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
require File.join(File.dirname(__FILE__), '../test_helper')
require File.join(File.dirname(__FILE__), '../fixtures/structs')
 
require 'thrift/transport/ttransport'
require 'thrift/protocol/tbinaryprotocol'
require 'thrift/protocol/tbinaryprotocolaccelerated'
 
class TBinaryProtocolAcceleratedTest < Test::Unit::TestCase
  I8_MIN = -128
  I8_MAX = 127
  I16_MIN = -32768
  I16_MAX = 32767
  I32_MIN = -2147483648
  I32_MAX = 2147483647
  I64_MIN = -9223372036854775808
  I64_MAX = 9223372036854775807
  DBL_MIN = Float::MIN
  DBL_MAX = Float::MAX
 
  # booleans might be read back differently, so we supply a list [write_value, read_value]
  BOOL_VALUES = [[0,true], [14,true], [-14,true], [true,true], [false,false], ["",true]]
  BYTE_VALUES = [14, -14, I8_MIN, I8_MAX]
  I16_VALUES = [400, 0, -234, I16_MIN, I16_MAX]
  I32_VALUES = [325, 0, -1, -1073741825, -278, -4352388, I32_MIN, I32_MAX]
  I64_VALUES = [15, 0, -33, I64_MIN, I64_MAX]
  DBL_VALUES = [DBL_MIN, -33.8755, 0, 3658.1279, DBL_MAX]
  STR_VALUES = ["", "welcome to my test"]
  
  def setup
    @trans = TMemoryBuffer.new
    @fast_proto = TBinaryProtocolAccelerated.new(@trans)
    @slow_proto = TBinaryProtocol.new(@trans)
  end
  
  def assert_encodes_struct(obj)
    obj.write(@slow_proto)
    expected = @trans.read(@trans.available) # read it all baby
    assert_equal expected, @fast_proto.encode_binary(obj)
  end
  
  # Assumes encode works
  def assert_decodes_struct(obj, eql_obj = nil)
    data = @fast_proto.encode_binary(obj)
    @trans.write data
    assert_equal (eql_obj || obj), @fast_proto.decode_binary(obj.class.new, @trans)
  end
  
  def test_encodes_and_decodes_bools
    BOOL_VALUES.each do |(write_val, read_val)|
      obj = Fixtures::Structs::OneBool.new(:bool => write_val)
      assert_encodes_struct obj
      assert_decodes_struct obj, Fixtures::Structs::OneBool.new(:bool => read_val)
    end
  end
  
  def test_encodes_and_decodes_bytes
    BYTE_VALUES.each do |val|
      obj = Fixtures::Structs::OneByte.new(:byte => val)
      assert_encodes_struct obj
      assert_decodes_struct obj
    end
  end
  
  def test_encodes_and_decodes_i16
    I16_VALUES.each do |val|
      obj = Fixtures::Structs::OneI16.new(:i16 => val)
      assert_encodes_struct obj
      assert_decodes_struct obj
    end
  end
  
  def test_encodes_and_decodes_i32
    I32_VALUES.each do |val|
      obj = Fixtures::Structs::OneI32.new(:i32 => val)
      assert_encodes_struct obj
      assert_decodes_struct obj
    end
  end
  
  def test_encodes_and_decodes_i64
    I64_VALUES.each do |val|
      obj = Fixtures::Structs::OneI64.new(:i64 => val)
      assert_encodes_struct obj
      assert_decodes_struct obj
    end
  end
  
  def test_encodes_and_decodes_double
    DBL_VALUES.each do |val|
      obj = Fixtures::Structs::OneDouble.new(:double => val)
      assert_encodes_struct obj
      assert_decodes_struct obj
    end
  end
  
  def test_encodes_strings
    STR_VALUES.each do |val|
      obj = Fixtures::Structs::OneString.new(:string => val)
      assert_encodes_struct obj
      assert_decodes_struct obj
    end
  end
  
  def test_encodes_maps
    obj = Fixtures::Structs::OneMap.new(:map => {"a" => "b", "c" => "d"})
    assert_encodes_struct obj
    assert_decodes_struct obj
  end
  
  def test_encodes_lists
    obj = Fixtures::Structs::OneList.new(:list => ["a", "b", "c", "d"])
    assert_encodes_struct obj
    assert_decodes_struct obj
  end
  
  def test_encodes_sets
    obj = Fixtures::Structs::OneSet.new(:set => {"a" => true, "b" => true, "c" => true})
    assert_encodes_struct obj
    assert_decodes_struct obj
  end
  
  def test_encodes_maps_of_maps
    obj = Fixtures::Structs::NestedMap.new(:map => { 1 => { 2 => 3 }})
    assert_encodes_struct obj
    assert_decodes_struct obj
  end
  
  def test_encodes_list_of_lists
    obj = Fixtures::Structs::NestedList.new(:list => [[1,2,3], [4,5,6]])
    assert_encodes_struct obj
    assert_decodes_struct obj
  end
 
  def test_encodes_set_of_sets
    obj = Fixtures::Structs::NestedSet.new(:set => {{'a' => true} => true})
    assert_encodes_struct obj
 
    # Nested hashes won't work with ==, so we do it by hand
    data = @fast_proto.encode_binary(obj)
    @trans.write data
    decoded = @fast_proto.decode_binary(obj.class.new, @trans)
    assert_equal decoded.set.keys, [{'a' => true}]
    assert_equal decoded.set.values, [true]
  end
  
  if ENV['MEM_TEST']
    def test_for_memory_leaks_on_exceptions
      ooe = Fixtures::Structs::OneOfEach.new
      ooe.im_true = true
      ooe.im_false = false
      ooe.a_bite = -42
      ooe.integer16 = 27000
      ooe.integer32 = 1<<24
      ooe.integer64 = 6000 * 1000 * 1000
      ooe.double_precision = Math::PI
      ooe.some_characters = "Debug THIS!"
      ooe.zomg_unicode = "\xd7\n\a\t"
    
      10_000.times do
        data = @fast_proto.encode_binary(ooe)
        bytes = []
        data.each_byte do |b|
          bytes << b
          transport = TMemoryBuffer.new
          transport.write(bytes.pack("c#{bytes.length}"))
      
          begin
            @fast_proto.decode_binary(Fixtures::Structs::OneOfEach.new, transport)
          rescue Exception
          end
        end
      end
    
    end
  end
  
  unless ENV['FAST_TEST']
    def test_encodes_and_decodes_struct_of_structs
      ooe = Fixtures::Structs::OneOfEach.new
      ooe.im_true = true
      ooe.im_false = false
      ooe.a_bite = -42
      ooe.integer16 = 27000
      ooe.integer32 = 1<<24
      ooe.integer64 = 6000 * 1000 * 1000
      ooe.double_precision = Math::PI
      ooe.some_characters = "Debug THIS!"
      ooe.zomg_unicode = "\xd7\n\a\t"
  
      n1 = Fixtures::Structs::Nested1.new
      n1.a_list = []
      n1.a_list << ooe << ooe << ooe << ooe
      n1.i32_map = {}
      n1.i32_map[1234] = ooe
      n1.i32_map[46345] = ooe
      n1.i32_map[-34264] = ooe
      n1.i64_map = {}
      n1.i64_map[43534986783945] = ooe
      n1.i64_map[-32434639875122] = ooe
      n1.dbl_map = {}
      n1.dbl_map[324.65469834] = ooe
      n1.dbl_map[-9458672340.4986798345112] = ooe
      n1.str_map = {}
      n1.str_map['sdoperuix'] = ooe
      n1.str_map['pwoerxclmn'] = ooe
    
      n2 = Fixtures::Structs::Nested2.new
      n2.a_list = []
      n2.a_list << n1 << n1 << n1 << n1 << n1
      n2.i32_map = {}
      n2.i32_map[398345] = n1
      n2.i32_map[-2345] = n1
      n2.i32_map[12312] = n1
      n2.i64_map = {}
      n2.i64_map[2349843765934] = n1
      n2.i64_map[-123234985495] = n1
      n2.i64_map[0] = n1
      n2.dbl_map = {}
      n2.dbl_map[23345345.38927834] = n1
      n2.dbl_map[-1232349.5489345] = n1
      n2.dbl_map[-234984574.23498725] = n1
      n2.str_map = {}
      n2.str_map[''] = n1
      n2.str_map['sdflkertpioux'] = n1
      n2.str_map['sdfwepwdcjpoi'] = n1
  
      n3 = Fixtures::Structs::Nested3.new
      n3.a_list = []
      n3.a_list << n2 << n2 << n2 << n2 << n2
      n3.i32_map = {}
      n3.i32_map[398345] = n2
      n3.i32_map[-2345] = n2
      n3.i32_map[12312] = n2
      n3.i64_map = {}
      n3.i64_map[2349843765934] = n2
      n3.i64_map[-123234985495] = n2
      n3.i64_map[0] = n2
      n3.dbl_map = {}
      n3.dbl_map[23345345.38927834] = n2
      n3.dbl_map[-1232349.5489345] = n2
      n3.dbl_map[-234984574.23498725] = n2
      n3.str_map = {}
      n3.str_map[''] = n2
      n3.str_map['sdflkertpioux'] = n2
      n3.str_map['sdfwepwdcjpoi'] = n2
  
      n4 = Fixtures::Structs::Nested4.new
      n4.a_list = []
      n4.a_list << n3
      n4.i32_map = {}
      n4.i32_map[-2345] = n3
      n4.i64_map = {}
      n4.i64_map[2349843765934] = n3
      n4.dbl_map = {}
      n4.dbl_map[-1232349.5489345] = n3
      n4.str_map = {}
      n4.str_map[''] = n3
    
      assert_encodes_struct n4
      assert_decodes_struct n4
    end
  end
end