提交 f166ce82 编写于 作者: C Cleber Rosa

avocado.utils.datadrainer: introduce buffered file descriptor drainer

Signed-off-by: NCleber Rosa <crosa@redhat.com>
上级 5ce82b9c
......@@ -143,3 +143,25 @@ class FDDrainer(BaseDrainer):
def write(self, data):
# necessary to avoid pylint W0223
raise NotImplementedError
class BufferFDDrainer(FDDrainer):
"""
Drains data from a file descriptor and stores it in an internal buffer
"""
name = 'avocado.utils.datadrainer.BufferFDDrainer'
def __init__(self, source, stop_check=None, name=None):
super(BufferFDDrainer, self).__init__(source, stop_check, name)
self._data = io.BytesIO()
def write(self, data):
self._data.write(data)
@property
def data(self):
"""
Returns the buffer data, as bytes
"""
return self._data.getvalue()
......@@ -71,3 +71,31 @@ class CustomSocket(unittest.TestCase):
def tearDown(self):
self.socket1.close()
self.socket2.close()
class SocketBuffer(datadrainer.BufferFDDrainer):
name = 'test_utils_datadrainer.SocketBuffer'
def __init__(self, source):
super(SocketBuffer, self).__init__(source)
self._stop_check = lambda: len(self.data) > 2
class CustomSocketBuffer(unittest.TestCase):
def setUp(self):
self.socket1, self.socket2 = socket.socketpair(socket.AF_UNIX)
def test(self):
socket_drainer = SocketBuffer(self.socket2.fileno())
socket_drainer.start()
self.socket1.send(b'1')
self.socket1.send(b'2')
self.socket1.send(b'3')
socket_drainer.wait()
self.assertEqual(socket_drainer.data, b'123')
def tearDown(self):
self.socket1.close()
self.socket2.close()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册