Skip to content

Commit

Permalink
[sapphire] Remove timeout getter/setter
Browse files Browse the repository at this point in the history
  • Loading branch information
tysmith committed Sep 27, 2023
1 parent 6e48e73 commit 3fe3e45
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 41 deletions.
7 changes: 2 additions & 5 deletions sapphire/connection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 4 additions & 30 deletions sapphire/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -123,7 +124,6 @@ def __init__(
else:
self._socket = sock
self.scheme = "http"
self._timeout = None
self.timeout = timeout

def __enter__(self):
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand Down
7 changes: 1 addition & 6 deletions sapphire/test_sapphire.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 3fe3e45

Please sign in to comment.