From: Nicki Křížek Date: Thu, 19 Jun 2025 12:09:57 +0000 (+0200) Subject: Use time.monotonic() for time measumeremts in pytest X-Git-Tag: v9.21.10~44^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=069e4ef0f72bfe045123db817908460144ff70f7;p=thirdparty%2Fbind9.git Use time.monotonic() for time measumeremts in pytest For duration measurements, i.e. deadlines and timeouts, it's more suitable to use monotonic time as it's guaranteed to only go forward, unlike time.time() which can be affected by local clock settings. --- diff --git a/bin/tests/system/doth/tests_gnutls.py b/bin/tests/system/doth/tests_gnutls.py index 8770206fa82..f49b4401c51 100644 --- a/bin/tests/system/doth/tests_gnutls.py +++ b/bin/tests/system/doth/tests_gnutls.py @@ -72,11 +72,11 @@ def test_gnutls_cli_query(gnutls_cli_executable, named_tlsport): # upon receiving a DNS response. selector = selectors.DefaultSelector() selector.register(gnutls_cli.stdout, selectors.EVENT_READ) - deadline = time.time() + 10 + deadline = time.monotonic() + 10 gnutls_cli_output = b"" response = b"" while not response and not gnutls_cli.poll(): - if not selector.select(timeout=deadline - time.time()): + if not selector.select(timeout=deadline - time.monotonic()): break gnutls_cli_output += gnutls_cli.stdout.read(512) try: diff --git a/bin/tests/system/isctest/log/watchlog.py b/bin/tests/system/isctest/log/watchlog.py index b17cd0597fd..ffa75e156df 100644 --- a/bin/tests/system/isctest/log/watchlog.py +++ b/bin/tests/system/isctest/log/watchlog.py @@ -229,8 +229,8 @@ class WatchLog(abc.ABC): raise WatchLogException("No file to watch") leftover = "" assert timeout, "Do not use this class unless you want to WAIT for something." - deadline = time.time() + timeout - while time.time() < deadline: + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: for line in self._fd.readlines(): if line[-1] != "\n": # Line is not completely written yet, buffer and keep on waiting diff --git a/bin/tests/system/isctest/run.py b/bin/tests/system/isctest/run.py index b26190e4a3d..5c994ce3778 100644 --- a/bin/tests/system/isctest/run.py +++ b/bin/tests/system/isctest/run.py @@ -126,9 +126,9 @@ def perl(script: str, args: Optional[List[str]] = None) -> None: def retry_with_timeout(func, timeout, delay=1, msg=None): - start_time = time.time() + start_time = time.monotonic() exc_msg = None - while time.time() < start_time + timeout: + while time.monotonic() < start_time + timeout: exc_msg = None try: if func(): diff --git a/bin/tests/system/pipelined/ans5/ans.py b/bin/tests/system/pipelined/ans5/ans.py index bac5ed35d80..b58ad84bef9 100644 --- a/bin/tests/system/pipelined/ans5/ans.py +++ b/bin/tests/system/pipelined/ans5/ans.py @@ -84,7 +84,7 @@ class TCPDelayer(threading.Thread): while self.running: curr_timeout = 0.5 try: - curr_timeout = self.queue[0][0] - time.time() + curr_timeout = self.queue[0][0] - time.monotonic() except StopIteration: pass if curr_timeout > 0: @@ -97,14 +97,14 @@ class TCPDelayer(threading.Thread): data = self.conn.recv(65535) if not data: return - self.queue.append((time.time() + DELAY, data)) + self.queue.append((time.monotonic() + DELAY, data)) if self.cconn in rfds: data = self.cconn.recv(65535) if not data == 0: return self.conn.send(data) try: - while self.queue[0][0] - time.time() < 0: + while self.queue[0][0] - time.monotonic() < 0: _, data = self.queue.pop(0) self.cconn.send(data) except StopIteration: @@ -133,7 +133,7 @@ class UDPDelayer(threading.Thread): while self.running: curr_timeout = 0.5 if self.queue: - curr_timeout = self.queue[0][0] - time.time() + curr_timeout = self.queue[0][0] - time.monotonic() if curr_timeout >= 0: if curr_timeout == 0: curr_timeout = 0.5 @@ -144,7 +144,7 @@ class UDPDelayer(threading.Thread): data, addr = self.sock.recvfrom(65535) if not data: return - self.queue.append((time.time() + DELAY, data)) + self.queue.append((time.monotonic() + DELAY, data)) qid = struct.unpack(">H", data[:2])[0] log("Received a query from %s, queryid %d" % (str(addr), qid)) self.qid_mapping[qid] = addr @@ -160,7 +160,7 @@ class UDPDelayer(threading.Thread): "Received a response from %s, queryid %d, sending to %s" % (str(addr), qid, str(dst)) ) - while self.queue and self.queue[0][0] - time.time() < 0: + while self.queue and self.queue[0][0] - time.monotonic() < 0: _, data = self.queue.pop(0) qid = struct.unpack(">H", data[:2])[0] log("Sending a query to %s, queryid %d" % (str(self.dst), qid)) diff --git a/bin/tests/system/tcp/ans6/ans.py b/bin/tests/system/tcp/ans6/ans.py index 4595ddcd07d..b94697e2cd9 100644 --- a/bin/tests/system/tcp/ans6/ans.py +++ b/bin/tests/system/tcp/ans6/ans.py @@ -71,9 +71,9 @@ def open_connections(active_conns, count, host, port): else: queued.append(sock) - start = time.time() + start = time.monotonic() while queued: - now = time.time() + now = time.monotonic() time_left = OPEN_TIMEOUT - (now - start) if time_left <= 0: break