upfluence-thrift 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +43 -0
  3. data/benchmark/Benchmark.thrift +24 -0
  4. data/benchmark/benchmark.rb +271 -0
  5. data/benchmark/client.rb +74 -0
  6. data/benchmark/gen-rb/benchmark_constants.rb +11 -0
  7. data/benchmark/gen-rb/benchmark_service.rb +80 -0
  8. data/benchmark/gen-rb/benchmark_types.rb +10 -0
  9. data/benchmark/server.rb +82 -0
  10. data/benchmark/thin_server.rb +44 -0
  11. data/ext/binary_protocol_accelerated.c +460 -0
  12. data/ext/binary_protocol_accelerated.h +20 -0
  13. data/ext/bytes.c +36 -0
  14. data/ext/bytes.h +31 -0
  15. data/ext/compact_protocol.c +637 -0
  16. data/ext/compact_protocol.h +20 -0
  17. data/ext/constants.h +99 -0
  18. data/ext/extconf.rb +34 -0
  19. data/ext/macros.h +41 -0
  20. data/ext/memory_buffer.c +134 -0
  21. data/ext/memory_buffer.h +20 -0
  22. data/ext/protocol.c +0 -0
  23. data/ext/protocol.h +0 -0
  24. data/ext/strlcpy.c +41 -0
  25. data/ext/strlcpy.h +34 -0
  26. data/ext/struct.c +707 -0
  27. data/ext/struct.h +25 -0
  28. data/ext/thrift_native.c +201 -0
  29. data/lib/thrift.rb +68 -0
  30. data/lib/thrift/bytes.rb +131 -0
  31. data/lib/thrift/client.rb +71 -0
  32. data/lib/thrift/core_ext.rb +23 -0
  33. data/lib/thrift/core_ext/fixnum.rb +29 -0
  34. data/lib/thrift/exceptions.rb +87 -0
  35. data/lib/thrift/multiplexed_processor.rb +76 -0
  36. data/lib/thrift/processor.rb +57 -0
  37. data/lib/thrift/protocol/base_protocol.rb +379 -0
  38. data/lib/thrift/protocol/binary_protocol.rb +237 -0
  39. data/lib/thrift/protocol/binary_protocol_accelerated.rb +39 -0
  40. data/lib/thrift/protocol/compact_protocol.rb +435 -0
  41. data/lib/thrift/protocol/json_protocol.rb +769 -0
  42. data/lib/thrift/protocol/multiplexed_protocol.rb +40 -0
  43. data/lib/thrift/protocol/protocol_decorator.rb +194 -0
  44. data/lib/thrift/serializer/deserializer.rb +33 -0
  45. data/lib/thrift/serializer/serializer.rb +34 -0
  46. data/lib/thrift/server/base_server.rb +31 -0
  47. data/lib/thrift/server/mongrel_http_server.rb +60 -0
  48. data/lib/thrift/server/nonblocking_server.rb +305 -0
  49. data/lib/thrift/server/rack_application.rb +61 -0
  50. data/lib/thrift/server/simple_server.rb +43 -0
  51. data/lib/thrift/server/thin_http_server.rb +51 -0
  52. data/lib/thrift/server/thread_pool_server.rb +75 -0
  53. data/lib/thrift/server/threaded_server.rb +47 -0
  54. data/lib/thrift/struct.rb +237 -0
  55. data/lib/thrift/struct_union.rb +192 -0
  56. data/lib/thrift/thrift_native.rb +24 -0
  57. data/lib/thrift/transport/base_server_transport.rb +37 -0
  58. data/lib/thrift/transport/base_transport.rb +109 -0
  59. data/lib/thrift/transport/buffered_transport.rb +114 -0
  60. data/lib/thrift/transport/framed_transport.rb +117 -0
  61. data/lib/thrift/transport/http_client_transport.rb +56 -0
  62. data/lib/thrift/transport/io_stream_transport.rb +39 -0
  63. data/lib/thrift/transport/memory_buffer_transport.rb +125 -0
  64. data/lib/thrift/transport/server_socket.rb +63 -0
  65. data/lib/thrift/transport/socket.rb +139 -0
  66. data/lib/thrift/transport/unix_server_socket.rb +60 -0
  67. data/lib/thrift/transport/unix_socket.rb +40 -0
  68. data/lib/thrift/types.rb +101 -0
  69. data/lib/thrift/union.rb +179 -0
  70. data/spec/BaseService.thrift +27 -0
  71. data/spec/ExtendedService.thrift +25 -0
  72. data/spec/Referenced.thrift +44 -0
  73. data/spec/ThriftNamespacedSpec.thrift +53 -0
  74. data/spec/ThriftSpec.thrift +183 -0
  75. data/spec/base_protocol_spec.rb +217 -0
  76. data/spec/base_transport_spec.rb +350 -0
  77. data/spec/binary_protocol_accelerated_spec.rb +42 -0
  78. data/spec/binary_protocol_spec.rb +66 -0
  79. data/spec/binary_protocol_spec_shared.rb +455 -0
  80. data/spec/bytes_spec.rb +160 -0
  81. data/spec/client_spec.rb +99 -0
  82. data/spec/compact_protocol_spec.rb +143 -0
  83. data/spec/exception_spec.rb +141 -0
  84. data/spec/flat_spec.rb +62 -0
  85. data/spec/gen-rb/base/base_service.rb +80 -0
  86. data/spec/gen-rb/base/base_service_constants.rb +11 -0
  87. data/spec/gen-rb/base/base_service_types.rb +26 -0
  88. data/spec/gen-rb/extended/extended_service.rb +78 -0
  89. data/spec/gen-rb/extended/extended_service_constants.rb +11 -0
  90. data/spec/gen-rb/extended/extended_service_types.rb +12 -0
  91. data/spec/gen-rb/flat/namespaced_nonblocking_service.rb +272 -0
  92. data/spec/gen-rb/flat/referenced_constants.rb +11 -0
  93. data/spec/gen-rb/flat/referenced_types.rb +17 -0
  94. data/spec/gen-rb/flat/thrift_namespaced_spec_constants.rb +11 -0
  95. data/spec/gen-rb/flat/thrift_namespaced_spec_types.rb +28 -0
  96. data/spec/gen-rb/namespaced_spec_namespace/namespaced_nonblocking_service.rb +272 -0
  97. data/spec/gen-rb/namespaced_spec_namespace/thrift_namespaced_spec_constants.rb +11 -0
  98. data/spec/gen-rb/namespaced_spec_namespace/thrift_namespaced_spec_types.rb +28 -0
  99. data/spec/gen-rb/nonblocking_service.rb +272 -0
  100. data/spec/gen-rb/other_namespace/referenced_constants.rb +11 -0
  101. data/spec/gen-rb/other_namespace/referenced_types.rb +17 -0
  102. data/spec/gen-rb/thrift_spec_constants.rb +11 -0
  103. data/spec/gen-rb/thrift_spec_types.rb +538 -0
  104. data/spec/http_client_spec.rb +120 -0
  105. data/spec/json_protocol_spec.rb +513 -0
  106. data/spec/namespaced_spec.rb +67 -0
  107. data/spec/nonblocking_server_spec.rb +263 -0
  108. data/spec/processor_spec.rb +80 -0
  109. data/spec/serializer_spec.rb +67 -0
  110. data/spec/server_socket_spec.rb +79 -0
  111. data/spec/server_spec.rb +147 -0
  112. data/spec/socket_spec.rb +61 -0
  113. data/spec/socket_spec_shared.rb +104 -0
  114. data/spec/spec_helper.rb +64 -0
  115. data/spec/struct_nested_containers_spec.rb +191 -0
  116. data/spec/struct_spec.rb +293 -0
  117. data/spec/thin_http_server_spec.rb +141 -0
  118. data/spec/types_spec.rb +115 -0
  119. data/spec/union_spec.rb +203 -0
  120. data/spec/unix_socket_spec.rb +107 -0
  121. data/test/debug_proto/gen-rb/debug_proto_test_constants.rb +274 -0
  122. data/test/debug_proto/gen-rb/debug_proto_test_types.rb +761 -0
  123. data/test/debug_proto/gen-rb/empty_service.rb +24 -0
  124. data/test/debug_proto/gen-rb/inherited.rb +79 -0
  125. data/test/debug_proto/gen-rb/reverse_order_service.rb +82 -0
  126. data/test/debug_proto/gen-rb/service_for_exception_with_a_map.rb +81 -0
  127. data/test/debug_proto/gen-rb/srv.rb +330 -0
  128. metadata +388 -0
@@ -0,0 +1,67 @@
1
+ #
2
+ # Licensed to the Apache Software Foundation (ASF) under one
3
+ # or more contributor license agreements. See the NOTICE file
4
+ # distributed with this work for additional information
5
+ # regarding copyright ownership. The ASF licenses this file
6
+ # to you under the Apache License, Version 2.0 (the
7
+ # "License"); you may not use this file except in compliance
8
+ # with the License. You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing,
13
+ # software distributed under the License is distributed on an
14
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ # KIND, either express or implied. See the License for the
16
+ # specific language governing permissions and limitations
17
+ # under the License.
18
+ #
19
+
20
+ require 'spec_helper'
21
+
22
+ describe 'namespaced generation' do
23
+ before do
24
+ require 'namespaced_spec_namespace/namespaced_nonblocking_service'
25
+ end
26
+
27
+ it "generated the right files" do
28
+ prefix = File.expand_path("../gen-rb", __FILE__)
29
+ ["namespaced_spec_namespace/namespaced_nonblocking_service.rb",
30
+ "namespaced_spec_namespace/thrift_namespaced_spec_constants.rb",
31
+ "namespaced_spec_namespace/thrift_namespaced_spec_types.rb",
32
+ "other_namespace/referenced_constants.rb",
33
+ "other_namespace/referenced_types.rb"
34
+ ].each do |name|
35
+ File.exist?(File.join(prefix, name)).should be_true
36
+ end
37
+ end
38
+
39
+ it "did not generate the wrong files" do
40
+ prefix = File.expand_path("../gen-rb", __FILE__)
41
+ ["namespaced_nonblocking_service.rb",
42
+ "thrift_namespaced_spec_constants.rb",
43
+ "thrift_namespaced_spec_types.rb",
44
+ "referenced_constants.rb",
45
+ "referenced_types.rb"
46
+ ].each do |name|
47
+ File.exist?(File.join(prefix, name)).should_not be_true
48
+ end
49
+ end
50
+
51
+ it "has a service class in the right place" do
52
+ defined?(NamespacedSpecNamespace::NamespacedNonblockingService).should be_true
53
+ end
54
+
55
+ it "has a struct in the right place" do
56
+ defined?(NamespacedSpecNamespace::Hello).should be_true
57
+ end
58
+
59
+ it "required an included file" do
60
+ defined?(OtherNamespace::SomeEnum).should be_true
61
+ end
62
+
63
+ it "extended a service" do
64
+ require "extended/extended_service"
65
+ end
66
+
67
+ end
@@ -0,0 +1,263 @@
1
+ #
2
+ # Licensed to the Apache Software Foundation (ASF) under one
3
+ # or more contributor license agreements. See the NOTICE file
4
+ # distributed with this work for additional information
5
+ # regarding copyright ownership. The ASF licenses this file
6
+ # to you under the Apache License, Version 2.0 (the
7
+ # "License"); you may not use this file except in compliance
8
+ # with the License. You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing,
13
+ # software distributed under the License is distributed on an
14
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ # KIND, either express or implied. See the License for the
16
+ # specific language governing permissions and limitations
17
+ # under the License.
18
+ #
19
+
20
+ require 'spec_helper'
21
+
22
+ describe 'NonblockingServer' do
23
+
24
+ class Handler
25
+ def initialize
26
+ @queue = Queue.new
27
+ end
28
+
29
+ attr_accessor :server
30
+
31
+ def greeting(english)
32
+ if english
33
+ SpecNamespace::Hello.new
34
+ else
35
+ SpecNamespace::Hello.new(:greeting => "Aloha!")
36
+ end
37
+ end
38
+
39
+ def block
40
+ @queue.pop
41
+ end
42
+
43
+ def unblock(n)
44
+ n.times { @queue.push true }
45
+ end
46
+
47
+ def sleep(time)
48
+ Kernel.sleep time
49
+ end
50
+
51
+ def shutdown
52
+ @server.shutdown(0, false)
53
+ end
54
+ end
55
+
56
+ class SpecTransport < Thrift::BaseTransport
57
+ def initialize(transport, queue)
58
+ @transport = transport
59
+ @queue = queue
60
+ @flushed = false
61
+ end
62
+
63
+ def open?
64
+ @transport.open?
65
+ end
66
+
67
+ def open
68
+ @transport.open
69
+ end
70
+
71
+ def close
72
+ @transport.close
73
+ end
74
+
75
+ def read(sz)
76
+ @transport.read(sz)
77
+ end
78
+
79
+ def write(buf,sz=nil)
80
+ @transport.write(buf, sz)
81
+ end
82
+
83
+ def flush
84
+ @queue.push :flushed unless @flushed or @queue.nil?
85
+ @flushed = true
86
+ @transport.flush
87
+ end
88
+ end
89
+
90
+ class SpecServerSocket < Thrift::ServerSocket
91
+ def initialize(host, port, queue)
92
+ super(host, port)
93
+ @queue = queue
94
+ end
95
+
96
+ def listen
97
+ super
98
+ @queue.push :listen
99
+ end
100
+ end
101
+
102
+ describe Thrift::NonblockingServer do
103
+ before(:each) do
104
+ @port = 43251
105
+ handler = Handler.new
106
+ processor = SpecNamespace::NonblockingService::Processor.new(handler)
107
+ queue = Queue.new
108
+ @transport = SpecServerSocket.new('localhost', @port, queue)
109
+ transport_factory = Thrift::FramedTransportFactory.new
110
+ logger = Logger.new(STDERR)
111
+ logger.level = Logger::WARN
112
+ @server = Thrift::NonblockingServer.new(processor, @transport, transport_factory, nil, 5, logger)
113
+ handler.server = @server
114
+ @server_thread = Thread.new(Thread.current) do |master_thread|
115
+ begin
116
+ @server.serve
117
+ rescue => e
118
+ p e
119
+ puts e.backtrace * "\n"
120
+ master_thread.raise e
121
+ end
122
+ end
123
+ queue.pop
124
+
125
+ @clients = []
126
+ @catch_exceptions = false
127
+ end
128
+
129
+ after(:each) do
130
+ @clients.each { |client, trans| trans.close }
131
+ # @server.shutdown(1)
132
+ @server_thread.kill
133
+ @transport.close
134
+ end
135
+
136
+ def setup_client(queue = nil)
137
+ transport = SpecTransport.new(Thrift::FramedTransport.new(Thrift::Socket.new('localhost', @port)), queue)
138
+ protocol = Thrift::BinaryProtocol.new(transport)
139
+ client = SpecNamespace::NonblockingService::Client.new(protocol)
140
+ transport.open
141
+ @clients << [client, transport]
142
+ client
143
+ end
144
+
145
+ def setup_client_thread(result)
146
+ queue = Queue.new
147
+ Thread.new do
148
+ begin
149
+ client = setup_client
150
+ while (cmd = queue.pop)
151
+ msg, *args = cmd
152
+ case msg
153
+ when :block
154
+ result << client.block
155
+ when :unblock
156
+ client.unblock(args.first)
157
+ when :hello
158
+ result << client.greeting(true) # ignore result
159
+ when :sleep
160
+ client.sleep(args[0] || 0.5)
161
+ result << :slept
162
+ when :shutdown
163
+ client.shutdown
164
+ when :exit
165
+ result << :done
166
+ break
167
+ end
168
+ end
169
+ @clients.each { |c,t| t.close and break if c == client } #close the transport
170
+ rescue => e
171
+ raise e unless @catch_exceptions
172
+ end
173
+ end
174
+ queue
175
+ end
176
+
177
+ it "should handle basic message passing" do
178
+ client = setup_client
179
+ client.greeting(true).should == SpecNamespace::Hello.new
180
+ client.greeting(false).should == SpecNamespace::Hello.new(:greeting => 'Aloha!')
181
+ @server.shutdown
182
+ end
183
+
184
+ it "should handle concurrent clients" do
185
+ queue = Queue.new
186
+ trans_queue = Queue.new
187
+ 4.times do
188
+ Thread.new(Thread.current) do |main_thread|
189
+ begin
190
+ queue.push setup_client(trans_queue).block
191
+ rescue => e
192
+ main_thread.raise e
193
+ end
194
+ end
195
+ end
196
+ 4.times { trans_queue.pop }
197
+ setup_client.unblock(4)
198
+ 4.times { queue.pop.should be_true }
199
+ @server.shutdown
200
+ end
201
+
202
+ it "should handle messages from more than 5 long-lived connections" do
203
+ queues = []
204
+ result = Queue.new
205
+ 7.times do |i|
206
+ queues << setup_client_thread(result)
207
+ Thread.pass if i == 4 # give the server time to accept connections
208
+ end
209
+ client = setup_client
210
+ # block 4 connections
211
+ 4.times { |i| queues[i] << :block }
212
+ queues[4] << :hello
213
+ queues[5] << :hello
214
+ queues[6] << :hello
215
+ 3.times { result.pop.should == SpecNamespace::Hello.new }
216
+ client.greeting(true).should == SpecNamespace::Hello.new
217
+ queues[5] << [:unblock, 4]
218
+ 4.times { result.pop.should be_true }
219
+ queues[2] << :hello
220
+ result.pop.should == SpecNamespace::Hello.new
221
+ client.greeting(false).should == SpecNamespace::Hello.new(:greeting => 'Aloha!')
222
+ 7.times { queues.shift << :exit }
223
+ client.greeting(true).should == SpecNamespace::Hello.new
224
+ @server.shutdown
225
+ end
226
+
227
+ it "should shut down when asked" do
228
+ # connect first to ensure it's running
229
+ client = setup_client
230
+ client.greeting(false) # force a message pass
231
+ @server.shutdown
232
+ @server_thread.join(2).should be_an_instance_of(Thread)
233
+ end
234
+
235
+ it "should continue processing active messages when shutting down" do
236
+ result = Queue.new
237
+ client = setup_client_thread(result)
238
+ client << :sleep
239
+ sleep 0.1 # give the server time to start processing the client's message
240
+ @server.shutdown
241
+ @server_thread.join(2).should be_an_instance_of(Thread)
242
+ result.pop.should == :slept
243
+ end
244
+
245
+ it "should kill active messages when they don't expire while shutting down" do
246
+ result = Queue.new
247
+ client = setup_client_thread(result)
248
+ client << [:sleep, 10]
249
+ sleep 0.1 # start processing the client's message
250
+ @server.shutdown(1)
251
+ @catch_exceptions = true
252
+ @server_thread.join(3).should_not be_nil
253
+ result.should be_empty
254
+ end
255
+
256
+ it "should allow shutting down in response to a message" do
257
+ client = setup_client
258
+ client.greeting(true).should == SpecNamespace::Hello.new
259
+ client.shutdown
260
+ @server_thread.join(2).should_not be_nil
261
+ end
262
+ end
263
+ end
@@ -0,0 +1,80 @@
1
+ #
2
+ # Licensed to the Apache Software Foundation (ASF) under one
3
+ # or more contributor license agreements. See the NOTICE file
4
+ # distributed with this work for additional information
5
+ # regarding copyright ownership. The ASF licenses this file
6
+ # to you under the Apache License, Version 2.0 (the
7
+ # "License"); you may not use this file except in compliance
8
+ # with the License. You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing,
13
+ # software distributed under the License is distributed on an
14
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ # KIND, either express or implied. See the License for the
16
+ # specific language governing permissions and limitations
17
+ # under the License.
18
+ #
19
+
20
+ require 'spec_helper'
21
+
22
+ describe 'Processor' do
23
+
24
+ class ProcessorSpec
25
+ include Thrift::Processor
26
+ end
27
+
28
+ describe Thrift::Processor do
29
+ before(:each) do
30
+ @processor = ProcessorSpec.new(mock("MockHandler"))
31
+ @prot = mock("MockProtocol")
32
+ end
33
+
34
+ def mock_trans(obj)
35
+ obj.should_receive(:trans).ordered.and_return do
36
+ mock("trans").tap do |trans|
37
+ trans.should_receive(:flush).ordered
38
+ end
39
+ end
40
+ end
41
+
42
+ it "should call process_<message> when it receives that message" do
43
+ @prot.should_receive(:read_message_begin).ordered.and_return ['testMessage', Thrift::MessageTypes::CALL, 17]
44
+ @processor.should_receive(:process_testMessage).with(17, @prot, @prot).ordered
45
+ @processor.process(@prot, @prot).should == true
46
+ end
47
+
48
+ it "should raise an ApplicationException when the received message cannot be processed" do
49
+ @prot.should_receive(:read_message_begin).ordered.and_return ['testMessage', Thrift::MessageTypes::CALL, 4]
50
+ @prot.should_receive(:skip).with(Thrift::Types::STRUCT).ordered
51
+ @prot.should_receive(:read_message_end).ordered
52
+ @prot.should_receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::EXCEPTION, 4).ordered
53
+ e = mock(Thrift::ApplicationException)
54
+ e.should_receive(:write).with(@prot).ordered
55
+ Thrift::ApplicationException.should_receive(:new).with(Thrift::ApplicationException::UNKNOWN_METHOD, "Unknown function testMessage").and_return(e)
56
+ @prot.should_receive(:write_message_end).ordered
57
+ mock_trans(@prot)
58
+ @processor.process(@prot, @prot)
59
+ end
60
+
61
+ it "should pass args off to the args class" do
62
+ args_class = mock("MockArgsClass")
63
+ args = mock("#<MockArgsClass:mock>").tap do |args|
64
+ args.should_receive(:read).with(@prot).ordered
65
+ end
66
+ args_class.should_receive(:new).and_return args
67
+ @prot.should_receive(:read_message_end).ordered
68
+ @processor.read_args(@prot, args_class).should eql(args)
69
+ end
70
+
71
+ it "should write out a reply when asked" do
72
+ @prot.should_receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::REPLY, 23).ordered
73
+ result = mock("MockResult")
74
+ result.should_receive(:write).with(@prot).ordered
75
+ @prot.should_receive(:write_message_end).ordered
76
+ mock_trans(@prot)
77
+ @processor.write_result(result, @prot, 'testMessage', 23)
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,67 @@
1
+ #
2
+ # Licensed to the Apache Software Foundation (ASF) under one
3
+ # or more contributor license agreements. See the NOTICE file
4
+ # distributed with this work for additional information
5
+ # regarding copyright ownership. The ASF licenses this file
6
+ # to you under the Apache License, Version 2.0 (the
7
+ # "License"); you may not use this file except in compliance
8
+ # with the License. You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing,
13
+ # software distributed under the License is distributed on an
14
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ # KIND, either express or implied. See the License for the
16
+ # specific language governing permissions and limitations
17
+ # under the License.
18
+ #
19
+
20
+ require 'spec_helper'
21
+
22
+ describe 'Serializer' do
23
+
24
+ describe Thrift::Serializer do
25
+ it "should serialize structs to binary by default" do
26
+ serializer = Thrift::Serializer.new(Thrift::BinaryProtocolAcceleratedFactory.new)
27
+ data = serializer.serialize(SpecNamespace::Hello.new(:greeting => "'Ello guv'nor!"))
28
+ data.should == "\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00"
29
+ end
30
+
31
+ it "should serialize structs to the given protocol" do
32
+ protocol = Thrift::BaseProtocol.new(mock("transport"))
33
+ protocol.should_receive(:write_struct_begin).with("SpecNamespace::Hello")
34
+ protocol.should_receive(:write_field_begin).with("greeting", Thrift::Types::STRING, 1)
35
+ protocol.should_receive(:write_string).with("Good day")
36
+ protocol.should_receive(:write_field_end)
37
+ protocol.should_receive(:write_field_stop)
38
+ protocol.should_receive(:write_struct_end)
39
+ protocol_factory = mock("ProtocolFactory")
40
+ protocol_factory.stub!(:get_protocol).and_return(protocol)
41
+ serializer = Thrift::Serializer.new(protocol_factory)
42
+ serializer.serialize(SpecNamespace::Hello.new(:greeting => "Good day"))
43
+ end
44
+ end
45
+
46
+ describe Thrift::Deserializer do
47
+ it "should deserialize structs from binary by default" do
48
+ deserializer = Thrift::Deserializer.new
49
+ data = "\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00"
50
+ deserializer.deserialize(SpecNamespace::Hello.new, data).should == SpecNamespace::Hello.new(:greeting => "'Ello guv'nor!")
51
+ end
52
+
53
+ it "should deserialize structs from the given protocol" do
54
+ protocol = Thrift::BaseProtocol.new(mock("transport"))
55
+ protocol.should_receive(:read_struct_begin).and_return("SpecNamespace::Hello")
56
+ protocol.should_receive(:read_field_begin).and_return(["greeting", Thrift::Types::STRING, 1],
57
+ [nil, Thrift::Types::STOP, 0])
58
+ protocol.should_receive(:read_string).and_return("Good day")
59
+ protocol.should_receive(:read_field_end)
60
+ protocol.should_receive(:read_struct_end)
61
+ protocol_factory = mock("ProtocolFactory")
62
+ protocol_factory.stub!(:get_protocol).and_return(protocol)
63
+ deserializer = Thrift::Deserializer.new(protocol_factory)
64
+ deserializer.deserialize(SpecNamespace::Hello.new, "").should == SpecNamespace::Hello.new(:greeting => "Good day")
65
+ end
66
+ end
67
+ end