1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
require 'spec_helper'
describe 'Server' do
describe Thrift::BaseServer do
before(:each) do
@processor = double("Processor")
@serverTrans = double("ServerTransport")
@trans = double("BaseTransport")
@prot = double("BaseProtocol")
@server = described_class.new(@processor, @serverTrans, @trans, @prot)
end
it "should default to BaseTransportFactory and BinaryProtocolFactory when not specified" do
@server = Thrift::BaseServer.new(double("Processor"), double("BaseServerTransport"))
expect(@server.instance_variable_get(:'@transport_factory')).to be_an_instance_of(Thrift::BaseTransportFactory)
expect(@server.instance_variable_get(:'@protocol_factory')).to be_an_instance_of(Thrift::BinaryProtocolFactory)
end
it "should not serve" do
expect { @server.serve()}.to raise_error(NotImplementedError)
end
it "should provide a reasonable to_s" do
expect(@serverTrans).to receive(:to_s).once.and_return("serverTrans")
expect(@trans).to receive(:to_s).once.and_return("trans")
expect(@prot).to receive(:to_s).once.and_return("prot")
expect(@server.to_s).to eq("server(prot(trans(serverTrans)))")
end
end
describe Thrift::SimpleServer do
before(:each) do
@processor = double("Processor")
@serverTrans = double("ServerTransport")
@trans = double("BaseTransport")
@prot = double("BaseProtocol")
@client = double("Client")
@server = described_class.new(@processor, @serverTrans, @trans, @prot)
end
it "should provide a reasonable to_s" do
expect(@serverTrans).to receive(:to_s).once.and_return("serverTrans")
expect(@trans).to receive(:to_s).once.and_return("trans")
expect(@prot).to receive(:to_s).once.and_return("prot")
expect(@server.to_s).to eq("simple(server(prot(trans(serverTrans))))")
end
it "should serve in the main thread" do
expect(@serverTrans).to receive(:listen).ordered
expect(@serverTrans).to receive(:accept).exactly(3).times.and_return(@client)
expect(@trans).to receive(:get_transport).exactly(3).times.with(@client).and_return(@trans)
expect(@prot).to receive(:get_protocol).exactly(3).times.with(@trans).and_return(@prot)
x = 0
expect(@processor).to receive(:process).exactly(3).times.with(@prot, @prot) do
case (x += 1)
when 1 then raise Thrift::TransportException
when 2 then raise Thrift::ProtocolException
when 3 then throw :stop
end
end
expect(@trans).to receive(:close).exactly(3).times
expect(@serverTrans).to receive(:close).ordered
expect { @server.serve }.to throw_symbol(:stop)
end
end
describe Thrift::ThreadedServer do
before(:each) do
@processor = double("Processor")
@serverTrans = double("ServerTransport")
@trans = double("BaseTransport")
@prot = double("BaseProtocol")
@client = double("Client")
@server = described_class.new(@processor, @serverTrans, @trans, @prot)
end
it "should provide a reasonable to_s" do
expect(@serverTrans).to receive(:to_s).once.and_return("serverTrans")
expect(@trans).to receive(:to_s).once.and_return("trans")
expect(@prot).to receive(:to_s).once.and_return("prot")
expect(@server.to_s).to eq("threaded(server(prot(trans(serverTrans))))")
end
it "should serve using threads" do
expect(@serverTrans).to receive(:listen).ordered
expect(@serverTrans).to receive(:accept).exactly(3).times.and_return(@client)
expect(@trans).to receive(:get_transport).exactly(3).times.with(@client).and_return(@trans)
expect(@prot).to receive(:get_protocol).exactly(3).times.with(@trans).and_return(@prot)
expect(Thread).to receive(:new).with(@prot, @trans).exactly(3).times.and_yield(@prot, @trans)
x = 0
expect(@processor).to receive(:process).exactly(3).times.with(@prot, @prot) do
case (x += 1)
when 1 then raise Thrift::TransportException
when 2 then raise Thrift::ProtocolException
when 3 then throw :stop
end
end
expect(@trans).to receive(:close).exactly(3).times
expect(@serverTrans).to receive(:close).ordered
expect { @server.serve }.to throw_symbol(:stop)
end
end
describe Thrift::ThreadPoolServer do
before(:each) do
@processor = double("Processor")
@server_trans = double("ServerTransport")
@trans = double("BaseTransport")
@prot = double("BaseProtocol")
@client = double("Client")
@server = described_class.new(@processor, @server_trans, @trans, @prot)
sleep(0.15)
end
it "should provide a reasonable to_s" do
expect(@server_trans).to receive(:to_s).once.and_return("server_trans")
expect(@trans).to receive(:to_s).once.and_return("trans")
expect(@prot).to receive(:to_s).once.and_return("prot")
expect(@server.to_s).to eq("threadpool(server(prot(trans(server_trans))))")
end
it "should serve inside a thread" do
exception_q = @server.instance_variable_get(:@exception_q)
expect_any_instance_of(described_class).to receive(:serve) do
exception_q.push(StandardError.new('ERROR'))
end
expect { @server.rescuable_serve }.to(raise_error('ERROR'))
sleep(0.15)
end
it "should avoid running the server twice when retrying rescuable_serve" do
exception_q = @server.instance_variable_get(:@exception_q)
expect_any_instance_of(described_class).to receive(:serve) do
exception_q.push(StandardError.new('ERROR1'))
exception_q.push(StandardError.new('ERROR2'))
end
expect { @server.rescuable_serve }.to(raise_error('ERROR1'))
expect { @server.rescuable_serve }.to(raise_error('ERROR2'))
end
it "should serve using a thread pool" do
thread_q = double("SizedQueue")
exception_q = double("Queue")
@server.instance_variable_set(:@thread_q, thread_q)
@server.instance_variable_set(:@exception_q, exception_q)
expect(@server_trans).to receive(:listen).ordered
expect(thread_q).to receive(:push).with(:token)
expect(thread_q).to receive(:pop)
expect(Thread).to receive(:new).and_yield
expect(@server_trans).to receive(:accept).exactly(3).times.and_return(@client)
expect(@trans).to receive(:get_transport).exactly(3).times.and_return(@trans)
expect(@prot).to receive(:get_protocol).exactly(3).times.and_return(@prot)
x = 0
error = RuntimeError.new("Stopped")
expect(@processor).to receive(:process).exactly(3).times.with(@prot, @prot) do
case (x += 1)
when 1 then raise Thrift::TransportException
when 2 then raise Thrift::ProtocolException
when 3 then raise error
end
end
expect(@trans).to receive(:close).exactly(3).times
expect(exception_q).to receive(:push).with(error).and_throw(:stop)
expect(@server_trans).to receive(:close)
expect { @server.serve }.to(throw_symbol(:stop))
end
end
end
|