TestCommunicationEndPoint.py
3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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: