From e4ee569bd2f5bbab55987659edcf3cdf7ccf12d7 Mon Sep 17 00:00:00 2001 From: Unrud Date: Sun, 9 Sep 2018 07:28:36 +0200 Subject: [PATCH] Measure coverage of forked processes --- radicale/server.py | 30 +++++++++++++++--------------- radicale/tests/__init__.py | 22 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/radicale/server.py b/radicale/server.py index 2ec3f94..296fa2f 100644 --- a/radicale/server.py +++ b/radicale/server.py @@ -133,6 +133,7 @@ class ParallelHTTPServer(ParallelizationMixIn, return super().finish_request(request, client_address) def finish_request(self, request, client_address): + """Don't overwrite this! (Modified by tests.)""" with self.connections_guard: return self.finish_request_locked(request, client_address) @@ -164,22 +165,21 @@ class ParallelHTTPSServer(ParallelHTTPServer): ssl_version=self.protocol, ciphers=self.ciphers, do_handshake_on_connect=False) - def finish_request(self, request, client_address): - with self.connections_guard: + def finish_request_locked(self, request, client_address): + try: try: - try: - request.do_handshake() - except socket.timeout: - raise - except Exception as e: - raise RuntimeError("SSL handshake failed: %s" % e) from e - except Exception: - try: - self.handle_error(request, client_address) - finally: - self.shutdown_request(request) - return - return super().finish_request_locked(request, client_address) + request.do_handshake() + except socket.timeout: + raise + except Exception as e: + raise RuntimeError("SSL handshake failed: %s" % e) from e + except Exception: + try: + self.handle_error(request, client_address) + finally: + self.shutdown_request(request) + return + return super().finish_request_locked(request, client_address) class ServerHandler(wsgiref.simple_server.ServerHandler): diff --git a/radicale/tests/__init__.py b/radicale/tests/__init__.py index 30d40ea..b9b8dac 100644 --- a/radicale/tests/__init__.py +++ b/radicale/tests/__init__.py @@ -26,6 +26,28 @@ import radicale import sys from io import BytesIO +from pytest_cov import embed + +from radicale import server + +# Measure coverage of forked processes +finish_request = server.ParallelHTTPServer.finish_request +pid = os.getpid() + + +def finish_request_cov(self, request, client_address): + cov = None + if pid != os.getpid(): + cov = embed.init() + try: + return finish_request(self, request, client_address) + finally: + if cov: + embed.cleanup(cov) + + +server.ParallelHTTPServer.finish_request = finish_request_cov + # Allow importing of tests.custom.... sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) # Enable debug output