From e6b73612d1dcd0bace2361338e6ae82eb591282b Mon Sep 17 00:00:00 2001 From: Matthew Oliver Date: Tue, 13 Aug 2024 17:04:40 +1000 Subject: [PATCH] FakeStatsdClient: Stop issuing DNS calls for host. Running unittests on my home server, even though is a beast, would be painfully slow. Running them on my laptop ran much faster. I tried everything and couldn't figure it out. Chris, co-author, dug in and after we ran out of options he fired up his packet capture and realised most tests were issuing DNS queries for `host.`. On my home server, it runs ipv6 dual stack, so these calls would need to timeout before a test can continue. On finding this `host.` Ben, another co-author, dug into the code and found the only reference we have to `host` is in FakeStatsdClient. Sure enough as I dug a little further it turns out our FakeStatsdClient used to override a StatsdClient function `_determine_sock_family(self, host, port)` to stop actually creating a real socket. However, at some point the StatsdClient was refactored and that method was renamed and changed. Which leads to a DNS lookup every time we create a debug_logger as it brings up the socket. As you can imagine, this happens alot! This patch overrides the new function that handles to socket creation `_set_sock_family_and_target(self, host, port)`. Which eliminates the DNS call all together. Co-Authored-By: Ben Formosa Co-Authored-By: Chris Smart Change-Id: Ie393075f79447627714692e3f01bb53e967a71e8 --- test/debug_logger.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/debug_logger.py b/test/debug_logger.py index b91f46d2b2..fbf782131c 100644 --- a/test/debug_logger.py +++ b/test/debug_logger.py @@ -65,8 +65,8 @@ class FakeStatsdClient(statsd_client.StatsdClient): return func(*args, **kwargs) return wrapper - def _determine_sock_family(self, host, port): - return None, None + def _set_sock_family_and_target(self, host, port): + self._target = (host, port) def _open_socket(self): return self.recording_socket @@ -113,6 +113,7 @@ class CaptureLog(object): Captures log records passed to the ``handle`` method and provides accessor functions to the captured logs. """ + def __init__(self): self.clear() @@ -280,6 +281,7 @@ class ForwardingLogHandler(logging.NullHandler): to a given handler function. This can be useful to forward records to a handler without the handler itself needing to subclass LogHandler. """ + def __init__(self, handler_fn): super(ForwardingLogHandler, self).__init__() self.handler_fn = handler_fn @@ -293,6 +295,7 @@ class CaptureLogAdapter(utils.LogAdapter, CaptureLog): A LogAdapter that is capable of capturing logs for inspection via accessor methods. """ + def __init__(self, logger, name): super(CaptureLogAdapter, self).__init__(logger, name) self.clear()