tiny_thrift 1.0.0.0

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