Skip to content

Fix deprecated socket API usage#55

Open
deacon-mp wants to merge 1 commit intomasterfrom
fix/issue-51-deprecated-socket-api
Open

Fix deprecated socket API usage#55
deacon-mp wants to merge 1 commit intomasterfrom
fix/issue-51-deprecated-socket-api

Conversation

@deacon-mp
Copy link
Copy Markdown
Contributor

Summary

  • Adds Connection class (app/c_connection.py) that wraps asyncio StreamReader/StreamWriter to replace the removed TransportSocket.send/recv methods (removed in Python 3.12)
  • Updates h_terminal.py to use modern aiohttp WebSocket API: socket.receive() instead of deprecated socket.recv(), socket.send_str() instead of deprecated socket.send()
  • Adds unit tests for the Connection class and verifies WebSocket API usage

Fixes #51

Supersedes #42 — takes the same approach (Connection wrapper) but also fixes the WebSocket API deprecations in h_terminal.py and adds tests.

Test plan

  • Run python3 -m unittest tests.test_connection -v to verify all 6 tests pass
  • Deploy Manx agent and verify it connects and stays alive
  • Verify terminal WebSocket commands work end-to-end

- Add Connection class wrapping asyncio StreamReader/StreamWriter to
  replace removed TransportSocket.send/recv (Python 3.12+)
- Update h_terminal.py to use modern aiohttp WebSocket API:
  socket.receive() instead of socket.recv(),
  socket.send_str() instead of socket.send()
- Add unit tests for Connection class and WebSocket API usage
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the Manx plugin to avoid deprecated/removed socket APIs in newer Python/aiohttp versions by introducing a small asyncio stream wrapper and modernizing WebSocket calls used by the terminal handler.

Changes:

  • Added Connection wrapper over asyncio StreamReader/StreamWriter with send/recv helpers.
  • Updated terminal WebSocket handler to use socket.receive() and socket.send_str() instead of deprecated APIs.
  • Added unit tests covering the new Connection wrapper and checking terminal handler WebSocket API usage.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 8 comments.

File Description
app/c_connection.py Introduces Connection wrapper to replace deprecated transport socket methods.
app/h_terminal.py Updates terminal WebSocket handler to modern aiohttp WebSocket APIs.
tests/test_connection.py Adds unit tests for Connection and validates WebSocket API usage in h_terminal.py.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +25 to +26
"""Close the underlying writer."""
self.writer.close()
Comment on lines +33 to +36
reader = MockReader()
conn = Connection(reader, MockWriter())
result = asyncio.get_event_loop().run_until_complete(conn.recv(1024))
self.assertEqual(result, b"hello")
Comment on lines +57 to +61
writer = MockWriter()
conn = Connection(MockReader(), writer)
asyncio.get_event_loop().run_until_complete(conn.send(b"world"))
self.assertEqual(writer.written, b"world")
self.assertTrue(writer.drained)
Comment on lines +80 to +83
writer = MockWriter()
conn = Connection(MockReader(), writer)
asyncio.get_event_loop().run_until_complete(conn.send("text"))
self.assertEqual(writer.written, b"text")
Comment on lines +114 to +124
# Ensure deprecated methods are not used
self.assertNotIn("socket.recv()", source,
"Should use socket.receive() instead of deprecated socket.recv()")
self.assertNotIn("await socket.send(", source,
"Should use socket.send_str() instead of deprecated socket.send()")

# Ensure modern methods are used
self.assertIn("socket.receive()", source,
"Should call socket.receive() for reading WebSocket messages")
self.assertIn("socket.send_str(", source,
"Should call socket.send_str() for sending WebSocket messages")
Comment on lines +111 to +113
with open("app/h_terminal.py") as f:
source = f.read()

Comment on lines +114 to +124
# Ensure deprecated methods are not used
self.assertNotIn("socket.recv()", source,
"Should use socket.receive() instead of deprecated socket.recv()")
self.assertNotIn("await socket.send(", source,
"Should use socket.send_str() instead of deprecated socket.send()")

# Ensure modern methods are used
self.assertIn("socket.receive()", source,
"Should call socket.receive() for reading WebSocket messages")
self.assertIn("socket.send_str(", source,
"Should call socket.send_str() for sending WebSocket messages")
Comment on lines +16 to +17
msg = await socket.receive()
cmd = msg.data if hasattr(msg, 'data') else str(msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TransportSocket object has no attribute 'send'

2 participants