@@ -81,7 +81,7 @@ def start_server0(port0, verify_mode, start_immediately, args = {}, &block); req
81
81
ctx_proc . call ( context ) if ctx_proc
82
82
83
83
Socket . do_not_reverse_lookup = true
84
- tcp_server = nil
84
+
85
85
port = port0
86
86
begin
87
87
tcp_server = TCPServer . new ( "127.0.0.1" , port )
@@ -93,13 +93,16 @@ def start_server0(port0, verify_mode, start_immediately, args = {}, &block); req
93
93
ssls = OpenSSL ::SSL ::SSLServer . new ( tcp_server , context )
94
94
ssls . start_immediately = start_immediately
95
95
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
+
96
99
begin
97
100
server = Thread . new do
98
101
Thread . current . abort_on_exception = true
99
102
server_loop0 ( context , ssls , server_proc )
100
103
end
101
104
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
103
106
104
107
block . call ( server , port . to_i )
105
108
ensure
@@ -136,10 +139,12 @@ def start_server(verify_mode, start_immediately, args = {}, &block); require 'so
136
139
ssls = OpenSSL ::SSL ::SSLServer . new ( tcps , ctx )
137
140
ssls . start_immediately = start_immediately
138
141
142
+ test_method = caller_locations ( 3 , 1 ) [ 0 ] . label
143
+
139
144
threads = [ ]
140
145
begin
141
146
server = Thread . new do
142
- # Thread.current.abort_on_exception = true
147
+ Thread . current . report_on_exception = false
143
148
begin
144
149
server_loop ( ctx , ssls , stop_pipe_r , ignore_listener_error , server_proc , threads )
145
150
ensure
@@ -148,17 +153,31 @@ def start_server(verify_mode, start_immediately, args = {}, &block); require 'so
148
153
end
149
154
threads . unshift server
150
155
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
152
157
153
158
client = Thread . new do
154
159
begin
155
160
block . call ( server , port . to_i )
156
161
ensure
162
+ # Stop accepting new connection
157
163
stop_pipe_w . close
164
+ server . join
158
165
end
159
166
end
160
167
threads . unshift client
161
168
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
162
181
assert_join_threads ( threads )
163
182
end
164
183
end
@@ -185,35 +204,21 @@ def tcp_server_close(thread, tcp_server)
185
204
tcp_server . close if tcp_server
186
205
end
187
206
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
-
202
207
def server_loop0 ( context , server , server_proc )
203
208
loop do
204
- ssl = nil
205
209
begin
206
210
ssl = server . accept
207
211
rescue OpenSSL ::SSL ::SSLError
208
212
retry
209
213
end
210
214
211
215
Thread . start do
212
- Thread . current . abort_on_exception = true
216
+ Thread . current . report_on_exception = false
213
217
server_proc . call ( context , ssl )
214
218
end
215
219
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
217
222
end
218
223
219
224
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
222
227
readable , = IO . select ( [ ssls , stop_pipe_r ] )
223
228
return if readable . include? stop_pipe_r
224
229
ssl = ssls . accept
225
- rescue OpenSSL ::SSL ::SSLError
230
+ rescue OpenSSL ::SSL ::SSLError , IOError , Errno :: EBADF , Errno :: EINVAL , Errno :: ECONNABORTED , Errno :: ENOTSOCK , Errno :: ECONNRESET
226
231
if ignore_listener_error
232
+ warn "#{ __method__ } (retry): #{ $!. inspect } " if $DEBUG
227
233
retry
228
234
else
229
- raise
235
+ warn " #{ __method__ } : #{ $! . inspect } " if $DEBUG
230
236
end
237
+ raise
231
238
end
232
239
233
240
threads << Thread . start do
241
+ Thread . current . report_on_exception = false
234
242
begin
235
243
server_proc . call ( ctx , ssl )
236
244
ensure
237
245
ssl . close
238
246
end
239
247
end
240
248
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
242
250
raise ( ex ) unless ignore_listener_error
243
- puts ex . inspect if $VERBOSE
244
251
end
245
252
246
253
def server_connect ( port , ctx = nil )
@@ -273,6 +280,7 @@ def readwrite_loop(context, ssl)
273
280
ssl . write ( line )
274
281
end
275
282
rescue IOError , OpenSSL ::SSL ::SSLError
283
+ warn "#{ __method__ } : #{ $!. inspect } " if $DEBUG
276
284
ensure
277
285
ssl . close rescue nil
278
286
end
0 commit comments