thrift 0.0.751142 → 0.0.810255.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. data/CHANGELOG +3 -1
  2. data/Manifest +47 -44
  3. data/README +19 -6
  4. data/Rakefile +1 -1
  5. data/benchmark/Benchmark.thrift +19 -0
  6. data/benchmark/benchmark.rb +20 -3
  7. data/benchmark/client.rb +20 -2
  8. data/benchmark/server.rb +22 -4
  9. data/benchmark/thin_server.rb +22 -4
  10. data/ext/binary_protocol_accelerated.c +32 -21
  11. data/ext/binary_protocol_accelerated.h +19 -0
  12. data/ext/compact_protocol.c +665 -0
  13. data/ext/compact_protocol.h +20 -0
  14. data/ext/constants.h +19 -1
  15. data/ext/extconf.rb +19 -0
  16. data/ext/macros.h +41 -0
  17. data/ext/memory_buffer.c +29 -5
  18. data/ext/memory_buffer.h +19 -0
  19. data/ext/protocol.c +20 -1
  20. data/ext/protocol.h +19 -0
  21. data/ext/struct.c +115 -83
  22. data/ext/struct.h +19 -0
  23. data/ext/thrift_native.c +23 -2
  24. data/lib/thrift/client.rb +19 -1
  25. data/lib/thrift/core_ext/fixnum.rb +29 -0
  26. data/lib/thrift/core_ext.rb +23 -0
  27. data/lib/thrift/exceptions.rb +19 -2
  28. data/lib/thrift/processor.rb +19 -1
  29. data/lib/thrift/protocol/base_protocol.rb +290 -0
  30. data/lib/thrift/protocol/{binaryprotocol.rb → binary_protocol.rb} +27 -15
  31. data/lib/thrift/protocol/binary_protocol_accelerated.rb +35 -0
  32. data/lib/thrift/protocol/compact_protocol.rb +422 -0
  33. data/lib/thrift/serializer/deserializer.rb +33 -0
  34. data/lib/thrift/serializer/serializer.rb +34 -0
  35. data/lib/thrift/server/base_server.rb +31 -0
  36. data/lib/thrift/server/{httpserver.rb → mongrel_http_server.rb} +19 -5
  37. data/lib/thrift/server/{nonblockingserver.rb → nonblocking_server.rb} +42 -24
  38. data/lib/thrift/server/simple_server.rb +43 -0
  39. data/lib/thrift/server/thread_pool_server.rb +75 -0
  40. data/lib/thrift/server/threaded_server.rb +47 -0
  41. data/lib/thrift/struct.rb +73 -47
  42. data/lib/thrift/thrift_native.rb +24 -0
  43. data/lib/thrift/transport/base_server_transport.rb +37 -0
  44. data/lib/thrift/transport/base_transport.rb +70 -0
  45. data/lib/thrift/transport/buffered_transport.rb +77 -0
  46. data/lib/thrift/transport/framed_transport.rb +90 -0
  47. data/lib/thrift/transport/http_client_transport.rb +45 -0
  48. data/lib/thrift/transport/io_stream_transport.rb +39 -0
  49. data/lib/thrift/transport/memory_buffer_transport.rb +96 -0
  50. data/lib/thrift/transport/server_socket.rb +63 -0
  51. data/lib/thrift/transport/socket.rb +22 -53
  52. data/lib/thrift/transport/unix_server_socket.rb +60 -0
  53. data/lib/thrift/transport/unix_socket.rb +40 -0
  54. data/lib/thrift/types.rb +20 -2
  55. data/lib/thrift.rb +50 -19
  56. data/script/proto_benchmark.rb +121 -0
  57. data/script/read_struct.rb +43 -0
  58. data/script/write_struct.rb +30 -0
  59. data/spec/ThriftSpec.thrift +40 -2
  60. data/spec/{protocol_spec.rb → base_protocol_spec.rb} +25 -7
  61. data/spec/{transport_spec.rb → base_transport_spec.rb} +44 -52
  62. data/spec/binary_protocol_accelerated_spec.rb +41 -0
  63. data/spec/{binaryprotocol_spec.rb → binary_protocol_spec.rb} +20 -2
  64. data/spec/{binaryprotocol_spec_shared.rb → binary_protocol_spec_shared.rb} +117 -16
  65. data/spec/client_spec.rb +23 -4
  66. data/spec/compact_protocol_spec.rb +117 -0
  67. data/spec/exception_spec.rb +19 -0
  68. data/spec/http_client_spec.rb +49 -0
  69. data/spec/{httpserver_spec.rb → mongrel_http_server_spec.rb} +26 -7
  70. data/spec/{nonblockingserver_spec.rb → nonblocking_server_spec.rb} +25 -5
  71. data/spec/processor_spec.rb +19 -0
  72. data/spec/serializer_spec.rb +27 -10
  73. data/spec/server_socket_spec.rb +80 -0
  74. data/spec/server_spec.rb +35 -16
  75. data/spec/socket_spec.rb +19 -55
  76. data/spec/socket_spec_shared.rb +19 -0
  77. data/spec/spec_helper.rb +34 -9
  78. data/spec/struct_spec.rb +26 -18
  79. data/spec/types_spec.rb +19 -1
  80. data/spec/{unixsocket_spec.rb → unix_socket_spec.rb} +19 -1
  81. data/thrift.gemspec +6 -6
  82. data.tar.gz.sig +0 -0
  83. metadata +80 -70
  84. metadata.gz.sig +0 -0
  85. data/COPYING +0 -14
  86. data/LICENSE +0 -14
  87. data/Makefile.am +0 -15
  88. data/benchmark/gen-rb/BenchmarkService.rb +0 -81
  89. data/benchmark/gen-rb/Benchmark_constants.rb +0 -11
  90. data/benchmark/gen-rb/Benchmark_types.rb +0 -10
  91. data/lib/thrift/deprecation.rb +0 -155
  92. data/lib/thrift/protocol/binaryprotocolaccelerated.rb +0 -19
  93. data/lib/thrift/protocol/tbinaryprotocol.rb +0 -2
  94. data/lib/thrift/protocol/tprotocol.rb +0 -2
  95. data/lib/thrift/protocol.rb +0 -270
  96. data/lib/thrift/serializer.rb +0 -27
  97. data/lib/thrift/server/thttpserver.rb +0 -2
  98. data/lib/thrift/server/tserver.rb +0 -2
  99. data/lib/thrift/server.rb +0 -135
  100. data/lib/thrift/thrift.rb +0 -14
  101. data/lib/thrift/transport/httpclient.rb +0 -29
  102. data/lib/thrift/transport/thttpclient.rb +0 -2
  103. data/lib/thrift/transport/tsocket.rb +0 -2
  104. data/lib/thrift/transport/ttransport.rb +0 -2
  105. data/lib/thrift/transport/unixsocket.rb +0 -58
  106. data/lib/thrift/transport.rb +0 -319
  107. data/spec/backwards_compatibility_spec.rb +0 -136
  108. data/spec/binaryprotocolaccelerated_spec.rb +0 -101
  109. data/spec/deprecation_spec.rb +0 -443
  110. data/spec/gen-rb/NonblockingService.rb +0 -268
  111. data/spec/gen-rb/ThriftSpec_constants.rb +0 -11
  112. data/spec/gen-rb/ThriftSpec_types.rb +0 -134
  113. data/spec/httpclient_spec.rb +0 -31
@@ -0,0 +1,30 @@
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/spec_helper"
21
+
22
+ path, factory_class = ARGV
23
+
24
+ factory = eval(factory_class).new
25
+
26
+ ser = Thrift::Serializer.new(factory)
27
+
28
+ File.open(path, "w") do |file|
29
+ file.write(ser.serialize(Fixtures::COMPACT_PROTOCOL_TEST_STRUCT))
30
+ end
@@ -1,3 +1,41 @@
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
+ #
21
+ # Licensed to the Apache Software Foundation (ASF) under one
22
+ # or more contributor license agreements. See the NOTICE file
23
+ # distributed with this work for additional information
24
+ # regarding copyright ownership. The ASF licenses this file
25
+ # to you under the Apache License, Version 2.0 (the
26
+ # "License"); you may not use this file except in compliance
27
+ # with the License. You may obtain a copy of the License at
28
+ #
29
+ # http://www.apache.org/licenses/LICENSE-2.0
30
+ #
31
+ # Unless required by applicable law or agreed to in writing,
32
+ # software distributed under the License is distributed on an
33
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
34
+ # KIND, either express or implied. See the License for the
35
+ # specific language governing permissions and limitations
36
+ # under the License.
37
+ #
38
+
1
39
  namespace rb SpecNamespace
2
40
 
3
41
  struct Hello {
@@ -40,7 +78,7 @@ exception Xception {
40
78
  service NonblockingService {
41
79
  Hello greeting(1:bool english)
42
80
  bool block()
43
- async void unblock(1:i32 n)
44
- async void shutdown()
81
+ oneway void unblock(1:i32 n)
82
+ oneway void shutdown()
45
83
  void sleep(1:double seconds)
46
84
  }
@@ -1,15 +1,33 @@
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
+
1
20
  require File.dirname(__FILE__) + '/spec_helper'
2
- require "thrift_native"
3
21
 
4
- class ThriftProtocolSpec < Spec::ExampleGroup
22
+ class ThriftBaseProtocolSpec < Spec::ExampleGroup
5
23
  include Thrift
6
24
 
7
25
  before(:each) do
8
26
  @trans = mock("MockTransport")
9
- @prot = Protocol.new(@trans)
27
+ @prot = BaseProtocol.new(@trans)
10
28
  end
11
29
 
12
- describe Protocol do
30
+ describe BaseProtocol do
13
31
  # most of the methods are stubs, so we can ignore them
14
32
 
15
33
  it "should make trans accessible" do
@@ -133,10 +151,10 @@ class ThriftProtocolSpec < Spec::ExampleGroup
133
151
  end
134
152
  end
135
153
 
136
- describe ProtocolFactory do
137
- it "should return nil" do
154
+ describe BaseProtocolFactory do
155
+ it "should raise NotImplementedError" do
138
156
  # returning nil since Protocol is just an abstract class
139
- ProtocolFactory.new.get_protocol(mock("MockTransport")).should be_nil
157
+ lambda {BaseProtocolFactory.new.get_protocol(mock("MockTransport"))}.should raise_error(NotImplementedError)
140
158
  end
141
159
  end
142
160
  end
@@ -1,6 +1,25 @@
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
+
1
20
  require File.dirname(__FILE__) + '/spec_helper'
2
21
 
3
- class ThriftTransportSpec < Spec::ExampleGroup
22
+ class ThriftBaseTransportSpec < Spec::ExampleGroup
4
23
  include Thrift
5
24
 
6
25
  describe TransportException do
@@ -11,9 +30,9 @@ class ThriftTransportSpec < Spec::ExampleGroup
11
30
  end
12
31
  end
13
32
 
14
- describe Transport do
33
+ describe BaseTransport do
15
34
  it "should read the specified size" do
16
- transport = Transport.new
35
+ transport = BaseTransport.new
17
36
  transport.should_receive(:read).with(40).ordered.and_return("10 letters")
18
37
  transport.should_receive(:read).with(30).ordered.and_return("fifteen letters")
19
38
  transport.should_receive(:read).with(15).ordered.and_return("more characters")
@@ -23,27 +42,27 @@ class ThriftTransportSpec < Spec::ExampleGroup
23
42
  it "should stub out the rest of the methods" do
24
43
  # can't test for stubbiness, so just make sure they're defined
25
44
  [:open?, :open, :close, :read, :write, :flush].each do |sym|
26
- Transport.method_defined?(sym).should be_true
45
+ BaseTransport.method_defined?(sym).should be_true
27
46
  end
28
47
  end
29
48
 
30
49
  it "should alias << to write" do
31
- Transport.instance_method(:<<).should == Transport.instance_method(:write)
50
+ BaseTransport.instance_method(:<<).should == BaseTransport.instance_method(:write)
32
51
  end
33
52
  end
34
53
 
35
- describe ServerTransport do
54
+ describe BaseServerTransport do
36
55
  it "should stub out its methods" do
37
56
  [:listen, :accept, :close].each do |sym|
38
- ServerTransport.method_defined?(sym).should be_true
57
+ BaseServerTransport.method_defined?(sym).should be_true
39
58
  end
40
59
  end
41
60
  end
42
61
 
43
- describe TransportFactory do
62
+ describe BaseTransportFactory do
44
63
  it "should return the transport it's given" do
45
64
  transport = mock("Transport")
46
- TransportFactory.new.get_transport(transport).should eql(transport)
65
+ BaseTransportFactory.new.get_transport(transport).should eql(transport)
47
66
  end
48
67
  end
49
68
 
@@ -221,40 +240,6 @@ class ThriftTransportSpec < Spec::ExampleGroup
221
240
  @trans.should_receive(:write).with("\000\000\000\000")
222
241
  ftrans.flush
223
242
  end
224
-
225
- it "should refill its buffer when borrow is called and it is empty" do
226
- ftrans = FramedTransport.new(@trans)
227
- @trans.should_receive(:read_all).with(4).and_return([10].pack("N"))
228
- @trans.should_receive(:read_all).with(10).and_return("1234567890")
229
- ftrans.borrow(10).should == "1234567890"
230
- end
231
-
232
- it "should not consume any data when borrow is called" do
233
- ftrans = FramedTransport.new(@trans)
234
- @trans.should_receive(:read_all).with(4).and_return([10].pack("N"))
235
- @trans.should_receive(:read_all).with(10).and_return("1234567890")
236
- ftrans.borrow(10).should == "1234567890"
237
- ftrans.borrow(10).should == "1234567890"
238
- end
239
-
240
- it "should remove data from the buffer when consume! is called" do
241
- ftrans = FramedTransport.new(@trans)
242
- @trans.should_receive(:read_all).with(4).ordered.and_return([10].pack("N"))
243
- @trans.should_receive(:read_all).with(10).ordered.and_return("1234567890")
244
- ftrans.borrow(5).should == "1234567890"
245
- ftrans.consume!(5).should == "12345"
246
- ftrans.borrow(5).should == "67890"
247
- end
248
-
249
- it "should raise an EOFError when it is out of data and borrow is called" do
250
- ftrans = FramedTransport.new(@trans)
251
- @trans.should_receive(:read_all).with(4).ordered.and_return([10].pack("N"), [0].pack("N"))
252
- @trans.should_receive(:read_all).with(10).ordered.and_return("1234567890")
253
- @trans.should_receive(:read_all).with(0).ordered.and_return("")
254
- ftrans.borrow(10).should == "1234567890"
255
- ftrans.consume!(10).should == "1234567890"
256
- lambda {ftrans.borrow(10)}.should raise_error(EOFError)
257
- end
258
243
  end
259
244
 
260
245
  describe FramedTransportFactory do
@@ -265,14 +250,14 @@ class ThriftTransportSpec < Spec::ExampleGroup
265
250
  end
266
251
  end
267
252
 
268
- describe MemoryBuffer do
253
+ describe MemoryBufferTransport do
269
254
  before(:each) do
270
- @buffer = MemoryBuffer.new
255
+ @buffer = MemoryBufferTransport.new
271
256
  end
272
257
 
273
258
  it "should accept a buffer on input and use it directly" do
274
259
  s = "this is a test"
275
- @buffer = MemoryBuffer.new(s)
260
+ @buffer = MemoryBufferTransport.new(s)
276
261
  @buffer.read(4).should == "this"
277
262
  s.slice!(-4..-1)
278
263
  @buffer.read(@buffer.available).should == " is a "
@@ -291,7 +276,7 @@ class ThriftTransportSpec < Spec::ExampleGroup
291
276
  @buffer.read(4)
292
277
  @buffer.peek.should be_true
293
278
  @buffer.available.should == 5
294
- @buffer.read(16)
279
+ @buffer.read(5)
295
280
  @buffer.peek.should be_false
296
281
  @buffer.available.should == 0
297
282
  end
@@ -300,12 +285,12 @@ class ThriftTransportSpec < Spec::ExampleGroup
300
285
  @buffer.write "test data"
301
286
  @buffer.reset_buffer("foobar")
302
287
  @buffer.available.should == 6
303
- @buffer.read(10).should == "foobar"
288
+ @buffer.read(@buffer.available).should == "foobar"
304
289
  @buffer.reset_buffer
305
290
  @buffer.available.should == 0
306
291
  end
307
292
 
308
- it "should copy the given string whne resetting the buffer" do
293
+ it "should copy the given string when resetting the buffer" do
309
294
  s = "this is a test"
310
295
  @buffer.reset_buffer(s)
311
296
  @buffer.available.should == 14
@@ -317,11 +302,18 @@ class ThriftTransportSpec < Spec::ExampleGroup
317
302
  it "should return from read what was given in write" do
318
303
  @buffer.write "test data"
319
304
  @buffer.read(4).should == "test"
320
- @buffer.read(10).should == " data"
321
- @buffer.read(10).should == ""
305
+ @buffer.read(@buffer.available).should == " data"
322
306
  @buffer.write "foo"
323
307
  @buffer.write " bar"
324
- @buffer.read(10).should == "foo 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)
325
317
  end
326
318
  end
327
319
 
@@ -0,0 +1,41 @@
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.dirname(__FILE__) + '/spec_helper'
21
+ require File.dirname(__FILE__) + '/binary_protocol_spec_shared'
22
+
23
+ class ThriftBinaryProtocolAcceleratedSpec < Spec::ExampleGroup
24
+ include Thrift
25
+
26
+ describe Thrift::BinaryProtocolAccelerated do
27
+ # since BinaryProtocolAccelerated should be directly equivalent to
28
+ # BinaryProtocol, we don't need any custom specs!
29
+ it_should_behave_like 'a binary protocol'
30
+
31
+ def protocol_class
32
+ BinaryProtocolAccelerated
33
+ end
34
+ end
35
+
36
+ describe BinaryProtocolAcceleratedFactory do
37
+ it "should create a BinaryProtocolAccelerated" do
38
+ BinaryProtocolAcceleratedFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(BinaryProtocolAccelerated)
39
+ end
40
+ end
41
+ end
@@ -1,6 +1,24 @@
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
+
1
20
  require File.dirname(__FILE__) + '/spec_helper'
2
- require 'thrift/protocol/binaryprotocol'
3
- require File.dirname(__FILE__) + '/binaryprotocol_spec_shared'
21
+ require File.dirname(__FILE__) + '/binary_protocol_spec_shared'
4
22
 
5
23
  class ThriftBinaryProtocolSpec < Spec::ExampleGroup
6
24
  include Thrift
@@ -1,8 +1,27 @@
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
+
1
20
  require File.dirname(__FILE__) + '/spec_helper'
2
21
 
3
22
  shared_examples_for 'a binary protocol' do
4
23
  before(:each) do
5
- @trans = Thrift::MemoryBuffer.new
24
+ @trans = Thrift::MemoryBufferTransport.new
6
25
  @prot = protocol_class.new(@trans)
7
26
  end
8
27
 
@@ -22,19 +41,19 @@ shared_examples_for 'a binary protocol' do
22
41
 
23
42
  it "should write the message header" do
24
43
  @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
25
- @trans.read(1000).should == [protocol_class.const_get(:VERSION_1) | Thrift::MessageTypes::CALL, "testMessage".size, "testMessage", 17].pack("NNa11N")
44
+ @trans.read(@trans.available).should == [protocol_class.const_get(:VERSION_1) | Thrift::MessageTypes::CALL, "testMessage".size, "testMessage", 17].pack("NNa11N")
26
45
  end
27
46
 
28
47
  it "should write the message header without version when writes are not strict" do
29
48
  @prot = protocol_class.new(@trans, true, false) # no strict write
30
49
  @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
31
- @trans.read(1000).should == "\000\000\000\vtestMessage\001\000\000\000\021"
50
+ @trans.read(@trans.available).should == "\000\000\000\vtestMessage\001\000\000\000\021"
32
51
  end
33
52
 
34
53
  it "should write the message header with a version when writes are strict" do
35
54
  @prot = protocol_class.new(@trans) # strict write
36
55
  @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
37
- @trans.read(1000).should == "\200\001\000\001\000\000\000\vtestMessage\000\000\000\021"
56
+ @trans.read(@trans.available).should == "\200\001\000\001\000\000\000\vtestMessage\000\000\000\021"
38
57
  end
39
58
 
40
59
 
@@ -42,7 +61,7 @@ shared_examples_for 'a binary protocol' do
42
61
 
43
62
  it "should write the field header" do
44
63
  @prot.write_field_begin('foo', Thrift::Types::DOUBLE, 3)
45
- @trans.read(1000).should == [Thrift::Types::DOUBLE, 3].pack("cn")
64
+ @trans.read(@trans.available).should == [Thrift::Types::DOUBLE, 3].pack("cn")
46
65
  end
47
66
 
48
67
  # field footer is a noop
@@ -54,27 +73,27 @@ shared_examples_for 'a binary protocol' do
54
73
 
55
74
  it "should write the map header" do
56
75
  @prot.write_map_begin(Thrift::Types::STRING, Thrift::Types::LIST, 17)
57
- @trans.read(1000).should == [Thrift::Types::STRING, Thrift::Types::LIST, 17].pack("ccN");
76
+ @trans.read(@trans.available).should == [Thrift::Types::STRING, Thrift::Types::LIST, 17].pack("ccN");
58
77
  end
59
78
 
60
79
  # map footer is a noop
61
80
 
62
81
  it "should write the list header" do
63
82
  @prot.write_list_begin(Thrift::Types::I16, 42)
64
- @trans.read(1000).should == [Thrift::Types::I16, 42].pack("cN")
83
+ @trans.read(@trans.available).should == [Thrift::Types::I16, 42].pack("cN")
65
84
  end
66
85
 
67
86
  # list footer is a noop
68
87
 
69
88
  it "should write the set header" do
70
89
  @prot.write_set_begin(Thrift::Types::I16, 42)
71
- @trans.read(1000).should == [Thrift::Types::I16, 42].pack("cN")
90
+ @trans.read(@trans.available).should == [Thrift::Types::I16, 42].pack("cN")
72
91
  end
73
92
 
74
93
  it "should write a bool" do
75
94
  @prot.write_bool(true)
76
95
  @prot.write_bool(false)
77
- @trans.read(1000).should == "\001\000"
96
+ @trans.read(@trans.available).should == "\001\000"
78
97
  end
79
98
 
80
99
  it "should treat a nil bool as false" do
@@ -88,8 +107,6 @@ shared_examples_for 'a binary protocol' do
88
107
  @prot.write_byte(i)
89
108
  @trans.read(1).should == [i].pack('c')
90
109
  end
91
- (-128..127).each do |i|
92
- end
93
110
  # handing it numbers out of signed range should clip
94
111
  @trans.rspec_verify
95
112
  (128..255).each do |i|
@@ -113,7 +130,7 @@ shared_examples_for 'a binary protocol' do
113
130
  # and try something out of signed range, it should clip
114
131
  @prot.write_i16(2**15 + 5)
115
132
 
116
- @trans.read(1000).should == "\200\000\374\000\000\021\000\000\330\360\006\273\177\377\200\005"
133
+ @trans.read(@trans.available).should == "\200\000\374\000\000\021\000\000\330\360\006\273\177\377\200\005"
117
134
 
118
135
  # a Bignum should error
119
136
  # lambda { @prot.write_i16(2**65) }.should raise_error(RangeError)
@@ -130,7 +147,7 @@ shared_examples_for 'a binary protocol' do
130
147
  @prot.write_i32(i)
131
148
  end
132
149
  # try something out of signed range, it should clip
133
- @trans.read(1000).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"
150
+ @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"
134
151
  [2 ** 31 + 5, 2 ** 65 + 5].each do |i|
135
152
  lambda { @prot.write_i32(i) }.should raise_error(RangeError)
136
153
  end
@@ -147,7 +164,7 @@ shared_examples_for 'a binary protocol' do
147
164
  @prot.write_i64(i)
148
165
  end
149
166
  # try something out of signed range, it should clip
150
- @trans.read(1000).should == ["\200\000\000\000\000\000\000\000",
167
+ @trans.read(@trans.available).should == ["\200\000\000\000\000\000\000\000",
151
168
  "\377\377\364\303\035\244+]",
152
169
  "\377\377\377\377\376\231:\341",
153
170
  "\377\377\377\377\377\377\377\026",
@@ -168,7 +185,7 @@ shared_examples_for 'a binary protocol' do
168
185
  values = [Float::MIN,-1231.15325, -123123.23, -23.23515123, 0, 12351.1325, 523.23, Float::MAX]
169
186
  values.each do |f|
170
187
  @prot.write_double(f)
171
- @trans.read(1000).should == [f].pack("G")
188
+ @trans.read(@trans.available).should == [f].pack("G")
172
189
  end
173
190
  end
174
191
 
@@ -179,13 +196,25 @@ shared_examples_for 'a binary protocol' do
179
196
  it "should write a string" do
180
197
  str = "hello world"
181
198
  @prot.write_string(str)
182
- @trans.read(1000).should == [str.size].pack("N") + str
199
+ @trans.read(@trans.available).should == [str.size].pack("N") + str
183
200
  end
184
201
 
185
202
  it "should error gracefully when trying to write a nil string" do
186
203
  lambda { @prot.write_string(nil) }.should raise_error
187
204
  end
188
205
 
206
+ it "should write the message header without version when writes are not strict" do
207
+ @prot = protocol_class.new(@trans, true, false) # no strict write
208
+ @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
209
+ @trans.read(@trans.available).should == "\000\000\000\vtestMessage\001\000\000\000\021"
210
+ end
211
+
212
+ it "should write the message header with a version when writes are strict" do
213
+ @prot = protocol_class.new(@trans) # strict write
214
+ @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
215
+ @trans.read(@trans.available).should == "\200\001\000\001\000\000\000\vtestMessage\000\000\000\021"
216
+ end
217
+
189
218
  # message footer is a noop
190
219
 
191
220
  it "should read a field header" do
@@ -271,4 +300,76 @@ shared_examples_for 'a binary protocol' do
271
300
  @trans.write([str.size].pack("N") + str)
272
301
  @prot.read_string.should == str
273
302
  end
303
+
304
+ it "should perform a complete rpc with no args or return" do
305
+ srv_test(
306
+ proc {|client| client.send_voidMethod()},
307
+ proc {|client| client.recv_voidMethod.should == nil}
308
+ )
309
+ end
310
+
311
+ it "should perform a complete rpc with a primitive return type" do
312
+ srv_test(
313
+ proc {|client| client.send_primitiveMethod()},
314
+ proc {|client| client.recv_primitiveMethod.should == 1}
315
+ )
316
+ end
317
+
318
+ it "should perform a complete rpc with a struct return type" do
319
+ srv_test(
320
+ proc {|client| client.send_structMethod()},
321
+ proc {|client|
322
+ result = client.recv_structMethod
323
+ result.set_byte_map = nil
324
+ result.map_byte_map = nil
325
+ result.should == Fixtures::COMPACT_PROTOCOL_TEST_STRUCT
326
+ }
327
+ )
328
+ end
329
+
330
+ def get_socket_connection
331
+ server = Thrift::ServerSocket.new("localhost", 9090)
332
+ server.listen
333
+
334
+ clientside = Thrift::Socket.new("localhost", 9090)
335
+ clientside.open
336
+ serverside = server.accept
337
+ [clientside, serverside, server]
338
+ end
339
+
340
+ def srv_test(firstblock, secondblock)
341
+ clientside, serverside, server = get_socket_connection
342
+
343
+ clientproto = protocol_class.new(clientside)
344
+ serverproto = protocol_class.new(serverside)
345
+
346
+ processor = Srv::Processor.new(SrvHandler.new)
347
+
348
+ client = Srv::Client.new(clientproto, clientproto)
349
+
350
+ # first block
351
+ firstblock.call(client)
352
+
353
+ processor.process(serverproto, serverproto)
354
+
355
+ # second block
356
+ secondblock.call(client)
357
+ ensure
358
+ clientside.close
359
+ serverside.close
360
+ server.close
361
+ end
362
+
363
+ class SrvHandler
364
+ def voidMethod()
365
+ end
366
+
367
+ def primitiveMethod
368
+ 1
369
+ end
370
+
371
+ def structMethod
372
+ Fixtures::COMPACT_PROTOCOL_TEST_STRUCT
373
+ end
374
+ end
274
375
  end
data/spec/client_spec.rb CHANGED
@@ -1,3 +1,22 @@
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
+
1
20
  require File.dirname(__FILE__) + '/spec_helper'
2
21
 
3
22
  class ThriftClientSpec < Spec::ExampleGroup
@@ -40,10 +59,10 @@ class ThriftClientSpec < Spec::ExampleGroup
40
59
  @prot.should_receive(:write_message_begin).with('testMessage2', MessageTypes::CALL, 1).ordered
41
60
  @prot.should_receive(:write_message_begin).with('testMessage3', MessageTypes::CALL, 2).ordered
42
61
  @prot.stub!(:write_message_end)
43
- @prot.stub!(:trans).and_return stub_everything("trans")
44
- @client.send_message('testMessage', stub_everything("args class"))
45
- @client.send_message('testMessage2', stub_everything("args class"))
46
- @client.send_message('testMessage3', stub_everything("args class"))
62
+ @prot.stub!(:trans).and_return mock("trans").as_null_object
63
+ @client.send_message('testMessage', mock("args class").as_null_object)
64
+ @client.send_message('testMessage2', mock("args class").as_null_object)
65
+ @client.send_message('testMessage3', mock("args class").as_null_object)
47
66
  end
48
67
  end
49
68