Commit 393ebf73 authored by Grégory Wisniewski's avatar Grégory Wisniewski

Update tests according to previous commit.

- testUtil module is now empty, but kept for futur tests.
- NeoTestBase got a new getNextTID() method with a copy/paste from master's transactions manager to generate TIDs.
- testProtocol module no more import getNextTID() from neo.util module
- testTransactions test module is updated to test new features provides by transactions manager.

git-svn-id: https://svn.erp5.org/repos/neo/trunk@1456 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 624c40b7
...@@ -23,6 +23,8 @@ from neo import logging ...@@ -23,6 +23,8 @@ from neo import logging
from mock import Mock from mock import Mock
from neo import protocol from neo import protocol
from neo.protocol import Packets from neo.protocol import Packets
from time import time, gmtime
from struct import pack, unpack
DB_PREFIX = 'test_neo_' DB_PREFIX = 'test_neo_'
DB_ADMIN = 'root' DB_ADMIN = 'root'
...@@ -94,6 +96,29 @@ class NeoTestBase(unittest.TestCase): ...@@ -94,6 +96,29 @@ class NeoTestBase(unittest.TestCase):
self.uuid = getNewUUID() self.uuid = getNewUUID()
return self.uuid return self.uuid
def getNextTID(self, ltid):
tm = time()
gmt = gmtime(tm)
upper = ((((gmt.tm_year - 1900) * 12 + gmt.tm_mon - 1) * 31 \
+ gmt.tm_mday - 1) * 24 + gmt.tm_hour) * 60 + gmt.tm_min
lower = int((gmt.tm_sec % 60 + (tm - int(tm))) / (60.0 / 65536.0 / 65536.0))
tid = pack('!LL', upper, lower)
if ltid is not None and tid <= ltid:
upper, lower = unpack('!LL', self._last_tid)
if lower == 0xffffffff:
# This should not happen usually.
from datetime import timedelta, datetime
d = datetime(gmt.tm_year, gmt.tm_mon, gmt.tm_mday,
gmt.tm_hour, gmt.tm_min) \
+ timedelta(0, 60)
upper = ((((d.year - 1900) * 12 + d.month - 1) * 31 \
+ d.day - 1) * 24 + d.hour) * 60 + d.minute
lower = 0
else:
lower += 1
tid = pack('!LL', upper, lower)
return tid
def getTwoIDs(self): def getTwoIDs(self):
""" Return a tuple of two sorted UUIDs """ """ Return a tuple of two sorted UUIDs """
# generate two ptid, first is lower # generate two ptid, first is lower
......
...@@ -35,7 +35,7 @@ class MasterClientHandlerTests(NeoTestBase): ...@@ -35,7 +35,7 @@ class MasterClientHandlerTests(NeoTestBase):
self.app.pt.setID(pack('!Q', 1)) self.app.pt.setID(pack('!Q', 1))
self.app.em = Mock({"getConnectionList" : []}) self.app.em = Mock({"getConnectionList" : []})
self.app.loid = '\0' * 8 self.app.loid = '\0' * 8
self.app.ltid = '\0' * 8 self.app.tm.setLastTID('\0' * 8)
for address in self.app.master_node_list: for address in self.app.master_node_list:
self.app.nm.createMaster(address=address) self.app.nm.createMaster(address=address)
self.service = ClientServiceHandler(self.app) self.service = ClientServiceHandler(self.app)
...@@ -145,15 +145,15 @@ class MasterClientHandlerTests(NeoTestBase): ...@@ -145,15 +145,15 @@ class MasterClientHandlerTests(NeoTestBase):
uuid = self.identifyToMasterNode() uuid = self.identifyToMasterNode()
packet = Packets.AskBeginTransaction() packet = Packets.AskBeginTransaction()
packet.setId(0) packet.setId(0)
ltid = self.app.ltid ltid = self.app.tm.getLastTID()
# client call it # client call it
client_uuid = self.identifyToMasterNode(node_type=NodeTypes.CLIENT, port=self.client_port) client_uuid = self.identifyToMasterNode(node_type=NodeTypes.CLIENT, port=self.client_port)
conn = self.getFakeConnection(client_uuid, self.client_address) conn = self.getFakeConnection(client_uuid, self.client_address)
service.askBeginTransaction(conn, packet, None) service.askBeginTransaction(conn, packet, None)
self.failUnless(ltid < self.app.ltid) self.failUnless(ltid < self.app.tm.getLastTID())
self.assertEqual(len(self.app.tm.getPendingList()), 1) self.assertEqual(len(self.app.tm.getPendingList()), 1)
tid = self.app.tm.getPendingList()[0] tid = self.app.tm.getPendingList()[0]
self.assertEquals(tid, self.app.ltid) self.assertEquals(tid, self.app.tm.getLastTID())
def test_08_askNewOIDs(self): def test_08_askNewOIDs(self):
service = self.service service = self.service
...@@ -176,7 +176,7 @@ class MasterClientHandlerTests(NeoTestBase): ...@@ -176,7 +176,7 @@ class MasterClientHandlerTests(NeoTestBase):
client_uuid = self.identifyToMasterNode(node_type=NodeTypes.CLIENT, port=self.client_port) client_uuid = self.identifyToMasterNode(node_type=NodeTypes.CLIENT, port=self.client_port)
conn = self.getFakeConnection(client_uuid, self.client_address) conn = self.getFakeConnection(client_uuid, self.client_address)
oid_list = [] oid_list = []
upper, lower = unpack('!LL', self.app.ltid) upper, lower = unpack('!LL', self.app.tm.getLastTID())
new_tid = pack('!LL', upper, lower + 10) new_tid = pack('!LL', upper, lower + 10)
self.checkUnexpectedPacketRaised(service.finishTransaction, conn, packet, oid_list, new_tid) self.checkUnexpectedPacketRaised(service.finishTransaction, conn, packet, oid_list, new_tid)
old_node = self.app.nm.getByUUID(uuid) old_node = self.app.nm.getByUUID(uuid)
...@@ -191,7 +191,7 @@ class MasterClientHandlerTests(NeoTestBase): ...@@ -191,7 +191,7 @@ class MasterClientHandlerTests(NeoTestBase):
conn = self.getFakeConnection(client_uuid, self.client_address) conn = self.getFakeConnection(client_uuid, self.client_address)
service.askBeginTransaction(conn, packet, None) service.askBeginTransaction(conn, packet, None)
oid_list = [] oid_list = []
tid = self.app.ltid tid = self.app.tm.getLastTID()
conn = self.getFakeConnection(client_uuid, self.client_address) conn = self.getFakeConnection(client_uuid, self.client_address)
self.app.em = Mock({"getConnectionList" : [conn, storage_conn]}) self.app.em = Mock({"getConnectionList" : [conn, storage_conn]})
service.finishTransaction(conn, packet, oid_list, tid) service.finishTransaction(conn, packet, oid_list, tid)
...@@ -217,7 +217,7 @@ class MasterClientHandlerTests(NeoTestBase): ...@@ -217,7 +217,7 @@ class MasterClientHandlerTests(NeoTestBase):
self.assertFalse(self.app.tm.hasPending()) self.assertFalse(self.app.tm.hasPending())
# give a known tid # give a known tid
conn = self.getFakeConnection(client_uuid, self.client_address) conn = self.getFakeConnection(client_uuid, self.client_address)
tid = self.app.ltid tid = self.app.tm.getLastTID()
self.app.tm.remove(tid) self.app.tm.remove(tid)
self.app.tm.begin(Mock({'__hash__': 1}), tid) self.app.tm.begin(Mock({'__hash__': 1}), tid)
self.assertTrue(self.app.tm.hasPending()) self.assertTrue(self.app.tm.hasPending())
......
...@@ -153,7 +153,7 @@ class MasterRecoveryTests(NeoTestBase): ...@@ -153,7 +153,7 @@ class MasterRecoveryTests(NeoTestBase):
uuid = self.identifyToMasterNode() uuid = self.identifyToMasterNode()
packet = Packets.AnswerLastIDs() packet = Packets.AnswerLastIDs()
loid = self.app.loid loid = self.app.loid
ltid = self.app.ltid ltid = self.app.tm.getLastTID()
lptid = self.app.pt.getID() lptid = self.app.pt.getID()
# send information which are later to what PMN knows, this must update target node # send information which are later to what PMN knows, this must update target node
conn = self.getFakeConnection(uuid, self.storage_port) conn = self.getFakeConnection(uuid, self.storage_port)
...@@ -166,11 +166,11 @@ class MasterRecoveryTests(NeoTestBase): ...@@ -166,11 +166,11 @@ class MasterRecoveryTests(NeoTestBase):
new_tid = pack('!LL', upper, lower + 10) new_tid = pack('!LL', upper, lower + 10)
self.failUnless(new_ptid > self.app.pt.getID()) self.failUnless(new_ptid > self.app.pt.getID())
self.failUnless(new_oid > self.app.loid) self.failUnless(new_oid > self.app.loid)
self.failUnless(new_tid > self.app.ltid) self.failUnless(new_tid > self.app.tm.getLastTID())
self.assertEquals(self.app.target_uuid, None) self.assertEquals(self.app.target_uuid, None)
recovery.answerLastIDs(conn, packet, new_oid, new_tid, new_ptid) recovery.answerLastIDs(conn, packet, new_oid, new_tid, new_ptid)
self.assertEquals(new_oid, self.app.loid) self.assertEquals(new_oid, self.app.loid)
self.assertEquals(new_tid, self.app.ltid) self.assertEquals(new_tid, self.app.tm.getLastTID())
self.assertEquals(new_ptid, self.app.pt.getID()) self.assertEquals(new_ptid, self.app.pt.getID())
self.assertEquals(self.app.target_uuid,uuid) self.assertEquals(self.app.target_uuid,uuid)
......
...@@ -184,7 +184,8 @@ class MasterStorageHandlerTests(NeoTestBase): ...@@ -184,7 +184,8 @@ class MasterStorageHandlerTests(NeoTestBase):
conn = self.getFakeConnection(node.getUUID(), self.storage_address) conn = self.getFakeConnection(node.getUUID(), self.storage_address)
ptid = self.app.pt.getID() ptid = self.app.pt.getID()
oid = self.app.loid = '\1' * 8 oid = self.app.loid = '\1' * 8
tid = self.app.ltid = '\1' * 8 tid = '\1' * 8
self.app.tm.setLastTID(tid)
service.askLastIDs(conn, packet) service.askLastIDs(conn, packet)
packet = self.checkAnswerLastIDs(conn, answered_packet=packet) packet = self.checkAnswerLastIDs(conn, answered_packet=packet)
loid, ltid, lptid = packet.decode() loid, ltid, lptid = packet.decode()
......
...@@ -51,7 +51,6 @@ class testTransactionManager(NeoTestBase): ...@@ -51,7 +51,6 @@ class testTransactionManager(NeoTestBase):
def testManager(self): def testManager(self):
# test data # test data
node = Mock({'__hash__': 1}) node = Mock({'__hash__': 1})
tid = self.makeTID(1)
msg_id = 1 msg_id = 1
oid_list = (oid1, oid2) = self.makeOID(1), self.makeOID(2) oid_list = (oid1, oid2) = self.makeOID(1), self.makeOID(2)
uuid_list = (uuid1, uuid2) = self.makeUUID(1), self.makeUUID(2) uuid_list = (uuid1, uuid2) = self.makeUUID(1), self.makeUUID(2)
...@@ -59,9 +58,9 @@ class testTransactionManager(NeoTestBase): ...@@ -59,9 +58,9 @@ class testTransactionManager(NeoTestBase):
txnman = TransactionManager() txnman = TransactionManager()
self.assertFalse(txnman.hasPending()) self.assertFalse(txnman.hasPending())
self.assertEqual(txnman.getPendingList(), []) self.assertEqual(txnman.getPendingList(), [])
self.assertRaises(KeyError, txnman.__getitem__, tid)
# begin the transaction # begin the transaction
txnman.begin(node, tid) tid = txnman.begin(node, None)
self.assertTrue(tid is not None)
self.assertTrue(txnman.hasPending()) self.assertTrue(txnman.hasPending())
self.assertEqual(len(txnman.getPendingList()), 1) self.assertEqual(len(txnman.getPendingList()), 1)
self.assertEqual(txnman.getPendingList()[0], tid) self.assertEqual(txnman.getPendingList()[0], tid)
...@@ -81,14 +80,13 @@ class testTransactionManager(NeoTestBase): ...@@ -81,14 +80,13 @@ class testTransactionManager(NeoTestBase):
def testAbortFor(self): def testAbortFor(self):
node1 = Mock({'__hash__': 1}) node1 = Mock({'__hash__': 1})
node2 = Mock({'__hash__': 2}) node2 = Mock({'__hash__': 2})
tid11, tid12 = self.makeTID(11), self.makeTID(12)
tid21, tid22 = self.makeTID(21), self.makeTID(22)
txnman = TransactionManager() txnman = TransactionManager()
# register 4 transactions made by two nodes # register 4 transactions made by two nodes
txnman.begin(node1, tid11) tid11 = txnman.begin(node1, None)
txnman.begin(node1, tid12) tid12 = txnman.begin(node1, None)
txnman.begin(node2, tid21) tid21 = txnman.begin(node2, None)
txnman.begin(node2, tid22) tid22 = txnman.begin(node2, None)
self.assertTrue(tid11 < tid12 < tid21 < tid22)
self.assertEqual(len(txnman.getPendingList()), 4) self.assertEqual(len(txnman.getPendingList()), 4)
# abort transactions of one node # abort transactions of one node
txnman.abortFor(node1) txnman.abortFor(node1)
...@@ -101,6 +99,28 @@ class testTransactionManager(NeoTestBase): ...@@ -101,6 +99,28 @@ class testTransactionManager(NeoTestBase):
self.assertEqual(txnman.getPendingList(), []) self.assertEqual(txnman.getPendingList(), [])
self.assertFalse(txnman.hasPending()) self.assertFalse(txnman.hasPending())
def test_getNextTID(self):
txnman = TransactionManager()
# no previous TID
self.assertEqual(txnman.getLastTID(), None)
# first transaction
node1 = Mock({'__hash__': 1})
tid1 = txnman.begin(node1, None)
self.assertTrue(tid1 is not None)
self.assertEqual(txnman.getLastTID(), tid1)
# set a new last TID
from struct import pack, unpack
ntid = pack('!Q', unpack('!Q', tid1)[0] + 10)
txnman.setLastTID(ntid)
self.assertEqual(txnman.getLastTID(), ntid)
self.assertTrue(ntid > tid1)
# new trancation
node2 = Mock({'__hash__': 2})
tid2 = txnman.begin(node2, None)
self.assertTrue(tid2 is not None)
self.assertTrue(tid2 > ntid > tid1)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
...@@ -42,7 +42,7 @@ class MasterVerificationTests(NeoTestBase): ...@@ -42,7 +42,7 @@ class MasterVerificationTests(NeoTestBase):
self.app.asking_uuid_dict = {} self.app.asking_uuid_dict = {}
self.app.unfinished_tid_set = set() self.app.unfinished_tid_set = set()
self.app.loid = '\0' * 8 self.app.loid = '\0' * 8
self.app.ltid = '\0' * 8 self.app.tm.setLastTID('\0' * 8)
for node in self.app.nm.getMasterList(): for node in self.app.nm.getMasterList():
self.app.unconnected_master_node_set.add(node.getAddress()) self.app.unconnected_master_node_set.add(node.getAddress())
node.setState(NodeStates.RUNNING) node.setState(NodeStates.RUNNING)
...@@ -109,7 +109,7 @@ class MasterVerificationTests(NeoTestBase): ...@@ -109,7 +109,7 @@ class MasterVerificationTests(NeoTestBase):
uuid = self.identifyToMasterNode() uuid = self.identifyToMasterNode()
packet = Packets.AnswerLastIDs() packet = Packets.AnswerLastIDs()
loid = self.app.loid loid = self.app.loid
ltid = self.app.ltid ltid = self.app.tm.getLastTID()
lptid = '\0' * 8 lptid = '\0' * 8
# send information which are later to what PMN knows, this must raise # send information which are later to what PMN knows, this must raise
conn = self.getFakeConnection(uuid, self.storage_address) conn = self.getFakeConnection(uuid, self.storage_address)
...@@ -122,10 +122,10 @@ class MasterVerificationTests(NeoTestBase): ...@@ -122,10 +122,10 @@ class MasterVerificationTests(NeoTestBase):
new_tid = pack('!LL', upper, lower + 10) new_tid = pack('!LL', upper, lower + 10)
self.failUnless(new_ptid > self.app.pt.getID()) self.failUnless(new_ptid > self.app.pt.getID())
self.failUnless(new_oid > self.app.loid) self.failUnless(new_oid > self.app.loid)
self.failUnless(new_tid > self.app.ltid) self.failUnless(new_tid > self.app.tm.getLastTID())
self.assertRaises(VerificationFailure, verification.answerLastIDs, conn, packet, new_oid, new_tid, new_ptid) self.assertRaises(VerificationFailure, verification.answerLastIDs, conn, packet, new_oid, new_tid, new_ptid)
self.assertNotEquals(new_oid, self.app.loid) self.assertNotEquals(new_oid, self.app.loid)
self.assertNotEquals(new_tid, self.app.ltid) self.assertNotEquals(new_tid, self.app.tm.getLastTID())
self.assertNotEquals(new_ptid, self.app.pt.getID()) self.assertNotEquals(new_ptid, self.app.pt.getID())
def test_11_answerUnfinishedTransactions(self): def test_11_answerUnfinishedTransactions(self):
...@@ -138,7 +138,7 @@ class MasterVerificationTests(NeoTestBase): ...@@ -138,7 +138,7 @@ class MasterVerificationTests(NeoTestBase):
self.app.asking_uuid_dict[uuid] = True self.app.asking_uuid_dict[uuid] = True
self.assertTrue(self.app.asking_uuid_dict.has_key(uuid)) self.assertTrue(self.app.asking_uuid_dict.has_key(uuid))
self.assertEquals(len(self.app.unfinished_tid_set), 0) self.assertEquals(len(self.app.unfinished_tid_set), 0)
upper, lower = unpack('!LL', self.app.ltid) upper, lower = unpack('!LL', self.app.tm.getLastTID())
new_tid = pack('!LL', upper, lower + 10) new_tid = pack('!LL', upper, lower + 10)
verification.answerUnfinishedTransactions(conn, packet, [new_tid]) verification.answerUnfinishedTransactions(conn, packet, [new_tid])
self.assertEquals(len(self.app.unfinished_tid_set), 0) self.assertEquals(len(self.app.unfinished_tid_set), 0)
...@@ -147,7 +147,7 @@ class MasterVerificationTests(NeoTestBase): ...@@ -147,7 +147,7 @@ class MasterVerificationTests(NeoTestBase):
self.app.asking_uuid_dict[uuid] = False self.app.asking_uuid_dict[uuid] = False
self.assertTrue(self.app.asking_uuid_dict.has_key(uuid)) self.assertTrue(self.app.asking_uuid_dict.has_key(uuid))
self.assertEquals(len(self.app.unfinished_tid_set), 0) self.assertEquals(len(self.app.unfinished_tid_set), 0)
upper, lower = unpack('!LL', self.app.ltid) upper, lower = unpack('!LL', self.app.tm.getLastTID())
new_tid = pack('!LL', upper, lower + 10) new_tid = pack('!LL', upper, lower + 10)
verification.answerUnfinishedTransactions(conn, packet, [new_tid,]) verification.answerUnfinishedTransactions(conn, packet, [new_tid,])
self.assertTrue(self.app.asking_uuid_dict[uuid]) self.assertTrue(self.app.asking_uuid_dict[uuid])
...@@ -165,7 +165,7 @@ class MasterVerificationTests(NeoTestBase): ...@@ -165,7 +165,7 @@ class MasterVerificationTests(NeoTestBase):
self.app.asking_uuid_dict[uuid] = False self.app.asking_uuid_dict[uuid] = False
self.app.unfinished_oid_set = None self.app.unfinished_oid_set = None
self.assertTrue(self.app.asking_uuid_dict.has_key(uuid)) self.assertTrue(self.app.asking_uuid_dict.has_key(uuid))
upper, lower = unpack('!LL', self.app.ltid) upper, lower = unpack('!LL', self.app.tm.getLastTID())
new_tid = pack('!LL', upper, lower + 10) new_tid = pack('!LL', upper, lower + 10)
oid = unpack('!Q', self.app.loid)[0] oid = unpack('!Q', self.app.loid)[0]
new_oid = pack('!Q', oid + 1) new_oid = pack('!Q', oid + 1)
...@@ -232,7 +232,7 @@ class MasterVerificationTests(NeoTestBase): ...@@ -232,7 +232,7 @@ class MasterVerificationTests(NeoTestBase):
uuid = self.identifyToMasterNode() uuid = self.identifyToMasterNode()
packet = Packets.AnswerObjectPresent() packet = Packets.AnswerObjectPresent()
# do nothing as asking_uuid_dict is True # do nothing as asking_uuid_dict is True
upper, lower = unpack('!LL', self.app.ltid) upper, lower = unpack('!LL', self.app.tm.getLastTID())
new_tid = pack('!LL', upper, lower + 10) new_tid = pack('!LL', upper, lower + 10)
oid = unpack('!Q', self.app.loid)[0] oid = unpack('!Q', self.app.loid)[0]
new_oid = pack('!Q', oid + 1) new_oid = pack('!Q', oid + 1)
......
...@@ -21,21 +21,20 @@ from neo import protocol ...@@ -21,21 +21,20 @@ from neo import protocol
from neo.protocol import Packets from neo.protocol import Packets
from neo.protocol import NodeTypes, NodeStates, CellStates from neo.protocol import NodeTypes, NodeStates, CellStates
from neo.protocol import ErrorCodes, Packets, Packet from neo.protocol import ErrorCodes, Packets, Packet
from neo.protocol import INVALID_TID, PACKET_HEADER_SIZE from neo.protocol import PACKET_HEADER_SIZE
from neo.tests import NeoTestBase from neo.tests import NeoTestBase
from neo.util import getNextTID
from time import time, gmtime from time import time, gmtime
class ProtocolTests(NeoTestBase): class ProtocolTests(NeoTestBase):
def setUp(self): def setUp(self):
self.ltid = INVALID_TID self.ltid = None
def tearDown(self): def tearDown(self):
pass pass
def getNextTID(self): def getNextTID(self):
self.ltid = getNextTID(self.ltid) self.ltid = super(ProtocolTests, self).getNextTID(self.ltid)
return self.ltid return self.ltid
def test_03_protocolError(self): def test_03_protocolError(self):
......
...@@ -22,16 +22,7 @@ from neo import util ...@@ -22,16 +22,7 @@ from neo import util
class UtilTests(NeoTestBase): class UtilTests(NeoTestBase):
def test_getNextTID(self): pass
tid0 = '\0' * 8
tid1 = util.getNextTID(tid0)
self.assertTrue(tid1 > tid0)
tid2 = util.getNextTID(tid1)
self.assertTrue(tid2 > tid1)
tidx = '\0' + chr(0xFF) * 7
tid3 = util.getNextTID(tidx)
self.assertTrue(tid3 > tidx)
if __name__ == "__main__": if __name__ == "__main__":
unittest.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