thrift 0.8.0 → 0.13.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 (99) hide show
  1. checksums.yaml +7 -0
  2. data/{README → README.md} +0 -0
  3. data/ext/binary_protocol_accelerated.c +33 -14
  4. data/ext/bytes.c +36 -0
  5. data/ext/bytes.h +31 -0
  6. data/ext/compact_protocol.c +27 -8
  7. data/ext/constants.h +8 -5
  8. data/ext/extconf.rb +5 -1
  9. data/ext/memory_buffer.c +12 -9
  10. data/ext/protocol.c +0 -185
  11. data/ext/protocol.h +0 -20
  12. data/ext/strlcpy.h +7 -3
  13. data/ext/struct.c +27 -7
  14. data/ext/thrift_native.c +16 -11
  15. data/lib/thrift.rb +10 -4
  16. data/lib/thrift/bytes.rb +131 -0
  17. data/lib/thrift/client.rb +13 -4
  18. data/lib/thrift/exceptions.rb +3 -0
  19. data/lib/thrift/multiplexed_processor.rb +76 -0
  20. data/lib/thrift/processor.rb +24 -6
  21. data/lib/thrift/protocol/base_protocol.rb +109 -12
  22. data/lib/thrift/protocol/binary_protocol.rb +22 -7
  23. data/lib/thrift/protocol/binary_protocol_accelerated.rb +8 -0
  24. data/lib/thrift/protocol/compact_protocol.rb +23 -6
  25. data/lib/thrift/protocol/json_protocol.rb +786 -0
  26. data/lib/thrift/protocol/multiplexed_protocol.rb +44 -0
  27. data/lib/thrift/protocol/protocol_decorator.rb +194 -0
  28. data/lib/thrift/server/base_server.rb +8 -2
  29. data/lib/thrift/server/mongrel_http_server.rb +2 -0
  30. data/lib/thrift/server/simple_server.rb +5 -1
  31. data/lib/thrift/server/thin_http_server.rb +91 -0
  32. data/lib/thrift/server/thread_pool_server.rb +5 -1
  33. data/lib/thrift/server/threaded_server.rb +5 -1
  34. data/lib/thrift/struct.rb +1 -1
  35. data/lib/thrift/struct_union.rb +2 -2
  36. data/lib/thrift/transport/base_server_transport.rb +1 -1
  37. data/lib/thrift/transport/base_transport.rb +30 -20
  38. data/lib/thrift/transport/buffered_transport.rb +25 -11
  39. data/lib/thrift/transport/framed_transport.rb +20 -11
  40. data/lib/thrift/transport/http_client_transport.rb +16 -6
  41. data/lib/thrift/transport/io_stream_transport.rb +5 -2
  42. data/lib/thrift/transport/memory_buffer_transport.rb +10 -6
  43. data/lib/thrift/transport/server_socket.rb +6 -1
  44. data/lib/thrift/transport/socket.rb +23 -17
  45. data/lib/thrift/transport/ssl_server_socket.rb +41 -0
  46. data/lib/thrift/transport/ssl_socket.rb +51 -0
  47. data/lib/thrift/transport/unix_server_socket.rb +5 -1
  48. data/lib/thrift/transport/unix_socket.rb +5 -1
  49. data/lib/thrift/union.rb +3 -6
  50. data/spec/BaseService.thrift +27 -0
  51. data/spec/ExtendedService.thrift +25 -0
  52. data/spec/Referenced.thrift +44 -0
  53. data/spec/ThriftNamespacedSpec.thrift +53 -0
  54. data/spec/ThriftSpec.thrift +52 -1
  55. data/spec/base_protocol_spec.rb +158 -93
  56. data/spec/base_transport_spec.rb +194 -157
  57. data/spec/binary_protocol_accelerated_spec.rb +14 -14
  58. data/spec/binary_protocol_spec.rb +29 -16
  59. data/spec/binary_protocol_spec_shared.rb +148 -65
  60. data/spec/bytes_spec.rb +160 -0
  61. data/spec/client_spec.rb +45 -47
  62. data/spec/compact_protocol_spec.rb +36 -22
  63. data/spec/exception_spec.rb +79 -80
  64. data/spec/flat_spec.rb +62 -0
  65. data/spec/http_client_spec.rb +91 -16
  66. data/spec/json_protocol_spec.rb +552 -0
  67. data/spec/namespaced_spec.rb +67 -0
  68. data/spec/nonblocking_server_spec.rb +26 -28
  69. data/spec/processor_spec.rb +29 -32
  70. data/spec/serializer_spec.rb +31 -33
  71. data/spec/server_socket_spec.rb +32 -28
  72. data/spec/server_spec.rb +112 -84
  73. data/spec/socket_spec.rb +27 -20
  74. data/spec/socket_spec_shared.rb +32 -32
  75. data/spec/spec_helper.rb +17 -11
  76. data/spec/ssl_server_socket_spec.rb +34 -0
  77. data/spec/ssl_socket_spec.rb +78 -0
  78. data/spec/struct_nested_containers_spec.rb +191 -0
  79. data/spec/struct_spec.rb +159 -161
  80. data/spec/thin_http_server_spec.rb +141 -0
  81. data/spec/types_spec.rb +71 -69
  82. data/spec/union_spec.rb +97 -76
  83. data/spec/unix_socket_spec.rb +49 -41
  84. metadata +268 -188
  85. data/CHANGELOG +0 -1
  86. data/benchmark/gen-rb/benchmark_constants.rb +0 -10
  87. data/benchmark/gen-rb/benchmark_service.rb +0 -80
  88. data/benchmark/gen-rb/benchmark_types.rb +0 -9
  89. data/spec/gen-rb/nonblocking_service.rb +0 -272
  90. data/spec/gen-rb/thrift_spec_constants.rb +0 -10
  91. data/spec/gen-rb/thrift_spec_types.rb +0 -345
  92. data/spec/mongrel_http_server_spec.rb +0 -117
  93. data/test/debug_proto/gen-rb/debug_proto_test_constants.rb +0 -273
  94. data/test/debug_proto/gen-rb/debug_proto_test_types.rb +0 -760
  95. data/test/debug_proto/gen-rb/empty_service.rb +0 -24
  96. data/test/debug_proto/gen-rb/inherited.rb +0 -79
  97. data/test/debug_proto/gen-rb/reverse_order_service.rb +0 -82
  98. data/test/debug_proto/gen-rb/service_for_exception_with_a_map.rb +0 -81
  99. data/test/debug_proto/gen-rb/srv.rb +0 -330
@@ -17,30 +17,30 @@
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
+ expect(Thrift::BinaryProtocolAcceleratedFactory.new.get_protocol(double("MockTransport"))).to be_instance_of(Thrift::BinaryProtocolAccelerated)
37
+ end
38
+
39
+ it "should provide a reasonable to_s" do
40
+ expect(Thrift::BinaryProtocolAcceleratedFactory.new.to_s).to eq("binary-accel")
41
41
  end
42
42
  end
43
43
  end
44
44
  else
45
45
  puts "skipping BinaryProtocolAccelerated spec because it is not defined."
46
- end
46
+ end
@@ -17,45 +17,58 @@
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
34
39
  @trans.write([protocol_class.const_get(:VERSION_1) | Thrift::MessageTypes::REPLY].pack('N'))
35
40
  @trans.write([42].pack('N'))
36
- @prot.should_receive(:read_string).and_return('testMessage')
37
- @prot.read_message_begin.should == ['testMessage', Thrift::MessageTypes::REPLY, 42]
41
+ expect(@prot).to receive(:read_string).and_return('testMessage')
42
+ expect(@prot.read_message_begin).to eq(['testMessage', Thrift::MessageTypes::REPLY, 42])
38
43
  end
39
44
 
40
45
  it "should raise an exception if the message header has the wrong version" do
41
- @prot.should_receive(:read_i32).and_return(-1)
42
- lambda { @prot.read_message_begin }.should raise_error(Thrift::ProtocolException, 'Missing version identifier') do |e|
46
+ expect(@prot).to receive(:read_i32).and_return(-1)
47
+ expect { @prot.read_message_begin }.to raise_error(Thrift::ProtocolException, 'Missing version identifier') do |e|
43
48
  e.type == Thrift::ProtocolException::BAD_VERSION
44
49
  end
45
50
  end
46
51
 
47
52
  it "should raise an exception if the message header does not exist and strict_read is enabled" do
48
- @prot.should_receive(:read_i32).and_return(42)
49
- @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|
53
+ expect(@prot).to receive(:read_i32).and_return(42)
54
+ expect(@prot).to receive(:strict_read).and_return(true)
55
+ expect { @prot.read_message_begin }.to 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
59
+
60
+ it "should provide a reasonable to_s" do
61
+ expect(@prot.to_s).to eq("binary(memory)")
62
+ end
54
63
  end
55
64
 
56
- describe BinaryProtocolFactory do
65
+ describe Thrift::BinaryProtocolFactory do
57
66
  it "should create a BinaryProtocol" do
58
- BinaryProtocolFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(BinaryProtocol)
67
+ expect(Thrift::BinaryProtocolFactory.new.get_protocol(double("MockTransport"))).to be_instance_of(Thrift::BinaryProtocol)
68
+ end
69
+
70
+ it "should provide a reasonable to_s" do
71
+ expect(Thrift::BinaryProtocolFactory.new.to_s).to eq("binary")
59
72
  end
60
73
  end
61
74
  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
@@ -26,34 +27,34 @@ shared_examples_for 'a binary protocol' do
26
27
  end
27
28
 
28
29
  it "should define the proper VERSION_1, VERSION_MASK AND TYPE_MASK" do
29
- protocol_class.const_get(:VERSION_MASK).should == 0xffff0000
30
- protocol_class.const_get(:VERSION_1).should == 0x80010000
31
- protocol_class.const_get(:TYPE_MASK).should == 0x000000ff
30
+ expect(protocol_class.const_get(:VERSION_MASK)).to eq(0xffff0000)
31
+ expect(protocol_class.const_get(:VERSION_1)).to eq(0x80010000)
32
+ expect(protocol_class.const_get(:TYPE_MASK)).to eq(0x000000ff)
32
33
  end
33
34
 
34
35
  it "should make strict_read readable" do
35
- @prot.strict_read.should eql(true)
36
+ expect(@prot.strict_read).to eql(true)
36
37
  end
37
38
 
38
39
  it "should make strict_write readable" do
39
- @prot.strict_write.should eql(true)
40
+ expect(@prot.strict_write).to eql(true)
40
41
  end
41
42
 
42
43
  it "should write the message header" do
43
44
  @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
44
- @trans.read(@trans.available).should == [protocol_class.const_get(:VERSION_1) | Thrift::MessageTypes::CALL, "testMessage".size, "testMessage", 17].pack("NNa11N")
45
+ expect(@trans.read(@trans.available)).to eq([protocol_class.const_get(:VERSION_1) | Thrift::MessageTypes::CALL, "testMessage".size, "testMessage", 17].pack("NNa11N"))
45
46
  end
46
47
 
47
48
  it "should write the message header without version when writes are not strict" do
48
49
  @prot = protocol_class.new(@trans, true, false) # no strict write
49
50
  @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
50
- @trans.read(@trans.available).should == "\000\000\000\vtestMessage\001\000\000\000\021"
51
+ expect(@trans.read(@trans.available)).to eq("\000\000\000\vtestMessage\001\000\000\000\021")
51
52
  end
52
53
 
53
54
  it "should write the message header with a version when writes are strict" do
54
55
  @prot = protocol_class.new(@trans) # strict write
55
56
  @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
56
- @trans.read(@trans.available).should == "\200\001\000\001\000\000\000\vtestMessage\000\000\000\021"
57
+ expect(@trans.read(@trans.available)).to eq("\200\001\000\001\000\000\000\vtestMessage\000\000\000\021")
57
58
  end
58
59
 
59
60
 
@@ -61,64 +62,67 @@ shared_examples_for 'a binary protocol' do
61
62
 
62
63
  it "should write the field header" do
63
64
  @prot.write_field_begin('foo', Thrift::Types::DOUBLE, 3)
64
- @trans.read(@trans.available).should == [Thrift::Types::DOUBLE, 3].pack("cn")
65
+ expect(@trans.read(@trans.available)).to eq([Thrift::Types::DOUBLE, 3].pack("cn"))
65
66
  end
66
67
 
67
68
  # field footer is a noop
68
69
 
69
70
  it "should write the STOP field" do
70
71
  @prot.write_field_stop
71
- @trans.read(1).should == "\000"
72
+ expect(@trans.read(1)).to eq("\000")
72
73
  end
73
74
 
74
75
  it "should write the map header" do
75
76
  @prot.write_map_begin(Thrift::Types::STRING, Thrift::Types::LIST, 17)
76
- @trans.read(@trans.available).should == [Thrift::Types::STRING, Thrift::Types::LIST, 17].pack("ccN");
77
+ expect(@trans.read(@trans.available)).to eq([Thrift::Types::STRING, Thrift::Types::LIST, 17].pack("ccN"));
77
78
  end
78
79
 
79
80
  # map footer is a noop
80
81
 
81
82
  it "should write the list header" do
82
83
  @prot.write_list_begin(Thrift::Types::I16, 42)
83
- @trans.read(@trans.available).should == [Thrift::Types::I16, 42].pack("cN")
84
+ expect(@trans.read(@trans.available)).to eq([Thrift::Types::I16, 42].pack("cN"))
84
85
  end
85
86
 
86
87
  # list footer is a noop
87
88
 
88
89
  it "should write the set header" do
89
90
  @prot.write_set_begin(Thrift::Types::I16, 42)
90
- @trans.read(@trans.available).should == [Thrift::Types::I16, 42].pack("cN")
91
+ expect(@trans.read(@trans.available)).to eq([Thrift::Types::I16, 42].pack("cN"))
91
92
  end
92
93
 
93
94
  it "should write a bool" do
94
95
  @prot.write_bool(true)
95
96
  @prot.write_bool(false)
96
- @trans.read(@trans.available).should == "\001\000"
97
+ expect(@trans.read(@trans.available)).to eq("\001\000")
97
98
  end
98
99
 
99
100
  it "should treat a nil bool as false" do
100
101
  @prot.write_bool(nil)
101
- @trans.read(1).should == "\000"
102
+ expect(@trans.read(1)).to eq("\000")
102
103
  end
103
104
 
104
105
  it "should write a byte" do
105
106
  # byte is small enough, let's check -128..127
106
107
  (-128..127).each do |i|
107
108
  @prot.write_byte(i)
108
- @trans.read(1).should == [i].pack('c')
109
+ expect(@trans.read(1)).to eq([i].pack('c'))
109
110
  end
110
- # handing it numbers out of signed range should clip
111
- @trans.rspec_verify
111
+ end
112
+
113
+ it "should clip numbers out of signed range" do
112
114
  (128..255).each do |i|
113
115
  @prot.write_byte(i)
114
- @trans.read(1).should == [i].pack('c')
116
+ expect(@trans.read(1)).to eq([i].pack('c'))
115
117
  end
116
- # and lastly, a Bignum is going to error out
117
- lambda { @prot.write_byte(2**65) }.should raise_error(RangeError)
118
+ end
119
+
120
+ it "errors out with a Bignum" do
121
+ expect { @prot.write_byte(2**65) }.to raise_error(RangeError)
118
122
  end
119
123
 
120
124
  it "should error gracefully when trying to write a nil byte" do
121
- lambda { @prot.write_byte(nil) }.should raise_error
125
+ expect { @prot.write_byte(nil) }.to raise_error
122
126
  end
123
127
 
124
128
  it "should write an i16" do
@@ -130,14 +134,14 @@ shared_examples_for 'a binary protocol' do
130
134
  # and try something out of signed range, it should clip
131
135
  @prot.write_i16(2**15 + 5)
132
136
 
133
- @trans.read(@trans.available).should == "\200\000\374\000\000\021\000\000\330\360\006\273\177\377\200\005"
137
+ expect(@trans.read(@trans.available)).to eq("\200\000\374\000\000\021\000\000\330\360\006\273\177\377\200\005")
134
138
 
135
139
  # a Bignum should error
136
140
  # lambda { @prot.write_i16(2**65) }.should raise_error(RangeError)
137
141
  end
138
142
 
139
143
  it "should error gracefully when trying to write a nil i16" do
140
- lambda { @prot.write_i16(nil) }.should raise_error
144
+ expect { @prot.write_i16(nil) }.to raise_error
141
145
  end
142
146
 
143
147
  it "should write an i32" do
@@ -147,14 +151,14 @@ shared_examples_for 'a binary protocol' do
147
151
  @prot.write_i32(i)
148
152
  end
149
153
  # try something out of signed range, it should clip
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"
154
+ expect(@trans.read(@trans.available)).to eq("\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")
151
155
  [2 ** 31 + 5, 2 ** 65 + 5].each do |i|
152
- lambda { @prot.write_i32(i) }.should raise_error(RangeError)
156
+ expect { @prot.write_i32(i) }.to raise_error(RangeError)
153
157
  end
154
158
  end
155
159
 
156
160
  it "should error gracefully when trying to write a nil i32" do
157
- lambda { @prot.write_i32(nil) }.should raise_error
161
+ expect { @prot.write_i32(nil) }.to raise_error
158
162
  end
159
163
 
160
164
  it "should write an i64" do
@@ -164,7 +168,7 @@ shared_examples_for 'a binary protocol' do
164
168
  @prot.write_i64(i)
165
169
  end
166
170
  # try something out of signed range, it should clip
167
- @trans.read(@trans.available).should == ["\200\000\000\000\000\000\000\000",
171
+ expect(@trans.read(@trans.available)).to eq(["\200\000\000\000\000\000\000\000",
168
172
  "\377\377\364\303\035\244+]",
169
173
  "\377\377\377\377\376\231:\341",
170
174
  "\377\377\377\377\377\377\377\026",
@@ -172,12 +176,12 @@ shared_examples_for 'a binary protocol' do
172
176
  "\000\000\000\000\000\000\004\317",
173
177
  "\000\000\000\000\000#\340\204",
174
178
  "\000\000\000\002\340\311~\365",
175
- "\177\377\377\377\377\377\377\377"].join("")
176
- lambda { @prot.write_i64(2 ** 65 + 5) }.should raise_error(RangeError)
179
+ "\177\377\377\377\377\377\377\377"].join(""))
180
+ expect { @prot.write_i64(2 ** 65 + 5) }.to raise_error(RangeError)
177
181
  end
178
182
 
179
183
  it "should error gracefully when trying to write a nil i64" do
180
- lambda { @prot.write_i64(nil) }.should raise_error
184
+ expect { @prot.write_i64(nil) }.to raise_error
181
185
  end
182
186
 
183
187
  it "should write a double" do
@@ -185,81 +189,124 @@ shared_examples_for 'a binary protocol' do
185
189
  values = [Float::MIN,-1231.15325, -123123.23, -23.23515123, 0, 12351.1325, 523.23, Float::MAX]
186
190
  values.each do |f|
187
191
  @prot.write_double(f)
188
- @trans.read(@trans.available).should == [f].pack("G")
192
+ expect(@trans.read(@trans.available)).to eq([f].pack("G"))
189
193
  end
190
194
  end
191
195
 
192
196
  it "should error gracefully when trying to write a nil double" do
193
- lambda { @prot.write_double(nil) }.should raise_error
197
+ expect { @prot.write_double(nil) }.to raise_error
194
198
  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
199
+
200
+ if RUBY_VERSION >= '1.9'
201
+ it 'should write a string' do
202
+ str = 'abc'
203
+ @prot.write_string(str)
204
+ a = @trans.read(@trans.available)
205
+ expect(a.encoding).to eq(Encoding::BINARY)
206
+ expect(a.unpack('C*')).to eq([0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63])
207
+ end
208
+
209
+ it 'should write a string with unicode characters' do
210
+ str = "abc \u20AC \u20AD".encode('UTF-8')
211
+ @prot.write_string(str)
212
+ a = @trans.read(@trans.available)
213
+ expect(a.encoding).to eq(Encoding::BINARY)
214
+ expect(a.unpack('C*')).to eq([0x00, 0x00, 0x00, 0x0B, 0x61, 0x62, 0x63, 0x20,
215
+ 0xE2, 0x82, 0xAC, 0x20, 0xE2, 0x82, 0xAD])
216
+ end
217
+
218
+ it 'should write should write a string with unicode characters and transcoding' do
219
+ str = "abc \u20AC".encode('ISO-8859-15')
220
+ @prot.write_string(str)
221
+ a = @trans.read(@trans.available)
222
+ expect(a.encoding).to eq(Encoding::BINARY)
223
+ expect(a.unpack('C*')).to eq([0x00, 0x00, 0x00, 0x07, 0x61, 0x62, 0x63, 0x20, 0xE2, 0x82, 0xAC])
224
+ end
225
+
226
+ it 'should write a binary string' do
227
+ buffer = [0, 1, 2, 3].pack('C*')
228
+ @prot.write_binary(buffer)
229
+ a = @trans.read(@trans.available)
230
+ expect(a.encoding).to eq(Encoding::BINARY)
231
+ expect(a.unpack('C*')).to eq([0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03])
232
+ end
233
+ else
234
+ it 'should write a string' do
235
+ str = 'abc'
236
+ @prot.write_string(str)
237
+ a = @trans.read(@trans.available)
238
+ expect(a.unpack('C*')).to eq([0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63])
239
+ end
240
+
241
+ it 'should write a binary string' do
242
+ buffer = [0, 1, 2, 3].pack('C*')
243
+ @prot.write_binary(buffer)
244
+ a = @trans.read(@trans.available)
245
+ expect(a.unpack('C*')).to eq([0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03])
246
+ end
200
247
  end
201
-
248
+
202
249
  it "should error gracefully when trying to write a nil string" do
203
- lambda { @prot.write_string(nil) }.should raise_error
250
+ expect { @prot.write_string(nil) }.to raise_error
204
251
  end
205
252
 
206
253
  it "should write the message header without version when writes are not strict" do
207
254
  @prot = protocol_class.new(@trans, true, false) # no strict write
208
255
  @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
209
- @trans.read(@trans.available).should == "\000\000\000\vtestMessage\001\000\000\000\021"
256
+ expect(@trans.read(@trans.available)).to eq("\000\000\000\vtestMessage\001\000\000\000\021")
210
257
  end
211
258
 
212
259
  it "should write the message header with a version when writes are strict" do
213
260
  @prot = protocol_class.new(@trans) # strict write
214
261
  @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"
262
+ expect(@trans.read(@trans.available)).to eq("\200\001\000\001\000\000\000\vtestMessage\000\000\000\021")
216
263
  end
217
264
 
218
265
  # message footer is a noop
219
266
 
220
267
  it "should read a field header" do
221
268
  @trans.write([Thrift::Types::STRING, 3].pack("cn"))
222
- @prot.read_field_begin.should == [nil, Thrift::Types::STRING, 3]
269
+ expect(@prot.read_field_begin).to eq([nil, Thrift::Types::STRING, 3])
223
270
  end
224
271
 
225
272
  # field footer is a noop
226
273
 
227
274
  it "should read a stop field" do
228
275
  @trans.write([Thrift::Types::STOP].pack("c"));
229
- @prot.read_field_begin.should == [nil, Thrift::Types::STOP, 0]
276
+ expect(@prot.read_field_begin).to eq([nil, Thrift::Types::STOP, 0])
230
277
  end
231
278
 
232
279
  it "should read a map header" do
233
280
  @trans.write([Thrift::Types::DOUBLE, Thrift::Types::I64, 42].pack("ccN"))
234
- @prot.read_map_begin.should == [Thrift::Types::DOUBLE, Thrift::Types::I64, 42]
281
+ expect(@prot.read_map_begin).to eq([Thrift::Types::DOUBLE, Thrift::Types::I64, 42])
235
282
  end
236
283
 
237
284
  # map footer is a noop
238
285
 
239
286
  it "should read a list header" do
240
287
  @trans.write([Thrift::Types::STRING, 17].pack("cN"))
241
- @prot.read_list_begin.should == [Thrift::Types::STRING, 17]
288
+ expect(@prot.read_list_begin).to eq([Thrift::Types::STRING, 17])
242
289
  end
243
290
 
244
291
  # list footer is a noop
245
292
 
246
293
  it "should read a set header" do
247
294
  @trans.write([Thrift::Types::STRING, 17].pack("cN"))
248
- @prot.read_set_begin.should == [Thrift::Types::STRING, 17]
295
+ expect(@prot.read_set_begin).to eq([Thrift::Types::STRING, 17])
249
296
  end
250
297
 
251
298
  # set footer is a noop
252
299
 
253
300
  it "should read a bool" do
254
301
  @trans.write("\001\000");
255
- @prot.read_bool.should == true
256
- @prot.read_bool.should == false
302
+ expect(@prot.read_bool).to eq(true)
303
+ expect(@prot.read_bool).to eq(false)
257
304
  end
258
305
 
259
306
  it "should read a byte" do
260
307
  [-128, -57, -3, 0, 17, 24, 127].each do |i|
261
308
  @trans.write([i].pack("c"))
262
- @prot.read_byte.should == i
309
+ expect(@prot.read_byte).to eq(i)
263
310
  end
264
311
  end
265
312
 
@@ -267,7 +314,7 @@ shared_examples_for 'a binary protocol' do
267
314
  # try a scattering of values, including min/max
268
315
  [-2**15, -5237, -353, 0, 1527, 2234, 2**15-1].each do |i|
269
316
  @trans.write([i].pack("n"));
270
- @prot.read_i16.should == i
317
+ expect(@prot.read_i16).to eq(i)
271
318
  end
272
319
  end
273
320
 
@@ -275,7 +322,7 @@ shared_examples_for 'a binary protocol' do
275
322
  # try a scattering of values, including min/max
276
323
  [-2**31, -235125, -6236, 0, 2351, 123123, 2**31-1].each do |i|
277
324
  @trans.write([i].pack("N"))
278
- @prot.read_i32.should == i
325
+ expect(@prot.read_i32).to eq(i)
279
326
  end
280
327
  end
281
328
 
@@ -283,7 +330,7 @@ shared_examples_for 'a binary protocol' do
283
330
  # try a scattering of values, including min/max
284
331
  [-2**63, -123512312, -6346, 0, 32, 2346322323, 2**63-1].each do |i|
285
332
  @trans.write([i >> 32, i & 0xFFFFFFFF].pack("NN"))
286
- @prot.read_i64.should == i
333
+ expect(@prot.read_i64).to eq(i)
287
334
  end
288
335
  end
289
336
 
@@ -291,27 +338,63 @@ shared_examples_for 'a binary protocol' do
291
338
  # try a random scattering of values, including min/max
292
339
  [Float::MIN, -231231.12351, -323.233513, 0, 123.2351235, 2351235.12351235, Float::MAX].each do |f|
293
340
  @trans.write([f].pack("G"));
294
- @prot.read_double.should == f
341
+ expect(@prot.read_double).to eq(f)
295
342
  end
296
343
  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
344
+
345
+ if RUBY_VERSION >= '1.9'
346
+ it 'should read a string' do
347
+ # i32 of value 3, followed by three characters/UTF-8 bytes 'a', 'b', 'c'
348
+ buffer = [0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63].pack('C*')
349
+ @trans.write(buffer)
350
+ a = @prot.read_string
351
+ expect(a).to eq('abc'.encode('UTF-8'))
352
+ expect(a.encoding).to eq(Encoding::UTF_8)
353
+ end
354
+
355
+ it 'should read a string containing unicode characters from UTF-8 encoded buffer' do
356
+ # i32 of value 3, followed by one character U+20AC made up of three bytes
357
+ buffer = [0x00, 0x00, 0x00, 0x03, 0xE2, 0x82, 0xAC].pack('C*')
358
+ @trans.write(buffer)
359
+ a = @prot.read_string
360
+ expect(a).to eq("\u20AC".encode('UTF-8'))
361
+ expect(a.encoding).to eq(Encoding::UTF_8)
362
+ end
363
+
364
+ it 'should read a binary string' do
365
+ buffer = [0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03].pack('C*')
366
+ @trans.write(buffer)
367
+ a = @prot.read_binary
368
+ expect(a).to eq([0x00, 0x01, 0x02, 0x03].pack('C*'))
369
+ expect(a.encoding).to eq(Encoding::BINARY)
370
+ end
371
+ else
372
+ it 'should read a string' do
373
+ # i32 of value 3, followed by three characters/UTF-8 bytes 'a', 'b', 'c'
374
+ buffer = [0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63].pack('C*')
375
+ @trans.write(buffer)
376
+ expect(@prot.read_string).to eq('abc')
377
+ end
378
+
379
+ it 'should read a binary string' do
380
+ buffer = [0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03].pack('C*')
381
+ @trans.write(buffer)
382
+ a = @prot.read_binary
383
+ expect(a).to eq([0x00, 0x01, 0x02, 0x03].pack('C*'))
384
+ end
302
385
  end
303
386
 
304
387
  it "should perform a complete rpc with no args or return" do
305
388
  srv_test(
306
389
  proc {|client| client.send_voidMethod()},
307
- proc {|client| client.recv_voidMethod.should == nil}
390
+ proc {|client| expect(client.recv_voidMethod).to eq(nil)}
308
391
  )
309
392
  end
310
393
 
311
394
  it "should perform a complete rpc with a primitive return type" do
312
395
  srv_test(
313
396
  proc {|client| client.send_primitiveMethod()},
314
- proc {|client| client.recv_primitiveMethod.should == 1}
397
+ proc {|client| expect(client.recv_primitiveMethod).to eq(1)}
315
398
  )
316
399
  end
317
400
 
@@ -322,7 +405,7 @@ shared_examples_for 'a binary protocol' do
322
405
  result = client.recv_structMethod
323
406
  result.set_byte_map = nil
324
407
  result.map_byte_map = nil
325
- result.should == Fixtures::COMPACT_PROTOCOL_TEST_STRUCT
408
+ expect(result).to eq(Fixtures::COMPACT_PROTOCOL_TEST_STRUCT)
326
409
  }
327
410
  )
328
411
  end
@@ -343,9 +426,9 @@ shared_examples_for 'a binary protocol' do
343
426
  clientproto = protocol_class.new(clientside)
344
427
  serverproto = protocol_class.new(serverside)
345
428
 
346
- processor = Srv::Processor.new(SrvHandler.new)
429
+ processor = Thrift::Test::Srv::Processor.new(SrvHandler.new)
347
430
 
348
- client = Srv::Client.new(clientproto, clientproto)
431
+ client = Thrift::Test::Srv::Client.new(clientproto, clientproto)
349
432
 
350
433
  # first block
351
434
  firstblock.call(client)