Skip to content

Commit 207e3bd

Browse files
Add timeout argument for yaqc (#55)
* Add timeout argument for yaqc * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * py<3.10 timeout errors * Change default to 10 seconds --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 6e36220 commit 207e3bd

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

tests/connection/test_connection.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ def test_invalid_argument():
8686
initial._socket._socket.sendall.assert_not_called()
8787

8888

89+
def test_timeout():
90+
# Open a socket which is NOT a yaq daemon
91+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
92+
s.bind(("", 36098))
93+
s.listen(1)
94+
# socket.timeout is actually deprecated, but is an alias for TimeoutError in py3.10+
95+
# and its own OSError subclass in py<3.10
96+
with pytest.raises(socket.timeout):
97+
yaqc.Client(36098, timeout=0.1)
98+
99+
89100
if __name__ == "__main__":
90101
test_shutdown()
91102
test_restart()

yaqc/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).
88
### Fixed
99
- fastavro >=1.7 prevents infrequent CPU locking from bad string parsing
1010

11+
### Added
12+
- `timeout` argument to Client, defaulting to 10 seconds
13+
1114
## [2022.5.0]
1215

1316
### Changed

yaqc/yaqc/_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ def inner(self, *args, **kwargs):
3535

3636

3737
class Client:
38-
def __init__(self, port, host="127.0.0.1"):
38+
def __init__(self, port, host="127.0.0.1", timeout=10):
3939
self._host = host
4040
self._port = port
41-
self._socket = Socket(self._host, self._port)
41+
self._socket = Socket(self._host, self._port, timeout)
4242
self._id_counter = 0
4343
self._connection_callbacks = []
4444
self._mutex = Lock()

yaqc/yaqc/_socket.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515

1616
class Socket:
17-
def __init__(self, host, port):
17+
def __init__(self, host, port, timeout=None):
1818
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
19-
self._socket.settimeout(None)
19+
self._socket.settimeout(timeout)
2020
self._socket.connect((host, port))
2121
self._named_types = {}
2222

0 commit comments

Comments
 (0)