TestCommunicationEndPoint.py 3.28 KB
import unittest
import mock

from os.path import dirname, realpath
from sys import argv, path
path.append(dirname(dirname(realpath(__file__))) + '/lib')

from Communication.EndPoint import CommunicationEndPoint

class TestCommunicationEndPoint(unittest.TestCase):
    def setUp(self):
        self._transport = mock.Mock()
        self._protocol  = mock.Mock()
        self._bufsize   = 11773
        self._endpoint  = CommunicationEndPoint(
            self._transport, self._protocol, self._bufsize)

    def testSetClose(self):
        self.assertFalse(self._endpoint.shouldClose())
        self._endpoint.setClose()
        self.assertTrue(self._endpoint.shouldClose())

    def testHasProtocol(self):
        self.assertTrue(self._endpoint.hasProtocol(mock.Mock))

    def testHasPendingData(self):
        self.assertFalse(self._endpoint.hasPendingData())

    def testGetTransport(self):
        self.assertEqual(self._endpoint.getTransport(), self._transport)

    def testGetProtocol(self):
        self.assertEqual(self._endpoint.getProtocol(), self._protocol)

    def testGetHandle(self):
        self._transport.getHandle.return_value = 10
        self.assertEqual(self._endpoint.getHandle(), 10)
        self._transport.getHandle.assert_call_once()

    def testBufferRead(self):
        self._transport.recv.return_value = False
        self.assertFalse(self._endpoint.bufferRead())
        self._transport.recv.assert_call_once_with(11773)

        self._transport.reset_mock()
        self._endpoint.appendReadData = mock.Mock()

        self._transport.recv.side_effect = iter(['111', '2222', '33333', False])
        self.assertTrue(self._endpoint.bufferRead())
        self._transport.recv.assert_call_with(11773)
        self.assertEqual(self._transport.recv.call_count, 4)
        self.assertEqual(self._endpoint.appendReadData.call_count, 3)

    def testWriteBuffered(self):
        self._endpoint.nextWriteData = mock.Mock()
        self._endpoint.nextWriteData.return_value = ('', 1212)
        self.assertFalse(self._endpoint.writeBuffered())
        self._endpoint.nextWriteData.assert_called_once_with()

        self._endpoint.nextWriteData.reset_mock()
        self._endpoint.nextWriteData.return_value = ('12345', 1212)
        self._endpoint.appendWriteData = mock.Mock()
        self._transport.send.return_value = 0
        self.assertFalse(self._endpoint.writeBuffered())
        self._endpoint.nextWriteData.assert_called_once_with()
        self._transport.send.assert_called_once_with('12345', 1212)

        self._transport.reset_mock()
        self._endpoint.nextWriteData.reset_mock()
        self._endpoint.nextWriteData.side_effect = iter(
            [('111222', 1212), ('333', 1313), ('', 1212)])
        self._endpoint.appendWriteData = mock.Mock()
        self._transport.send.return_value = 3
        self.assertTrue(self._endpoint.writeBuffered())
        self._endpoint.nextWriteData.assert_called_with()
        self._transport.send.assert_any_call('111222', 1212)
        self._transport.send.assert_any_call('222', 1212)
        self._transport.send.assert_called_with('333', 1313)


def suite():
    return unittest.TestLoader().loadTestsFromTestCase(TestCommunicationEndPoint)

if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(suite())

# vim: set ft=python et ts=8 sw=4 sts=4: