def setUp(self): super(MasterClientElectionTests, self).setUp() # create an application object config = self.getMasterConfiguration(master_number=1) self.app = Application(config) self.app.em.close() self.app.pt.clear() self.app.em = Mock() self.app.uuid = self.getMasterUUID() self.app.server = (self.local_ip, 10000) self.app.name = 'NEOCLUSTER' self.election = ClientElectionHandler(self.app) self.app.unconnected_master_node_set = set() self.app.negotiating_master_node_set = set() # apply monkey patches ClientConnection._addPacket = _addPacket
class MasterClientElectionTests(MasterClientElectionTestBase): def setUp(self): super(MasterClientElectionTests, self).setUp() # create an application object config = self.getMasterConfiguration(master_number=1) self.app = Application(config) self.app.em.close() self.app.pt.clear() self.app.em = Mock() self.app.uuid = self.getMasterUUID() self.app.server = (self.local_ip, 10000) self.app.name = 'NEOCLUSTER' self.election = ClientElectionHandler(self.app) self.app.unconnected_master_node_set = set() self.app.negotiating_master_node_set = set() # apply monkey patches ClientConnection._addPacket = _addPacket def _tearDown(self, success): # restore patched methods del ClientConnection._addPacket NeoUnitTestBase._tearDown(self, success) def _checkUnconnected(self, node): addr = node.getAddress() self.assertFalse(addr in self.app.negotiating_master_node_set) def test_connectionFailed(self): node, conn = self.identifyToMasterNode() self.assertTrue(node.isUnknown()) self._checkUnconnected(node) self.election.connectionFailed(conn) self._checkUnconnected(node) self.assertTrue(node.isUnknown()) def test_connectionCompleted(self): node, conn = self.identifyToMasterNode() self.assertTrue(node.isUnknown()) self._checkUnconnected(node) self.election.connectionCompleted(conn) self._checkUnconnected(node) self.assertTrue(node.isUnknown()) self.checkRequestIdentification(conn) def _setNegociating(self, node): self._checkUnconnected(node) addr = node.getAddress() self.app.negotiating_master_node_set.add(addr) def test_connectionClosed(self): node, conn = self.identifyToMasterNode() self._setNegociating(node) self.election.connectionClosed(conn) self.assertTrue(node.isUnknown()) addr = node.getAddress() self.assertFalse(addr in self.app.negotiating_master_node_set) def test_acceptIdentification1(self): """ A non-master node accept identification """ node, conn = self.identifyToMasterNode() args = (node.getUUID(), 0, 10, self.app.uuid, None, self._getMasterList()) self.election.acceptIdentification(conn, NodeTypes.CLIENT, *args) self.assertFalse(node in self.app.negotiating_master_node_set) self.checkClosed(conn) def test_acceptIdentificationDoesNotKnowPrimary(self): master1, master1_conn = self.identifyToMasterNode() master1_uuid = master1.getUUID() self.election.acceptIdentification( master1_conn, NodeTypes.MASTER, master1_uuid, 1, 0, self.app.uuid, None, [(master1.getAddress(), master1_uuid)], ) self.assertEqual(self.app.primary_master_node, None) def test_acceptIdentificationKnowsPrimary(self): master1, master1_conn = self.identifyToMasterNode() master1_uuid = master1.getUUID() primary1 = master1.getAddress() self.election.acceptIdentification( master1_conn, NodeTypes.MASTER, master1_uuid, 1, 0, self.app.uuid, primary1, [(master1.getAddress(), master1_uuid)], ) self.assertNotEqual(self.app.primary_master_node, None) def test_acceptIdentificationMultiplePrimaries(self): master1, master1_conn = self.identifyToMasterNode() master2, master2_conn = self.identifyToMasterNode() master3, _ = self.identifyToMasterNode() master1_uuid = master1.getUUID() master2_uuid = master2.getUUID() master3_uuid = master3.getUUID() primary1 = master1.getAddress() primary3 = master3.getAddress() master1_address = master1.getAddress() master2_address = master2.getAddress() master3_address = master3.getAddress() self.election.acceptIdentification( master1_conn, NodeTypes.MASTER, master1_uuid, 1, 0, self.app.uuid, primary1, [(master1_address, master1_uuid)], ) self.assertRaises( ElectionFailure, self.election.acceptIdentification, master2_conn, NodeTypes.MASTER, master2_uuid, 1, 0, self.app.uuid, primary3, [ (master1_address, master1_uuid), (master2_address, master2_uuid), (master3_address, master3_uuid), ], ) def test_acceptIdentification3(self): """ Identification accepted """ node, conn = self.identifyToMasterNode() args = (node.getUUID(), 0, 10, self.app.uuid, None, self._getMasterList()) self.election.acceptIdentification(conn, NodeTypes.MASTER, *args) self.checkUUIDSet(conn, node.getUUID()) self.assertEqual(self.app.primary is False, self.app.server < node.getAddress()) self.assertFalse(node in self.app.negotiating_master_node_set) def _getMasterList(self, with_node=None): master_list = self.app.nm.getMasterList() return [(x.getAddress(), x.getUUID()) for x in master_list]