thrift 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/benchmark/gen-rb/benchmark_constants.rb +3 -2
- data/benchmark/gen-rb/benchmark_service.rb +52 -52
- data/benchmark/gen-rb/benchmark_types.rb +3 -2
- data/ext/binary_protocol_accelerated.c +5 -2
- data/ext/bytes.c +36 -0
- data/ext/bytes.h +31 -0
- data/ext/compact_protocol.c +7 -4
- data/ext/constants.h +4 -0
- data/ext/extconf.rb +3 -1
- data/ext/memory_buffer.c +11 -8
- data/ext/thrift_native.c +9 -0
- data/lib/thrift.rb +2 -0
- data/lib/thrift/bytes.rb +131 -0
- data/lib/thrift/protocol/base_protocol.rb +10 -0
- data/lib/thrift/protocol/binary_protocol.rb +5 -5
- data/lib/thrift/protocol/compact_protocol.rb +4 -3
- data/lib/thrift/protocol/json_protocol.rb +765 -0
- data/lib/thrift/transport/base_transport.rb +22 -20
- data/lib/thrift/transport/buffered_transport.rb +16 -10
- data/lib/thrift/transport/framed_transport.rb +11 -10
- data/lib/thrift/transport/http_client_transport.rb +7 -5
- data/lib/thrift/transport/io_stream_transport.rb +1 -1
- data/lib/thrift/transport/memory_buffer_transport.rb +6 -6
- data/lib/thrift/transport/socket.rb +4 -2
- data/spec/ThriftSpec.thrift +52 -1
- data/spec/base_protocol_spec.rb +44 -45
- data/spec/base_transport_spec.rb +49 -50
- data/spec/binary_protocol_accelerated_spec.rb +9 -13
- data/spec/binary_protocol_spec.rb +15 -10
- data/spec/binary_protocol_spec_shared.rb +62 -12
- data/spec/bytes_spec.rb +160 -0
- data/spec/client_spec.rb +13 -14
- data/spec/compact_protocol_spec.rb +3 -2
- data/spec/exception_spec.rb +39 -40
- data/spec/gen-rb/nonblocking_service.rb +193 -193
- data/spec/gen-rb/thrift_spec_constants.rb +3 -2
- data/spec/gen-rb/thrift_spec_types.rb +455 -262
- data/spec/http_client_spec.rb +16 -9
- data/spec/json_protocol_spec.rb +513 -0
- data/spec/mongrel_http_server_spec.rb +19 -22
- data/spec/nonblocking_server_spec.rb +18 -20
- data/spec/processor_spec.rb +13 -16
- data/spec/serializer_spec.rb +17 -19
- data/spec/server_socket_spec.rb +6 -7
- data/spec/server_spec.rb +46 -58
- data/spec/socket_spec.rb +11 -11
- data/spec/socket_spec_shared.rb +1 -1
- data/spec/spec_helper.rb +13 -10
- data/spec/struct_nested_containers_spec.rb +191 -0
- data/spec/struct_spec.rb +84 -86
- data/spec/types_spec.rb +65 -66
- data/spec/union_spec.rb +44 -46
- data/spec/unix_socket_spec.rb +8 -9
- data/test/debug_proto/gen-rb/debug_proto_test_constants.rb +8 -7
- data/test/debug_proto/gen-rb/debug_proto_test_types.rb +24 -23
- data/test/debug_proto/gen-rb/empty_service.rb +1 -1
- data/test/debug_proto/gen-rb/inherited.rb +3 -3
- data/test/debug_proto/gen-rb/reverse_order_service.rb +1 -1
- data/test/debug_proto/gen-rb/service_for_exception_with_a_map.rb +3 -3
- data/test/debug_proto/gen-rb/srv.rb +2 -2
- metadata +43 -49
@@ -17,65 +17,62 @@
|
|
17
17
|
# under the License.
|
18
18
|
#
|
19
19
|
|
20
|
-
require
|
20
|
+
require 'spec_helper'
|
21
21
|
require 'thrift/server/mongrel_http_server'
|
22
22
|
|
23
|
-
|
24
|
-
include Thrift
|
23
|
+
describe 'HTTPServer' do
|
25
24
|
|
26
|
-
|
27
|
-
|
28
|
-
describe MongrelHTTPServer do
|
25
|
+
describe Thrift::MongrelHTTPServer do
|
29
26
|
it "should have appropriate defaults" do
|
30
27
|
mock_factory = mock("BinaryProtocolFactory")
|
31
28
|
mock_proc = mock("Processor")
|
32
|
-
BinaryProtocolFactory.should_receive(:new).and_return(mock_factory)
|
29
|
+
Thrift::BinaryProtocolFactory.should_receive(:new).and_return(mock_factory)
|
33
30
|
Mongrel::HttpServer.should_receive(:new).with("0.0.0.0", 80).and_return do
|
34
|
-
mock("Mongrel::HttpServer").
|
31
|
+
mock("Mongrel::HttpServer").tap do |mock|
|
35
32
|
handler = mock("Handler")
|
36
|
-
Handler.should_receive(:new).with(mock_proc, mock_factory).and_return(handler)
|
33
|
+
Thrift::MongrelHTTPServer::Handler.should_receive(:new).with(mock_proc, mock_factory).and_return(handler)
|
37
34
|
mock.should_receive(:register).with("/", handler)
|
38
35
|
end
|
39
36
|
end
|
40
|
-
MongrelHTTPServer.new(mock_proc)
|
37
|
+
Thrift::MongrelHTTPServer.new(mock_proc)
|
41
38
|
end
|
42
39
|
|
43
40
|
it "should understand :ip, :port, :path, and :protocol_factory" do
|
44
41
|
mock_proc = mock("Processor")
|
45
42
|
mock_factory = mock("ProtocolFactory")
|
46
43
|
Mongrel::HttpServer.should_receive(:new).with("1.2.3.4", 1234).and_return do
|
47
|
-
mock("Mongrel::HttpServer").
|
44
|
+
mock("Mongrel::HttpServer").tap do |mock|
|
48
45
|
handler = mock("Handler")
|
49
|
-
Handler.should_receive(:new).with(mock_proc, mock_factory).and_return(handler)
|
46
|
+
Thrift::MongrelHTTPServer::Handler.should_receive(:new).with(mock_proc, mock_factory).and_return(handler)
|
50
47
|
mock.should_receive(:register).with("/foo", handler)
|
51
48
|
end
|
52
49
|
end
|
53
|
-
MongrelHTTPServer.new(mock_proc, :ip => "1.2.3.4", :port => 1234, :path => "foo",
|
50
|
+
Thrift::MongrelHTTPServer.new(mock_proc, :ip => "1.2.3.4", :port => 1234, :path => "foo",
|
54
51
|
:protocol_factory => mock_factory)
|
55
52
|
end
|
56
53
|
|
57
54
|
it "should serve using Mongrel::HttpServer" do
|
58
|
-
BinaryProtocolFactory.stub!(:new)
|
55
|
+
Thrift::BinaryProtocolFactory.stub!(:new)
|
59
56
|
Mongrel::HttpServer.should_receive(:new).and_return do
|
60
|
-
mock("Mongrel::HttpServer").
|
61
|
-
Handler.stub!(:new)
|
57
|
+
mock("Mongrel::HttpServer").tap do |mock|
|
58
|
+
Thrift::MongrelHTTPServer::Handler.stub!(:new)
|
62
59
|
mock.stub!(:register)
|
63
60
|
mock.should_receive(:run).and_return do
|
64
|
-
mock("Mongrel::HttpServer.run").
|
61
|
+
mock("Mongrel::HttpServer.run").tap do |runner|
|
65
62
|
runner.should_receive(:join)
|
66
63
|
end
|
67
64
|
end
|
68
65
|
end
|
69
66
|
end
|
70
|
-
MongrelHTTPServer.new(nil).serve
|
67
|
+
Thrift::MongrelHTTPServer.new(nil).serve
|
71
68
|
end
|
72
69
|
end
|
73
70
|
|
74
|
-
describe MongrelHTTPServer::Handler do
|
71
|
+
describe Thrift::MongrelHTTPServer::Handler do
|
75
72
|
before(:each) do
|
76
73
|
@processor = mock("Processor")
|
77
74
|
@factory = mock("ProtocolFactory")
|
78
|
-
@handler =
|
75
|
+
@handler = described_class.new(@processor, @factory)
|
79
76
|
end
|
80
77
|
|
81
78
|
it "should return 404 for non-POST requests" do
|
@@ -91,7 +88,7 @@ class ThriftHTTPServerSpec < Spec::ExampleGroup
|
|
91
88
|
response = mock("response")
|
92
89
|
head = mock("head")
|
93
90
|
head.should_receive(:[]=).with("Content-Type", "application/x-thrift")
|
94
|
-
IOStreamTransport.stub!(:new)
|
91
|
+
Thrift::IOStreamTransport.stub!(:new)
|
95
92
|
@factory.stub!(:get_protocol)
|
96
93
|
@processor.stub!(:process)
|
97
94
|
response.should_receive(:start).with(200).and_yield(head, nil)
|
@@ -107,7 +104,7 @@ class ThriftHTTPServerSpec < Spec::ExampleGroup
|
|
107
104
|
out = mock("out")
|
108
105
|
protocol = mock("protocol")
|
109
106
|
transport = mock("transport")
|
110
|
-
IOStreamTransport.should_receive(:new).with(body, out).and_return(transport)
|
107
|
+
Thrift::IOStreamTransport.should_receive(:new).with(body, out).and_return(transport)
|
111
108
|
@factory.should_receive(:get_protocol).with(transport).and_return(protocol)
|
112
109
|
@processor.should_receive(:process).with(protocol, protocol)
|
113
110
|
response.should_receive(:start).with(200).and_yield(head, out)
|
@@ -17,11 +17,9 @@
|
|
17
17
|
# under the License.
|
18
18
|
#
|
19
19
|
|
20
|
-
require
|
20
|
+
require 'spec_helper'
|
21
21
|
|
22
|
-
|
23
|
-
include Thrift
|
24
|
-
include SpecNamespace
|
22
|
+
describe 'NonblockingServer' do
|
25
23
|
|
26
24
|
class Handler
|
27
25
|
def initialize
|
@@ -55,7 +53,7 @@ class ThriftNonblockingServerSpec < Spec::ExampleGroup
|
|
55
53
|
end
|
56
54
|
end
|
57
55
|
|
58
|
-
class SpecTransport < BaseTransport
|
56
|
+
class SpecTransport < Thrift::BaseTransport
|
59
57
|
def initialize(transport, queue)
|
60
58
|
@transport = transport
|
61
59
|
@queue = queue
|
@@ -89,7 +87,7 @@ class ThriftNonblockingServerSpec < Spec::ExampleGroup
|
|
89
87
|
end
|
90
88
|
end
|
91
89
|
|
92
|
-
class SpecServerSocket < ServerSocket
|
90
|
+
class SpecServerSocket < Thrift::ServerSocket
|
93
91
|
def initialize(host, port, queue)
|
94
92
|
super(host, port)
|
95
93
|
@queue = queue
|
@@ -105,13 +103,13 @@ class ThriftNonblockingServerSpec < Spec::ExampleGroup
|
|
105
103
|
before(:each) do
|
106
104
|
@port = 43251
|
107
105
|
handler = Handler.new
|
108
|
-
processor = NonblockingService::Processor.new(handler)
|
106
|
+
processor = SpecNamespace::NonblockingService::Processor.new(handler)
|
109
107
|
queue = Queue.new
|
110
108
|
@transport = SpecServerSocket.new('localhost', @port, queue)
|
111
|
-
transport_factory = FramedTransportFactory.new
|
109
|
+
transport_factory = Thrift::FramedTransportFactory.new
|
112
110
|
logger = Logger.new(STDERR)
|
113
111
|
logger.level = Logger::WARN
|
114
|
-
@server = NonblockingServer.new(processor, @transport, transport_factory, nil, 5, logger)
|
112
|
+
@server = Thrift::NonblockingServer.new(processor, @transport, transport_factory, nil, 5, logger)
|
115
113
|
handler.server = @server
|
116
114
|
@server_thread = Thread.new(Thread.current) do |master_thread|
|
117
115
|
begin
|
@@ -136,9 +134,9 @@ class ThriftNonblockingServerSpec < Spec::ExampleGroup
|
|
136
134
|
end
|
137
135
|
|
138
136
|
def setup_client(queue = nil)
|
139
|
-
transport = SpecTransport.new(FramedTransport.new(Socket.new('localhost', @port)), queue)
|
140
|
-
protocol = BinaryProtocol.new(transport)
|
141
|
-
client = NonblockingService::Client.new(protocol)
|
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)
|
142
140
|
transport.open
|
143
141
|
@clients << [client, transport]
|
144
142
|
client
|
@@ -178,8 +176,8 @@ class ThriftNonblockingServerSpec < Spec::ExampleGroup
|
|
178
176
|
|
179
177
|
it "should handle basic message passing" do
|
180
178
|
client = setup_client
|
181
|
-
client.greeting(true).should == Hello.new
|
182
|
-
client.greeting(false).should == Hello.new(:greeting => 'Aloha!')
|
179
|
+
client.greeting(true).should == SpecNamespace::Hello.new
|
180
|
+
client.greeting(false).should == SpecNamespace::Hello.new(:greeting => 'Aloha!')
|
183
181
|
@server.shutdown
|
184
182
|
end
|
185
183
|
|
@@ -214,15 +212,15 @@ class ThriftNonblockingServerSpec < Spec::ExampleGroup
|
|
214
212
|
queues[4] << :hello
|
215
213
|
queues[5] << :hello
|
216
214
|
queues[6] << :hello
|
217
|
-
3.times { result.pop.should == Hello.new }
|
218
|
-
client.greeting(true).should == Hello.new
|
215
|
+
3.times { result.pop.should == SpecNamespace::Hello.new }
|
216
|
+
client.greeting(true).should == SpecNamespace::Hello.new
|
219
217
|
queues[5] << [:unblock, 4]
|
220
218
|
4.times { result.pop.should be_true }
|
221
219
|
queues[2] << :hello
|
222
|
-
result.pop.should == Hello.new
|
223
|
-
client.greeting(false).should == Hello.new(:greeting => 'Aloha!')
|
220
|
+
result.pop.should == SpecNamespace::Hello.new
|
221
|
+
client.greeting(false).should == SpecNamespace::Hello.new(:greeting => 'Aloha!')
|
224
222
|
7.times { queues.shift << :exit }
|
225
|
-
client.greeting(true).should == Hello.new
|
223
|
+
client.greeting(true).should == SpecNamespace::Hello.new
|
226
224
|
@server.shutdown
|
227
225
|
end
|
228
226
|
|
@@ -257,7 +255,7 @@ class ThriftNonblockingServerSpec < Spec::ExampleGroup
|
|
257
255
|
|
258
256
|
it "should allow shutting down in response to a message" do
|
259
257
|
client = setup_client
|
260
|
-
client.greeting(true).should == Hello.new
|
258
|
+
client.greeting(true).should == SpecNamespace::Hello.new
|
261
259
|
client.shutdown
|
262
260
|
@server_thread.join(2).should_not be_nil
|
263
261
|
end
|
data/spec/processor_spec.rb
CHANGED
@@ -17,16 +17,15 @@
|
|
17
17
|
# under the License.
|
18
18
|
#
|
19
19
|
|
20
|
-
require
|
20
|
+
require 'spec_helper'
|
21
21
|
|
22
|
-
|
23
|
-
include Thrift
|
22
|
+
describe 'Processor' do
|
24
23
|
|
25
24
|
class ProcessorSpec
|
26
25
|
include Thrift::Processor
|
27
26
|
end
|
28
27
|
|
29
|
-
describe
|
28
|
+
describe Thrift::Processor do
|
30
29
|
before(:each) do
|
31
30
|
@processor = ProcessorSpec.new(mock("MockHandler"))
|
32
31
|
@prot = mock("MockProtocol")
|
@@ -34,28 +33,26 @@ class ThriftProcessorSpec < Spec::ExampleGroup
|
|
34
33
|
|
35
34
|
def mock_trans(obj)
|
36
35
|
obj.should_receive(:trans).ordered.and_return do
|
37
|
-
mock("trans").
|
36
|
+
mock("trans").tap do |trans|
|
38
37
|
trans.should_receive(:flush).ordered
|
39
38
|
end
|
40
39
|
end
|
41
40
|
end
|
42
41
|
|
43
42
|
it "should call process_<message> when it receives that message" do
|
44
|
-
@prot.should_receive(:read_message_begin).ordered.and_return ['testMessage', MessageTypes::CALL, 17]
|
43
|
+
@prot.should_receive(:read_message_begin).ordered.and_return ['testMessage', Thrift::MessageTypes::CALL, 17]
|
45
44
|
@processor.should_receive(:process_testMessage).with(17, @prot, @prot).ordered
|
46
45
|
@processor.process(@prot, @prot).should == true
|
47
46
|
end
|
48
47
|
|
49
48
|
it "should raise an ApplicationException when the received message cannot be processed" do
|
50
|
-
@prot.should_receive(:read_message_begin).ordered.and_return ['testMessage', MessageTypes::CALL, 4]
|
51
|
-
@prot.should_receive(:skip).with(Types::STRUCT).ordered
|
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
|
52
51
|
@prot.should_receive(:read_message_end).ordered
|
53
|
-
@prot.should_receive(:write_message_begin).with('testMessage', MessageTypes::EXCEPTION, 4).ordered
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
end
|
58
|
-
end
|
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)
|
59
56
|
@prot.should_receive(:write_message_end).ordered
|
60
57
|
mock_trans(@prot)
|
61
58
|
@processor.process(@prot, @prot)
|
@@ -63,7 +60,7 @@ class ThriftProcessorSpec < Spec::ExampleGroup
|
|
63
60
|
|
64
61
|
it "should pass args off to the args class" do
|
65
62
|
args_class = mock("MockArgsClass")
|
66
|
-
args = mock("#<MockArgsClass:mock>").
|
63
|
+
args = mock("#<MockArgsClass:mock>").tap do |args|
|
67
64
|
args.should_receive(:read).with(@prot).ordered
|
68
65
|
end
|
69
66
|
args_class.should_receive(:new).and_return args
|
@@ -72,7 +69,7 @@ class ThriftProcessorSpec < Spec::ExampleGroup
|
|
72
69
|
end
|
73
70
|
|
74
71
|
it "should write out a reply when asked" do
|
75
|
-
@prot.should_receive(:write_message_begin).with('testMessage', MessageTypes::REPLY, 23).ordered
|
72
|
+
@prot.should_receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::REPLY, 23).ordered
|
76
73
|
result = mock("MockResult")
|
77
74
|
result.should_receive(:write).with(@prot).ordered
|
78
75
|
@prot.should_receive(:write_message_end).ordered
|
data/spec/serializer_spec.rb
CHANGED
@@ -17,53 +17,51 @@
|
|
17
17
|
# under the License.
|
18
18
|
#
|
19
19
|
|
20
|
-
require
|
20
|
+
require 'spec_helper'
|
21
21
|
|
22
|
-
|
23
|
-
include Thrift
|
24
|
-
include SpecNamespace
|
22
|
+
describe 'Serializer' do
|
25
23
|
|
26
|
-
describe Serializer do
|
24
|
+
describe Thrift::Serializer do
|
27
25
|
it "should serialize structs to binary by default" do
|
28
|
-
serializer = Serializer.new(Thrift::BinaryProtocolAcceleratedFactory.new)
|
29
|
-
data = serializer.serialize(Hello.new(:greeting => "'Ello guv'nor!"))
|
26
|
+
serializer = Thrift::Serializer.new(Thrift::BinaryProtocolAcceleratedFactory.new)
|
27
|
+
data = serializer.serialize(SpecNamespace::Hello.new(:greeting => "'Ello guv'nor!"))
|
30
28
|
data.should == "\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00"
|
31
29
|
end
|
32
30
|
|
33
31
|
it "should serialize structs to the given protocol" do
|
34
|
-
protocol = BaseProtocol.new(mock("transport"))
|
32
|
+
protocol = Thrift::BaseProtocol.new(mock("transport"))
|
35
33
|
protocol.should_receive(:write_struct_begin).with("SpecNamespace::Hello")
|
36
|
-
protocol.should_receive(:write_field_begin).with("greeting", Types::STRING, 1)
|
34
|
+
protocol.should_receive(:write_field_begin).with("greeting", Thrift::Types::STRING, 1)
|
37
35
|
protocol.should_receive(:write_string).with("Good day")
|
38
36
|
protocol.should_receive(:write_field_end)
|
39
37
|
protocol.should_receive(:write_field_stop)
|
40
38
|
protocol.should_receive(:write_struct_end)
|
41
39
|
protocol_factory = mock("ProtocolFactory")
|
42
40
|
protocol_factory.stub!(:get_protocol).and_return(protocol)
|
43
|
-
serializer = Serializer.new(protocol_factory)
|
44
|
-
serializer.serialize(Hello.new(:greeting => "Good day"))
|
41
|
+
serializer = Thrift::Serializer.new(protocol_factory)
|
42
|
+
serializer.serialize(SpecNamespace::Hello.new(:greeting => "Good day"))
|
45
43
|
end
|
46
44
|
end
|
47
45
|
|
48
|
-
describe Deserializer do
|
46
|
+
describe Thrift::Deserializer do
|
49
47
|
it "should deserialize structs from binary by default" do
|
50
|
-
deserializer = Deserializer.new
|
48
|
+
deserializer = Thrift::Deserializer.new
|
51
49
|
data = "\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00"
|
52
|
-
deserializer.deserialize(Hello.new, data).should == Hello.new(:greeting => "'Ello guv'nor!")
|
50
|
+
deserializer.deserialize(SpecNamespace::Hello.new, data).should == SpecNamespace::Hello.new(:greeting => "'Ello guv'nor!")
|
53
51
|
end
|
54
52
|
|
55
53
|
it "should deserialize structs from the given protocol" do
|
56
|
-
protocol = BaseProtocol.new(mock("transport"))
|
54
|
+
protocol = Thrift::BaseProtocol.new(mock("transport"))
|
57
55
|
protocol.should_receive(:read_struct_begin).and_return("SpecNamespace::Hello")
|
58
|
-
protocol.should_receive(:read_field_begin).and_return(["greeting", Types::STRING, 1],
|
59
|
-
[nil, Types::STOP, 0])
|
56
|
+
protocol.should_receive(:read_field_begin).and_return(["greeting", Thrift::Types::STRING, 1],
|
57
|
+
[nil, Thrift::Types::STOP, 0])
|
60
58
|
protocol.should_receive(:read_string).and_return("Good day")
|
61
59
|
protocol.should_receive(:read_field_end)
|
62
60
|
protocol.should_receive(:read_struct_end)
|
63
61
|
protocol_factory = mock("ProtocolFactory")
|
64
62
|
protocol_factory.stub!(:get_protocol).and_return(protocol)
|
65
|
-
deserializer = Deserializer.new(protocol_factory)
|
66
|
-
deserializer.deserialize(Hello.new, "").should == Hello.new(:greeting => "Good day")
|
63
|
+
deserializer = Thrift::Deserializer.new(protocol_factory)
|
64
|
+
deserializer.deserialize(SpecNamespace::Hello.new, "").should == SpecNamespace::Hello.new(:greeting => "Good day")
|
67
65
|
end
|
68
66
|
end
|
69
67
|
end
|
data/spec/server_socket_spec.rb
CHANGED
@@ -17,15 +17,14 @@
|
|
17
17
|
# under the License.
|
18
18
|
#
|
19
19
|
|
20
|
-
require
|
20
|
+
require 'spec_helper'
|
21
21
|
require File.expand_path("#{File.dirname(__FILE__)}/socket_spec_shared")
|
22
22
|
|
23
|
-
|
24
|
-
include Thrift
|
23
|
+
describe 'Thrift::ServerSocket' do
|
25
24
|
|
26
|
-
describe ServerSocket do
|
25
|
+
describe Thrift::ServerSocket do
|
27
26
|
before(:each) do
|
28
|
-
@socket = ServerSocket.new(1234)
|
27
|
+
@socket = Thrift::ServerSocket.new(1234)
|
29
28
|
end
|
30
29
|
|
31
30
|
it "should create a handle when calling listen" do
|
@@ -34,7 +33,7 @@ class ThriftServerSocketSpec < Spec::ExampleGroup
|
|
34
33
|
end
|
35
34
|
|
36
35
|
it "should accept an optional host argument" do
|
37
|
-
@socket = ServerSocket.new('localhost', 1234)
|
36
|
+
@socket = Thrift::ServerSocket.new('localhost', 1234)
|
38
37
|
TCPServer.should_receive(:new).with('localhost', 1234)
|
39
38
|
@socket.listen
|
40
39
|
end
|
@@ -46,7 +45,7 @@ class ThriftServerSocketSpec < Spec::ExampleGroup
|
|
46
45
|
sock = mock("sock")
|
47
46
|
handle.should_receive(:accept).and_return(sock)
|
48
47
|
trans = mock("Socket")
|
49
|
-
|
48
|
+
Thrift::Socket.should_receive(:new).and_return(trans)
|
50
49
|
trans.should_receive(:handle=).with(sock)
|
51
50
|
@socket.accept.should == trans
|
52
51
|
end
|
data/spec/server_spec.rb
CHANGED
@@ -16,39 +16,30 @@
|
|
16
16
|
# specific language governing permissions and limitations
|
17
17
|
# under the License.
|
18
18
|
#
|
19
|
-
require
|
19
|
+
require 'spec_helper'
|
20
20
|
|
21
|
-
|
22
|
-
include Thrift
|
21
|
+
describe 'Server' do
|
23
22
|
|
24
|
-
describe BaseServer do
|
23
|
+
describe Thrift::BaseServer do
|
25
24
|
it "should default to BaseTransportFactory and BinaryProtocolFactory when not specified" do
|
26
|
-
server = BaseServer.new(mock("Processor"), mock("BaseServerTransport"))
|
27
|
-
server.instance_variable_get(:'@transport_factory').should be_an_instance_of(BaseTransportFactory)
|
28
|
-
server.instance_variable_get(:'@protocol_factory').should be_an_instance_of(BinaryProtocolFactory)
|
25
|
+
server = Thrift::BaseServer.new(mock("Processor"), mock("BaseServerTransport"))
|
26
|
+
server.instance_variable_get(:'@transport_factory').should be_an_instance_of(Thrift::BaseTransportFactory)
|
27
|
+
server.instance_variable_get(:'@protocol_factory').should be_an_instance_of(Thrift::BinaryProtocolFactory)
|
29
28
|
end
|
30
29
|
|
31
30
|
# serve is a noop, so can't test that
|
32
31
|
end
|
33
32
|
|
34
|
-
|
33
|
+
describe Thrift::SimpleServer do
|
35
34
|
before(:each) do
|
36
35
|
@processor = mock("Processor")
|
37
36
|
@serverTrans = mock("ServerTransport")
|
38
37
|
@trans = mock("BaseTransport")
|
39
38
|
@prot = mock("BaseProtocol")
|
40
39
|
@client = mock("Client")
|
41
|
-
@server =
|
40
|
+
@server = described_class.new(@processor, @serverTrans, @trans, @prot)
|
42
41
|
end
|
43
|
-
|
44
|
-
|
45
|
-
describe SimpleServer do
|
46
|
-
it_should_behave_like "servers"
|
47
|
-
|
48
|
-
def server_type
|
49
|
-
SimpleServer
|
50
|
-
end
|
51
|
-
|
42
|
+
|
52
43
|
it "should serve in the main thread" do
|
53
44
|
@serverTrans.should_receive(:listen).ordered
|
54
45
|
@serverTrans.should_receive(:accept).exactly(3).times.and_return(@client)
|
@@ -68,11 +59,14 @@ class ThriftServerSpec < Spec::ExampleGroup
|
|
68
59
|
end
|
69
60
|
end
|
70
61
|
|
71
|
-
describe ThreadedServer do
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
62
|
+
describe Thrift::ThreadedServer do
|
63
|
+
before(:each) do
|
64
|
+
@processor = mock("Processor")
|
65
|
+
@serverTrans = mock("ServerTransport")
|
66
|
+
@trans = mock("BaseTransport")
|
67
|
+
@prot = mock("BaseProtocol")
|
68
|
+
@client = mock("Client")
|
69
|
+
@server = described_class.new(@processor, @serverTrans, @trans, @prot)
|
76
70
|
end
|
77
71
|
|
78
72
|
it "should serve using threads" do
|
@@ -95,50 +89,44 @@ class ThriftServerSpec < Spec::ExampleGroup
|
|
95
89
|
end
|
96
90
|
end
|
97
91
|
|
98
|
-
describe ThreadPoolServer do
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
@
|
104
|
-
|
105
|
-
@
|
106
|
-
Queue.should_receive(:new).and_return(@excQ)
|
107
|
-
ThreadPoolServer
|
108
|
-
end
|
109
|
-
|
110
|
-
it "should set up the queues" do
|
111
|
-
@server.instance_variable_get(:'@thread_q').should be(@threadQ)
|
112
|
-
@server.instance_variable_get(:'@exception_q').should be(@excQ)
|
92
|
+
describe Thrift::ThreadPoolServer do
|
93
|
+
before(:each) do
|
94
|
+
@processor = mock("Processor")
|
95
|
+
@server_trans = mock("ServerTransport")
|
96
|
+
@trans = mock("BaseTransport")
|
97
|
+
@prot = mock("BaseProtocol")
|
98
|
+
@client = mock("Client")
|
99
|
+
@server = described_class.new(@processor, @server_trans, @trans, @prot)
|
113
100
|
end
|
114
101
|
|
115
102
|
it "should serve inside a thread" do
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
@server.rspec_verify
|
103
|
+
exception_q = @server.instance_variable_get(:@exception_q)
|
104
|
+
described_class.any_instance.should_receive(:serve) do
|
105
|
+
exception_q.push(StandardError.new('ERROR'))
|
120
106
|
end
|
121
|
-
@
|
122
|
-
lambda { @server.rescuable_serve }.should throw_symbol(:popped)
|
107
|
+
expect { @server.rescuable_serve }.to(raise_error('ERROR'))
|
123
108
|
end
|
124
109
|
|
125
110
|
it "should avoid running the server twice when retrying rescuable_serve" do
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
111
|
+
exception_q = @server.instance_variable_get(:@exception_q)
|
112
|
+
described_class.any_instance.should_receive(:serve) do
|
113
|
+
exception_q.push(StandardError.new('ERROR1'))
|
114
|
+
exception_q.push(StandardError.new('ERROR2'))
|
130
115
|
end
|
131
|
-
@
|
132
|
-
|
133
|
-
lambda { @server.rescuable_serve }.should throw_symbol(:popped)
|
116
|
+
expect { @server.rescuable_serve }.to(raise_error('ERROR1'))
|
117
|
+
expect { @server.rescuable_serve }.to(raise_error('ERROR2'))
|
134
118
|
end
|
135
119
|
|
136
120
|
it "should serve using a thread pool" do
|
137
|
-
|
138
|
-
|
139
|
-
@
|
121
|
+
thread_q = mock("SizedQueue")
|
122
|
+
exception_q = mock("Queue")
|
123
|
+
@server.instance_variable_set(:@thread_q, thread_q)
|
124
|
+
@server.instance_variable_set(:@exception_q, exception_q)
|
125
|
+
@server_trans.should_receive(:listen).ordered
|
126
|
+
thread_q.should_receive(:push).with(:token)
|
127
|
+
thread_q.should_receive(:pop)
|
140
128
|
Thread.should_receive(:new).and_yield
|
141
|
-
@
|
129
|
+
@server_trans.should_receive(:accept).exactly(3).times.and_return(@client)
|
142
130
|
@trans.should_receive(:get_transport).exactly(3).times.and_return(@trans)
|
143
131
|
@prot.should_receive(:get_protocol).exactly(3).times.and_return(@prot)
|
144
132
|
x = 0
|
@@ -151,9 +139,9 @@ class ThriftServerSpec < Spec::ExampleGroup
|
|
151
139
|
end
|
152
140
|
end
|
153
141
|
@trans.should_receive(:close).exactly(3).times
|
154
|
-
|
155
|
-
@
|
156
|
-
|
142
|
+
exception_q.should_receive(:push).with(error).and_throw(:stop)
|
143
|
+
@server_trans.should_receive(:close)
|
144
|
+
expect { @server.serve }.to(throw_symbol(:stop))
|
157
145
|
end
|
158
146
|
end
|
159
147
|
end
|