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:
Christian Kellner 2021-05-07 18:17:34 +00:00 committed by Tom Gundersen
parent 610d1c45d5
commit 70b971b83d
2 changed files with 23 additions and 0 deletions

View file

@ -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()

View file

@ -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)