296
296
self.started = threading.Event()
297
297
self.serving = threading.Event()
298
298
self.stopped = threading.Event()
299
# We collect the resources used by the clients so we can release them
300
303
def server_bind(self):
301
304
# We need to override the SocketServer bind, yet, we still want to use
323
326
"""Verify the request.
325
328
Return True if we should proceed with this request, False if we should
326
not even touch a single byte in the socket ! This is used to stop the
327
server with a dummy last connection.
329
not even touch a single byte in the socket ! This is useful when we
330
stop the server with a dummy last connection.
329
332
return self.serving.isSet()
331
def stop_clients(self):
335
class TestingTCPServer(TestingTCPServerMixin, SocketServer.TCPServer):
337
def __init__(self, server_address, request_handler_class):
338
TestingTCPServerMixin.__init__(self, SocketServer.TCPServer)
339
SocketServer.TCPServer.__init__(self, server_address,
340
request_handler_class)
342
334
def handle_error(self, request, client_address):
343
335
# Stop serving and re-raise the last exception seen
344
336
self.serving.clear()
339
def stop_clients(self):
343
class TestingTCPServer(TestingTCPServerMixin, SocketServer.TCPServer):
345
def __init__(self, server_address, request_handler_class):
346
TestingTCPServerMixin.__init__(self, SocketServer.TCPServer)
347
SocketServer.TCPServer.__init__(self, server_address,
348
request_handler_class)
350
def get_request(self):
351
"""Get the request and client address from the socket."""
352
sock, addr = self.sibling_class.get_request(self)
353
self.clients.append((sock, addr))
356
# The following methods are called by the main thread
348
359
class TestingThreadingTCPServer(TestingTCPServerMixin,
349
360
SocketServer.ThreadingTCPServer):
353
364
SocketServer.TCPServer.__init__(self, server_address,
354
365
request_handler_class)
367
def get_request (self):
368
"""Get the request and client address from the socket."""
369
sock, addr = self.sibling_class.get_request(self)
370
# The thread is not create yet, it will be updated in process_request
371
self.clients.append((sock, addr, None))
374
def process_request_thread(self, stopped, request, client_address):
375
SocketServer.ThreadingTCPServer.process_request_thread(
376
self, request, client_address)
377
self.close_request(request)
380
def process_request(self, request, client_address):
381
"""Start a new thread to process the request."""
382
stopped = threading.Event()
383
t = test_server.ThreadWithException(
385
target = self.process_request_thread,
386
args = (stopped, request, client_address))
387
t.name = '%s -> %s' % (client_address, self.server_address)
388
if 'threads' in tests.selftest_debug_flags:
389
print 'Thread for: %s started' % (threading.currentThread().name,)
390
# Update the client description
392
self.clients.append((request, client_address, t))
357
396
class TestingTCPServerInAThread(object):
358
397
"""A server in a thread that re-raise thread exceptions."""
408
447
# server thread, it may happen that it's not blocked or even not
450
# We don't have to wait for the server to shut down to start shutting
451
# down the clients, so let's start now.
411
452
self.server.stop_clients()
412
453
# Now we wait for the thread running self.server.serve() to finish
413
454
self.server.stopped.wait()