From 3fe3e4521e427a0fdd3ba98b5b4676933a7dc39a Mon Sep 17 00:00:00 2001 From: Tyson Smith Date: Wed, 27 Sep 2023 11:44:54 -0700 Subject: [PATCH] [sapphire] Remove timeout getter/setter --- sapphire/connection_manager.py | 7 ++----- sapphire/core.py | 34 ++++------------------------------ sapphire/test_sapphire.py | 7 +------ 3 files changed, 7 insertions(+), 41 deletions(-) diff --git a/sapphire/connection_manager.py b/sapphire/connection_manager.py index 3f053a4b..9c67d318 100644 --- a/sapphire/connection_manager.py +++ b/sapphire/connection_manager.py @@ -120,16 +120,13 @@ def serve(self, timeout, continue_cb=None, shutdown_delay=SHUTDOWN_DELAY): assert self._job.pending assert self._socket.gettimeout() is not None assert shutdown_delay >= 0 + assert timeout >= 0 if continue_cb is not None and not callable(continue_cb): raise TypeError("continue_cb must be callable") self._deadline_exceeded = False start_time = time() - if not timeout: - self._deadline = None - else: - assert timeout > 0 - self._deadline = start_time + timeout + self._deadline = start_time + timeout if timeout else None launches = 0 running = 0 diff --git a/sapphire/core.py b/sapphire/core.py index 872c0a48..83d1eaf0 100644 --- a/sapphire/core.py +++ b/sapphire/core.py @@ -96,7 +96,7 @@ def create_listening_socket(attempts=10, port=0, remote=False, timeout=None): class Sapphire: LISTEN_TIMEOUT = 0.25 - __slots__ = ("_auto_close", "_max_workers", "_socket", "_timeout", "scheme") + __slots__ = ("_auto_close", "_max_workers", "_socket", "scheme", "timeout") def __init__( self, @@ -107,6 +107,7 @@ def __init__( port=0, timeout=60, ): + assert timeout >= 0 self._auto_close = auto_close # call 'window.close()' on 4xx error pages self._max_workers = max_workers # limit worker threads sock = create_listening_socket( @@ -123,7 +124,6 @@ def __init__( else: self._socket = sock self.scheme = "http" - self._timeout = None self.timeout = timeout def __enter__(self): @@ -210,7 +210,8 @@ def serve_path( tuple(int, tuple(str)): Status code and files served. """ assert isinstance(path, Path) - LOG.debug("serving '%s' (forever=%r, timeout=%r)", path, forever, self.timeout) + assert self.timeout >= 0 + LOG.debug("serving '%s' (forever=%r, timeout=%d)", path, forever, self.timeout) job = Job( path, auto_close=self._auto_close, @@ -227,33 +228,6 @@ def serve_path( LOG.debug("%s, timeout: %r", job.status, was_timeout) return (Served.TIMEOUT if was_timeout else job.status, tuple(job.served)) - @property - def timeout(self): - """The amount of time that must pass before exiting the serve loop and - indicating a timeout. - - Args: - None - - Returns: - int: Timeout in seconds. - """ - return self._timeout - - @timeout.setter - def timeout(self, value): - """The amount of time that must pass before exiting the serve loop and - indicating a timeout. - - Args: - value (int): Timeout in seconds. - - Returns: - None - """ - assert value >= 0 - self._timeout = value - @classmethod def main(cls, args): try: diff --git a/sapphire/test_sapphire.py b/sapphire/test_sapphire.py index b4523e59..2427906c 100644 --- a/sapphire/test_sapphire.py +++ b/sapphire/test_sapphire.py @@ -169,11 +169,7 @@ def test_sapphire_06(client, tmp_path): def test_sapphire_07(tmp_path): """test timeout of the server""" - with Sapphire(timeout=60) as serv: - assert serv.timeout == 60 # verify default - serv.timeout = 0 # disable timeout - assert serv.timeout == 0 - serv.timeout = 0.01 + with Sapphire(timeout=0.01) as serv: assert serv.timeout == 0.01 _create_test("test_case.html", tmp_path) status, files_served = serv.serve_path(tmp_path) @@ -659,7 +655,6 @@ def test_sapphire_27(client, tmp_path): def test_sapphire_28(client_factory, tmp_path): """test Sapphire.serve_path() with forever=True""" with Sapphire(timeout=10) as serv: - assert serv.timeout == 10 test = _create_test("test_case.html", tmp_path) clients = [client_factory() for _ in range(3)] for client in clients: