jml_thrift 1.0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +7 -0
  2. data/README +43 -0
  3. data/benchmark/Benchmark.thrift +24 -0
  4. data/benchmark/benchmark.rb +271 -0
  5. data/benchmark/client.rb +74 -0
  6. data/benchmark/server.rb +82 -0
  7. data/benchmark/thin_server.rb +44 -0
  8. data/ext/binary_protocol_accelerated.c +460 -0
  9. data/ext/binary_protocol_accelerated.h +20 -0
  10. data/ext/bytes.c +36 -0
  11. data/ext/bytes.h +31 -0
  12. data/ext/compact_protocol.c +635 -0
  13. data/ext/compact_protocol.h +20 -0
  14. data/ext/constants.h +96 -0
  15. data/ext/extconf.rb +32 -0
  16. data/ext/macros.h +41 -0
  17. data/ext/memory_buffer.c +134 -0
  18. data/ext/memory_buffer.h +20 -0
  19. data/ext/protocol.c +0 -0
  20. data/ext/protocol.h +0 -0
  21. data/ext/strlcpy.c +41 -0
  22. data/ext/strlcpy.h +34 -0
  23. data/ext/struct.c +688 -0
  24. data/ext/struct.h +25 -0
  25. data/ext/thrift_native.c +195 -0
  26. data/lib/thrift.rb +66 -0
  27. data/lib/thrift/bytes.rb +131 -0
  28. data/lib/thrift/client.rb +62 -0
  29. data/lib/thrift/core_ext.rb +23 -0
  30. data/lib/thrift/core_ext/fixnum.rb +29 -0
  31. data/lib/thrift/exceptions.rb +87 -0
  32. data/lib/thrift/processor.rb +57 -0
  33. data/lib/thrift/protocol/base_protocol.rb +377 -0
  34. data/lib/thrift/protocol/binary_protocol.rb +237 -0
  35. data/lib/thrift/protocol/binary_protocol_accelerated.rb +39 -0
  36. data/lib/thrift/protocol/compact_protocol.rb +434 -0
  37. data/lib/thrift/protocol/json_protocol.rb +769 -0
  38. data/lib/thrift/serializer/deserializer.rb +33 -0
  39. data/lib/thrift/serializer/serializer.rb +34 -0
  40. data/lib/thrift/server/base_server.rb +31 -0
  41. data/lib/thrift/server/mongrel_http_server.rb +60 -0
  42. data/lib/thrift/server/nonblocking_server.rb +305 -0
  43. data/lib/thrift/server/simple_server.rb +43 -0
  44. data/lib/thrift/server/thin_http_server.rb +91 -0
  45. data/lib/thrift/server/thread_pool_server.rb +75 -0
  46. data/lib/thrift/server/threaded_server.rb +47 -0
  47. data/lib/thrift/struct.rb +237 -0
  48. data/lib/thrift/struct_union.rb +192 -0
  49. data/lib/thrift/thrift_native.rb +24 -0
  50. data/lib/thrift/transport/base_server_transport.rb +37 -0
  51. data/lib/thrift/transport/base_transport.rb +109 -0
  52. data/lib/thrift/transport/buffered_transport.rb +114 -0
  53. data/lib/thrift/transport/framed_transport.rb +117 -0
  54. data/lib/thrift/transport/http_client_transport.rb +56 -0
  55. data/lib/thrift/transport/io_stream_transport.rb +39 -0
  56. data/lib/thrift/transport/memory_buffer_transport.rb +125 -0
  57. data/lib/thrift/transport/server_socket.rb +63 -0
  58. data/lib/thrift/transport/socket.rb +139 -0
  59. data/lib/thrift/transport/unix_server_socket.rb +60 -0
  60. data/lib/thrift/transport/unix_socket.rb +40 -0
  61. data/lib/thrift/types.rb +101 -0
  62. data/lib/thrift/union.rb +179 -0
  63. data/spec/ThriftSpec.thrift +183 -0
  64. data/spec/base_protocol_spec.rb +217 -0
  65. data/spec/base_transport_spec.rb +350 -0
  66. data/spec/binary_protocol_accelerated_spec.rb +42 -0
  67. data/spec/binary_protocol_spec.rb +66 -0
  68. data/spec/binary_protocol_spec_shared.rb +455 -0
  69. data/spec/bytes_spec.rb +160 -0
  70. data/spec/client_spec.rb +99 -0
  71. data/spec/compact_protocol_spec.rb +143 -0
  72. data/spec/exception_spec.rb +141 -0
  73. data/spec/http_client_spec.rb +120 -0
  74. data/spec/json_protocol_spec.rb +513 -0
  75. data/spec/nonblocking_server_spec.rb +263 -0
  76. data/spec/processor_spec.rb +80 -0
  77. data/spec/serializer_spec.rb +67 -0
  78. data/spec/server_socket_spec.rb +79 -0
  79. data/spec/server_spec.rb +147 -0
  80. data/spec/socket_spec.rb +61 -0
  81. data/spec/socket_spec_shared.rb +104 -0
  82. data/spec/spec_helper.rb +61 -0
  83. data/spec/struct_nested_containers_spec.rb +191 -0
  84. data/spec/struct_spec.rb +293 -0
  85. data/spec/thin_http_server_spec.rb +141 -0
  86. data/spec/types_spec.rb +115 -0
  87. data/spec/union_spec.rb +203 -0
  88. data/spec/unix_socket_spec.rb +107 -0
  89. metadata +310 -0
@@ -0,0 +1,42 @@
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 'spec_helper'
21
+ require File.expand_path("#{File.dirname(__FILE__)}/binary_protocol_spec_shared")
22
+
23
+ if defined? Thrift::BinaryProtocolAccelerated
24
+
25
+ describe 'BinaryProtocolAccelerated' do
26
+ # since BinaryProtocolAccelerated should be directly equivalent to
27
+ # BinaryProtocol, we don't need any custom specs!
28
+ it_should_behave_like 'a binary protocol'
29
+
30
+ def protocol_class
31
+ Thrift::BinaryProtocolAccelerated
32
+ end
33
+
34
+ describe Thrift::BinaryProtocolAcceleratedFactory do
35
+ it "should create a BinaryProtocolAccelerated" do
36
+ Thrift::BinaryProtocolAcceleratedFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(Thrift::BinaryProtocolAccelerated)
37
+ end
38
+ end
39
+ end
40
+ else
41
+ puts "skipping BinaryProtocolAccelerated spec because it is not defined."
42
+ end
@@ -0,0 +1,66 @@
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 'spec_helper'
21
+ require File.expand_path("#{File.dirname(__FILE__)}/binary_protocol_spec_shared")
22
+
23
+ describe 'BinaryProtocol' do
24
+
25
+ it_should_behave_like 'a binary protocol'
26
+
27
+ def protocol_class
28
+ Thrift::BinaryProtocol
29
+ end
30
+
31
+ describe Thrift::BinaryProtocol do
32
+
33
+ before(:each) do
34
+ @trans = Thrift::MemoryBufferTransport.new
35
+ @prot = protocol_class.new(@trans)
36
+ end
37
+
38
+ it "should read a message header" do
39
+ @trans.write([protocol_class.const_get(:VERSION_1) | Thrift::MessageTypes::REPLY].pack('N'))
40
+ @trans.write([42].pack('N'))
41
+ @prot.should_receive(:read_string).and_return('testMessage')
42
+ @prot.read_message_begin.should == ['testMessage', Thrift::MessageTypes::REPLY, 42]
43
+ end
44
+
45
+ it "should raise an exception if the message header has the wrong version" do
46
+ @prot.should_receive(:read_i32).and_return(-1)
47
+ lambda { @prot.read_message_begin }.should raise_error(Thrift::ProtocolException, 'Missing version identifier') do |e|
48
+ e.type == Thrift::ProtocolException::BAD_VERSION
49
+ end
50
+ end
51
+
52
+ it "should raise an exception if the message header does not exist and strict_read is enabled" do
53
+ @prot.should_receive(:read_i32).and_return(42)
54
+ @prot.should_receive(:strict_read).and_return(true)
55
+ lambda { @prot.read_message_begin }.should raise_error(Thrift::ProtocolException, 'No version identifier, old protocol client?') do |e|
56
+ e.type == Thrift::ProtocolException::BAD_VERSION
57
+ end
58
+ end
59
+ end
60
+
61
+ describe Thrift::BinaryProtocolFactory do
62
+ it "should create a BinaryProtocol" do
63
+ Thrift::BinaryProtocolFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(Thrift::BinaryProtocol)
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,455 @@
1
+ # encoding: ascii-8bit
2
+ #
3
+ # Licensed to the Apache Software Foundation (ASF) under one
4
+ # or more contributor license agreements. See the NOTICE file
5
+ # distributed with this work for additional information
6
+ # regarding copyright ownership. The ASF licenses this file
7
+ # to you under the Apache License, Version 2.0 (the
8
+ # "License"); you may not use this file except in compliance
9
+ # with the License. You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing,
14
+ # software distributed under the License is distributed on an
15
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ # KIND, either express or implied. See the License for the
17
+ # specific language governing permissions and limitations
18
+ # under the License.
19
+ #
20
+
21
+ require 'spec_helper'
22
+
23
+ shared_examples_for 'a binary protocol' do
24
+ before(:each) do
25
+ @trans = Thrift::MemoryBufferTransport.new
26
+ @prot = protocol_class.new(@trans)
27
+ end
28
+
29
+ it "should define the proper VERSION_1, VERSION_MASK AND TYPE_MASK" do
30
+ protocol_class.const_get(:VERSION_MASK).should == 0xffff0000
31
+ protocol_class.const_get(:VERSION_1).should == 0x80010000
32
+ protocol_class.const_get(:TYPE_MASK).should == 0x000000ff
33
+ end
34
+
35
+ it "should make strict_read readable" do
36
+ @prot.strict_read.should eql(true)
37
+ end
38
+
39
+ it "should make strict_write readable" do
40
+ @prot.strict_write.should eql(true)
41
+ end
42
+
43
+ it "should write the message header" do
44
+ @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
45
+ @trans.read(@trans.available).should == [protocol_class.const_get(:VERSION_1) | Thrift::MessageTypes::CALL, "testMessage".size, "testMessage", 17].pack("NNa11N")
46
+ end
47
+
48
+ it "should write the message header without version when writes are not strict" do
49
+ @prot = protocol_class.new(@trans, true, false) # no strict write
50
+ @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
51
+ @trans.read(@trans.available).should == "\000\000\000\vtestMessage\001\000\000\000\021"
52
+ end
53
+
54
+ it "should write the message header with a version when writes are strict" do
55
+ @prot = protocol_class.new(@trans) # strict write
56
+ @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
57
+ @trans.read(@trans.available).should == "\200\001\000\001\000\000\000\vtestMessage\000\000\000\021"
58
+ end
59
+
60
+
61
+ # message footer is a noop
62
+
63
+ it "should write the field header" do
64
+ @prot.write_field_begin('foo', Thrift::Types::DOUBLE, 3)
65
+ @trans.read(@trans.available).should == [Thrift::Types::DOUBLE, 3].pack("cn")
66
+ end
67
+
68
+ # field footer is a noop
69
+
70
+ it "should write the STOP field" do
71
+ @prot.write_field_stop
72
+ @trans.read(1).should == "\000"
73
+ end
74
+
75
+ it "should write the map header" do
76
+ @prot.write_map_begin(Thrift::Types::STRING, Thrift::Types::LIST, 17)
77
+ @trans.read(@trans.available).should == [Thrift::Types::STRING, Thrift::Types::LIST, 17].pack("ccN");
78
+ end
79
+
80
+ # map footer is a noop
81
+
82
+ it "should write the list header" do
83
+ @prot.write_list_begin(Thrift::Types::I16, 42)
84
+ @trans.read(@trans.available).should == [Thrift::Types::I16, 42].pack("cN")
85
+ end
86
+
87
+ # list footer is a noop
88
+
89
+ it "should write the set header" do
90
+ @prot.write_set_begin(Thrift::Types::I16, 42)
91
+ @trans.read(@trans.available).should == [Thrift::Types::I16, 42].pack("cN")
92
+ end
93
+
94
+ it "should write a bool" do
95
+ @prot.write_bool(true)
96
+ @prot.write_bool(false)
97
+ @trans.read(@trans.available).should == "\001\000"
98
+ end
99
+
100
+ it "should treat a nil bool as false" do
101
+ @prot.write_bool(nil)
102
+ @trans.read(1).should == "\000"
103
+ end
104
+
105
+ it "should write a byte" do
106
+ # byte is small enough, let's check -128..127
107
+ (-128..127).each do |i|
108
+ @prot.write_byte(i)
109
+ @trans.read(1).should == [i].pack('c')
110
+ end
111
+ # handing it numbers out of signed range should clip
112
+ @trans.rspec_verify
113
+ (128..255).each do |i|
114
+ @prot.write_byte(i)
115
+ @trans.read(1).should == [i].pack('c')
116
+ end
117
+ # and lastly, a Bignum is going to error out
118
+ lambda { @prot.write_byte(2**65) }.should raise_error(RangeError)
119
+ end
120
+
121
+ it "should error gracefully when trying to write a nil byte" do
122
+ lambda { @prot.write_byte(nil) }.should raise_error
123
+ end
124
+
125
+ it "should write an i16" do
126
+ # try a random scattering of values
127
+ # include the signed i16 minimum/maximum
128
+ [-2**15, -1024, 17, 0, -10000, 1723, 2**15-1].each do |i|
129
+ @prot.write_i16(i)
130
+ end
131
+ # and try something out of signed range, it should clip
132
+ @prot.write_i16(2**15 + 5)
133
+
134
+ @trans.read(@trans.available).should == "\200\000\374\000\000\021\000\000\330\360\006\273\177\377\200\005"
135
+
136
+ # a Bignum should error
137
+ # lambda { @prot.write_i16(2**65) }.should raise_error(RangeError)
138
+ end
139
+
140
+ it "should error gracefully when trying to write a nil i16" do
141
+ lambda { @prot.write_i16(nil) }.should raise_error
142
+ end
143
+
144
+ it "should write an i32" do
145
+ # try a random scattering of values
146
+ # include the signed i32 minimum/maximum
147
+ [-2**31, -123123, -2532, -3, 0, 2351235, 12331, 2**31-1].each do |i|
148
+ @prot.write_i32(i)
149
+ end
150
+ # try something out of signed range, it should clip
151
+ @trans.read(@trans.available).should == "\200\000\000\000" + "\377\376\037\r" + "\377\377\366\034" + "\377\377\377\375" + "\000\000\000\000" + "\000#\340\203" + "\000\0000+" + "\177\377\377\377"
152
+ [2 ** 31 + 5, 2 ** 65 + 5].each do |i|
153
+ lambda { @prot.write_i32(i) }.should raise_error(RangeError)
154
+ end
155
+ end
156
+
157
+ it "should error gracefully when trying to write a nil i32" do
158
+ lambda { @prot.write_i32(nil) }.should raise_error
159
+ end
160
+
161
+ it "should write an i64" do
162
+ # try a random scattering of values
163
+ # try the signed i64 minimum/maximum
164
+ [-2**63, -12356123612323, -23512351, -234, 0, 1231, 2351236, 12361236213, 2**63-1].each do |i|
165
+ @prot.write_i64(i)
166
+ end
167
+ # try something out of signed range, it should clip
168
+ @trans.read(@trans.available).should == ["\200\000\000\000\000\000\000\000",
169
+ "\377\377\364\303\035\244+]",
170
+ "\377\377\377\377\376\231:\341",
171
+ "\377\377\377\377\377\377\377\026",
172
+ "\000\000\000\000\000\000\000\000",
173
+ "\000\000\000\000\000\000\004\317",
174
+ "\000\000\000\000\000#\340\204",
175
+ "\000\000\000\002\340\311~\365",
176
+ "\177\377\377\377\377\377\377\377"].join("")
177
+ lambda { @prot.write_i64(2 ** 65 + 5) }.should raise_error(RangeError)
178
+ end
179
+
180
+ it "should error gracefully when trying to write a nil i64" do
181
+ lambda { @prot.write_i64(nil) }.should raise_error
182
+ end
183
+
184
+ it "should write a double" do
185
+ # try a random scattering of values, including min/max
186
+ values = [Float::MIN,-1231.15325, -123123.23, -23.23515123, 0, 12351.1325, 523.23, Float::MAX]
187
+ values.each do |f|
188
+ @prot.write_double(f)
189
+ @trans.read(@trans.available).should == [f].pack("G")
190
+ end
191
+ end
192
+
193
+ it "should error gracefully when trying to write a nil double" do
194
+ lambda { @prot.write_double(nil) }.should raise_error
195
+ end
196
+
197
+ if RUBY_VERSION >= '1.9'
198
+ it 'should write a string' do
199
+ str = 'abc'
200
+ @prot.write_string(str)
201
+ a = @trans.read(@trans.available)
202
+ a.encoding.should == Encoding::BINARY
203
+ a.unpack('C*').should == [0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63]
204
+ end
205
+
206
+ it 'should write a string with unicode characters' do
207
+ str = "abc \u20AC \u20AD".encode('UTF-8')
208
+ @prot.write_string(str)
209
+ a = @trans.read(@trans.available)
210
+ a.encoding.should == Encoding::BINARY
211
+ a.unpack('C*').should == [0x00, 0x00, 0x00, 0x0B, 0x61, 0x62, 0x63, 0x20,
212
+ 0xE2, 0x82, 0xAC, 0x20, 0xE2, 0x82, 0xAD]
213
+ end
214
+
215
+ it 'should write should write a string with unicode characters and transcoding' do
216
+ str = "abc \u20AC".encode('ISO-8859-15')
217
+ @prot.write_string(str)
218
+ a = @trans.read(@trans.available)
219
+ a.encoding.should == Encoding::BINARY
220
+ a.unpack('C*').should == [0x00, 0x00, 0x00, 0x07, 0x61, 0x62, 0x63, 0x20, 0xE2, 0x82, 0xAC]
221
+ end
222
+
223
+ it 'should write a binary string' do
224
+ buffer = [0, 1, 2, 3].pack('C*')
225
+ @prot.write_binary(buffer)
226
+ a = @trans.read(@trans.available)
227
+ a.encoding.should == Encoding::BINARY
228
+ a.unpack('C*').should == [0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03]
229
+ end
230
+ else
231
+ it 'should write a string' do
232
+ str = 'abc'
233
+ @prot.write_string(str)
234
+ a = @trans.read(@trans.available)
235
+ a.unpack('C*').should == [0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63]
236
+ end
237
+
238
+ it 'should write a binary string' do
239
+ buffer = [0, 1, 2, 3].pack('C*')
240
+ @prot.write_binary(buffer)
241
+ a = @trans.read(@trans.available)
242
+ a.unpack('C*').should == [0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03]
243
+ end
244
+ end
245
+
246
+ it "should error gracefully when trying to write a nil string" do
247
+ lambda { @prot.write_string(nil) }.should raise_error
248
+ end
249
+
250
+ it "should write the message header without version when writes are not strict" do
251
+ @prot = protocol_class.new(@trans, true, false) # no strict write
252
+ @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
253
+ @trans.read(@trans.available).should == "\000\000\000\vtestMessage\001\000\000\000\021"
254
+ end
255
+
256
+ it "should write the message header with a version when writes are strict" do
257
+ @prot = protocol_class.new(@trans) # strict write
258
+ @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
259
+ @trans.read(@trans.available).should == "\200\001\000\001\000\000\000\vtestMessage\000\000\000\021"
260
+ end
261
+
262
+ # message footer is a noop
263
+
264
+ it "should read a field header" do
265
+ @trans.write([Thrift::Types::STRING, 3].pack("cn"))
266
+ @prot.read_field_begin.should == [nil, Thrift::Types::STRING, 3]
267
+ end
268
+
269
+ # field footer is a noop
270
+
271
+ it "should read a stop field" do
272
+ @trans.write([Thrift::Types::STOP].pack("c"));
273
+ @prot.read_field_begin.should == [nil, Thrift::Types::STOP, 0]
274
+ end
275
+
276
+ it "should read a map header" do
277
+ @trans.write([Thrift::Types::DOUBLE, Thrift::Types::I64, 42].pack("ccN"))
278
+ @prot.read_map_begin.should == [Thrift::Types::DOUBLE, Thrift::Types::I64, 42]
279
+ end
280
+
281
+ # map footer is a noop
282
+
283
+ it "should read a list header" do
284
+ @trans.write([Thrift::Types::STRING, 17].pack("cN"))
285
+ @prot.read_list_begin.should == [Thrift::Types::STRING, 17]
286
+ end
287
+
288
+ # list footer is a noop
289
+
290
+ it "should read a set header" do
291
+ @trans.write([Thrift::Types::STRING, 17].pack("cN"))
292
+ @prot.read_set_begin.should == [Thrift::Types::STRING, 17]
293
+ end
294
+
295
+ # set footer is a noop
296
+
297
+ it "should read a bool" do
298
+ @trans.write("\001\000");
299
+ @prot.read_bool.should == true
300
+ @prot.read_bool.should == false
301
+ end
302
+
303
+ it "should read a byte" do
304
+ [-128, -57, -3, 0, 17, 24, 127].each do |i|
305
+ @trans.write([i].pack("c"))
306
+ @prot.read_byte.should == i
307
+ end
308
+ end
309
+
310
+ it "should read an i16" do
311
+ # try a scattering of values, including min/max
312
+ [-2**15, -5237, -353, 0, 1527, 2234, 2**15-1].each do |i|
313
+ @trans.write([i].pack("n"));
314
+ @prot.read_i16.should == i
315
+ end
316
+ end
317
+
318
+ it "should read an i32" do
319
+ # try a scattering of values, including min/max
320
+ [-2**31, -235125, -6236, 0, 2351, 123123, 2**31-1].each do |i|
321
+ @trans.write([i].pack("N"))
322
+ @prot.read_i32.should == i
323
+ end
324
+ end
325
+
326
+ it "should read an i64" do
327
+ # try a scattering of values, including min/max
328
+ [-2**63, -123512312, -6346, 0, 32, 2346322323, 2**63-1].each do |i|
329
+ @trans.write([i >> 32, i & 0xFFFFFFFF].pack("NN"))
330
+ @prot.read_i64.should == i
331
+ end
332
+ end
333
+
334
+ it "should read a double" do
335
+ # try a random scattering of values, including min/max
336
+ [Float::MIN, -231231.12351, -323.233513, 0, 123.2351235, 2351235.12351235, Float::MAX].each do |f|
337
+ @trans.write([f].pack("G"));
338
+ @prot.read_double.should == f
339
+ end
340
+ end
341
+
342
+ if RUBY_VERSION >= '1.9'
343
+ it 'should read a string' do
344
+ # i32 of value 3, followed by three characters/UTF-8 bytes 'a', 'b', 'c'
345
+ buffer = [0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63].pack('C*')
346
+ @trans.write(buffer)
347
+ a = @prot.read_string
348
+ a.should == 'abc'.encode('UTF-8')
349
+ a.encoding.should == Encoding::UTF_8
350
+ end
351
+
352
+ it 'should read a string containing unicode characters from UTF-8 encoded buffer' do
353
+ # i32 of value 3, followed by one character U+20AC made up of three bytes
354
+ buffer = [0x00, 0x00, 0x00, 0x03, 0xE2, 0x82, 0xAC].pack('C*')
355
+ @trans.write(buffer)
356
+ a = @prot.read_string
357
+ a.should == "\u20AC".encode('UTF-8')
358
+ a.encoding.should == Encoding::UTF_8
359
+ end
360
+
361
+ it 'should read a binary string' do
362
+ buffer = [0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03].pack('C*')
363
+ @trans.write(buffer)
364
+ a = @prot.read_binary
365
+ a.should == [0x00, 0x01, 0x02, 0x03].pack('C*')
366
+ a.encoding.should == Encoding::BINARY
367
+ end
368
+ else
369
+ it 'should read a string' do
370
+ # i32 of value 3, followed by three characters/UTF-8 bytes 'a', 'b', 'c'
371
+ buffer = [0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63].pack('C*')
372
+ @trans.write(buffer)
373
+ @prot.read_string.should == 'abc'
374
+ end
375
+
376
+ it 'should read a binary string' do
377
+ buffer = [0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03].pack('C*')
378
+ @trans.write(buffer)
379
+ a = @prot.read_binary
380
+ a.should == [0x00, 0x01, 0x02, 0x03].pack('C*')
381
+ end
382
+ end
383
+
384
+ it "should perform a complete rpc with no args or return" do
385
+ srv_test(
386
+ proc {|client| client.send_voidMethod()},
387
+ proc {|client| client.recv_voidMethod.should == nil}
388
+ )
389
+ end
390
+
391
+ it "should perform a complete rpc with a primitive return type" do
392
+ srv_test(
393
+ proc {|client| client.send_primitiveMethod()},
394
+ proc {|client| client.recv_primitiveMethod.should == 1}
395
+ )
396
+ end
397
+
398
+ it "should perform a complete rpc with a struct return type" do
399
+ srv_test(
400
+ proc {|client| client.send_structMethod()},
401
+ proc {|client|
402
+ result = client.recv_structMethod
403
+ result.set_byte_map = nil
404
+ result.map_byte_map = nil
405
+ result.should == Fixtures::COMPACT_PROTOCOL_TEST_STRUCT
406
+ }
407
+ )
408
+ end
409
+
410
+ def get_socket_connection
411
+ server = Thrift::ServerSocket.new("localhost", 9090)
412
+ server.listen
413
+
414
+ clientside = Thrift::Socket.new("localhost", 9090)
415
+ clientside.open
416
+ serverside = server.accept
417
+ [clientside, serverside, server]
418
+ end
419
+
420
+ def srv_test(firstblock, secondblock)
421
+ clientside, serverside, server = get_socket_connection
422
+
423
+ clientproto = protocol_class.new(clientside)
424
+ serverproto = protocol_class.new(serverside)
425
+
426
+ processor = Srv::Processor.new(SrvHandler.new)
427
+
428
+ client = Srv::Client.new(clientproto, clientproto)
429
+
430
+ # first block
431
+ firstblock.call(client)
432
+
433
+ processor.process(serverproto, serverproto)
434
+
435
+ # second block
436
+ secondblock.call(client)
437
+ ensure
438
+ clientside.close
439
+ serverside.close
440
+ server.close
441
+ end
442
+
443
+ class SrvHandler
444
+ def voidMethod()
445
+ end
446
+
447
+ def primitiveMethod
448
+ 1
449
+ end
450
+
451
+ def structMethod
452
+ Fixtures::COMPACT_PROTOCOL_TEST_STRUCT
453
+ end
454
+ end
455
+ end