Commit f4836f41 authored by Vincent Pelletier's avatar Vincent Pelletier

Factorise code handling expected answers.

Also, stop using a dict in an inefficient way, and replace it by a set.

git-svn-id: https://svn.erp5.org/repos/neo/trunk@2216 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 8aaff20f
......@@ -41,24 +41,40 @@ class VerificationManager(BaseServiceHandler):
BaseServiceHandler.__init__(self, app)
self._oid_set = set()
self._tid_set = set()
self._uuid_dict = {}
self._uuid_set = set()
self._object_present = False
def _askStorageNodesAndWait(self, packet, node_list):
poll = self.app.em.poll
operational = self.app.pt.operational
uuid_dict = self._uuid_dict
uuid_dict.clear()
uuid_set = self._uuid_set
uuid_set.clear()
for node in node_list:
uuid_dict[node.getUUID()] = False
uuid_set.add(node.getUUID())
node.ask(packet)
while True:
poll(1)
if not operational():
raise VerificationFailure
if False not in uuid_dict.values():
if not uuid_set:
break
def _gotAnswerFrom(self, uuid):
"""
Returns True if answer from given uuid is waited upon by
_askStorageNodesAndWait, False otherwise.
Also, mark this uuid as having answered, so it stops being waited upon
by _askStorageNodesAndWait.
"""
try:
self._uuid_set.remove(uuid)
except KeyError:
result = False
else:
result = True
return result
def getHandler(self):
return self
......@@ -179,18 +195,15 @@ class VerificationManager(BaseServiceHandler):
def answerUnfinishedTransactions(self, conn, tid_list):
uuid = conn.getUUID()
logging.info('got unfinished transactions %s from %r', tid_list, conn)
if self._uuid_dict.get(uuid, True):
# No interest.
if not self._gotAnswerFrom(uuid):
return
self._tid_set.update(tid_list)
self._uuid_dict[uuid] = True
def answerTransactionInformation(self, conn, tid,
user, desc, ext, packed, oid_list):
uuid = conn.getUUID()
app = self.app
if self._uuid_dict.get(uuid, True):
# No interest.
if not self._gotAnswerFrom(uuid):
return
oid_set = set(oid_list)
if self._oid_set is None:
......@@ -201,34 +214,26 @@ class VerificationManager(BaseServiceHandler):
self._oid_set.update(oid_set)
elif self._oid_set != oid_set:
self._oid_set = None
self._uuid_dict[uuid] = True
def tidNotFound(self, conn, message):
uuid = conn.getUUID()
logging.info('TID not found: %s', message)
if self._uuid_dict.get(uuid, True):
# No interest.
if not self._gotAnswerFrom(uuid):
return
self._oid_set = None
self._uuid_dict[uuid] = True
def answerObjectPresent(self, conn, oid, tid):
uuid = conn.getUUID()
logging.info('object %s:%s found', dump(oid), dump(tid))
if self._uuid_dict.get(uuid, True):
# No interest.
return
self._uuid_dict[uuid] = True
self._gotAnswerFrom(uuid)
def oidNotFound(self, conn, message):
uuid = conn.getUUID()
logging.info('OID not found: %s', message)
app = self.app
if self._uuid_dict.get(uuid, True):
# No interest.
if not self._gotAnswerFrom(uuid):
return
app.object_present = False
self._uuid_dict[uuid] = True
def connectionCompleted(self, conn):
pass
......
......@@ -123,19 +123,18 @@ class MasterVerificationTests(NeoTestBase):
uuid = self.identifyToMasterNode()
# do nothing
conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 0)
self.verification._uuid_dict[uuid] = True
self.assertEquals(len(self.verification._uuid_set), 0)
self.assertEquals(len(self.verification._tid_set), 0)
new_tid = self.getNextTID()
verification.answerUnfinishedTransactions(conn, [new_tid])
self.assertEquals(len(self.verification._tid_set), 0)
# update dict
conn = self.getFakeConnection(uuid, self.storage_address)
self.verification._uuid_dict[uuid] = False
self.verification._uuid_set.add(uuid)
self.assertEquals(len(self.verification._tid_set), 0)
new_tid = self.getNextTID(new_tid)
verification.answerUnfinishedTransactions(conn, [new_tid,])
self.assertTrue(self.verification._uuid_dict[uuid])
self.assertTrue(uuid not in self.verification._uuid_set)
self.assertEquals(len(self.verification._tid_set), 1)
self.assertTrue(new_tid in self.verification._tid_set)
......@@ -144,8 +143,8 @@ class MasterVerificationTests(NeoTestBase):
uuid = self.identifyToMasterNode()
# do nothing, as unfinished_oid_set is None
conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 0)
self.verification._uuid_dict[uuid] = False
self.assertEquals(len(self.verification._uuid_set), 0)
self.verification._uuid_set.add(uuid)
self.verification._oid_set = None
new_tid = self.getNextTID()
new_oid = self.getOID(1)
......@@ -154,8 +153,7 @@ class MasterVerificationTests(NeoTestBase):
self.assertEquals(self.verification._oid_set, None)
# do nothing as asking_uuid_dict is True
conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 1)
self.verification._uuid_dict[uuid] = True
self.assertEquals(len(self.verification._uuid_set), 0)
self.verification._oid_set = set()
self.assertEquals(len(self.verification._oid_set), 0)
verification.answerTransactionInformation(conn, new_tid,
......@@ -163,8 +161,8 @@ class MasterVerificationTests(NeoTestBase):
self.assertEquals(len(self.verification._oid_set), 0)
# do work
conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 1)
self.verification._uuid_dict[uuid] = False
self.assertEquals(len(self.verification._uuid_set), 0)
self.verification._uuid_set.add(uuid)
self.assertEquals(len(self.verification._oid_set), 0)
verification.answerTransactionInformation(conn, new_tid,
"user", "desc", "ext", False, [new_oid,])
......@@ -172,8 +170,8 @@ class MasterVerificationTests(NeoTestBase):
self.assertTrue(new_oid in self.verification._oid_set)
# do not work as oid is diff
conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 1)
self.verification._uuid_dict[uuid] = False
self.assertEquals(len(self.verification._uuid_set), 0)
self.verification._uuid_set.add(uuid)
self.assertEquals(len(self.verification._oid_set), 1)
new_oid = self.getOID(2)
verification.answerTransactionInformation(conn, new_tid,
......@@ -185,15 +183,14 @@ class MasterVerificationTests(NeoTestBase):
uuid = self.identifyToMasterNode()
# do nothing as asking_uuid_dict is True
conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 0)
self.verification._uuid_dict[uuid] = True
self.assertEquals(len(self.verification._uuid_set), 0)
self.verification._oid_set = []
verification.tidNotFound(conn, "msg")
self.assertNotEqual(self.verification._oid_set, None)
# do work as asking_uuid_dict is False
conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 1)
self.verification._uuid_dict[uuid] = False
self.assertEquals(len(self.verification._uuid_set), 0)
self.verification._uuid_set.add(uuid)
self.verification._oid_set = []
verification.tidNotFound(conn, "msg")
self.assertEqual(self.verification._oid_set, None)
......@@ -205,35 +202,33 @@ class MasterVerificationTests(NeoTestBase):
new_tid = self.getNextTID()
new_oid = self.getOID(1)
conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 0)
self.verification._uuid_dict[uuid] = True
self.assertEquals(len(self.verification._uuid_set), 0)
verification.answerObjectPresent(conn, new_oid, new_tid)
# do work
conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 1)
self.verification._uuid_dict[uuid] = False
self.assertEquals(len(self.verification._uuid_set), 0)
self.verification._uuid_set.add(uuid)
verification.answerObjectPresent(conn, new_oid, new_tid)
self.assertTrue(self.verification._uuid_dict[uuid])
self.assertTrue(uuid not in self.verification._uuid_set)
def test_15_oidNotFound(self):
verification = self.verification
uuid = self.identifyToMasterNode()
# do nothing as asking_uuid_dict is True
conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 0)
self.verification._uuid_dict[uuid] = True
self.assertEquals(len(self.verification._uuid_set), 0)
self.app.object_present = True
self.assertTrue(self.app.object_present)
verification.oidNotFound(conn, "msg")
self.assertTrue(self.app.object_present)
# do work as asking_uuid_dict is False
conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 1)
self.verification._uuid_dict[uuid] = False
self.assertEquals(len(self.verification._uuid_set), 0)
self.verification._uuid_set.add(uuid)
self.assertTrue(self.app.object_present)
verification.oidNotFound(conn, "msg")
self.assertFalse(self.app.object_present)
self.assertTrue(self.verification._uuid_dict[uuid ])
self.assertTrue(uuid not in self.verification._uuid_set)
if __name__ == '__main__':
unittest.main()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment