TestCommunicationManager.py
6.27 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import unittest
import mock
from os.path import dirname, realpath
from sys import argv, path
path.append(dirname(dirname(realpath(__file__))) + '/lib')
from Communication.Manager import CommunicationManager
class TestCommunicationManager(unittest.TestCase):
def setUp(self):
self._manager = CommunicationManager()
self._endpoint = mock.Mock()
self._transport = mock.Mock()
self._event = mock.Mock()
self._endpoint.getHandle.return_value = 123
self._endpoint.getTransport.return_value = self._transport
self._event.subject = self._endpoint
self._manager.issueEvent = mock.Mock()
def testEndPointAlreadyHandled(self):
"""
there really should be a test for this to ensure the if
is working correctly.
"""
pass
def testAddEndPoint(self):
self._transport.isListen.return_value = False
self._manager._rcons = mock.Mock()
self._manager.addEndPoint(self._endpoint)
self.assertIn(123, self._manager._cons)
self.assertEqual(self._endpoint, self._manager._cons[123])
self._manager._rcons.append.assert_called_with(123)
def testAddListenEndPoint(self):
self._transport.isListen.return_value = True
self._manager._rcons = mock.Mock()
self._manager.addEndPoint(self._endpoint)
self.assertIn(123, self._manager._listen)
self.assertEqual(self._endpoint, self._manager._listen[123])
self._manager._rcons.append.assert_called_with(123)
def test_addCon(self):
self._manager.addEndPoint = mock.Mock()
self.assertTrue(self._manager._addCon(self._event))
self._manager.addEndPoint.assert_called_once_with(self._endpoint)
def test_enableWriteOnWriteFinTransport(self):
self._transport._fin_state = 2
self.assertTrue(self._manager._enableWrite(self._event))
self.assertNotIn(123, self._manager._wcons)
def test_enableWrite(self):
self._transport._fin_state = 0
self.assertTrue(self._manager._enableWrite(self._event))
self.assertIn(123, self._manager._wcons)
def test_disableWriteNoShutdownRead(self):
self._transport._fin_state = 0
self.assertTrue(self._manager._disableWrite(self._event))
self.assertNotIn(123, self._manager._wcons)
self.test_enableWrite()
self.assertTrue(self._manager._disableWrite(self._event))
self.assertNotIn(123, self._manager._wcons)
def test_disableWriteNoShutdownRead(self):
self._transport._fin_state = 1
self.assertTrue(self._manager._disableWrite(self._event))
self.assertNotIn(123, self._manager._wcons)
self.test_enableWrite()
self.assertTrue(self._manager._disableWrite(self._event))
self.assertNotIn(123, self._manager._wcons)
self._manager.issueEvent.assert_called_with(
self._endpoint, 'shutdown_write')
def test_shutdown(self):
self._transport.isListen.return_value = True
endpoint2 = mock.Mock()
transport2 = mock.Mock()
endpoint2.getTransport.return_value = transport2
endpoint2.getHandle.return_value = 321
transport2.isListen.return_value = False
self._manager.addEndPoint(self._endpoint)
self._manager.addEndPoint(endpoint2)
self.assertFalse(self._manager._shutdown(None))
self._manager.issueEvent.assert_any_call(self._endpoint, 'close')
self._manager.issueEvent.assert_any_call(endpoint2, 'close')
self.assertEqual(self._manager._rcons, [])
self.assertEqual(self._manager._wcons, [])
def test_shutdownReadReadyToClose(self):
self._manager._rcons.append(123)
self._transport.shutdownRead.return_value = 3
self._endpoint.hasPendingData.return_value = False
self.assertFalse(self._manager._shutdownRead(self._event))
self._manager.issueEvent.assert_called_with(self._endpoint, 'close')
def test_shutdownReadReadyToShutdownWrite(self):
self._manager._rcons.append(123)
self._transport.shutdownRead.return_value = 0
self._endpoint.hasPendingData.return_value = False
self.assertFalse(self._manager._shutdownRead(self._event))
self._manager.issueEvent.assert_called_with(self._endpoint, 'shutdown_write')
def test_shutdownReadMarkAsClose(self):
self._manager._rcons.append(123)
self._transport.shutdownRead.return_value = 0
self._endpoint.hasPendingData.return_value = True
self.assertFalse(self._manager._shutdownRead(self._event))
self._endpoint.setClose.assert_called_once_with()
def test_shutdownWriteReadyToClose(self):
self._manager._wcons.append(123)
self._transport.shutdownWrite.return_value = 3
self.assertFalse(self._manager._shutdownWrite(self._event))
self._manager.issueEvent.assert_called_once_with(self._endpoint, 'close')
def test_shutdownWrite(self):
self._manager._wcons.append(123)
self._transport.shutdownWrite.return_value = 0
self.assertFalse(self._manager._shutdownWrite(self._event))
def test_closeCon(self):
self._manager._wcons.append(123)
self._manager._rcons.append(123)
self._manager._cons[123] = self._endpoint
self.assertFalse(self._manager._close(self._event))
self._transport.shutdown.assert_called_with()
self._transport.close.assert_called_with()
self.assertNotIn(123, self._manager._wcons)
self.assertNotIn(123, self._manager._rcons)
self.assertNotIn(123, self._manager._cons)
def test_closeListen(self):
self._manager._wcons.append(123)
self._manager._rcons.append(123)
self._manager._listen[123] = self._endpoint
self.assertFalse(self._manager._close(self._event))
self._transport.shutdown.assert_called_with()
self._transport.close.assert_called_with()
self.assertNotIn(123, self._manager._wcons)
self.assertNotIn(123, self._manager._rcons)
self.assertNotIn(123, self._manager._listen)
def suite():
return unittest.TestLoader().loadTestsFromTestCase(TestCommunicationManager)
if __name__ == '__main__':
unittest.TextTestRunner(verbosity=2).run(suite())
# vim: set ft=python et ts=8 sw=4 sts=4: