testHandler.py 3.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#
# Copyright (C) 2009  Nexedi SA
# 
# 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.
# 
# 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
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import unittest, os
from mock import Mock, ReturnValues
20
from neo.tests import NeoTestBase
21 22 23
from neo.handler import EventHandler
from neo.protocol import UnexpectedPacketError, MASTER_NODE_TYPE, \
        CLIENT_NODE_TYPE, STORAGE_NODE_TYPE, ADMIN_NODE_TYPE
24 25 26
from neo.protocol import PacketMalformedError, UnexpectedPacketError, \
        BrokenNodeDisallowedError, NotReadyError, ProtocolError

27
class HandlerTests(NeoTestBase):
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

    def setUp(self):
        self.handler = EventHandler()
        self.fake_type = 'FAKE_PACKET_TYPE'

    def setFakeMethod(self, method):
        self.handler.packet_dispatch_table[self.fake_type] = method

    def getFakePacket(self):
        return Mock({'getType': self.fake_type, 'decode': ()})

    def checkFakeCalled(self):
        method = self.handler.packet_dispatch_table[self.fake_type]
        calls = method.getNamedCalls('__call__')
        self.assertEquals(len(calls), 1)

    def test_dispatch(self):
        conn = Mock({'getAddress': ('127.0.0.1', 10000)})
        packet = self.getFakePacket()
        # all is ok
        self.setFakeMethod(lambda c, p: None)
        self.handler.dispatch(conn, packet)
        # raise KeyError and ValueError
        conn.mockCalledMethods = {} 
        def fake(c, p): raise KeyError
        self.setFakeMethod(fake)
        self.handler.dispatch(conn, packet)
        self.checkErrorPacket(conn)
        conn.mockCalledMethods = {} 
        def fake(c, p): raise ValueError
        self.setFakeMethod(fake)
        self.handler.dispatch(conn, packet)
        self.checkErrorPacket(conn)
        # raise UnexpectedPacketError 
        conn.mockCalledMethods = {} 
        def fake(c, p): raise UnexpectedPacketError('fake packet')
        self.setFakeMethod(fake)
        self.handler.dispatch(conn, packet)
        self.checkErrorPacket(conn)
        self.checkAborted(conn)
        # raise PacketMalformedError
        conn.mockCalledMethods = {} 
        def fake(c, p): raise PacketMalformedError('message')
        self.setFakeMethod(fake)
        self.handler.dispatch(conn, packet)
        self.checkErrorPacket(conn)
        self.checkAborted(conn)
        # raise BrokenNodeDisallowedError
        conn.mockCalledMethods = {} 
        def fake(c, p): raise BrokenNodeDisallowedError
        self.setFakeMethod(fake)
        self.handler.dispatch(conn, packet)
        self.checkErrorPacket(conn)
        self.checkAborted(conn)
        # raise NotReadyError
        conn.mockCalledMethods = {} 
        def fake(c, p): raise NotReadyError
        self.setFakeMethod(fake)
        self.handler.dispatch(conn, packet)
        self.checkErrorPacket(conn)
        self.checkAborted(conn)
        # raise ProtocolError
        conn.mockCalledMethods = {} 
        def fake(c, p): raise ProtocolError
        self.setFakeMethod(fake)
        self.handler.dispatch(conn, packet)
        self.checkErrorPacket(conn)
        self.checkAborted(conn)



99 100
if __name__ == '__main__':
    unittest.main()