thrift 0.23.0 → 0.25.0

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 (120) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +203 -40
  3. data/ext/binary_protocol_accelerated.c +130 -40
  4. data/ext/bytes.c +14 -0
  5. data/ext/compact_protocol.c +184 -91
  6. data/ext/constants.h +3 -0
  7. data/ext/extconf.rb +12 -4
  8. data/ext/macros.h +11 -0
  9. data/ext/memory_buffer.c +67 -12
  10. data/ext/struct.c +192 -71
  11. data/ext/thrift_native.c +7 -0
  12. data/lib/thrift/bytes.rb +6 -3
  13. data/lib/thrift/client.rb +31 -33
  14. data/lib/thrift/exceptions.rb +11 -8
  15. data/lib/thrift/multiplexed_processor.rb +17 -8
  16. data/lib/thrift/processor.rb +25 -3
  17. data/lib/thrift/protocol/base_protocol.rb +66 -31
  18. data/lib/thrift/protocol/binary_protocol.rb +80 -37
  19. data/lib/thrift/protocol/binary_protocol_accelerated.rb +7 -2
  20. data/lib/thrift/protocol/compact_protocol.rb +204 -88
  21. data/lib/thrift/protocol/header_protocol.rb +15 -3
  22. data/lib/thrift/protocol/json_protocol.rb +158 -109
  23. data/lib/thrift/protocol/multiplexed_protocol.rb +3 -3
  24. data/lib/thrift/protocol/protocol_decorator.rb +6 -2
  25. data/lib/thrift/serializer/deserializer.rb +1 -0
  26. data/lib/thrift/serializer/serializer.rb +7 -0
  27. data/lib/thrift/server/base_server.rb +11 -2
  28. data/lib/thrift/server/nonblocking_server.rb +69 -20
  29. data/lib/thrift/server/rack_application.rb +69 -0
  30. data/lib/thrift/server/simple_server.rb +27 -15
  31. data/lib/thrift/server/thin_http_server.rb +21 -44
  32. data/lib/thrift/server/thread_pool_server.rb +21 -17
  33. data/lib/thrift/server/threaded_server.rb +22 -18
  34. data/lib/thrift/struct.rb +34 -18
  35. data/lib/thrift/struct_union.rb +37 -36
  36. data/lib/thrift/thrift_native.rb +2 -1
  37. data/lib/thrift/transport/base_server_transport.rb +3 -0
  38. data/lib/thrift/transport/base_transport.rb +26 -4
  39. data/lib/thrift/transport/buffered_transport.rb +5 -4
  40. data/lib/thrift/transport/framed_transport.rb +33 -7
  41. data/lib/thrift/transport/header_transport.rb +159 -33
  42. data/lib/thrift/transport/http_client_transport.rb +28 -14
  43. data/lib/thrift/transport/io_stream_transport.rb +3 -1
  44. data/lib/thrift/transport/memory_buffer_transport.rb +14 -9
  45. data/lib/thrift/transport/server_socket.rb +10 -8
  46. data/lib/thrift/transport/socket.rb +85 -48
  47. data/lib/thrift/transport/ssl_server_socket.rb +67 -6
  48. data/lib/thrift/transport/ssl_socket.rb +73 -14
  49. data/lib/thrift/transport/unix_server_socket.rb +6 -2
  50. data/lib/thrift/transport/unix_socket.rb +5 -6
  51. data/lib/thrift/types.rb +23 -22
  52. data/lib/thrift/union.rb +21 -11
  53. data/lib/thrift/uuid.rb +9 -8
  54. data/lib/thrift.rb +40 -39
  55. metadata +7 -266
  56. data/benchmark/Benchmark.thrift +0 -24
  57. data/benchmark/benchmark.rb +0 -285
  58. data/benchmark/client.rb +0 -117
  59. data/benchmark/server.rb +0 -120
  60. data/benchmark/thin_server.rb +0 -45
  61. data/lib/thrift/server/mongrel_http_server.rb +0 -60
  62. data/spec/BaseService.thrift +0 -27
  63. data/spec/ExtendedService.thrift +0 -25
  64. data/spec/Referenced.thrift +0 -44
  65. data/spec/ThriftNamespacedSpec.thrift +0 -53
  66. data/spec/ThriftSpec.thrift +0 -187
  67. data/spec/base_protocol_spec.rb +0 -224
  68. data/spec/base_transport_spec.rb +0 -387
  69. data/spec/binary_protocol_accelerated_spec.rb +0 -46
  70. data/spec/binary_protocol_spec.rb +0 -72
  71. data/spec/binary_protocol_spec_shared.rb +0 -445
  72. data/spec/bytes_spec.rb +0 -99
  73. data/spec/client_spec.rb +0 -166
  74. data/spec/compact_protocol_spec.rb +0 -196
  75. data/spec/constants_demo_spec.rb +0 -101
  76. data/spec/exception_spec.rb +0 -140
  77. data/spec/flat_spec.rb +0 -62
  78. data/spec/header_protocol_spec.rb +0 -475
  79. data/spec/header_transport_spec.rb +0 -386
  80. data/spec/http_client_spec.rb +0 -159
  81. data/spec/json_protocol_spec.rb +0 -552
  82. data/spec/namespaced_spec.rb +0 -66
  83. data/spec/nonblocking_server_spec.rb +0 -361
  84. data/spec/processor_spec.rb +0 -79
  85. data/spec/serializer_spec.rb +0 -66
  86. data/spec/server_socket_spec.rb +0 -84
  87. data/spec/server_spec.rb +0 -186
  88. data/spec/socket_spec.rb +0 -67
  89. data/spec/socket_spec_shared.rb +0 -167
  90. data/spec/spec_helper.rb +0 -69
  91. data/spec/ssl_server_socket_spec.rb +0 -45
  92. data/spec/ssl_socket_spec.rb +0 -87
  93. data/spec/struct_nested_containers_spec.rb +0 -190
  94. data/spec/struct_spec.rb +0 -397
  95. data/spec/support/header_protocol_helper.rb +0 -54
  96. data/spec/thin_http_server_spec.rb +0 -126
  97. data/spec/types_spec.rb +0 -117
  98. data/spec/union_spec.rb +0 -272
  99. data/spec/unix_socket_spec.rb +0 -115
  100. data/spec/uuid_validation_spec.rb +0 -238
  101. data/test/fuzz/Makefile.am +0 -173
  102. data/test/fuzz/README.md +0 -149
  103. data/test/fuzz/fuzz_common.rb +0 -95
  104. data/test/fuzz/fuzz_parse_binary_protocol.rb +0 -22
  105. data/test/fuzz/fuzz_parse_binary_protocol_accelerated.rb +0 -22
  106. data/test/fuzz/fuzz_parse_binary_protocol_accelerated_harness.rb +0 -22
  107. data/test/fuzz/fuzz_parse_binary_protocol_harness.rb +0 -22
  108. data/test/fuzz/fuzz_parse_compact_protocol.rb +0 -22
  109. data/test/fuzz/fuzz_parse_compact_protocol_harness.rb +0 -22
  110. data/test/fuzz/fuzz_parse_json_protocol.rb +0 -22
  111. data/test/fuzz/fuzz_parse_json_protocol_harness.rb +0 -22
  112. data/test/fuzz/fuzz_roundtrip_binary_protocol.rb +0 -22
  113. data/test/fuzz/fuzz_roundtrip_binary_protocol_accelerated.rb +0 -22
  114. data/test/fuzz/fuzz_roundtrip_binary_protocol_accelerated_harness.rb +0 -22
  115. data/test/fuzz/fuzz_roundtrip_binary_protocol_harness.rb +0 -22
  116. data/test/fuzz/fuzz_roundtrip_compact_protocol.rb +0 -22
  117. data/test/fuzz/fuzz_roundtrip_compact_protocol_harness.rb +0 -22
  118. data/test/fuzz/fuzz_roundtrip_json_protocol.rb +0 -22
  119. data/test/fuzz/fuzz_roundtrip_json_protocol_harness.rb +0 -22
  120. data/test/fuzz/fuzz_tracer.rb +0 -28
@@ -1,361 +0,0 @@
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 'timeout'
22
-
23
- describe 'NonblockingServer' do
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
- expect(client.greeting(true)).to eq(SpecNamespace::Hello.new)
180
- expect(client.greeting(false)).to eq(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 { expect(queue.pop).to be_truthy }
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 { expect(result.pop).to eq(SpecNamespace::Hello.new) }
216
- expect(client.greeting(true)).to eq(SpecNamespace::Hello.new)
217
- queues[5] << [:unblock, 4]
218
- 4.times { expect(result.pop).to be_truthy }
219
- queues[2] << :hello
220
- expect(result.pop).to eq(SpecNamespace::Hello.new)
221
- expect(client.greeting(false)).to eq(SpecNamespace::Hello.new(:greeting => 'Aloha!'))
222
- 7.times { queues.shift << :exit }
223
- expect(client.greeting(true)).to eq(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
- expect(@server_thread.join(2)).to 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
- expect(@server_thread.join(2)).to be_an_instance_of(Thread)
242
- expect(result.pop).to eq(: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.0]
249
- sleep 0.1 # start processing the client's message
250
- @server.shutdown(1)
251
- @catch_exceptions = true
252
- expect(@server_thread.join(3)).not_to be_nil
253
- expect(result).to be_empty
254
- end
255
-
256
- it "should allow shutting down in response to a message" do
257
- client = setup_client
258
- expect(client.greeting(true)).to eq(SpecNamespace::Hello.new)
259
- client.shutdown
260
- expect(@server_thread.join(2)).not_to be_nil
261
- end
262
- end
263
-
264
- describe "#{Thrift::NonblockingServer} with TLS transport" do
265
- before(:each) do
266
- @port = available_port
267
- handler = Handler.new
268
- processor = SpecNamespace::NonblockingService::Processor.new(handler)
269
- @transport = Thrift::SSLServerSocket.new('localhost', @port, create_server_ssl_context)
270
- transport_factory = Thrift::FramedTransportFactory.new
271
- logger = Logger.new(STDERR)
272
- logger.level = Logger::WARN
273
- @server = Thrift::NonblockingServer.new(processor, @transport, transport_factory, nil, 5, logger)
274
- handler.server = @server
275
-
276
- @server_thread = Thread.new(Thread.current) do |master_thread|
277
- begin
278
- @server.serve
279
- rescue => e
280
- master_thread.raise e
281
- end
282
- end
283
-
284
- @clients = []
285
- wait_until_listening
286
- end
287
-
288
- after(:each) do
289
- @clients.each(&:close)
290
- @server.shutdown if @server
291
- @server_thread.join(2) if @server_thread
292
- @transport.close if @transport
293
- end
294
-
295
- it "should handle requests over TLS" do
296
- expect(@server_thread).to be_alive
297
-
298
- client = setup_tls_client
299
- expect(client.greeting(true)).to eq(SpecNamespace::Hello.new)
300
-
301
- @server.shutdown
302
- expect(@server_thread.join(2)).to be_an_instance_of(Thread)
303
- end
304
-
305
- def setup_tls_client
306
- transport = Thrift::FramedTransport.new(
307
- Thrift::SSLSocket.new('localhost', @port, nil, create_client_ssl_context)
308
- )
309
- protocol = Thrift::BinaryProtocol.new(transport)
310
- client = SpecNamespace::NonblockingService::Client.new(protocol)
311
- transport.open
312
- @clients << transport
313
- client
314
- end
315
-
316
- def wait_until_listening
317
- Timeout.timeout(2) do
318
- until @transport.handle
319
- raise "Server thread exited unexpectedly" unless @server_thread.alive?
320
- sleep 0.01
321
- end
322
- end
323
- end
324
-
325
- def available_port
326
- TCPServer.open('localhost', 0) { |server| server.addr[1] }
327
- end
328
-
329
- def ssl_keys_dir
330
- File.expand_path('../../../test/keys', __dir__)
331
- end
332
-
333
- def create_server_ssl_context
334
- OpenSSL::SSL::SSLContext.new.tap do |ctx|
335
- ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
336
- if ctx.respond_to?(:min_version=) && OpenSSL::SSL.const_defined?(:TLS1_2_VERSION)
337
- ctx.min_version = OpenSSL::SSL::TLS1_2_VERSION
338
- end
339
- ctx.ca_file = File.join(ssl_keys_dir, 'CA.pem')
340
- ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.join(ssl_keys_dir, 'server.crt')))
341
- ctx.cert_store = OpenSSL::X509::Store.new
342
- ctx.cert_store.add_file(File.join(ssl_keys_dir, 'client.pem'))
343
- ctx.key = OpenSSL::PKey::RSA.new(File.read(File.join(ssl_keys_dir, 'server.key')))
344
- end
345
- end
346
-
347
- def create_client_ssl_context
348
- OpenSSL::SSL::SSLContext.new.tap do |ctx|
349
- ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
350
- if ctx.respond_to?(:min_version=) && OpenSSL::SSL.const_defined?(:TLS1_2_VERSION)
351
- ctx.min_version = OpenSSL::SSL::TLS1_2_VERSION
352
- end
353
- ctx.ca_file = File.join(ssl_keys_dir, 'CA.pem')
354
- ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.join(ssl_keys_dir, 'client.crt')))
355
- ctx.cert_store = OpenSSL::X509::Store.new
356
- ctx.cert_store.add_file(File.join(ssl_keys_dir, 'server.pem'))
357
- ctx.key = OpenSSL::PKey::RSA.new(File.read(File.join(ssl_keys_dir, 'client.key')))
358
- end
359
- end
360
- end
361
- end
@@ -1,79 +0,0 @@
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
- class ProcessorSpec
24
- include Thrift::Processor
25
- end
26
-
27
- describe Thrift::Processor do
28
- before(:each) do
29
- @processor = ProcessorSpec.new(double("MockHandler"))
30
- @prot = double("MockProtocol")
31
- end
32
-
33
- def mock_trans(obj)
34
- expect(obj).to receive(:trans).ordered do
35
- double("trans").tap do |trans|
36
- expect(trans).to receive(:flush).ordered
37
- end
38
- end
39
- end
40
-
41
- it "should call process_<message> when it receives that message" do
42
- expect(@prot).to receive(:read_message_begin).ordered.and_return ['testMessage', Thrift::MessageTypes::CALL, 17]
43
- expect(@processor).to receive(:process_testMessage).with(17, @prot, @prot).ordered
44
- expect(@processor.process(@prot, @prot)).to eq(true)
45
- end
46
-
47
- it "should raise an ApplicationException when the received message cannot be processed" do
48
- expect(@prot).to receive(:read_message_begin).ordered.and_return ['testMessage', Thrift::MessageTypes::CALL, 4]
49
- expect(@prot).to receive(:skip).with(Thrift::Types::STRUCT).ordered
50
- expect(@prot).to receive(:read_message_end).ordered
51
- expect(@prot).to receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::EXCEPTION, 4).ordered
52
- e = double(Thrift::ApplicationException)
53
- expect(e).to receive(:write).with(@prot).ordered
54
- expect(Thrift::ApplicationException).to receive(:new).with(Thrift::ApplicationException::UNKNOWN_METHOD, "Unknown function testMessage").and_return(e)
55
- expect(@prot).to receive(:write_message_end).ordered
56
- mock_trans(@prot)
57
- @processor.process(@prot, @prot)
58
- end
59
-
60
- it "should pass args off to the args class" do
61
- args_class = double("MockArgsClass")
62
- args = double("#<MockArgsClass:mock>").tap do |args|
63
- expect(args).to receive(:read).with(@prot).ordered
64
- end
65
- expect(args_class).to receive(:new).and_return args
66
- expect(@prot).to receive(:read_message_end).ordered
67
- expect(@processor.read_args(@prot, args_class)).to eql(args)
68
- end
69
-
70
- it "should write out a reply when asked" do
71
- expect(@prot).to receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::REPLY, 23).ordered
72
- result = double("MockResult")
73
- expect(result).to receive(:write).with(@prot).ordered
74
- expect(@prot).to receive(:write_message_end).ordered
75
- mock_trans(@prot)
76
- @processor.write_result(result, @prot, 'testMessage', 23)
77
- end
78
- end
79
- end
@@ -1,66 +0,0 @@
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
- describe Thrift::Serializer do
24
- it "should serialize structs to binary by default" do
25
- serializer = Thrift::Serializer.new(Thrift::BinaryProtocolAcceleratedFactory.new)
26
- data = serializer.serialize(SpecNamespace::Hello.new(:greeting => "'Ello guv'nor!"))
27
- expect(data).to eq("\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00")
28
- end
29
-
30
- it "should serialize structs to the given protocol" do
31
- protocol = Thrift::BaseProtocol.new(double("transport"))
32
- expect(protocol).to receive(:write_struct_begin).with("SpecNamespace::Hello")
33
- expect(protocol).to receive(:write_field_begin).with("greeting", Thrift::Types::STRING, 1)
34
- expect(protocol).to receive(:write_string).with("Good day")
35
- expect(protocol).to receive(:write_field_end)
36
- expect(protocol).to receive(:write_field_stop)
37
- expect(protocol).to receive(:write_struct_end)
38
- protocol_factory = double("ProtocolFactory")
39
- allow(protocol_factory).to receive(:get_protocol).and_return(protocol)
40
- serializer = Thrift::Serializer.new(protocol_factory)
41
- serializer.serialize(SpecNamespace::Hello.new(:greeting => "Good day"))
42
- end
43
- end
44
-
45
- describe Thrift::Deserializer do
46
- it "should deserialize structs from binary by default" do
47
- deserializer = Thrift::Deserializer.new
48
- data = "\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00"
49
- expect(deserializer.deserialize(SpecNamespace::Hello.new, data)).to eq(SpecNamespace::Hello.new(:greeting => "'Ello guv'nor!"))
50
- end
51
-
52
- it "should deserialize structs from the given protocol" do
53
- protocol = Thrift::BaseProtocol.new(double("transport"))
54
- expect(protocol).to receive(:read_struct_begin).and_return("SpecNamespace::Hello")
55
- expect(protocol).to receive(:read_field_begin).and_return(["greeting", Thrift::Types::STRING, 1],
56
- [nil, Thrift::Types::STOP, 0])
57
- expect(protocol).to receive(:read_string).and_return("Good day")
58
- expect(protocol).to receive(:read_field_end)
59
- expect(protocol).to receive(:read_struct_end)
60
- protocol_factory = double("ProtocolFactory")
61
- allow(protocol_factory).to receive(:get_protocol).and_return(protocol)
62
- deserializer = Thrift::Deserializer.new(protocol_factory)
63
- expect(deserializer.deserialize(SpecNamespace::Hello.new, "")).to eq(SpecNamespace::Hello.new(:greeting => "Good day"))
64
- end
65
- end
66
- end
@@ -1,84 +0,0 @@
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
- describe Thrift::ServerSocket do
25
- before(:each) do
26
- @socket = Thrift::ServerSocket.new(1234)
27
- end
28
-
29
- it "should create a handle when calling listen" do
30
- expect(TCPServer).to receive(:new).with(nil, 1234)
31
- @socket.listen
32
- end
33
-
34
- it "should accept an optional host argument" do
35
- @socket = Thrift::ServerSocket.new('localhost', 1234)
36
- expect(TCPServer).to receive(:new).with('localhost', 1234)
37
- @socket.to_s == "server(localhost:1234)"
38
- @socket.listen
39
- end
40
-
41
- it "should create a Thrift::Socket to wrap accepted sockets" do
42
- handle = double("TCPServer")
43
- expect(TCPServer).to receive(:new).with(nil, 1234).and_return(handle)
44
- @socket.listen
45
- sock = double("sock")
46
- expect(sock).to receive(:setsockopt).with(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
47
- expect(handle).to receive(:accept).and_return(sock)
48
- trans = double("Socket")
49
- expect(Thrift::Socket).to receive(:new).and_return(trans)
50
- expect(trans).to receive(:handle=).with(sock)
51
- expect(@socket.accept).to eq(trans)
52
- end
53
-
54
- it "should close the handle when closed" do
55
- handle = double("TCPServer", :closed? => false)
56
- expect(TCPServer).to receive(:new).with(nil, 1234).and_return(handle)
57
- @socket.listen
58
- expect(handle).to receive(:close)
59
- @socket.close
60
- end
61
-
62
- it "should return nil when accepting if there is no handle" do
63
- expect(@socket.accept).to be_nil
64
- end
65
-
66
- it "should return true for closed? when appropriate" do
67
- handle = double("TCPServer", :closed? => false)
68
- allow(TCPServer).to receive(:new).and_return(handle)
69
- @socket.listen
70
- expect(@socket).not_to be_closed
71
- allow(handle).to receive(:close)
72
- @socket.close
73
- expect(@socket).to be_closed
74
- @socket.listen
75
- expect(@socket).not_to be_closed
76
- allow(handle).to receive(:closed?).and_return(true)
77
- expect(@socket).to be_closed
78
- end
79
-
80
- it "should provide a reasonable to_s" do
81
- expect(@socket.to_s).to eq("socket(:1234)")
82
- end
83
- end
84
- end