sessionm-thrift 0.8.0.1

Sign up to get free protection for your applications and to get access to all the features.
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,265 @@
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 ThriftNonblockingServerSpec < Spec::ExampleGroup
23
+ include Thrift
24
+ include SpecNamespace
25
+
26
+ class Handler
27
+ def initialize
28
+ @queue = Queue.new
29
+ end
30
+
31
+ attr_accessor :server
32
+
33
+ def greeting(english)
34
+ if english
35
+ SpecNamespace::Hello.new
36
+ else
37
+ SpecNamespace::Hello.new(:greeting => "Aloha!")
38
+ end
39
+ end
40
+
41
+ def block
42
+ @queue.pop
43
+ end
44
+
45
+ def unblock(n)
46
+ n.times { @queue.push true }
47
+ end
48
+
49
+ def sleep(time)
50
+ Kernel.sleep time
51
+ end
52
+
53
+ def shutdown
54
+ @server.shutdown(0, false)
55
+ end
56
+ end
57
+
58
+ class SpecTransport < BaseTransport
59
+ def initialize(transport, queue)
60
+ @transport = transport
61
+ @queue = queue
62
+ @flushed = false
63
+ end
64
+
65
+ def open?
66
+ @transport.open?
67
+ end
68
+
69
+ def open
70
+ @transport.open
71
+ end
72
+
73
+ def close
74
+ @transport.close
75
+ end
76
+
77
+ def read(sz)
78
+ @transport.read(sz)
79
+ end
80
+
81
+ def write(buf,sz=nil)
82
+ @transport.write(buf, sz)
83
+ end
84
+
85
+ def flush
86
+ @queue.push :flushed unless @flushed or @queue.nil?
87
+ @flushed = true
88
+ @transport.flush
89
+ end
90
+ end
91
+
92
+ class SpecServerSocket < ServerSocket
93
+ def initialize(host, port, queue)
94
+ super(host, port)
95
+ @queue = queue
96
+ end
97
+
98
+ def listen
99
+ super
100
+ @queue.push :listen
101
+ end
102
+ end
103
+
104
+ describe Thrift::NonblockingServer do
105
+ before(:each) do
106
+ @port = 43251
107
+ handler = Handler.new
108
+ processor = NonblockingService::Processor.new(handler)
109
+ queue = Queue.new
110
+ @transport = SpecServerSocket.new('localhost', @port, queue)
111
+ transport_factory = FramedTransportFactory.new
112
+ logger = Logger.new(STDERR)
113
+ logger.level = Logger::WARN
114
+ @server = NonblockingServer.new(processor, @transport, transport_factory, nil, 5, logger)
115
+ handler.server = @server
116
+ @server_thread = Thread.new(Thread.current) do |master_thread|
117
+ begin
118
+ @server.serve
119
+ rescue => e
120
+ p e
121
+ puts e.backtrace * "\n"
122
+ master_thread.raise e
123
+ end
124
+ end
125
+ queue.pop
126
+
127
+ @clients = []
128
+ @catch_exceptions = false
129
+ end
130
+
131
+ after(:each) do
132
+ @clients.each { |client, trans| trans.close }
133
+ # @server.shutdown(1)
134
+ @server_thread.kill
135
+ @transport.close
136
+ end
137
+
138
+ def setup_client(queue = nil)
139
+ transport = SpecTransport.new(FramedTransport.new(Socket.new('localhost', @port)), queue)
140
+ protocol = BinaryProtocol.new(transport)
141
+ client = NonblockingService::Client.new(protocol)
142
+ transport.open
143
+ @clients << [client, transport]
144
+ client
145
+ end
146
+
147
+ def setup_client_thread(result)
148
+ queue = Queue.new
149
+ Thread.new do
150
+ begin
151
+ client = setup_client
152
+ while (cmd = queue.pop)
153
+ msg, *args = cmd
154
+ case msg
155
+ when :block
156
+ result << client.block
157
+ when :unblock
158
+ client.unblock(args.first)
159
+ when :hello
160
+ result << client.greeting(true) # ignore result
161
+ when :sleep
162
+ client.sleep(args[0] || 0.5)
163
+ result << :slept
164
+ when :shutdown
165
+ client.shutdown
166
+ when :exit
167
+ result << :done
168
+ break
169
+ end
170
+ end
171
+ @clients.each { |c,t| t.close and break if c == client } #close the transport
172
+ rescue => e
173
+ raise e unless @catch_exceptions
174
+ end
175
+ end
176
+ queue
177
+ end
178
+
179
+ it "should handle basic message passing" do
180
+ client = setup_client
181
+ client.greeting(true).should == Hello.new
182
+ client.greeting(false).should == Hello.new(:greeting => 'Aloha!')
183
+ @server.shutdown
184
+ end
185
+
186
+ it "should handle concurrent clients" do
187
+ queue = Queue.new
188
+ trans_queue = Queue.new
189
+ 4.times do
190
+ Thread.new(Thread.current) do |main_thread|
191
+ begin
192
+ queue.push setup_client(trans_queue).block
193
+ rescue => e
194
+ main_thread.raise e
195
+ end
196
+ end
197
+ end
198
+ 4.times { trans_queue.pop }
199
+ setup_client.unblock(4)
200
+ 4.times { queue.pop.should be_true }
201
+ @server.shutdown
202
+ end
203
+
204
+ it "should handle messages from more than 5 long-lived connections" do
205
+ queues = []
206
+ result = Queue.new
207
+ 7.times do |i|
208
+ queues << setup_client_thread(result)
209
+ Thread.pass if i == 4 # give the server time to accept connections
210
+ end
211
+ client = setup_client
212
+ # block 4 connections
213
+ 4.times { |i| queues[i] << :block }
214
+ queues[4] << :hello
215
+ queues[5] << :hello
216
+ queues[6] << :hello
217
+ 3.times { result.pop.should == Hello.new }
218
+ client.greeting(true).should == Hello.new
219
+ queues[5] << [:unblock, 4]
220
+ 4.times { result.pop.should be_true }
221
+ queues[2] << :hello
222
+ result.pop.should == Hello.new
223
+ client.greeting(false).should == Hello.new(:greeting => 'Aloha!')
224
+ 7.times { queues.shift << :exit }
225
+ client.greeting(true).should == Hello.new
226
+ @server.shutdown
227
+ end
228
+
229
+ it "should shut down when asked" do
230
+ # connect first to ensure it's running
231
+ client = setup_client
232
+ client.greeting(false) # force a message pass
233
+ @server.shutdown
234
+ @server_thread.join(2).should be_an_instance_of(Thread)
235
+ end
236
+
237
+ it "should continue processing active messages when shutting down" do
238
+ result = Queue.new
239
+ client = setup_client_thread(result)
240
+ client << :sleep
241
+ sleep 0.1 # give the server time to start processing the client's message
242
+ @server.shutdown
243
+ @server_thread.join(2).should be_an_instance_of(Thread)
244
+ result.pop.should == :slept
245
+ end
246
+
247
+ it "should kill active messages when they don't expire while shutting down" do
248
+ result = Queue.new
249
+ client = setup_client_thread(result)
250
+ client << [:sleep, 10]
251
+ sleep 0.1 # start processing the client's message
252
+ @server.shutdown(1)
253
+ @catch_exceptions = true
254
+ @server_thread.join(3).should_not be_nil
255
+ result.should be_empty
256
+ end
257
+
258
+ it "should allow shutting down in response to a message" do
259
+ client = setup_client
260
+ client.greeting(true).should == Hello.new
261
+ client.shutdown
262
+ @server_thread.join(2).should_not be_nil
263
+ end
264
+ end
265
+ end
@@ -0,0 +1,83 @@
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 ThriftProcessorSpec < Spec::ExampleGroup
23
+ include Thrift
24
+
25
+ class ProcessorSpec
26
+ include Thrift::Processor
27
+ end
28
+
29
+ describe "Processor" do
30
+ before(:each) do
31
+ @processor = ProcessorSpec.new(mock("MockHandler"))
32
+ @prot = mock("MockProtocol")
33
+ end
34
+
35
+ def mock_trans(obj)
36
+ obj.should_receive(:trans).ordered.and_return do
37
+ mock("trans").tee do |trans|
38
+ trans.should_receive(:flush).ordered
39
+ end
40
+ end
41
+ end
42
+
43
+ it "should call process_<message> when it receives that message" do
44
+ @prot.should_receive(:read_message_begin).ordered.and_return ['testMessage', MessageTypes::CALL, 17]
45
+ @processor.should_receive(:process_testMessage).with(17, @prot, @prot).ordered
46
+ @processor.process(@prot, @prot).should == true
47
+ end
48
+
49
+ it "should raise an ApplicationException when the received message cannot be processed" do
50
+ @prot.should_receive(:read_message_begin).ordered.and_return ['testMessage', MessageTypes::CALL, 4]
51
+ @prot.should_receive(:skip).with(Types::STRUCT).ordered
52
+ @prot.should_receive(:read_message_end).ordered
53
+ @prot.should_receive(:write_message_begin).with('testMessage', MessageTypes::EXCEPTION, 4).ordered
54
+ ApplicationException.should_receive(:new).with(ApplicationException::UNKNOWN_METHOD, "Unknown function testMessage").and_return do
55
+ mock(ApplicationException).tee do |e|
56
+ e.should_receive(:write).with(@prot).ordered
57
+ end
58
+ end
59
+ @prot.should_receive(:write_message_end).ordered
60
+ mock_trans(@prot)
61
+ @processor.process(@prot, @prot)
62
+ end
63
+
64
+ it "should pass args off to the args class" do
65
+ args_class = mock("MockArgsClass")
66
+ args = mock("#<MockArgsClass:mock>").tee do |args|
67
+ args.should_receive(:read).with(@prot).ordered
68
+ end
69
+ args_class.should_receive(:new).and_return args
70
+ @prot.should_receive(:read_message_end).ordered
71
+ @processor.read_args(@prot, args_class).should eql(args)
72
+ end
73
+
74
+ it "should write out a reply when asked" do
75
+ @prot.should_receive(:write_message_begin).with('testMessage', MessageTypes::REPLY, 23).ordered
76
+ result = mock("MockResult")
77
+ result.should_receive(:write).with(@prot).ordered
78
+ @prot.should_receive(:write_message_end).ordered
79
+ mock_trans(@prot)
80
+ @processor.write_result(result, @prot, 'testMessage', 23)
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,69 @@
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 ThriftSerializerSpec < Spec::ExampleGroup
23
+ include Thrift
24
+ include SpecNamespace
25
+
26
+ describe Serializer do
27
+ it "should serialize structs to binary by default" do
28
+ serializer = Serializer.new(Thrift::BinaryProtocolAcceleratedFactory.new)
29
+ data = serializer.serialize(Hello.new(:greeting => "'Ello guv'nor!"))
30
+ data.should == "\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00"
31
+ end
32
+
33
+ it "should serialize structs to the given protocol" do
34
+ protocol = BaseProtocol.new(mock("transport"))
35
+ protocol.should_receive(:write_struct_begin).with("SpecNamespace::Hello")
36
+ protocol.should_receive(:write_field_begin).with("greeting", Types::STRING, 1)
37
+ protocol.should_receive(:write_string).with("Good day")
38
+ protocol.should_receive(:write_field_end)
39
+ protocol.should_receive(:write_field_stop)
40
+ protocol.should_receive(:write_struct_end)
41
+ protocol_factory = mock("ProtocolFactory")
42
+ protocol_factory.stub!(:get_protocol).and_return(protocol)
43
+ serializer = Serializer.new(protocol_factory)
44
+ serializer.serialize(Hello.new(:greeting => "Good day"))
45
+ end
46
+ end
47
+
48
+ describe Deserializer do
49
+ it "should deserialize structs from binary by default" do
50
+ deserializer = Deserializer.new
51
+ data = "\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00"
52
+ deserializer.deserialize(Hello.new, data).should == Hello.new(:greeting => "'Ello guv'nor!")
53
+ end
54
+
55
+ it "should deserialize structs from the given protocol" do
56
+ protocol = BaseProtocol.new(mock("transport"))
57
+ protocol.should_receive(:read_struct_begin).and_return("SpecNamespace::Hello")
58
+ protocol.should_receive(:read_field_begin).and_return(["greeting", Types::STRING, 1],
59
+ [nil, Types::STOP, 0])
60
+ protocol.should_receive(:read_string).and_return("Good day")
61
+ protocol.should_receive(:read_field_end)
62
+ protocol.should_receive(:read_struct_end)
63
+ protocol_factory = mock("ProtocolFactory")
64
+ protocol_factory.stub!(:get_protocol).and_return(protocol)
65
+ deserializer = Deserializer.new(protocol_factory)
66
+ deserializer.deserialize(Hello.new, "").should == Hello.new(:greeting => "Good day")
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,80 @@
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__)}/socket_spec_shared")
22
+
23
+ class ThriftServerSocketSpec < Spec::ExampleGroup
24
+ include Thrift
25
+
26
+ describe ServerSocket do
27
+ before(:each) do
28
+ @socket = ServerSocket.new(1234)
29
+ end
30
+
31
+ it "should create a handle when calling listen" do
32
+ TCPServer.should_receive(:new).with(nil, 1234)
33
+ @socket.listen
34
+ end
35
+
36
+ it "should accept an optional host argument" do
37
+ @socket = ServerSocket.new('localhost', 1234)
38
+ TCPServer.should_receive(:new).with('localhost', 1234)
39
+ @socket.listen
40
+ end
41
+
42
+ it "should create a Thrift::Socket to wrap accepted sockets" do
43
+ handle = mock("TCPServer")
44
+ TCPServer.should_receive(:new).with(nil, 1234).and_return(handle)
45
+ @socket.listen
46
+ sock = mock("sock")
47
+ handle.should_receive(:accept).and_return(sock)
48
+ trans = mock("Socket")
49
+ Socket.should_receive(:new).and_return(trans)
50
+ trans.should_receive(:handle=).with(sock)
51
+ @socket.accept.should == trans
52
+ end
53
+
54
+ it "should close the handle when closed" do
55
+ handle = mock("TCPServer", :closed? => false)
56
+ TCPServer.should_receive(:new).with(nil, 1234).and_return(handle)
57
+ @socket.listen
58
+ handle.should_receive(:close)
59
+ @socket.close
60
+ end
61
+
62
+ it "should return nil when accepting if there is no handle" do
63
+ @socket.accept.should be_nil
64
+ end
65
+
66
+ it "should return true for closed? when appropriate" do
67
+ handle = mock("TCPServer", :closed? => false)
68
+ TCPServer.stub!(:new).and_return(handle)
69
+ @socket.listen
70
+ @socket.should_not be_closed
71
+ handle.stub!(:close)
72
+ @socket.close
73
+ @socket.should be_closed
74
+ @socket.listen
75
+ @socket.should_not be_closed
76
+ handle.stub!(:closed?).and_return(true)
77
+ @socket.should be_closed
78
+ end
79
+ end
80
+ end