thrift 0.24.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 +107 -4
  3. data/ext/binary_protocol_accelerated.c +52 -17
  4. data/ext/compact_protocol.c +135 -70
  5. data/ext/constants.h +3 -0
  6. data/ext/extconf.rb +4 -4
  7. data/ext/macros.h +11 -0
  8. data/ext/memory_buffer.c +35 -17
  9. data/ext/struct.c +185 -64
  10. data/ext/thrift_native.c +6 -0
  11. data/lib/thrift/bytes.rb +2 -2
  12. data/lib/thrift/client.rb +31 -22
  13. data/lib/thrift/exceptions.rb +10 -8
  14. data/lib/thrift/multiplexed_processor.rb +5 -6
  15. data/lib/thrift/processor.rb +24 -3
  16. data/lib/thrift/protocol/base_protocol.rb +53 -27
  17. data/lib/thrift/protocol/binary_protocol.rb +54 -40
  18. data/lib/thrift/protocol/binary_protocol_accelerated.rb +6 -2
  19. data/lib/thrift/protocol/compact_protocol.rb +193 -88
  20. data/lib/thrift/protocol/header_protocol.rb +14 -3
  21. data/lib/thrift/protocol/json_protocol.rb +146 -104
  22. data/lib/thrift/protocol/multiplexed_protocol.rb +2 -3
  23. data/lib/thrift/protocol/protocol_decorator.rb +5 -2
  24. data/lib/thrift/serializer/serializer.rb +6 -0
  25. data/lib/thrift/server/base_server.rb +10 -2
  26. data/lib/thrift/server/nonblocking_server.rb +49 -26
  27. data/lib/thrift/server/rack_application.rb +69 -0
  28. data/lib/thrift/server/simple_server.rb +27 -23
  29. data/lib/thrift/server/thin_http_server.rb +20 -44
  30. data/lib/thrift/server/thread_pool_server.rb +20 -17
  31. data/lib/thrift/server/threaded_server.rb +22 -26
  32. data/lib/thrift/struct.rb +33 -18
  33. data/lib/thrift/struct_union.rb +36 -30
  34. data/lib/thrift/transport/base_transport.rb +23 -5
  35. data/lib/thrift/transport/buffered_transport.rb +4 -4
  36. data/lib/thrift/transport/framed_transport.rb +32 -7
  37. data/lib/thrift/transport/header_transport.rb +130 -50
  38. data/lib/thrift/transport/http_client_transport.rb +27 -14
  39. data/lib/thrift/transport/io_stream_transport.rb +2 -1
  40. data/lib/thrift/transport/memory_buffer_transport.rb +8 -10
  41. data/lib/thrift/transport/server_socket.rb +4 -5
  42. data/lib/thrift/transport/socket.rb +20 -22
  43. data/lib/thrift/transport/ssl_server_socket.rb +64 -4
  44. data/lib/thrift/transport/ssl_socket.rb +32 -5
  45. data/lib/thrift/transport/unix_server_socket.rb +1 -1
  46. data/lib/thrift/transport/unix_socket.rb +4 -6
  47. data/lib/thrift/types.rb +22 -22
  48. data/lib/thrift/union.rb +20 -11
  49. data/lib/thrift/uuid.rb +8 -8
  50. data/lib/thrift.rb +39 -39
  51. metadata +5 -230
  52. data/benchmark/Benchmark.thrift +0 -24
  53. data/benchmark/benchmark.rb +0 -286
  54. data/benchmark/client.rb +0 -118
  55. data/benchmark/server.rb +0 -121
  56. data/benchmark/thin_server.rb +0 -46
  57. data/lib/thrift/server/mongrel_http_server.rb +0 -61
  58. data/spec/BaseService.thrift +0 -27
  59. data/spec/ExtendedService.thrift +0 -25
  60. data/spec/Referenced.thrift +0 -44
  61. data/spec/ThriftNamespacedSpec.thrift +0 -53
  62. data/spec/ThriftSpec.thrift +0 -203
  63. data/spec/base_protocol_spec.rb +0 -244
  64. data/spec/base_transport_spec.rb +0 -428
  65. data/spec/binary_protocol_accelerated_spec.rb +0 -47
  66. data/spec/binary_protocol_spec.rb +0 -73
  67. data/spec/binary_protocol_spec_shared.rb +0 -509
  68. data/spec/bytes_spec.rb +0 -117
  69. data/spec/client_spec.rb +0 -167
  70. data/spec/compact_protocol_spec.rb +0 -295
  71. data/spec/constants_demo_spec.rb +0 -102
  72. data/spec/exception_spec.rb +0 -141
  73. data/spec/flat_spec.rb +0 -63
  74. data/spec/header_protocol_spec.rb +0 -476
  75. data/spec/header_transport_spec.rb +0 -431
  76. data/spec/http_client_spec.rb +0 -160
  77. data/spec/json_protocol_spec.rb +0 -574
  78. data/spec/multiplexed_processor_spec.rb +0 -75
  79. data/spec/namespaced_spec.rb +0 -67
  80. data/spec/nonblocking_server_spec.rb +0 -429
  81. data/spec/processor_spec.rb +0 -80
  82. data/spec/recursion_depth_spec.rb +0 -223
  83. data/spec/serializer_spec.rb +0 -67
  84. data/spec/server_socket_spec.rb +0 -121
  85. data/spec/server_spec.rb +0 -238
  86. data/spec/socket_spec.rb +0 -174
  87. data/spec/socket_spec_shared.rb +0 -168
  88. data/spec/spec_helper.rb +0 -70
  89. data/spec/ssl_server_socket_spec.rb +0 -85
  90. data/spec/ssl_socket_spec.rb +0 -248
  91. data/spec/struct_nested_containers_spec.rb +0 -191
  92. data/spec/struct_spec.rb +0 -398
  93. data/spec/support/header_protocol_helper.rb +0 -55
  94. data/spec/thin_http_server_spec.rb +0 -127
  95. data/spec/types_spec.rb +0 -118
  96. data/spec/union_spec.rb +0 -273
  97. data/spec/unix_socket_spec.rb +0 -131
  98. data/spec/uuid_validation_spec.rb +0 -239
  99. data/test/fuzz/Makefile +0 -779
  100. data/test/fuzz/Makefile.am +0 -173
  101. data/test/fuzz/Makefile.in +0 -775
  102. data/test/fuzz/README.md +0 -149
  103. data/test/fuzz/fuzz_common.rb +0 -96
  104. data/test/fuzz/fuzz_parse_binary_protocol.rb +0 -23
  105. data/test/fuzz/fuzz_parse_binary_protocol_accelerated.rb +0 -23
  106. data/test/fuzz/fuzz_parse_binary_protocol_accelerated_harness.rb +0 -23
  107. data/test/fuzz/fuzz_parse_binary_protocol_harness.rb +0 -23
  108. data/test/fuzz/fuzz_parse_compact_protocol.rb +0 -23
  109. data/test/fuzz/fuzz_parse_compact_protocol_harness.rb +0 -23
  110. data/test/fuzz/fuzz_parse_json_protocol.rb +0 -23
  111. data/test/fuzz/fuzz_parse_json_protocol_harness.rb +0 -23
  112. data/test/fuzz/fuzz_roundtrip_binary_protocol.rb +0 -23
  113. data/test/fuzz/fuzz_roundtrip_binary_protocol_accelerated.rb +0 -23
  114. data/test/fuzz/fuzz_roundtrip_binary_protocol_accelerated_harness.rb +0 -23
  115. data/test/fuzz/fuzz_roundtrip_binary_protocol_harness.rb +0 -23
  116. data/test/fuzz/fuzz_roundtrip_compact_protocol.rb +0 -23
  117. data/test/fuzz/fuzz_roundtrip_compact_protocol_harness.rb +0 -23
  118. data/test/fuzz/fuzz_roundtrip_json_protocol.rb +0 -23
  119. data/test/fuzz/fuzz_roundtrip_json_protocol_harness.rb +0 -23
  120. data/test/fuzz/fuzz_tracer.rb +0 -29
@@ -1,141 +0,0 @@
1
- # frozen_string_literal: true
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 'Exception' do
24
- describe Thrift::Exception do
25
- it "should have an accessible message" do
26
- e = Thrift::Exception.new("test message")
27
- expect(e.message).to eq("test message")
28
- end
29
- end
30
-
31
- describe Thrift::ApplicationException do
32
- it "should inherit from Thrift::Exception" do
33
- expect(Thrift::ApplicationException.superclass).to eq(Thrift::Exception)
34
- end
35
-
36
- it "should have an accessible type and message" do
37
- e = Thrift::ApplicationException.new
38
- expect(e.type).to eq(Thrift::ApplicationException::UNKNOWN)
39
- expect(e.message).to be_nil
40
- e = Thrift::ApplicationException.new(Thrift::ApplicationException::UNKNOWN_METHOD, "test message")
41
- expect(e.type).to eq(Thrift::ApplicationException::UNKNOWN_METHOD)
42
- expect(e.message).to eq("test message")
43
- end
44
-
45
- it "should read a struct off of a protocol" do
46
- prot = double("MockProtocol")
47
- expect(prot).to receive(:read_struct_begin).ordered
48
- expect(prot).to receive(:read_field_begin).exactly(3).times.and_return(
49
- ["message", Thrift::Types::STRING, 1],
50
- ["type", Thrift::Types::I32, 2],
51
- [nil, Thrift::Types::STOP, 0]
52
- )
53
- expect(prot).to receive(:read_string).ordered.and_return "test message"
54
- expect(prot).to receive(:read_i32).ordered.and_return Thrift::ApplicationException::BAD_SEQUENCE_ID
55
- expect(prot).to receive(:read_field_end).exactly(2).times
56
- expect(prot).to receive(:read_struct_end).ordered
57
-
58
- e = Thrift::ApplicationException.new
59
- e.read(prot)
60
- expect(e.message).to eq("test message")
61
- expect(e.type).to eq(Thrift::ApplicationException::BAD_SEQUENCE_ID)
62
- end
63
-
64
- it "should skip bad fields when reading a struct" do
65
- prot = double("MockProtocol")
66
- expect(prot).to receive(:read_struct_begin).ordered
67
- expect(prot).to receive(:read_field_begin).exactly(5).times.and_return(
68
- ["type", Thrift::Types::I32, 2],
69
- ["type", Thrift::Types::STRING, 2],
70
- ["message", Thrift::Types::MAP, 1],
71
- ["message", Thrift::Types::STRING, 3],
72
- [nil, Thrift::Types::STOP, 0]
73
- )
74
- expect(prot).to receive(:read_i32).and_return Thrift::ApplicationException::INVALID_MESSAGE_TYPE
75
- expect(prot).to receive(:skip).with(Thrift::Types::STRING).twice
76
- expect(prot).to receive(:skip).with(Thrift::Types::MAP)
77
- expect(prot).to receive(:read_field_end).exactly(4).times
78
- expect(prot).to receive(:read_struct_end).ordered
79
-
80
- e = Thrift::ApplicationException.new
81
- e.read(prot)
82
- expect(e.message).to be_nil
83
- expect(e.type).to eq(Thrift::ApplicationException::INVALID_MESSAGE_TYPE)
84
- end
85
-
86
- it "should write a Thrift::ApplicationException struct to the oprot" do
87
- prot = double("MockProtocol")
88
- expect(prot).to receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
89
- expect(prot).to receive(:write_field_begin).with("message", Thrift::Types::STRING, 1).ordered
90
- expect(prot).to receive(:write_string).with("test message").ordered
91
- expect(prot).to receive(:write_field_begin).with("type", Thrift::Types::I32, 2).ordered
92
- expect(prot).to receive(:write_i32).with(Thrift::ApplicationException::UNKNOWN_METHOD).ordered
93
- expect(prot).to receive(:write_field_end).twice
94
- expect(prot).to receive(:write_field_stop).ordered
95
- expect(prot).to receive(:write_struct_end).ordered
96
-
97
- e = Thrift::ApplicationException.new(Thrift::ApplicationException::UNKNOWN_METHOD, "test message")
98
- e.write(prot)
99
- end
100
-
101
- it "should skip nil fields when writing to the oprot" do
102
- prot = double("MockProtocol")
103
- expect(prot).to receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
104
- expect(prot).to receive(:write_field_begin).with("message", Thrift::Types::STRING, 1).ordered
105
- expect(prot).to receive(:write_string).with("test message").ordered
106
- expect(prot).to receive(:write_field_end).ordered
107
- expect(prot).to receive(:write_field_stop).ordered
108
- expect(prot).to receive(:write_struct_end).ordered
109
-
110
- e = Thrift::ApplicationException.new(nil, "test message")
111
- e.write(prot)
112
-
113
- prot = double("MockProtocol")
114
- expect(prot).to receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
115
- expect(prot).to receive(:write_field_begin).with("type", Thrift::Types::I32, 2).ordered
116
- expect(prot).to receive(:write_i32).with(Thrift::ApplicationException::BAD_SEQUENCE_ID).ordered
117
- expect(prot).to receive(:write_field_end).ordered
118
- expect(prot).to receive(:write_field_stop).ordered
119
- expect(prot).to receive(:write_struct_end).ordered
120
-
121
- e = Thrift::ApplicationException.new(Thrift::ApplicationException::BAD_SEQUENCE_ID)
122
- e.write(prot)
123
-
124
- prot = double("MockProtocol")
125
- expect(prot).to receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
126
- expect(prot).to receive(:write_field_stop).ordered
127
- expect(prot).to receive(:write_struct_end).ordered
128
-
129
- e = Thrift::ApplicationException.new(nil)
130
- e.write(prot)
131
- end
132
- end
133
-
134
- describe Thrift::ProtocolException do
135
- it "should have an accessible type" do
136
- prot = Thrift::ProtocolException.new(Thrift::ProtocolException::SIZE_LIMIT, "message")
137
- expect(prot.type).to eq(Thrift::ProtocolException::SIZE_LIMIT)
138
- expect(prot.message).to eq("message")
139
- end
140
- end
141
- end
data/spec/flat_spec.rb DELETED
@@ -1,63 +0,0 @@
1
- # frozen_string_literal: true
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 'generation' do
24
- before do
25
- require 'namespaced_nonblocking_service'
26
- end
27
-
28
- it "did not generate the wrong files" do
29
- prefix = File.expand_path("../gen-rb/flat", __FILE__)
30
- ["namespaced_spec_namespace/namespaced_nonblocking_service.rb",
31
- "namespaced_spec_namespace/thrift_namespaced_spec_constants.rb",
32
- "namespaced_spec_namespace/thrift_namespaced_spec_types.rb",
33
- "other_namespace/referenced_constants.rb",
34
- "other_namespace/referenced_types.rb"
35
- ].each do |name|
36
- expect(File.exist?(File.join(prefix, name))).not_to be_truthy
37
- end
38
- end
39
-
40
- it "generated the right files" do
41
- prefix = File.expand_path("../gen-rb/flat", __FILE__)
42
- ["namespaced_nonblocking_service.rb",
43
- "thrift_namespaced_spec_constants.rb",
44
- "thrift_namespaced_spec_types.rb",
45
- "referenced_constants.rb",
46
- "referenced_types.rb"
47
- ].each do |name|
48
- expect(File.exist?(File.join(prefix, name))).to be_truthy
49
- end
50
- end
51
-
52
- it "has a service class in the right place" do
53
- expect(defined?(NamespacedSpecNamespace::NamespacedNonblockingService)).to be_truthy
54
- end
55
-
56
- it "has a struct in the right place" do
57
- expect(defined?(NamespacedSpecNamespace::Hello)).to be_truthy
58
- end
59
-
60
- it "required an included file" do
61
- expect(defined?(OtherNamespace::SomeEnum)).to be_truthy
62
- end
63
- end
@@ -1,476 +0,0 @@
1
- # frozen_string_literal: true
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
- require_relative 'support/header_protocol_helper'
23
-
24
- describe 'HeaderProtocol' do
25
- include HeaderProtocolHelper
26
-
27
- describe Thrift::HeaderProtocol do
28
- before(:each) do
29
- @buffer = Thrift::MemoryBufferTransport.new
30
- @protocol = Thrift::HeaderProtocol.new(@buffer)
31
- end
32
-
33
- it "should provide a to_s" do
34
- expect(@protocol.to_s).to match(/header\(compact/)
35
- end
36
-
37
- it "should wrap transport in HeaderTransport" do
38
- expect(@protocol.trans).to be_a(Thrift::HeaderTransport)
39
- end
40
-
41
- it "should use existing HeaderTransport if passed" do
42
- header_trans = Thrift::HeaderTransport.new(@buffer)
43
- protocol = Thrift::HeaderProtocol.new(header_trans)
44
- expect(protocol.trans).to equal(header_trans)
45
- end
46
-
47
- describe "header management delegation" do
48
- it "should delegate get_headers" do
49
- # Write with headers and read back to populate read headers
50
- @protocol.set_header("key", "value")
51
- @protocol.write_message_begin("test", Thrift::MessageTypes::CALL, 1)
52
- @protocol.write_struct_begin("args")
53
- @protocol.write_field_stop
54
- @protocol.write_struct_end
55
- @protocol.write_message_end
56
- @protocol.trans.flush
57
-
58
- # Read back
59
- data = @buffer.read(@buffer.available)
60
- read_buffer = Thrift::MemoryBufferTransport.new(data)
61
- read_protocol = Thrift::HeaderProtocol.new(read_buffer)
62
-
63
- read_protocol.read_message_begin
64
- headers = read_protocol.get_headers
65
- expect(headers["key"]).to eq("value")
66
- end
67
-
68
- it "should delegate set_header" do
69
- expect(@protocol.trans).to receive(:set_header).with("key", "value")
70
- @protocol.set_header("key", "value")
71
- end
72
-
73
- it "should delegate clear_headers" do
74
- expect(@protocol.trans).to receive(:clear_headers)
75
- @protocol.clear_headers
76
- end
77
-
78
- it "should delegate add_transform" do
79
- expect(@protocol.trans).to receive(:add_transform).with(Thrift::HeaderTransformID::ZLIB)
80
- @protocol.add_transform(Thrift::HeaderTransformID::ZLIB)
81
- end
82
- end
83
-
84
- describe "protocol delegation with Binary protocol" do
85
- before(:each) do
86
- @buffer = Thrift::MemoryBufferTransport.new
87
- @protocol = Thrift::HeaderProtocol.new(@buffer, nil, Thrift::HeaderSubprotocolID::BINARY)
88
- end
89
-
90
- it "should write message begin" do
91
- @protocol.write_message_begin("test_method", Thrift::MessageTypes::CALL, 123)
92
- @protocol.write_message_end
93
- @protocol.trans.flush
94
-
95
- # Verify we can read it back
96
- data = @buffer.read(@buffer.available)
97
- read_buffer = Thrift::MemoryBufferTransport.new(data)
98
- read_protocol = Thrift::HeaderProtocol.new(read_buffer)
99
-
100
- name, type, seqid = read_protocol.read_message_begin
101
- expect(name).to eq("test_method")
102
- expect(type).to eq(Thrift::MessageTypes::CALL)
103
- expect(seqid).to eq(123)
104
- end
105
-
106
- it "should propagate seqid to the outer header frame" do
107
- @protocol.write_message_begin("test_method", Thrift::MessageTypes::CALL, 123)
108
- @protocol.write_message_end
109
- @protocol.trans.flush
110
-
111
- data = @buffer.read(@buffer.available)
112
- expect(data[8, 4].unpack('N').first).to eq(123)
113
- end
114
-
115
- it "should write and read structs" do
116
- @protocol.write_message_begin("test", Thrift::MessageTypes::CALL, 1)
117
- @protocol.write_struct_begin("TestStruct")
118
- @protocol.write_field_begin("field1", Thrift::Types::I32, 1)
119
- @protocol.write_i32(42)
120
- @protocol.write_field_end
121
- @protocol.write_field_stop
122
- @protocol.write_struct_end
123
- @protocol.write_message_end
124
- @protocol.trans.flush
125
-
126
- data = @buffer.read(@buffer.available)
127
- read_buffer = Thrift::MemoryBufferTransport.new(data)
128
- read_protocol = Thrift::HeaderProtocol.new(read_buffer)
129
-
130
- read_protocol.read_message_begin
131
- read_protocol.read_struct_begin
132
- name, type, id = read_protocol.read_field_begin
133
- expect(type).to eq(Thrift::Types::I32)
134
- expect(id).to eq(1)
135
- value = read_protocol.read_i32
136
- expect(value).to eq(42)
137
- end
138
-
139
- it "should write and read all primitive types" do
140
- @protocol.write_message_begin("test", Thrift::MessageTypes::CALL, 1)
141
- @protocol.write_struct_begin("TestStruct")
142
-
143
- @protocol.write_field_begin("bool", Thrift::Types::BOOL, 1)
144
- @protocol.write_bool(true)
145
- @protocol.write_field_end
146
-
147
- @protocol.write_field_begin("byte", Thrift::Types::BYTE, 2)
148
- @protocol.write_byte(127)
149
- @protocol.write_field_end
150
-
151
- @protocol.write_field_begin("i16", Thrift::Types::I16, 3)
152
- @protocol.write_i16(32767)
153
- @protocol.write_field_end
154
-
155
- @protocol.write_field_begin("i32", Thrift::Types::I32, 4)
156
- @protocol.write_i32(2147483647)
157
- @protocol.write_field_end
158
-
159
- @protocol.write_field_begin("i64", Thrift::Types::I64, 5)
160
- @protocol.write_i64(9223372036854775807)
161
- @protocol.write_field_end
162
-
163
- @protocol.write_field_begin("double", Thrift::Types::DOUBLE, 6)
164
- @protocol.write_double(3.14159)
165
- @protocol.write_field_end
166
-
167
- @protocol.write_field_begin("string", Thrift::Types::STRING, 7)
168
- @protocol.write_string("hello")
169
- @protocol.write_field_end
170
-
171
- @protocol.write_field_stop
172
- @protocol.write_struct_end
173
- @protocol.write_message_end
174
- @protocol.trans.flush
175
-
176
- data = @buffer.read(@buffer.available)
177
- read_buffer = Thrift::MemoryBufferTransport.new(data)
178
- read_protocol = Thrift::HeaderProtocol.new(read_buffer)
179
-
180
- read_protocol.read_message_begin
181
- read_protocol.read_struct_begin
182
-
183
- # bool
184
- _, type, _ = read_protocol.read_field_begin
185
- expect(type).to eq(Thrift::Types::BOOL)
186
- expect(read_protocol.read_bool).to eq(true)
187
- read_protocol.read_field_end
188
-
189
- # byte
190
- _, type, _ = read_protocol.read_field_begin
191
- expect(type).to eq(Thrift::Types::BYTE)
192
- expect(read_protocol.read_byte).to eq(127)
193
- read_protocol.read_field_end
194
-
195
- # i16
196
- _, type, _ = read_protocol.read_field_begin
197
- expect(type).to eq(Thrift::Types::I16)
198
- expect(read_protocol.read_i16).to eq(32767)
199
- read_protocol.read_field_end
200
-
201
- # i32
202
- _, type, _ = read_protocol.read_field_begin
203
- expect(type).to eq(Thrift::Types::I32)
204
- expect(read_protocol.read_i32).to eq(2147483647)
205
- read_protocol.read_field_end
206
-
207
- # i64
208
- _, type, _ = read_protocol.read_field_begin
209
- expect(type).to eq(Thrift::Types::I64)
210
- expect(read_protocol.read_i64).to eq(9223372036854775807)
211
- read_protocol.read_field_end
212
-
213
- # double
214
- _, type, _ = read_protocol.read_field_begin
215
- expect(type).to eq(Thrift::Types::DOUBLE)
216
- expect(read_protocol.read_double).to be_within(0.00001).of(3.14159)
217
- read_protocol.read_field_end
218
-
219
- # string
220
- _, type, _ = read_protocol.read_field_begin
221
- expect(type).to eq(Thrift::Types::STRING)
222
- expect(read_protocol.read_string).to eq("hello")
223
- read_protocol.read_field_end
224
- end
225
- end
226
-
227
- describe "protocol delegation with Compact protocol" do
228
- before(:each) do
229
- @buffer = Thrift::MemoryBufferTransport.new
230
- @protocol = Thrift::HeaderProtocol.new(
231
- @buffer,
232
- nil,
233
- Thrift::HeaderSubprotocolID::COMPACT
234
- )
235
- end
236
-
237
- it "should use Compact protocol" do
238
- expect(@protocol.to_s).to match(/header\(compact/)
239
- end
240
-
241
- it "should write and read with Compact protocol" do
242
- @protocol.write_message_begin("test", Thrift::MessageTypes::CALL, 1)
243
- @protocol.write_struct_begin("Test")
244
- @protocol.write_field_begin("field", Thrift::Types::I32, 1)
245
- @protocol.write_i32(999)
246
- @protocol.write_field_end
247
- @protocol.write_field_stop
248
- @protocol.write_struct_end
249
- @protocol.write_message_end
250
- @protocol.trans.flush
251
-
252
- data = @buffer.read(@buffer.available)
253
- read_buffer = Thrift::MemoryBufferTransport.new(data)
254
- read_protocol = Thrift::HeaderProtocol.new(read_buffer, nil, Thrift::HeaderSubprotocolID::COMPACT)
255
-
256
- read_protocol.read_message_begin
257
- read_protocol.read_struct_begin
258
- _, type, _ = read_protocol.read_field_begin
259
- expect(type).to eq(Thrift::Types::I32)
260
- expect(read_protocol.read_i32).to eq(999)
261
- end
262
- end
263
-
264
- describe "unknown protocol handling" do
265
- it "should write an exception response on unknown protocol id" do
266
- header_data = +""
267
- header_data << varint32(0x10)
268
- header_data << varint32(0)
269
- frame = build_header_frame(header_data)
270
-
271
- buffer = Thrift::MemoryBufferTransport.new(frame)
272
- protocol = Thrift::HeaderProtocol.new(buffer)
273
-
274
- expect { protocol.read_message_begin }.to raise_error(Thrift::ProtocolException)
275
-
276
- response = buffer.read(buffer.available)
277
- expect(response.bytesize).to be > 0
278
- magic = response[4, 2].unpack('n').first
279
- expect(magic).to eq(Thrift::HeaderTransport::HEADER_MAGIC)
280
- end
281
- end
282
-
283
- describe "protocol auto-detection with legacy frames" do
284
- it "should detect framed compact messages" do
285
- write_buffer = Thrift::MemoryBufferTransport.new
286
- write_protocol = Thrift::CompactProtocol.new(write_buffer)
287
-
288
- write_protocol.write_message_begin("legacy_framed", Thrift::MessageTypes::CALL, 7)
289
- write_protocol.write_struct_begin("Args")
290
- write_protocol.write_field_stop
291
- write_protocol.write_struct_end
292
- write_protocol.write_message_end
293
-
294
- payload = write_buffer.read(write_buffer.available)
295
- framed = [payload.bytesize].pack('N') + payload
296
-
297
- read_buffer = Thrift::MemoryBufferTransport.new(framed)
298
- protocol = Thrift::HeaderProtocol.new(read_buffer)
299
-
300
- name, type, seqid = protocol.read_message_begin
301
- expect(name).to eq("legacy_framed")
302
- expect(type).to eq(Thrift::MessageTypes::CALL)
303
- expect(seqid).to eq(7)
304
-
305
- protocol.read_struct_begin
306
- _, field_type, _ = protocol.read_field_begin
307
- expect(field_type).to eq(Thrift::Types::STOP)
308
- protocol.read_struct_end
309
- protocol.read_message_end
310
- end
311
-
312
- it "should detect unframed compact messages" do
313
- write_buffer = Thrift::MemoryBufferTransport.new
314
- write_protocol = Thrift::CompactProtocol.new(write_buffer)
315
-
316
- write_protocol.write_message_begin("legacy_unframed", Thrift::MessageTypes::CALL, 9)
317
- write_protocol.write_struct_begin("Args")
318
- write_protocol.write_field_stop
319
- write_protocol.write_struct_end
320
- write_protocol.write_message_end
321
-
322
- payload = write_buffer.read(write_buffer.available)
323
-
324
- read_buffer = Thrift::MemoryBufferTransport.new(payload)
325
- protocol = Thrift::HeaderProtocol.new(read_buffer)
326
-
327
- name, type, seqid = protocol.read_message_begin
328
- expect(name).to eq("legacy_unframed")
329
- expect(type).to eq(Thrift::MessageTypes::CALL)
330
- expect(seqid).to eq(9)
331
-
332
- protocol.read_struct_begin
333
- _, field_type, _ = protocol.read_field_begin
334
- expect(field_type).to eq(Thrift::Types::STOP)
335
- protocol.read_struct_end
336
- protocol.read_message_end
337
- end
338
- end
339
-
340
- describe "with compression" do
341
- it "should work with ZLIB transform" do
342
- @protocol.add_transform(Thrift::HeaderTransformID::ZLIB)
343
-
344
- @protocol.write_message_begin("compressed_test", Thrift::MessageTypes::CALL, 42)
345
- @protocol.write_struct_begin("Args")
346
- @protocol.write_field_begin("data", Thrift::Types::STRING, 1)
347
- @protocol.write_string("a" * 100) # Compressible data
348
- @protocol.write_field_end
349
- @protocol.write_field_stop
350
- @protocol.write_struct_end
351
- @protocol.write_message_end
352
- @protocol.trans.flush
353
-
354
- data = @buffer.read(@buffer.available)
355
- read_buffer = Thrift::MemoryBufferTransport.new(data)
356
- read_protocol = Thrift::HeaderProtocol.new(read_buffer)
357
-
358
- name, type, seqid = read_protocol.read_message_begin
359
- expect(name).to eq("compressed_test")
360
- expect(seqid).to eq(42)
361
-
362
- read_protocol.read_struct_begin
363
- _, _, _ = read_protocol.read_field_begin
364
- result = read_protocol.read_string
365
- expect(result).to eq("a" * 100)
366
- end
367
- end
368
-
369
- describe "containers" do
370
- it "should write and read lists" do
371
- @protocol.write_message_begin("test", Thrift::MessageTypes::CALL, 1)
372
- @protocol.write_struct_begin("Test")
373
- @protocol.write_field_begin("list", Thrift::Types::LIST, 1)
374
- @protocol.write_list_begin(Thrift::Types::I32, 3)
375
- @protocol.write_i32(1)
376
- @protocol.write_i32(2)
377
- @protocol.write_i32(3)
378
- @protocol.write_list_end
379
- @protocol.write_field_end
380
- @protocol.write_field_stop
381
- @protocol.write_struct_end
382
- @protocol.write_message_end
383
- @protocol.trans.flush
384
-
385
- data = @buffer.read(@buffer.available)
386
- read_buffer = Thrift::MemoryBufferTransport.new(data)
387
- read_protocol = Thrift::HeaderProtocol.new(read_buffer)
388
-
389
- read_protocol.read_message_begin
390
- read_protocol.read_struct_begin
391
- _, _, _ = read_protocol.read_field_begin
392
- etype, size = read_protocol.read_list_begin
393
- expect(etype).to eq(Thrift::Types::I32)
394
- expect(size).to eq(3)
395
- expect(read_protocol.read_i32).to eq(1)
396
- expect(read_protocol.read_i32).to eq(2)
397
- expect(read_protocol.read_i32).to eq(3)
398
- end
399
-
400
- it "should write and read maps" do
401
- @protocol.write_message_begin("test", Thrift::MessageTypes::CALL, 1)
402
- @protocol.write_struct_begin("Test")
403
- @protocol.write_field_begin("map", Thrift::Types::MAP, 1)
404
- @protocol.write_map_begin(Thrift::Types::STRING, Thrift::Types::I32, 2)
405
- @protocol.write_string("a")
406
- @protocol.write_i32(1)
407
- @protocol.write_string("b")
408
- @protocol.write_i32(2)
409
- @protocol.write_map_end
410
- @protocol.write_field_end
411
- @protocol.write_field_stop
412
- @protocol.write_struct_end
413
- @protocol.write_message_end
414
- @protocol.trans.flush
415
-
416
- data = @buffer.read(@buffer.available)
417
- read_buffer = Thrift::MemoryBufferTransport.new(data)
418
- read_protocol = Thrift::HeaderProtocol.new(read_buffer)
419
-
420
- read_protocol.read_message_begin
421
- read_protocol.read_struct_begin
422
- _, _, _ = read_protocol.read_field_begin
423
- ktype, vtype, size = read_protocol.read_map_begin
424
- expect(ktype).to eq(Thrift::Types::STRING)
425
- expect(vtype).to eq(Thrift::Types::I32)
426
- expect(size).to eq(2)
427
- end
428
-
429
- it "should write and read sets" do
430
- @protocol.write_message_begin("test", Thrift::MessageTypes::CALL, 1)
431
- @protocol.write_struct_begin("Test")
432
- @protocol.write_field_begin("set", Thrift::Types::SET, 1)
433
- @protocol.write_set_begin(Thrift::Types::STRING, 2)
434
- @protocol.write_string("x")
435
- @protocol.write_string("y")
436
- @protocol.write_set_end
437
- @protocol.write_field_end
438
- @protocol.write_field_stop
439
- @protocol.write_struct_end
440
- @protocol.write_message_end
441
- @protocol.trans.flush
442
-
443
- data = @buffer.read(@buffer.available)
444
- read_buffer = Thrift::MemoryBufferTransport.new(data)
445
- read_protocol = Thrift::HeaderProtocol.new(read_buffer)
446
-
447
- read_protocol.read_message_begin
448
- read_protocol.read_struct_begin
449
- _, _, _ = read_protocol.read_field_begin
450
- etype, size = read_protocol.read_set_begin
451
- expect(etype).to eq(Thrift::Types::STRING)
452
- expect(size).to eq(2)
453
- end
454
- end
455
- end
456
-
457
- describe Thrift::HeaderProtocolFactory do
458
- it "should create HeaderProtocol" do
459
- factory = Thrift::HeaderProtocolFactory.new
460
- buffer = Thrift::MemoryBufferTransport.new
461
- protocol = factory.get_protocol(buffer)
462
- expect(protocol).to be_a(Thrift::HeaderProtocol)
463
- end
464
-
465
- it "should provide a reasonable to_s" do
466
- expect(Thrift::HeaderProtocolFactory.new.to_s).to eq("header")
467
- end
468
-
469
- it "should pass configuration to protocol" do
470
- factory = Thrift::HeaderProtocolFactory.new(nil, Thrift::HeaderSubprotocolID::COMPACT)
471
- buffer = Thrift::MemoryBufferTransport.new
472
- protocol = factory.get_protocol(buffer)
473
- expect(protocol.to_s).to match(/compact/)
474
- end
475
- end
476
- end