From 9050e0f4b91c7e98465d2b67991c3f3dd5759a3f Mon Sep 17 00:00:00 2001 From: Tyson Smith Date: Thu, 5 Dec 2024 13:55:11 -0800 Subject: [PATCH] fix: use perf_counter() instead of time() where appropriate --- src/ffpuppet/bootstrapper.py | 12 ++++++------ src/ffpuppet/helpers.py | 6 +++--- src/ffpuppet/test_bootstrapper.py | 4 ++-- src/ffpuppet/test_helpers.py | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/ffpuppet/bootstrapper.py b/src/ffpuppet/bootstrapper.py index 0588545..c4eddca 100644 --- a/src/ffpuppet/bootstrapper.py +++ b/src/ffpuppet/bootstrapper.py @@ -7,7 +7,7 @@ from logging import getLogger from select import select from socket import SO_REUSEADDR, SOL_SOCKET, socket -from time import sleep, time +from time import perf_counter, sleep from typing import Callable from .exceptions import BrowserTerminatedError, BrowserTimeoutError, LaunchError @@ -150,7 +150,7 @@ def wait( None """ assert timeout >= 0 - start_time = time() + start_time = perf_counter() time_limit = start_time + timeout conn: socket | None = None try: @@ -163,7 +163,7 @@ def wait( raise BrowserTerminatedError( "Failure waiting for browser connection" ) - if time() >= time_limit: + if perf_counter() >= time_limit: raise BrowserTimeoutError( "Timeout waiting for browser connection" ) @@ -188,7 +188,7 @@ def wait( break if not cb_continue(): raise BrowserTerminatedError("Failure waiting for request") - if time() >= time_limit: + if perf_counter() >= time_limit: raise BrowserTimeoutError("Timeout waiting for request") if count_recv == 0: LOG.debug("connection failed, waiting for next connection...") @@ -206,7 +206,7 @@ def wait( "Connection: close\r\n\r\n" ) # set timeout to match remaining time - conn.settimeout(max(time_limit - time(), 1)) + conn.settimeout(max(time_limit - perf_counter(), 1)) LOG.debug("sending response (redirect %r)", url) try: conn.sendall(resp.encode("ascii")) @@ -218,7 +218,7 @@ def wait( raise BrowserTerminatedError("Failure during browser startup") if resp_timeout: raise BrowserTimeoutError("Timeout sending response") - LOG.debug("bootstrap complete (%0.1fs)", time() - start_time) + LOG.debug("bootstrap complete (%0.1fs)", perf_counter() - start_time) except OSError as exc: # pragma: no cover raise LaunchError(f"Error attempting to launch browser: {exc}") from exc finally: diff --git a/src/ffpuppet/helpers.py b/src/ffpuppet/helpers.py index d44e952..c6d37c9 100644 --- a/src/ffpuppet/helpers.py +++ b/src/ffpuppet/helpers.py @@ -10,7 +10,7 @@ from pathlib import Path from platform import system from subprocess import STDOUT, CalledProcessError, check_output -from time import sleep, time +from time import perf_counter, sleep from typing import TYPE_CHECKING from psutil import Process, process_iter @@ -335,10 +335,10 @@ def wait_on_files( assert timeout >= 0 all_closed = False poll_rate = min(poll_rate, timeout) - deadline = time() + timeout + deadline = perf_counter() + timeout while True: open_iter = files_in_use(wait_files) - if deadline <= time(): + if deadline <= perf_counter(): LOG.debug("wait_on_files() timeout (%ds)", timeout) for path, pid, name in open_iter: LOG.debug("%r open by %r (%d)", str(path), name, pid) diff --git a/src/ffpuppet/test_bootstrapper.py b/src/ffpuppet/test_bootstrapper.py index 36042c3..5e95f6d 100644 --- a/src/ffpuppet/test_bootstrapper.py +++ b/src/ffpuppet/test_bootstrapper.py @@ -49,7 +49,7 @@ def test_bootstrapper_01(): def test_bootstrapper_02(mocker, exc, msg, continue_cb): """test Bootstrapper.wait() failure waiting for initial connection""" mocker.patch("ffpuppet.bootstrapper.select", return_value=([], None, None)) - mocker.patch("ffpuppet.bootstrapper.time", side_effect=(1, 1, 2, 3)) + mocker.patch("ffpuppet.bootstrapper.perf_counter", side_effect=(1, 1, 2, 3)) fake_sock = mocker.MagicMock(spec_set=socket) with Bootstrapper(fake_sock) as bts: with raises(exc, match=msg): @@ -72,7 +72,7 @@ def test_bootstrapper_03(mocker): assert fake_conn.close.call_count == 1 fake_conn.reset_mock() # test timeout - mocker.patch("ffpuppet.bootstrapper.time", side_effect=(1, 1, 1, 1, 2)) + mocker.patch("ffpuppet.bootstrapper.perf_counter", side_effect=(1, 1, 1, 1, 2)) with raises(BrowserTimeoutError, match="Timeout waiting for request"): bts.wait(lambda: True, timeout=0.1) # should call recv() at least 2x for positive and negative timeout check diff --git a/src/ffpuppet/test_helpers.py b/src/ffpuppet/test_helpers.py index 1a0ece6..8e95992 100644 --- a/src/ffpuppet/test_helpers.py +++ b/src/ffpuppet/test_helpers.py @@ -197,7 +197,7 @@ def test_helpers_03(mocker, tmp_path): def test_helpers_04(mocker, tmp_path): """test wait_on_files()""" fake_sleep = mocker.patch("ffpuppet.helpers.sleep", autospec=True) - fake_time = mocker.patch("ffpuppet.helpers.time", autospec=True) + fake_time = mocker.patch("ffpuppet.helpers.perf_counter", autospec=True) t_file = tmp_path / "file.bin" t_file.touch() # test with open file (timeout)