thrift-mavericks 0.8.0 → 0.9.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +8 -8
  2. data/ext/binary_protocol_accelerated.c +21 -2
  3. data/ext/bytes.c +36 -0
  4. data/ext/bytes.h +31 -0
  5. data/ext/compact_protocol.c +24 -7
  6. data/ext/constants.h +5 -5
  7. data/ext/extconf.rb +3 -1
  8. data/ext/memory_buffer.c +11 -8
  9. data/ext/protocol.c +0 -185
  10. data/ext/protocol.h +0 -20
  11. data/ext/struct.c +0 -3
  12. data/ext/thrift_native.c +10 -11
  13. data/lib/thrift.rb +3 -0
  14. data/lib/thrift/bytes.rb +131 -0
  15. data/lib/thrift/exceptions.rb +3 -0
  16. data/lib/thrift/protocol/base_protocol.rb +96 -9
  17. data/lib/thrift/protocol/binary_protocol.rb +15 -7
  18. data/lib/thrift/protocol/compact_protocol.rb +14 -6
  19. data/lib/thrift/protocol/json_protocol.rb +766 -0
  20. data/lib/thrift/server/mongrel_http_server.rb +2 -0
  21. data/lib/thrift/server/thin_http_server.rb +91 -0
  22. data/lib/thrift/struct.rb +1 -1
  23. data/lib/thrift/struct_union.rb +2 -2
  24. data/lib/thrift/transport/base_transport.rb +22 -20
  25. data/lib/thrift/transport/buffered_transport.rb +16 -10
  26. data/lib/thrift/transport/framed_transport.rb +11 -10
  27. data/lib/thrift/transport/http_client_transport.rb +11 -6
  28. data/lib/thrift/transport/io_stream_transport.rb +1 -1
  29. data/lib/thrift/transport/memory_buffer_transport.rb +6 -6
  30. data/lib/thrift/transport/socket.rb +4 -2
  31. data/spec/ThriftSpec.thrift +52 -1
  32. data/spec/base_protocol_spec.rb +108 -51
  33. data/spec/base_transport_spec.rb +49 -50
  34. data/spec/binary_protocol_accelerated_spec.rb +9 -13
  35. data/spec/binary_protocol_spec.rb +15 -10
  36. data/spec/binary_protocol_spec_shared.rb +92 -12
  37. data/spec/bytes_spec.rb +160 -0
  38. data/spec/client_spec.rb +13 -14
  39. data/spec/compact_protocol_spec.rb +4 -5
  40. data/spec/exception_spec.rb +39 -40
  41. data/spec/gen-rb/thrift_spec_types.rb +192 -0
  42. data/spec/http_client_spec.rb +65 -9
  43. data/spec/json_protocol_spec.rb +513 -0
  44. data/spec/nonblocking_server_spec.rb +18 -20
  45. data/spec/processor_spec.rb +13 -16
  46. data/spec/serializer_spec.rb +17 -19
  47. data/spec/server_socket_spec.rb +6 -7
  48. data/spec/server_spec.rb +46 -58
  49. data/spec/socket_spec.rb +11 -11
  50. data/spec/socket_spec_shared.rb +1 -1
  51. data/spec/spec_helper.rb +13 -10
  52. data/spec/struct_nested_containers_spec.rb +191 -0
  53. data/spec/struct_spec.rb +84 -86
  54. data/spec/thin_http_server_spec.rb +140 -0
  55. data/spec/types_spec.rb +65 -66
  56. data/spec/union_spec.rb +57 -47
  57. data/spec/unix_socket_spec.rb +8 -9
  58. metadata +72 -14
  59. data/spec/mongrel_http_server_spec.rb +0 -117
@@ -17,27 +17,23 @@
17
17
  # under the License.
18
18
  #
19
19
 
20
- require File.expand_path("#{File.dirname(__FILE__)}/spec_helper")
20
+ require 'spec_helper'
21
21
  require File.expand_path("#{File.dirname(__FILE__)}/binary_protocol_spec_shared")
22
22
 
23
23
  if defined? Thrift::BinaryProtocolAccelerated
24
24
 
25
- class ThriftBinaryProtocolAcceleratedSpec < Spec::ExampleGroup
26
- include Thrift
25
+ describe 'BinaryProtocolAccelerated' do
26
+ # since BinaryProtocolAccelerated should be directly equivalent to
27
+ # BinaryProtocol, we don't need any custom specs!
28
+ it_should_behave_like 'a binary protocol'
27
29
 
28
- describe Thrift::BinaryProtocolAccelerated do
29
- # since BinaryProtocolAccelerated should be directly equivalent to
30
- # BinaryProtocol, we don't need any custom specs!
31
- it_should_behave_like 'a binary protocol'
32
-
33
- def protocol_class
34
- BinaryProtocolAccelerated
35
- end
30
+ def protocol_class
31
+ Thrift::BinaryProtocolAccelerated
36
32
  end
37
33
 
38
- describe BinaryProtocolAcceleratedFactory do
34
+ describe Thrift::BinaryProtocolAcceleratedFactory do
39
35
  it "should create a BinaryProtocolAccelerated" do
40
- BinaryProtocolAcceleratedFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(BinaryProtocolAccelerated)
36
+ Thrift::BinaryProtocolAcceleratedFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(Thrift::BinaryProtocolAccelerated)
41
37
  end
42
38
  end
43
39
  end
@@ -17,17 +17,22 @@
17
17
  # under the License.
18
18
  #
19
19
 
20
- require File.expand_path("#{File.dirname(__FILE__)}/spec_helper")
20
+ require 'spec_helper'
21
21
  require File.expand_path("#{File.dirname(__FILE__)}/binary_protocol_spec_shared")
22
22
 
23
- class ThriftBinaryProtocolSpec < Spec::ExampleGroup
24
- include Thrift
23
+ describe 'BinaryProtocol' do
25
24
 
26
- describe BinaryProtocol do
27
- it_should_behave_like 'a binary protocol'
25
+ it_should_behave_like 'a binary protocol'
28
26
 
29
- def protocol_class
30
- BinaryProtocol
27
+ def protocol_class
28
+ Thrift::BinaryProtocol
29
+ end
30
+
31
+ describe Thrift::BinaryProtocol do
32
+
33
+ before(:each) do
34
+ @trans = Thrift::MemoryBufferTransport.new
35
+ @prot = protocol_class.new(@trans)
31
36
  end
32
37
 
33
38
  it "should read a message header" do
@@ -47,15 +52,15 @@ class ThriftBinaryProtocolSpec < Spec::ExampleGroup
47
52
  it "should raise an exception if the message header does not exist and strict_read is enabled" do
48
53
  @prot.should_receive(:read_i32).and_return(42)
49
54
  @prot.should_receive(:strict_read).and_return(true)
50
- lambda { @prot.read_message_begin }.should raise_error(Thrift::ProtocolException, 'No version identifier, old protocol client?') do |e|
55
+ lambda { @prot.read_message_begin }.should raise_error(Thrift::ProtocolException, 'No version identifier, old protocol client?') do |e|
51
56
  e.type == Thrift::ProtocolException::BAD_VERSION
52
57
  end
53
58
  end
54
59
  end
55
60
 
56
- describe BinaryProtocolFactory do
61
+ describe Thrift::BinaryProtocolFactory do
57
62
  it "should create a BinaryProtocol" do
58
- BinaryProtocolFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(BinaryProtocol)
63
+ Thrift::BinaryProtocolFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(Thrift::BinaryProtocol)
59
64
  end
60
65
  end
61
66
  end
@@ -1,3 +1,4 @@
1
+ # encoding: ascii-8bit
1
2
  #
2
3
  # Licensed to the Apache Software Foundation (ASF) under one
3
4
  # or more contributor license agreements. See the NOTICE file
@@ -17,7 +18,7 @@
17
18
  # under the License.
18
19
  #
19
20
 
20
- require File.expand_path("#{File.dirname(__FILE__)}/spec_helper")
21
+ require 'spec_helper'
21
22
 
22
23
  shared_examples_for 'a binary protocol' do
23
24
  before(:each) do
@@ -192,13 +193,56 @@ shared_examples_for 'a binary protocol' do
192
193
  it "should error gracefully when trying to write a nil double" do
193
194
  lambda { @prot.write_double(nil) }.should raise_error
194
195
  end
195
-
196
- it "should write a string" do
197
- str = "hello world"
198
- @prot.write_string(str)
199
- @trans.read(@trans.available).should == [str.size].pack("N") + str
196
+
197
+ if RUBY_VERSION >= '1.9'
198
+ it 'should write a string' do
199
+ str = 'abc'
200
+ @prot.write_string(str)
201
+ a = @trans.read(@trans.available)
202
+ a.encoding.should == Encoding::BINARY
203
+ a.unpack('C*').should == [0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63]
204
+ end
205
+
206
+ it 'should write a string with unicode characters' do
207
+ str = "abc \u20AC \u20AD".encode('UTF-8')
208
+ @prot.write_string(str)
209
+ a = @trans.read(@trans.available)
210
+ a.encoding.should == Encoding::BINARY
211
+ a.unpack('C*').should == [0x00, 0x00, 0x00, 0x0B, 0x61, 0x62, 0x63, 0x20,
212
+ 0xE2, 0x82, 0xAC, 0x20, 0xE2, 0x82, 0xAD]
213
+ end
214
+
215
+ it 'should write should write a string with unicode characters and transcoding' do
216
+ str = "abc \u20AC".encode('ISO-8859-15')
217
+ @prot.write_string(str)
218
+ a = @trans.read(@trans.available)
219
+ a.encoding.should == Encoding::BINARY
220
+ a.unpack('C*').should == [0x00, 0x00, 0x00, 0x07, 0x61, 0x62, 0x63, 0x20, 0xE2, 0x82, 0xAC]
221
+ end
222
+
223
+ it 'should write a binary string' do
224
+ buffer = [0, 1, 2, 3].pack('C*')
225
+ @prot.write_binary(buffer)
226
+ a = @trans.read(@trans.available)
227
+ a.encoding.should == Encoding::BINARY
228
+ a.unpack('C*').should == [0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03]
229
+ end
230
+ else
231
+ it 'should write a string' do
232
+ str = 'abc'
233
+ @prot.write_string(str)
234
+ a = @trans.read(@trans.available)
235
+ a.unpack('C*').should == [0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63]
236
+ end
237
+
238
+ it 'should write a binary string' do
239
+ buffer = [0, 1, 2, 3].pack('C*')
240
+ @prot.write_binary(buffer)
241
+ a = @trans.read(@trans.available)
242
+ a.unpack('C*').should == [0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03]
243
+ end
200
244
  end
201
-
245
+
202
246
  it "should error gracefully when trying to write a nil string" do
203
247
  lambda { @prot.write_string(nil) }.should raise_error
204
248
  end
@@ -294,11 +338,47 @@ shared_examples_for 'a binary protocol' do
294
338
  @prot.read_double.should == f
295
339
  end
296
340
  end
297
-
298
- it "should read a string" do
299
- str = "hello world"
300
- @trans.write([str.size].pack("N") + str)
301
- @prot.read_string.should == str
341
+
342
+ if RUBY_VERSION >= '1.9'
343
+ it 'should read a string' do
344
+ # i32 of value 3, followed by three characters/UTF-8 bytes 'a', 'b', 'c'
345
+ buffer = [0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63].pack('C*')
346
+ @trans.write(buffer)
347
+ a = @prot.read_string
348
+ a.should == 'abc'.encode('UTF-8')
349
+ a.encoding.should == Encoding::UTF_8
350
+ end
351
+
352
+ it 'should read a string containing unicode characters from UTF-8 encoded buffer' do
353
+ # i32 of value 3, followed by one character U+20AC made up of three bytes
354
+ buffer = [0x00, 0x00, 0x00, 0x03, 0xE2, 0x82, 0xAC].pack('C*')
355
+ @trans.write(buffer)
356
+ a = @prot.read_string
357
+ a.should == "\u20AC".encode('UTF-8')
358
+ a.encoding.should == Encoding::UTF_8
359
+ end
360
+
361
+ it 'should read a binary string' do
362
+ buffer = [0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03].pack('C*')
363
+ @trans.write(buffer)
364
+ a = @prot.read_binary
365
+ a.should == [0x00, 0x01, 0x02, 0x03].pack('C*')
366
+ a.encoding.should == Encoding::BINARY
367
+ end
368
+ else
369
+ it 'should read a string' do
370
+ # i32 of value 3, followed by three characters/UTF-8 bytes 'a', 'b', 'c'
371
+ buffer = [0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63].pack('C*')
372
+ @trans.write(buffer)
373
+ @prot.read_string.should == 'abc'
374
+ end
375
+
376
+ it 'should read a binary string' do
377
+ buffer = [0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03].pack('C*')
378
+ @trans.write(buffer)
379
+ a = @prot.read_binary
380
+ a.should == [0x00, 0x01, 0x02, 0x03].pack('C*')
381
+ end
302
382
  end
303
383
 
304
384
  it "should perform a complete rpc with no args or return" do
@@ -0,0 +1,160 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Licensed to the Apache Software Foundation (ASF) under one
4
+ # or more contributor license agreements. See the NOTICE file
5
+ # distributed with this work for additional information
6
+ # regarding copyright ownership. The ASF licenses this file
7
+ # to you under the Apache License, Version 2.0 (the
8
+ # "License"); you may not use this file except in compliance
9
+ # with the License. You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing,
14
+ # software distributed under the License is distributed on an
15
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ # KIND, either express or implied. See the License for the
17
+ # specific language governing permissions and limitations
18
+ # under the License.
19
+ #
20
+
21
+ require 'spec_helper'
22
+
23
+ describe Thrift::Bytes do
24
+ if RUBY_VERSION >= '1.9'
25
+ describe '.empty_byte_buffer' do
26
+ it 'should create an empty buffer' do
27
+ b = Thrift::Bytes.empty_byte_buffer
28
+ b.length.should == 0
29
+ b.encoding.should == Encoding::BINARY
30
+ end
31
+
32
+ it 'should create an empty buffer of given size' do
33
+ b = Thrift::Bytes.empty_byte_buffer 2
34
+ b.length.should == 2
35
+ b.getbyte(0).should == 0
36
+ b.getbyte(1).should == 0
37
+ b.encoding.should == Encoding::BINARY
38
+ end
39
+ end
40
+
41
+ describe '.force_binary_encoding' do
42
+ it 'should change encoding' do
43
+ e = 'STRING'.encode('UTF-8')
44
+ e.encoding.should_not == Encoding::BINARY
45
+ a = Thrift::Bytes.force_binary_encoding e
46
+ a.encoding.should == Encoding::BINARY
47
+ end
48
+ end
49
+
50
+ describe '.get_string_byte' do
51
+ it 'should get the byte at index' do
52
+ s = "\x41\x42"
53
+ Thrift::Bytes.get_string_byte(s, 0).should == 0x41
54
+ Thrift::Bytes.get_string_byte(s, 1).should == 0x42
55
+ end
56
+ end
57
+
58
+ describe '.set_string_byte' do
59
+ it 'should set byte value at index' do
60
+ s = "\x41\x42"
61
+ Thrift::Bytes.set_string_byte(s, 0, 0x43)
62
+ s.getbyte(0).should == 0x43
63
+ s.should == 'CB'
64
+ end
65
+ end
66
+
67
+ describe '.convert_to_utf8_byte_buffer' do
68
+ it 'should convert UTF-8 String to byte buffer' do
69
+ e = "\u20AC".encode('UTF-8') # a string with euro sign character U+20AC
70
+ e.length.should == 1
71
+
72
+ a = Thrift::Bytes.convert_to_utf8_byte_buffer e
73
+ a.encoding.should == Encoding::BINARY
74
+ a.length.should == 3
75
+ a.unpack('C*').should == [0xE2, 0x82, 0xAC]
76
+ end
77
+
78
+ it 'should convert ISO-8859-15 String to UTF-8 byte buffer' do
79
+ # Assumptions
80
+ e = "\u20AC".encode('ISO-8859-15') # a string with euro sign character U+20AC, then converted to ISO-8859-15
81
+ e.length.should == 1
82
+ e.unpack('C*').should == [0xA4] # euro sign is a different code point in ISO-8859-15
83
+
84
+ a = Thrift::Bytes.convert_to_utf8_byte_buffer e
85
+ a.encoding.should == Encoding::BINARY
86
+ a.length.should == 3
87
+ a.unpack('C*').should == [0xE2, 0x82, 0xAC]
88
+ end
89
+ end
90
+
91
+ describe '.convert_to_string' do
92
+ it 'should convert UTF-8 byte buffer to a UTF-8 String' do
93
+ e = [0xE2, 0x82, 0xAC].pack("C*")
94
+ e.encoding.should == Encoding::BINARY
95
+ a = Thrift::Bytes.convert_to_string e
96
+ a.encoding.should == Encoding::UTF_8
97
+ a.should == "\u20AC"
98
+ end
99
+ end
100
+
101
+ else # RUBY_VERSION
102
+ describe '.empty_byte_buffer' do
103
+ it 'should create an empty buffer' do
104
+ b = Thrift::Bytes.empty_byte_buffer
105
+ b.length.should == 0
106
+ end
107
+
108
+ it 'should create an empty buffer of given size' do
109
+ b = Thrift::Bytes.empty_byte_buffer 2
110
+ b.length.should == 2
111
+ b[0].should == 0
112
+ b[1].should == 0
113
+ end
114
+ end
115
+
116
+ describe '.force_binary_encoding' do
117
+ it 'should be a no-op' do
118
+ e = 'STRING'
119
+ a = Thrift::Bytes.force_binary_encoding e
120
+ a.should == e
121
+ a.should be(e)
122
+ end
123
+ end
124
+
125
+ describe '.get_string_byte' do
126
+ it 'should get the byte at index' do
127
+ s = "\x41\x42"
128
+ Thrift::Bytes.get_string_byte(s, 0).should == 0x41
129
+ Thrift::Bytes.get_string_byte(s, 1).should == 0x42
130
+ end
131
+ end
132
+
133
+ describe '.set_string_byte' do
134
+ it 'should set byte value at index' do
135
+ s = "\x41\x42"
136
+ Thrift::Bytes.set_string_byte(s, 0, 0x43)
137
+ s[0].should == 0x43
138
+ s.should == 'CB'
139
+ end
140
+ end
141
+
142
+ describe '.convert_to_utf8_byte_buffer' do
143
+ it 'should be a no-op' do
144
+ e = 'STRING'
145
+ a = Thrift::Bytes.convert_to_utf8_byte_buffer e
146
+ a.should == e
147
+ a.should be(e)
148
+ end
149
+ end
150
+
151
+ describe '.convert_to_string' do
152
+ it 'should be a no-op' do
153
+ e = 'STRING'
154
+ a = Thrift::Bytes.convert_to_string e
155
+ a.should == e
156
+ a.should be(e)
157
+ end
158
+ end
159
+ end
160
+ end
data/spec/client_spec.rb CHANGED
@@ -17,10 +17,9 @@
17
17
  # under the License.
18
18
  #
19
19
 
20
- require File.expand_path("#{File.dirname(__FILE__)}/spec_helper")
20
+ require 'spec_helper'
21
21
 
22
- class ThriftClientSpec < Spec::ExampleGroup
23
- include Thrift
22
+ describe 'Client' do
24
23
 
25
24
  class ClientSpec
26
25
  include Thrift::Client
@@ -31,21 +30,21 @@ class ThriftClientSpec < Spec::ExampleGroup
31
30
  @client = ClientSpec.new(@prot)
32
31
  end
33
32
 
34
- describe Client do
33
+ describe Thrift::Client do
35
34
  it "should re-use iprot for oprot if not otherwise specified" do
36
35
  @client.instance_variable_get(:'@iprot').should eql(@prot)
37
36
  @client.instance_variable_get(:'@oprot').should eql(@prot)
38
37
  end
39
38
 
40
39
  it "should send a test message" do
41
- @prot.should_receive(:write_message_begin).with('testMessage', MessageTypes::CALL, 0)
40
+ @prot.should_receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::CALL, 0)
42
41
  mock_args = mock('#<TestMessage_args:mock>')
43
42
  mock_args.should_receive(:foo=).with('foo')
44
43
  mock_args.should_receive(:bar=).with(42)
45
44
  mock_args.should_receive(:write).with(@prot)
46
45
  @prot.should_receive(:write_message_end)
47
46
  @prot.should_receive(:trans) do
48
- mock('trans').tee do |trans|
47
+ mock('trans').tap do |trans|
49
48
  trans.should_receive(:flush)
50
49
  end
51
50
  end
@@ -55,19 +54,19 @@ class ThriftClientSpec < Spec::ExampleGroup
55
54
 
56
55
  it "should increment the sequence id when sending messages" do
57
56
  pending "it seems sequence ids are completely ignored right now" do
58
- @prot.should_receive(:write_message_begin).with('testMessage', MessageTypes::CALL, 0).ordered
59
- @prot.should_receive(:write_message_begin).with('testMessage2', MessageTypes::CALL, 1).ordered
60
- @prot.should_receive(:write_message_begin).with('testMessage3', MessageTypes::CALL, 2).ordered
57
+ @prot.should_receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::CALL, 0).ordered
58
+ @prot.should_receive(:write_message_begin).with('testMessage2', Thrift::MessageTypes::CALL, 1).ordered
59
+ @prot.should_receive(:write_message_begin).with('testMessage3', Thrift::MessageTypes::CALL, 2).ordered
61
60
  @prot.stub!(:write_message_end)
62
61
  @prot.stub!(:trans).and_return mock("trans").as_null_object
63
62
  @client.send_message('testMessage', mock("args class").as_null_object)
64
63
  @client.send_message('testMessage2', mock("args class").as_null_object)
65
- @client.send_message('testMessage3', mock("args class").as_null_object)
64
+ @client.send_message('testMessage3', mock("args class").as_null_object)
66
65
  end
67
66
  end
68
67
 
69
68
  it "should receive a test message" do
70
- @prot.should_receive(:read_message_begin).and_return [nil, MessageTypes::CALL, 0]
69
+ @prot.should_receive(:read_message_begin).and_return [nil, Thrift::MessageTypes::CALL, 0]
71
70
  @prot.should_receive(:read_message_end)
72
71
  mock_klass = mock("#<MockClass:mock>")
73
72
  mock_klass.should_receive(:read).with(@prot)
@@ -75,10 +74,10 @@ class ThriftClientSpec < Spec::ExampleGroup
75
74
  end
76
75
 
77
76
  it "should handle received exceptions" do
78
- @prot.should_receive(:read_message_begin).and_return [nil, MessageTypes::EXCEPTION, 0]
77
+ @prot.should_receive(:read_message_begin).and_return [nil, Thrift::MessageTypes::EXCEPTION, 0]
79
78
  @prot.should_receive(:read_message_end)
80
- ApplicationException.should_receive(:new).and_return do
81
- StandardError.new.tee do |mock_exc|
79
+ Thrift::ApplicationException.should_receive(:new).and_return do
80
+ StandardError.new.tap do |mock_exc|
82
81
  mock_exc.should_receive(:read).with(@prot)
83
82
  end
84
83
  end