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.
This commit is contained in:
Nicki Křížek 2025-06-19 14:09:57 +02:00
parent 481b46ffcc
commit 069e4ef0f7
5 changed files with 14 additions and 14 deletions

View file

@ -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:

View file

@ -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

View file

@ -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():

View file

@ -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))

View file

@ -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