testConnectionPool.py 2.87 KB
Newer Older
1
#
Grégory Wisniewski's avatar
Grégory Wisniewski committed
2
# Copyright (C) 2009-2010  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 15
# 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
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 18

import unittest
19
from mock import Mock
20

21
from neo.tests import NeoTestBase
22 23
from neo.client.app import ConnectionPool

24
class ConnectionPoolTests(NeoTestBase):
25 26 27 28

    def test_removeConnection(self):
        app = None
        pool = ConnectionPool(app)
29
        test_node_uuid = self.getNewUUID()
30 31
        other_node_uuid = test_node_uuid
        while other_node_uuid == test_node_uuid:
32
            other_node_uuid = self.getNewUUID()
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
        test_node = Mock({'getUUID': test_node_uuid})
        other_node = Mock({'getUUID': other_node_uuid})
        # Test sanity check
        self.assertEqual(getattr(pool, 'connection_dict', None), {})
        # Call must not raise if node is not known
        self.assertEqual(len(pool.connection_dict), 0)
        pool.removeConnection(test_node)
        # Test that removal with another uuid doesn't affect entry
        pool.connection_dict[test_node_uuid] = None
        self.assertEqual(len(pool.connection_dict), 1)
        pool.removeConnection(other_node)
        self.assertEqual(len(pool.connection_dict), 1)
        # Test that removeConnection works
        pool.removeConnection(test_node)
        self.assertEqual(len(pool.connection_dict), 0)

    # TODO: test getConnForNode (requires splitting complex functionalities)

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    def test_CellSortKey(self):
        pool = ConnectionPool(None)
        node_uuid_1 = self.getNewUUID()
        node_uuid_2 = self.getNewUUID()
        node_uuid_3 = self.getNewUUID()
        # We are connected to node 1
        pool.connection_dict[node_uuid_1] = None
        # A connection to node 3 failed, will be forgotten at 5
        pool._notifyFailure(node_uuid_3, 5)
        getCellSortKey = pool._getCellSortKey

        # At 0, key values are not ambiguous
        self.assertTrue(getCellSortKey(node_uuid_1, 0) < getCellSortKey(
            node_uuid_2, 0) < getCellSortKey(node_uuid_3, 0))
        # At 10, nodes 2 and 3 have the same key value
        self.assertTrue(getCellSortKey(node_uuid_1, 10) < getCellSortKey(
            node_uuid_2, 10))
        self.assertEqual(getCellSortKey(node_uuid_2, 10), getCellSortKey(
            node_uuid_3, 10))

71 72 73
if __name__ == '__main__':
    unittest.main()