util/jsoncomm: add send_and_recv helper
Often, a message is being sent and followed by a call to `recv` to wait for a reply. Create a simple helper `send_and_recv` that does both in one method. Add a simple check for that helper to the tests.
This commit is contained in:
parent
610d1c45d5
commit
70b971b83d
2 changed files with 23 additions and 0 deletions
|
|
@ -393,3 +393,13 @@ class Socket(contextlib.AbstractContextManager):
|
|||
|
||||
n = self._socket.sendmsg([serialized], cmsg, 0)
|
||||
assert n == len(serialized)
|
||||
|
||||
def send_and_recv(self, payload: object, *, fds: Optional[list] = None):
|
||||
"""Send a message and wait for a reply
|
||||
|
||||
This is a convenience helper that combines `send` and `recv`.
|
||||
See the individual methods for details about the parameters.
|
||||
"""
|
||||
|
||||
self.send(payload, fds=fds)
|
||||
return self.recv()
|
||||
|
|
|
|||
|
|
@ -203,3 +203,16 @@ class TestUtilJsonComm(unittest.TestCase):
|
|||
a.send(ping)
|
||||
pong, _, _ = b.recv()
|
||||
self.assertEqual(ping, pong)
|
||||
|
||||
def test_send_and_recv(self):
|
||||
#
|
||||
# Test for the send and receive helper method
|
||||
|
||||
a, b = jsoncomm.Socket.new_pair()
|
||||
|
||||
ping = {"osbuild": "yes"}
|
||||
a.send(ping)
|
||||
pong, _, _ = b.send_and_recv(ping)
|
||||
self.assertEqual(ping, pong)
|
||||
pong, _, _ = a.recv()
|
||||
self.assertEqual(ping, pong)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue