Skip to content

Commit ca3fb04

Browse files
committed
[test] review/cleanup ssl helper
1 parent f259529 commit ca3fb04

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

src/test/ruby/ssl/test_helper.rb

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def start_server0(port0, verify_mode, start_immediately, args = {}, &block); req
8181
ctx_proc.call(context) if ctx_proc
8282

8383
Socket.do_not_reverse_lookup = true
84-
tcp_server = nil
84+
8585
port = port0
8686
begin
8787
tcp_server = TCPServer.new("127.0.0.1", port)
@@ -93,13 +93,16 @@ def start_server0(port0, verify_mode, start_immediately, args = {}, &block); req
9393
ssls = OpenSSL::SSL::SSLServer.new(tcp_server, context)
9494
ssls.start_immediately = start_immediately
9595

96+
test_method = caller_locations(1, 1)[0]
97+
test_method = test_method.label.index('block in') ? "#{test_method.path}:#{test_method.lineno}" : test_method.label
98+
9699
begin
97100
server = Thread.new do
98101
Thread.current.abort_on_exception = true
99102
server_loop0(context, ssls, server_proc)
100103
end
101104

102-
$stderr.printf("%s started: pid=%d port=%d\n", SSL_SERVER, $$, port) #if $DEBUG
105+
printf("(%s) started: pid=%d port=%d\n", test_method, $$, port) if $VERBOSE
103106

104107
block.call(server, port.to_i)
105108
ensure
@@ -136,10 +139,12 @@ def start_server(verify_mode, start_immediately, args = {}, &block); require 'so
136139
ssls = OpenSSL::SSL::SSLServer.new(tcps, ctx)
137140
ssls.start_immediately = start_immediately
138141

142+
test_method = caller_locations(3, 1)[0].label
143+
139144
threads = []
140145
begin
141146
server = Thread.new do
142-
# Thread.current.abort_on_exception = true
147+
Thread.current.report_on_exception = false
143148
begin
144149
server_loop(ctx, ssls, stop_pipe_r, ignore_listener_error, server_proc, threads)
145150
ensure
@@ -148,17 +153,31 @@ def start_server(verify_mode, start_immediately, args = {}, &block); require 'so
148153
end
149154
threads.unshift server
150155

151-
$stderr.printf("SSL server started: pid=%d port=%d\n", $$, port) if $DEBUG
156+
printf("SSL server started (#{test_method}): pid=%d port=%d\n", $$, port) if $VERBOSE
152157

153158
client = Thread.new do
154159
begin
155160
block.call(server, port.to_i)
156161
ensure
162+
# Stop accepting new connection
157163
stop_pipe_w.close
164+
server.join
158165
end
159166
end
160167
threads.unshift client
161168
ensure
169+
# Terminate existing connections. If a thread did 'pend', re-raise it.
170+
pend = nil
171+
threads.each { |th|
172+
begin
173+
th.join(5) or th.raise(RuntimeError, "[start_server] thread did not exit in 5 secs")
174+
rescue Test::Unit::PendedError
175+
pend = $!
176+
rescue Exception
177+
warn "#{__method__} (#{test_method}): #{$!.inspect}" if $DEBUG
178+
end
179+
}
180+
raise pend if pend
162181
assert_join_threads(threads)
163182
end
164183
end
@@ -185,35 +204,21 @@ def tcp_server_close(thread, tcp_server)
185204
tcp_server.close if tcp_server
186205
end
187206

188-
def tcp_server_close(thread, tcp_server)
189-
tcp_server.close if (tcp_server)
190-
if thread
191-
thread.join(5)
192-
if thread.alive?
193-
thread.kill
194-
thread.join
195-
flunk("TCPServer was closed and SSLServer is still alive") unless $!
196-
end
197-
end
198-
end if RUBY_VERSION < '1.9.0' ||
199-
( defined? JRUBY_VERSION && JRUBY_VERSION < '1.7.0' )
200-
private :tcp_server_close
201-
202207
def server_loop0(context, server, server_proc)
203208
loop do
204-
ssl = nil
205209
begin
206210
ssl = server.accept
207211
rescue OpenSSL::SSL::SSLError
208212
retry
209213
end
210214

211215
Thread.start do
212-
Thread.current.abort_on_exception = true
216+
Thread.current.report_on_exception = false
213217
server_proc.call(context, ssl)
214218
end
215219
end
216-
rescue Errno::EBADF, IOError, Errno::EINVAL, Errno::ECONNABORTED, Errno::ENOTSOCK, Errno::ECONNRESET
220+
rescue IOError, Errno::EBADF, Errno::EINVAL, Errno::ECONNABORTED, Errno::ENOTSOCK, Errno::ECONNRESET
221+
warn "#{__method__}: #{$!.inspect}" if $DEBUG
217222
end
218223

219224
def server_loop(ctx, ssls, stop_pipe_r, ignore_listener_error, server_proc, threads)
@@ -222,25 +227,27 @@ def server_loop(ctx, ssls, stop_pipe_r, ignore_listener_error, server_proc, thre
222227
readable, = IO.select([ssls, stop_pipe_r])
223228
return if readable.include? stop_pipe_r
224229
ssl = ssls.accept
225-
rescue OpenSSL::SSL::SSLError
230+
rescue OpenSSL::SSL::SSLError, IOError, Errno::EBADF, Errno::EINVAL, Errno::ECONNABORTED, Errno::ENOTSOCK, Errno::ECONNRESET
226231
if ignore_listener_error
232+
warn "#{__method__} (retry): #{$!.inspect}" if $DEBUG
227233
retry
228234
else
229-
raise
235+
warn "#{__method__}: #{$!.inspect}" if $DEBUG
230236
end
237+
raise
231238
end
232239

233240
threads << Thread.start do
241+
Thread.current.report_on_exception = false
234242
begin
235243
server_proc.call(ctx, ssl)
236244
ensure
237245
ssl.close
238246
end
239247
end
240248
end
241-
rescue Errno::EBADF, IOError, Errno::EINVAL, Errno::ECONNABORTED, Errno::ENOTSOCK, Errno::ECONNRESET => ex
249+
rescue IOError, Errno::EBADF, Errno::EINVAL, Errno::ECONNABORTED, Errno::ENOTSOCK, Errno::ECONNRESET => ex
242250
raise(ex) unless ignore_listener_error
243-
puts ex.inspect if $VERBOSE
244251
end
245252

246253
def server_connect(port, ctx = nil)
@@ -273,6 +280,7 @@ def readwrite_loop(context, ssl)
273280
ssl.write(line)
274281
end
275282
rescue IOError, OpenSSL::SSL::SSLError
283+
warn "#{__method__}: #{$!.inspect}" if $DEBUG
276284
ensure
277285
ssl.close rescue nil
278286
end

0 commit comments

Comments
 (0)