218
218
# We use 'request' instead of 'self' below because the test matters
219
219
# more and we need a container to properly set connection_thread.
220
220
def handle_connection(request):
221
req = request.readline()
222
222
# Capture the thread and make it use 'caught' so we can wait on
223
223
# the event that will be set when the exception is caught. We
224
224
# also capture the thread to know where to look.
233
233
client.write(b'ping\n')
234
234
# Wait for the exception to be caught
236
self.assertEqual(b'', client.read()) # connection closed
236
self.assertEqual(b'', client.read()) # connection closed
237
237
# Check that the connection thread did catch the exception,
238
238
# http://pad.lv/869366 was wrongly checking the server thread which
239
239
# works for TestingTCPServer where the connection is handled in the
277
278
client.connect((server.host, server.port))
278
279
# Wait for the exception to be caught
280
self.assertEqual(b'', client.read()) # connection closed
281
self.assertEqual(b'', client.read()) # connection closed
281
282
# The connection wasn't served properly but the exception should have
282
283
# been swallowed (see test_server_crash_while_responding remark about
283
284
# http://pad.lv/869366 explaining why we can't check the server thread
302
303
class TestTestingSmartServer(tests.TestCase):
304
305
def test_sets_client_timeout(self):
305
server = test_server.TestingSmartServer(('localhost', 0), None, None,
306
server = test_server.TestingSmartServer(
307
('localhost', 0), None, None,
306
308
root_client_path='/no-such-client/path')
307
309
self.assertEqual(test_server._DEFAULT_TESTING_CLIENT_TIMEOUT,
308
310
server._client_timeout)
326
328
server_sock, client_sock = portable_socket_pair()
327
329
# This should timeout quickly, but not generate an exception.
328
handler = test_server.TestingSmartConnectionHandler(server_sock,
329
server_sock.getpeername(), s)
330
test_server.TestingSmartConnectionHandler(
331
server_sock, server_sock.getpeername(), s)
331
333
def test_connection_shutdown_while_serving_no_error(self):
333
335
server_sock, client_sock = portable_socket_pair()
334
337
class ShutdownConnectionHandler(
335
test_server.TestingSmartConnectionHandler):
338
test_server.TestingSmartConnectionHandler):
337
340
def _build_protocol(self):
338
341
self.finished = True
339
342
return super(ShutdownConnectionHandler, self)._build_protocol()
340
343
# This should trigger shutdown after the entering _build_protocol, and
341
344
# we should exit cleanly, without raising an exception.
342
handler = ShutdownConnectionHandler(server_sock,
343
server_sock.getpeername(), s)
345
ShutdownConnectionHandler(server_sock, server_sock.getpeername(), s)