testHandler.py 2.91 KB
Newer Older
1
#
2
# Copyright (C) 2009-2015  Nexedi SA
3
#
4 5 6 7
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
8
#
9 10 11 12 13 14
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

17 18
import unittest
from mock import Mock
19
from . import NeoUnitTestBase
20 21
from neo.lib.handler import EventHandler
from neo.lib.protocol import PacketMalformedError, UnexpectedPacketError, \
22 23
        BrokenNodeDisallowedError, NotReadyError, ProtocolError

24
class HandlerTests(NeoUnitTestBase):
25 26

    def setUp(self):
27
        NeoUnitTestBase.setUp(self)
28 29
        app = Mock()
        self.handler = EventHandler(app)
30 31

    def setFakeMethod(self, method):
32
        self.handler.fake_method = method
33 34

    def getFakePacket(self):
35
        p = Mock({
36 37 38
            'decode': (),
            '__repr__': 'Fake Packet',
        })
39 40
        p.handler_method_name = 'fake_method'
        return p
41 42

    def test_dispatch(self):
43
        conn = self.getFakeConnection()
44 45
        packet = self.getFakePacket()
        # all is ok
46
        self.setFakeMethod(lambda c: None)
47
        self.handler.dispatch(conn, packet)
48 49
        # raise UnexpectedPacketError
        conn.mockCalledMethods = {}
50 51
        def fake(c):
            raise UnexpectedPacketError('fake packet')
52 53 54 55 56
        self.setFakeMethod(fake)
        self.handler.dispatch(conn, packet)
        self.checkErrorPacket(conn)
        self.checkAborted(conn)
        # raise PacketMalformedError
57
        conn.mockCalledMethods = {}
58 59
        def fake(c):
            raise PacketMalformedError('message')
60 61
        self.setFakeMethod(fake)
        self.handler.dispatch(conn, packet)
62
        self.checkClosed(conn)
63
        # raise BrokenNodeDisallowedError
64
        conn.mockCalledMethods = {}
65 66
        def fake(c):
            raise BrokenNodeDisallowedError
67 68 69 70 71
        self.setFakeMethod(fake)
        self.handler.dispatch(conn, packet)
        self.checkErrorPacket(conn)
        self.checkAborted(conn)
        # raise NotReadyError
72
        conn.mockCalledMethods = {}
73 74
        def fake(c):
            raise NotReadyError
75 76 77 78 79
        self.setFakeMethod(fake)
        self.handler.dispatch(conn, packet)
        self.checkErrorPacket(conn)
        self.checkAborted(conn)
        # raise ProtocolError
80
        conn.mockCalledMethods = {}
81 82
        def fake(c):
            raise ProtocolError
83 84 85 86 87 88 89
        self.setFakeMethod(fake)
        self.handler.dispatch(conn, packet)
        self.checkErrorPacket(conn)
        self.checkAborted(conn)



90 91
if __name__ == '__main__':
    unittest.main()