Merge "PY3: Fix datagram message to be bytes"

This commit is contained in:
Zuul 2019-11-11 03:04:50 +00:00 committed by Gerrit Code Review
commit ed131d9ba4
2 changed files with 11 additions and 5 deletions
storlets/sbus/client
tests/unit/sbus/client

@ -74,7 +74,7 @@ class SBusClient(object):
# in local side before reading response
os.close(write_fd)
reply = ''
reply = b''
while True:
try:
buf = os.read(read_fd, self.chunk_size)
@ -87,6 +87,9 @@ class SBusClient(object):
finally:
os.close(read_fd)
if not isinstance(reply, str):
reply = reply.decode('utf-8')
return self._parse_response(reply)
def execute(self, *args, **kwargs):

@ -112,21 +112,24 @@ class TestSBusClient(unittest.TestCase):
self.assertTrue(_pipe[1].closed)
def _test_service_request(self, method, *args, **kwargs):
raw_resp = json.dumps({'status': True, 'message': 'OK'})
raw_resp = json.dumps(
{'status': True, 'message': 'OK'}).encode("utf-8")
with _mock_os_pipe([raw_resp]) as pipes, _mock_sbus(0):
resp = method(*args, **kwargs)
self.assertTrue(resp.status)
self.assertEqual('OK', resp.message)
self._check_all_pipes_closed(pipes)
raw_resp = json.dumps({'status': False, 'message': 'ERROR'})
raw_resp = json.dumps(
{'status': False, 'message': 'ERROR'}).encode("utf-8")
with _mock_os_pipe([raw_resp]) as pipes, _mock_sbus(0):
resp = method(*args, **kwargs)
self.assertFalse(resp.status)
self.assertEqual('ERROR', resp.message)
self._check_all_pipes_closed(pipes)
raw_resp = json.dumps({'status': True, 'message': 'OK'})
raw_resp = json.dumps(
{'status': True, 'message': 'OK'}).encode("utf-8")
with _mock_os_pipe([raw_resp]) as pipes, _mock_sbus(-1):
with self.assertRaises(SBusClientSendError):
method(*args, **kwargs)
@ -134,7 +137,7 @@ class TestSBusClient(unittest.TestCase):
# TODO(takashi): Add IOError case
with _mock_os_pipe(['Foo']) as pipes, _mock_sbus(0):
with _mock_os_pipe([b'Foo']) as pipes, _mock_sbus(0):
with self.assertRaises(SBusClientMalformedResponse):
method(*args, **kwargs)
self._check_all_pipes_closed(pipes)