Repository navigation
fix: preserve WLAN timeout across reconnects #311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
YashAnand69
wants to merge
2
commits into
fossasia:main
Choose a base branch
from
YashAnand69:fix/wlan-timeout-reconnect
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+134
−4
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| """WLAN timeout configuration tests without a physical PSLab device.""" | ||
|
|
||
| import socket | ||
|
|
||
| import pytest | ||
|
|
||
| from pslab.connection.wlan import WLANHandler | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("timeout", [0.25, 2.0, None]) | ||
| def test_timeout_update_survives_reconnect(monkeypatch, timeout): | ||
| """Keep the requested timeout on the current and replacement sockets.""" | ||
| monkeypatch.setattr(WLANHandler, "get_version", lambda self: "PSLab V6") | ||
|
|
||
| with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server: | ||
| server.bind(("127.0.0.1", 0)) | ||
| server.listen() | ||
| handler = WLANHandler(host="127.0.0.1", port=server.getsockname()[1]) | ||
|
|
||
| try: | ||
| handler.timeout = timeout | ||
| assert handler._sock.gettimeout() == timeout | ||
|
|
||
| handler.connect() | ||
| connection, _ = server.accept() | ||
| connection.close() | ||
| handler.disconnect() | ||
|
|
||
| handler.connect() | ||
| connection, _ = server.accept() | ||
| connection.close() | ||
| assert handler._sock.gettimeout() == timeout | ||
| assert handler.timeout == timeout | ||
| finally: | ||
| handler.disconnect() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("timeout", [0.25, 2.0, None]) | ||
| def test_timeout_property_tracks_update(timeout): | ||
| """Report the updated value consistently with the live socket.""" | ||
| handler = WLANHandler() | ||
|
|
||
| try: | ||
| handler.timeout = timeout | ||
| assert handler._sock.gettimeout() == timeout | ||
| assert handler.timeout == timeout | ||
| finally: | ||
| handler.disconnect() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("timeout, error", [(-1, ValueError), ("invalid", TypeError)]) | ||
| def test_invalid_timeout_preserves_previous_value(timeout, error): | ||
| """Reject an invalid value without changing the configured timeout.""" | ||
| handler = WLANHandler(timeout=0.5) | ||
|
|
||
| try: | ||
| with pytest.raises(error): | ||
| handler.timeout = timeout | ||
|
|
||
| assert handler.timeout == 0.5 | ||
| assert handler._sock.gettimeout() == 0.5 | ||
| finally: | ||
| handler.disconnect() | ||
|
|
||
|
|
||
| def test_initial_timeout_matches_socket(): | ||
| """Expose the configured constructor timeout without an update.""" | ||
| handler = WLANHandler(timeout=0.5) | ||
|
|
||
| try: | ||
| assert handler.timeout == 0.5 | ||
| assert handler._sock.gettimeout() == 0.5 | ||
| finally: | ||
| handler.disconnect() | ||
|
|
||
|
|
||
| def test_timeout_update_preserves_transfers(monkeypatch): | ||
| """Keep normal transfers working after changing the timeout.""" | ||
| monkeypatch.setattr(WLANHandler, "get_version", lambda self: "PSLab V6") | ||
|
|
||
| with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server: | ||
| server.bind(("127.0.0.1", 0)) | ||
| server.listen() | ||
| handler = WLANHandler(host="127.0.0.1", port=server.getsockname()[1]) | ||
|
|
||
| try: | ||
| handler.timeout = 0.5 | ||
| handler.connect() | ||
| connection, _ = server.accept() | ||
|
|
||
| with connection: | ||
| connection.settimeout(0.5) | ||
| assert handler.write(b"PSLab") == 5 | ||
| with connection.makefile("rb") as response: | ||
| assert response.read(5) == b"PSLab" | ||
| connection.sendall(b"OK") | ||
| assert handler.read(2) == b"OK" | ||
| assert handler.write(b"") == 0 | ||
| assert handler.read(0) == b"" | ||
|
|
||
| assert repr(handler) == f"WLANHandler[127.0.0.1:{handler.port}]" | ||
| finally: | ||
| handler.disconnect() | ||
|
|
||
|
|
||
| def test_failed_handshake_closes_socket(monkeypatch): | ||
| """Preserve socket cleanup when the device handshake fails.""" | ||
|
|
||
| def fail_handshake(self): | ||
| raise ConnectionError("Not a PSLab device") | ||
|
|
||
| monkeypatch.setattr(WLANHandler, "get_version", fail_handshake) | ||
|
|
||
| with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server: | ||
| server.bind(("127.0.0.1", 0)) | ||
| server.listen() | ||
| handler = WLANHandler(host="127.0.0.1", port=server.getsockname()[1]) | ||
|
|
||
| try: | ||
| with pytest.raises(ConnectionError, match="Not a PSLab device"): | ||
| handler.connect() | ||
|
|
||
| connection, _ = server.accept() | ||
| connection.close() | ||
| assert handler._sock.fileno() == -1 | ||
| finally: | ||
| handler.disconnect() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.