sessionm-thrift 0.8.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. data/CHANGELOG +1 -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 +441 -0
  9. data/ext/binary_protocol_accelerated.h +20 -0
  10. data/ext/compact_protocol.c +618 -0
  11. data/ext/compact_protocol.h +20 -0
  12. data/ext/constants.h +96 -0
  13. data/ext/extconf.rb +30 -0
  14. data/ext/macros.h +41 -0
  15. data/ext/memory_buffer.c +131 -0
  16. data/ext/memory_buffer.h +20 -0
  17. data/ext/protocol.c +185 -0
  18. data/ext/protocol.h +20 -0
  19. data/ext/strlcpy.c +41 -0
  20. data/ext/strlcpy.h +30 -0
  21. data/ext/struct.c +691 -0
  22. data/ext/struct.h +25 -0
  23. data/ext/thrift_native.c +196 -0
  24. data/lib/thrift.rb +64 -0
  25. data/lib/thrift/client.rb +62 -0
  26. data/lib/thrift/core_ext.rb +23 -0
  27. data/lib/thrift/core_ext/fixnum.rb +29 -0
  28. data/lib/thrift/exceptions.rb +84 -0
  29. data/lib/thrift/processor.rb +57 -0
  30. data/lib/thrift/protocol/base_protocol.rb +290 -0
  31. data/lib/thrift/protocol/binary_protocol.rb +229 -0
  32. data/lib/thrift/protocol/binary_protocol_accelerated.rb +39 -0
  33. data/lib/thrift/protocol/compact_protocol.rb +426 -0
  34. data/lib/thrift/serializer/deserializer.rb +33 -0
  35. data/lib/thrift/serializer/serializer.rb +34 -0
  36. data/lib/thrift/server/base_server.rb +31 -0
  37. data/lib/thrift/server/mongrel_http_server.rb +58 -0
  38. data/lib/thrift/server/nonblocking_server.rb +305 -0
  39. data/lib/thrift/server/simple_server.rb +43 -0
  40. data/lib/thrift/server/thread_pool_server.rb +75 -0
  41. data/lib/thrift/server/threaded_server.rb +47 -0
  42. data/lib/thrift/struct.rb +237 -0
  43. data/lib/thrift/struct_union.rb +192 -0
  44. data/lib/thrift/thrift_native.rb +24 -0
  45. data/lib/thrift/transport/base_server_transport.rb +37 -0
  46. data/lib/thrift/transport/base_transport.rb +107 -0
  47. data/lib/thrift/transport/buffered_transport.rb +108 -0
  48. data/lib/thrift/transport/framed_transport.rb +116 -0
  49. data/lib/thrift/transport/http_client_transport.rb +51 -0
  50. data/lib/thrift/transport/io_stream_transport.rb +39 -0
  51. data/lib/thrift/transport/memory_buffer_transport.rb +125 -0
  52. data/lib/thrift/transport/server_socket.rb +63 -0
  53. data/lib/thrift/transport/socket.rb +137 -0
  54. data/lib/thrift/transport/unix_server_socket.rb +60 -0
  55. data/lib/thrift/transport/unix_socket.rb +40 -0
  56. data/lib/thrift/types.rb +101 -0
  57. data/lib/thrift/union.rb +179 -0
  58. data/spec/ThriftSpec.thrift +132 -0
  59. data/spec/base_protocol_spec.rb +160 -0
  60. data/spec/base_transport_spec.rb +351 -0
  61. data/spec/binary_protocol_accelerated_spec.rb +46 -0
  62. data/spec/binary_protocol_spec.rb +61 -0
  63. data/spec/binary_protocol_spec_shared.rb +375 -0
  64. data/spec/client_spec.rb +100 -0
  65. data/spec/compact_protocol_spec.rb +144 -0
  66. data/spec/exception_spec.rb +142 -0
  67. data/spec/http_client_spec.rb +64 -0
  68. data/spec/mongrel_http_server_spec.rb +117 -0
  69. data/spec/nonblocking_server_spec.rb +265 -0
  70. data/spec/processor_spec.rb +83 -0
  71. data/spec/serializer_spec.rb +69 -0
  72. data/spec/server_socket_spec.rb +80 -0
  73. data/spec/server_spec.rb +159 -0
  74. data/spec/socket_spec.rb +61 -0
  75. data/spec/socket_spec_shared.rb +104 -0
  76. data/spec/spec_helper.rb +58 -0
  77. data/spec/struct_spec.rb +295 -0
  78. data/spec/types_spec.rb +116 -0
  79. data/spec/union_spec.rb +193 -0
  80. data/spec/unix_socket_spec.rb +108 -0
  81. metadata +247 -0
@@ -0,0 +1,351 @@
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.expand_path("#{File.dirname(__FILE__)}/spec_helper")
21
+
22
+ class ThriftBaseTransportSpec < Spec::ExampleGroup
23
+ include Thrift
24
+
25
+ describe TransportException do
26
+ it "should make type accessible" do
27
+ exc = TransportException.new(TransportException::ALREADY_OPEN, "msg")
28
+ exc.type.should == TransportException::ALREADY_OPEN
29
+ exc.message.should == "msg"
30
+ end
31
+ end
32
+
33
+ describe BaseTransport do
34
+ it "should read the specified size" do
35
+ transport = BaseTransport.new
36
+ transport.should_receive(:read).with(40).ordered.and_return("10 letters")
37
+ transport.should_receive(:read).with(30).ordered.and_return("fifteen letters")
38
+ transport.should_receive(:read).with(15).ordered.and_return("more characters")
39
+ transport.read_all(40).should == "10 lettersfifteen lettersmore characters"
40
+ end
41
+
42
+ it "should stub out the rest of the methods" do
43
+ # can't test for stubbiness, so just make sure they're defined
44
+ [:open?, :open, :close, :read, :write, :flush].each do |sym|
45
+ BaseTransport.method_defined?(sym).should be_true
46
+ end
47
+ end
48
+
49
+ it "should alias << to write" do
50
+ BaseTransport.instance_method(:<<).should == BaseTransport.instance_method(:write)
51
+ end
52
+ end
53
+
54
+ describe BaseServerTransport do
55
+ it "should stub out its methods" do
56
+ [:listen, :accept, :close].each do |sym|
57
+ BaseServerTransport.method_defined?(sym).should be_true
58
+ end
59
+ end
60
+ end
61
+
62
+ describe BaseTransportFactory do
63
+ it "should return the transport it's given" do
64
+ transport = mock("Transport")
65
+ BaseTransportFactory.new.get_transport(transport).should eql(transport)
66
+ end
67
+ end
68
+
69
+ describe BufferedTransport do
70
+ it "should pass through everything but write/flush/read" do
71
+ trans = mock("Transport")
72
+ trans.should_receive(:open?).ordered.and_return("+ open?")
73
+ trans.should_receive(:open).ordered.and_return("+ open")
74
+ trans.should_receive(:flush).ordered # from the close
75
+ trans.should_receive(:close).ordered.and_return("+ close")
76
+ btrans = BufferedTransport.new(trans)
77
+ btrans.open?.should == "+ open?"
78
+ btrans.open.should == "+ open"
79
+ btrans.close.should == "+ close"
80
+ end
81
+
82
+ it "should buffer reads in chunks of #{BufferedTransport::DEFAULT_BUFFER}" do
83
+ trans = mock("Transport")
84
+ trans.should_receive(:read).with(BufferedTransport::DEFAULT_BUFFER).and_return("lorum ipsum dolor emet")
85
+ btrans = BufferedTransport.new(trans)
86
+ btrans.read(6).should == "lorum "
87
+ btrans.read(6).should == "ipsum "
88
+ btrans.read(6).should == "dolor "
89
+ btrans.read(6).should == "emet"
90
+ end
91
+
92
+ it "should buffer writes and send them on flush" do
93
+ trans = mock("Transport")
94
+ btrans = BufferedTransport.new(trans)
95
+ btrans.write("one/")
96
+ btrans.write("two/")
97
+ btrans.write("three/")
98
+ trans.should_receive(:write).with("one/two/three/").ordered
99
+ trans.should_receive(:flush).ordered
100
+ btrans.flush
101
+ end
102
+
103
+ it "should only send buffered data once" do
104
+ trans = mock("Transport")
105
+ btrans = BufferedTransport.new(trans)
106
+ btrans.write("one/")
107
+ btrans.write("two/")
108
+ btrans.write("three/")
109
+ trans.should_receive(:write).with("one/two/three/")
110
+ trans.stub!(:flush)
111
+ btrans.flush
112
+ # Nothing to flush with no data
113
+ btrans.flush
114
+ end
115
+
116
+ it "should flush on close" do
117
+ trans = mock("Transport")
118
+ trans.should_receive(:close)
119
+ btrans = BufferedTransport.new(trans)
120
+ btrans.should_receive(:flush)
121
+ btrans.close
122
+ end
123
+
124
+ it "should not write to socket if there's no data" do
125
+ trans = mock("Transport")
126
+ trans.should_receive(:flush)
127
+ btrans = BufferedTransport.new(trans)
128
+ btrans.flush
129
+ end
130
+ end
131
+
132
+ describe BufferedTransportFactory do
133
+ it "should wrap the given transport in a BufferedTransport" do
134
+ trans = mock("Transport")
135
+ btrans = mock("BufferedTransport")
136
+ BufferedTransport.should_receive(:new).with(trans).and_return(btrans)
137
+ BufferedTransportFactory.new.get_transport(trans).should == btrans
138
+ end
139
+ end
140
+
141
+ describe FramedTransport do
142
+ before(:each) do
143
+ @trans = mock("Transport")
144
+ end
145
+
146
+ it "should pass through open?/open/close" do
147
+ ftrans = FramedTransport.new(@trans)
148
+ @trans.should_receive(:open?).ordered.and_return("+ open?")
149
+ @trans.should_receive(:open).ordered.and_return("+ open")
150
+ @trans.should_receive(:close).ordered.and_return("+ close")
151
+ ftrans.open?.should == "+ open?"
152
+ ftrans.open.should == "+ open"
153
+ ftrans.close.should == "+ close"
154
+ end
155
+
156
+ it "should pass through read when read is turned off" do
157
+ ftrans = FramedTransport.new(@trans, false, true)
158
+ @trans.should_receive(:read).with(17).ordered.and_return("+ read")
159
+ ftrans.read(17).should == "+ read"
160
+ end
161
+
162
+ it "should pass through write/flush when write is turned off" do
163
+ ftrans = FramedTransport.new(@trans, true, false)
164
+ @trans.should_receive(:write).with("foo").ordered.and_return("+ write")
165
+ @trans.should_receive(:flush).ordered.and_return("+ flush")
166
+ ftrans.write("foo").should == "+ write"
167
+ ftrans.flush.should == "+ flush"
168
+ end
169
+
170
+ it "should return a full frame if asked for >= the frame's length" do
171
+ frame = "this is a frame"
172
+ @trans.should_receive(:read_all).with(4).and_return("\000\000\000\017")
173
+ @trans.should_receive(:read_all).with(frame.length).and_return(frame)
174
+ FramedTransport.new(@trans).read(frame.length + 10).should == frame
175
+ end
176
+
177
+ it "should return slices of the frame when asked for < the frame's length" do
178
+ frame = "this is a frame"
179
+ @trans.should_receive(:read_all).with(4).and_return("\000\000\000\017")
180
+ @trans.should_receive(:read_all).with(frame.length).and_return(frame)
181
+ ftrans = FramedTransport.new(@trans)
182
+ ftrans.read(4).should == "this"
183
+ ftrans.read(4).should == " is "
184
+ ftrans.read(16).should == "a frame"
185
+ end
186
+
187
+ it "should return nothing if asked for <= 0" do
188
+ FramedTransport.new(@trans).read(-2).should == ""
189
+ end
190
+
191
+ it "should pull a new frame when the first is exhausted" do
192
+ frame = "this is a frame"
193
+ frame2 = "yet another frame"
194
+ @trans.should_receive(:read_all).with(4).and_return("\000\000\000\017", "\000\000\000\021")
195
+ @trans.should_receive(:read_all).with(frame.length).and_return(frame)
196
+ @trans.should_receive(:read_all).with(frame2.length).and_return(frame2)
197
+ ftrans = FramedTransport.new(@trans)
198
+ ftrans.read(4).should == "this"
199
+ ftrans.read(8).should == " is a fr"
200
+ ftrans.read(6).should == "ame"
201
+ ftrans.read(4).should == "yet "
202
+ ftrans.read(16).should == "another frame"
203
+ end
204
+
205
+ it "should buffer writes" do
206
+ ftrans = FramedTransport.new(@trans)
207
+ @trans.should_not_receive(:write)
208
+ ftrans.write("foo")
209
+ ftrans.write("bar")
210
+ ftrans.write("this is a frame")
211
+ end
212
+
213
+ it "should write slices of the buffer" do
214
+ ftrans = FramedTransport.new(@trans)
215
+ ftrans.write("foobar", 3)
216
+ ftrans.write("barfoo", 1)
217
+ @trans.stub!(:flush)
218
+ @trans.should_receive(:write).with("\000\000\000\004foob")
219
+ ftrans.flush
220
+ end
221
+
222
+ it "should flush frames with a 4-byte header" do
223
+ ftrans = FramedTransport.new(@trans)
224
+ @trans.should_receive(:write).with("\000\000\000\035one/two/three/this is a frame").ordered
225
+ @trans.should_receive(:flush).ordered
226
+ ftrans.write("one/")
227
+ ftrans.write("two/")
228
+ ftrans.write("three/")
229
+ ftrans.write("this is a frame")
230
+ ftrans.flush
231
+ end
232
+
233
+ it "should not flush the same buffered data twice" do
234
+ ftrans = FramedTransport.new(@trans)
235
+ @trans.should_receive(:write).with("\000\000\000\007foo/bar")
236
+ @trans.stub!(:flush)
237
+ ftrans.write("foo")
238
+ ftrans.write("/bar")
239
+ ftrans.flush
240
+ @trans.should_receive(:write).with("\000\000\000\000")
241
+ ftrans.flush
242
+ end
243
+ end
244
+
245
+ describe FramedTransportFactory do
246
+ it "should wrap the given transport in a FramedTransport" do
247
+ trans = mock("Transport")
248
+ FramedTransport.should_receive(:new).with(trans)
249
+ FramedTransportFactory.new.get_transport(trans)
250
+ end
251
+ end
252
+
253
+ describe MemoryBufferTransport do
254
+ before(:each) do
255
+ @buffer = MemoryBufferTransport.new
256
+ end
257
+
258
+ it "should accept a buffer on input and use it directly" do
259
+ s = "this is a test"
260
+ @buffer = MemoryBufferTransport.new(s)
261
+ @buffer.read(4).should == "this"
262
+ s.slice!(-4..-1)
263
+ @buffer.read(@buffer.available).should == " is a "
264
+ end
265
+
266
+ it "should always remain open" do
267
+ @buffer.should be_open
268
+ @buffer.close
269
+ @buffer.should be_open
270
+ end
271
+
272
+ it "should respond to peek and available" do
273
+ @buffer.write "some data"
274
+ @buffer.peek.should be_true
275
+ @buffer.available.should == 9
276
+ @buffer.read(4)
277
+ @buffer.peek.should be_true
278
+ @buffer.available.should == 5
279
+ @buffer.read(5)
280
+ @buffer.peek.should be_false
281
+ @buffer.available.should == 0
282
+ end
283
+
284
+ it "should be able to reset the buffer" do
285
+ @buffer.write "test data"
286
+ @buffer.reset_buffer("foobar")
287
+ @buffer.available.should == 6
288
+ @buffer.read(@buffer.available).should == "foobar"
289
+ @buffer.reset_buffer
290
+ @buffer.available.should == 0
291
+ end
292
+
293
+ it "should copy the given string when resetting the buffer" do
294
+ s = "this is a test"
295
+ @buffer.reset_buffer(s)
296
+ @buffer.available.should == 14
297
+ @buffer.read(10)
298
+ @buffer.available.should == 4
299
+ s.should == "this is a test"
300
+ end
301
+
302
+ it "should return from read what was given in write" do
303
+ @buffer.write "test data"
304
+ @buffer.read(4).should == "test"
305
+ @buffer.read(@buffer.available).should == " data"
306
+ @buffer.write "foo"
307
+ @buffer.write " bar"
308
+ @buffer.read(@buffer.available).should == "foo bar"
309
+ end
310
+
311
+ it "should throw an EOFError when there isn't enough data in the buffer" do
312
+ @buffer.reset_buffer("")
313
+ lambda{@buffer.read(1)}.should raise_error(EOFError)
314
+
315
+ @buffer.reset_buffer("1234")
316
+ lambda{@buffer.read(5)}.should raise_error(EOFError)
317
+ end
318
+ end
319
+
320
+ describe IOStreamTransport do
321
+ before(:each) do
322
+ @input = mock("Input", :closed? => false)
323
+ @output = mock("Output", :closed? => false)
324
+ @trans = IOStreamTransport.new(@input, @output)
325
+ end
326
+
327
+ it "should be open as long as both input or output are open" do
328
+ @trans.should be_open
329
+ @input.stub!(:closed?).and_return(true)
330
+ @trans.should be_open
331
+ @input.stub!(:closed?).and_return(false)
332
+ @output.stub!(:closed?).and_return(true)
333
+ @trans.should be_open
334
+ @input.stub!(:closed?).and_return(true)
335
+ @trans.should_not be_open
336
+ end
337
+
338
+ it "should pass through read/write to input/output" do
339
+ @input.should_receive(:read).with(17).and_return("+ read")
340
+ @output.should_receive(:write).with("foobar").and_return("+ write")
341
+ @trans.read(17).should == "+ read"
342
+ @trans.write("foobar").should == "+ write"
343
+ end
344
+
345
+ it "should close both input and output when closed" do
346
+ @input.should_receive(:close)
347
+ @output.should_receive(:close)
348
+ @trans.close
349
+ end
350
+ end
351
+ end
@@ -0,0 +1,46 @@
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.expand_path("#{File.dirname(__FILE__)}/spec_helper")
21
+ require File.expand_path("#{File.dirname(__FILE__)}/binary_protocol_spec_shared")
22
+
23
+ if defined? Thrift::BinaryProtocolAccelerated
24
+
25
+ class ThriftBinaryProtocolAcceleratedSpec < Spec::ExampleGroup
26
+ include Thrift
27
+
28
+ describe Thrift::BinaryProtocolAccelerated do
29
+ # since BinaryProtocolAccelerated should be directly equivalent to
30
+ # BinaryProtocol, we don't need any custom specs!
31
+ it_should_behave_like 'a binary protocol'
32
+
33
+ def protocol_class
34
+ BinaryProtocolAccelerated
35
+ end
36
+ end
37
+
38
+ describe BinaryProtocolAcceleratedFactory do
39
+ it "should create a BinaryProtocolAccelerated" do
40
+ BinaryProtocolAcceleratedFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(BinaryProtocolAccelerated)
41
+ end
42
+ end
43
+ end
44
+ else
45
+ puts "skipping BinaryProtocolAccelerated spec because it is not defined."
46
+ end
@@ -0,0 +1,61 @@
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.expand_path("#{File.dirname(__FILE__)}/spec_helper")
21
+ require File.expand_path("#{File.dirname(__FILE__)}/binary_protocol_spec_shared")
22
+
23
+ class ThriftBinaryProtocolSpec < Spec::ExampleGroup
24
+ include Thrift
25
+
26
+ describe BinaryProtocol do
27
+ it_should_behave_like 'a binary protocol'
28
+
29
+ def protocol_class
30
+ BinaryProtocol
31
+ end
32
+
33
+ it "should read a message header" do
34
+ @trans.write([protocol_class.const_get(:VERSION_1) | Thrift::MessageTypes::REPLY].pack('N'))
35
+ @trans.write([42].pack('N'))
36
+ @prot.should_receive(:read_string).and_return('testMessage')
37
+ @prot.read_message_begin.should == ['testMessage', Thrift::MessageTypes::REPLY, 42]
38
+ end
39
+
40
+ it "should raise an exception if the message header has the wrong version" do
41
+ @prot.should_receive(:read_i32).and_return(-1)
42
+ lambda { @prot.read_message_begin }.should raise_error(Thrift::ProtocolException, 'Missing version identifier') do |e|
43
+ e.type == Thrift::ProtocolException::BAD_VERSION
44
+ end
45
+ end
46
+
47
+ it "should raise an exception if the message header does not exist and strict_read is enabled" do
48
+ @prot.should_receive(:read_i32).and_return(42)
49
+ @prot.should_receive(:strict_read).and_return(true)
50
+ lambda { @prot.read_message_begin }.should raise_error(Thrift::ProtocolException, 'No version identifier, old protocol client?') do |e|
51
+ e.type == Thrift::ProtocolException::BAD_VERSION
52
+ end
53
+ end
54
+ end
55
+
56
+ describe BinaryProtocolFactory do
57
+ it "should create a BinaryProtocol" do
58
+ BinaryProtocolFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(BinaryProtocol)
59
+ end
60
+ end
61
+ end