Commit 9d940973 authored by Grégory Wisniewski's avatar Grégory Wisniewski

Fix storage's client handler test since transactions manager.

git-svn-id: https://svn.erp5.org/repos/neo/trunk@1652 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent fbe68561
...@@ -21,7 +21,7 @@ from mock import Mock ...@@ -21,7 +21,7 @@ from mock import Mock
from collections import deque from collections import deque
from neo.tests import NeoTestBase from neo.tests import NeoTestBase
from neo.storage.app import Application from neo.storage.app import Application
from neo.storage.handlers.client import TransactionInformation from neo.storage.transactions import ConflictError, DelayedError
from neo.storage.handlers.client import ClientOperationHandler from neo.storage.handlers.client import ClientOperationHandler
from neo.protocol import Packets, Packet, INVALID_PARTITION from neo.protocol import Packets, Packet, INVALID_PARTITION
from neo.protocol import INVALID_TID, INVALID_OID, INVALID_SERIAL from neo.protocol import INVALID_TID, INVALID_OID, INVALID_SERIAL
...@@ -46,6 +46,7 @@ class StorageClientHandlerTests(NeoTestBase): ...@@ -46,6 +46,7 @@ class StorageClientHandlerTests(NeoTestBase):
self.app.store_lock_dict = {} self.app.store_lock_dict = {}
self.app.load_lock_dict = {} self.app.load_lock_dict = {}
self.app.event_queue = deque() self.app.event_queue = deque()
self.app.tm = Mock()
# handler # handler
self.operation = ClientOperationHandler(self.app) self.operation = ClientOperationHandler(self.app)
# set pmn # set pmn
...@@ -58,44 +59,31 @@ class StorageClientHandlerTests(NeoTestBase): ...@@ -58,44 +59,31 @@ class StorageClientHandlerTests(NeoTestBase):
def tearDown(self): def tearDown(self):
NeoTestBase.tearDown(self) NeoTestBase.tearDown(self)
def test_01_TransactionInformation(self): def _getConnection(self, uuid=None):
return Mock({'getUUID': uuid, 'getAddress': ('127.0.0.1', 1000)})
def _checkTransactionsAborted(self, uuid):
calls = self.app.tm.mockGetNamedCalls('abortFor')
self.assertEqual(len(calls), 1)
calls[0].checkArgs(uuid)
def test_connectionClosed(self):
uuid = self.getNewUUID() uuid = self.getNewUUID()
transaction = TransactionInformation(uuid) conn = self._getConnection(uuid=uuid)
# uuid self.operation.connectionClosed(conn)
self.assertEquals(transaction._uuid, uuid) self._checkTransactionsAborted(uuid)
self.assertEquals(transaction.getUUID(), uuid)
# objects
self.assertEquals(transaction._object_dict, {})
object = (self.getNewUUID(), 1, 2, 3, )
transaction.addObject(*object)
objects = transaction.getObjectList()
self.assertEquals(len(objects), 1)
self.assertEquals(objects[0], object)
# transactions
self.assertEquals(transaction._transaction, None)
t = ((1, 2, 3), 'user', 'desc', '')
transaction.addTransaction(*t)
self.assertEquals(transaction.getTransaction(), t)
def test_05_dealWithClientFailure(self): def test_timeoutExpired(self):
# check if client's transaction are cleaned
uuid = self.getNewUUID() uuid = self.getNewUUID()
client = self.app.nm.createClient( conn = self._getConnection(uuid=uuid)
uuid=uuid, self.operation.connectionClosed(conn)
address=('127.0.0.1', 10010) self._checkTransactionsAborted(uuid)
)
self.app.store_lock_dict[0] = object() def test_peerBroken(self):
transaction = Mock({ uuid = self.getNewUUID()
'getUUID': uuid, conn = self._getConnection(uuid=uuid)
'getObjectList': ((0, ), ), self.operation.connectionClosed(conn)
}) self._checkTransactionsAborted(uuid)
self.app.transaction_dict[0] = transaction
self.assertTrue(1 not in self.app.store_lock_dict)
self.assertTrue(1 not in self.app.transaction_dict)
self.operation.dealWithClientFailure(uuid)
# objects and transaction removed
self.assertTrue(0 not in self.app.store_lock_dict)
self.assertTrue(0 not in self.app.transaction_dict)
def test_18_askTransactionInformation1(self): def test_18_askTransactionInformation1(self):
# transaction does not exists # transaction does not exists
...@@ -115,6 +103,7 @@ class StorageClientHandlerTests(NeoTestBase): ...@@ -115,6 +103,7 @@ class StorageClientHandlerTests(NeoTestBase):
# delayed response # delayed response
conn = Mock({}) conn = Mock({})
self.app.dm = Mock() self.app.dm = Mock()
self.app.tm = Mock({'loadLocked': True})
self.app.load_lock_dict[INVALID_OID] = object() self.app.load_lock_dict[INVALID_OID] = object()
self.assertEquals(len(self.app.event_queue), 0) self.assertEquals(len(self.app.event_queue), 0)
self.operation.askObject(conn, oid=INVALID_OID, self.operation.askObject(conn, oid=INVALID_OID,
...@@ -205,87 +194,62 @@ class StorageClientHandlerTests(NeoTestBase): ...@@ -205,87 +194,62 @@ class StorageClientHandlerTests(NeoTestBase):
self.operation.askObjectHistory(conn, INVALID_OID, 1, 2) self.operation.askObjectHistory(conn, INVALID_OID, 1, 2)
self.checkAnswerObjectHistory(conn) self.checkAnswerObjectHistory(conn)
def test_27_askStoreTransaction2(self): def test_askStoreTransaction(self):
# add transaction entry uuid = self.getNewUUID()
conn = Mock({'getUUID': self.getNewUUID()}) conn = self._getConnection(uuid=uuid)
self.operation.askStoreTransaction(conn, INVALID_TID, '', '', '', ()) tid = self.getNextTID()
t = self.app.transaction_dict.get(INVALID_TID, None) user = 'USER'
self.assertNotEquals(t, None) desc = 'DESC'
self.assertTrue(isinstance(t, TransactionInformation)) ext = 'EXT'
self.assertEquals(t.getTransaction(), ((), '', '', '')) oid_list = (self.getOID(1), self.getOID(2))
self.operation.askStoreTransaction(conn, tid, user, desc, ext, oid_list)
calls = self.app.tm.mockGetNamedCalls('storeTransaction')
self.assertEqual(len(calls), 1)
self.checkAnswerStoreTransaction(conn) self.checkAnswerStoreTransaction(conn)
def test_28_askStoreObject2(self): def _getObject(self):
# locked => delayed response oid = self.getOID(0)
conn = Mock({'getUUID': self.app.uuid}) serial = self.getNextTID()
oid = '\x02' * 8 return (oid, serial, 1, '1', 'DATA')
tid1, tid2 = self.getTwoIDs()
self.app.store_lock_dict[oid] = tid1
self.assertTrue(oid in self.app.store_lock_dict)
t_before = self.app.transaction_dict.items()[:]
self.operation.askStoreObject(conn, oid, INVALID_SERIAL, 0, 0, '', tid2)
self.assertEquals(len(self.app.event_queue), 1)
t_after = self.app.transaction_dict.items()[:]
self.assertEquals(t_before, t_after)
self.checkNoPacketSent(conn)
self.assertTrue(oid in self.app.store_lock_dict)
def test_28_askStoreObject3(self): def _checkStoreObjectCalled(self, *args):
# locked => unresolvable conflict => answer calls = self.app.tm.mockGetNamedCalls('storeObject')
conn = Mock({'getUUID': self.app.uuid}) self.assertEqual(len(calls), 1)
tid1, tid2 = self.getTwoIDs() calls[0].checkArgs(*args)
self.app.store_lock_dict[INVALID_OID] = tid2
self.operation.askStoreObject(conn, INVALID_OID,
INVALID_SERIAL, 0, 0, '', tid1)
self.checkAnswerStoreObject(conn)
self.assertEquals(self.app.store_lock_dict[INVALID_OID], tid2)
# conflicting
packet = conn.mockGetNamedCalls('answer')[0].getParam(0)
self.assertTrue(unpack('!B8s8s', packet._body)[0])
def test_28_askStoreObject4(self): def test_askStoreObject1(self):
# resolvable conflict => answer # no conflict => answer
conn = Mock({'getUUID': self.app.uuid}) uuid = self.getNewUUID()
self.app.dm = Mock({'getObjectHistory':((self.getNewUUID(), ), )}) conn = self._getConnection(uuid=uuid)
self.assertEquals(self.app.store_lock_dict.get(INVALID_OID, None), None) tid = self.getNextTID()
self.operation.askStoreObject(conn, INVALID_OID, oid, serial, comp, checksum, data = self._getObject()
INVALID_SERIAL, 0, 0, '', INVALID_TID) self.operation.askStoreObject(conn, oid, serial, comp, checksum,
data, tid)
self._checkStoreObjectCalled(uuid, tid, serial, oid, comp,
checksum, data)
self.checkAnswerStoreObject(conn) self.checkAnswerStoreObject(conn)
self.assertEquals(self.app.store_lock_dict.get(INVALID_OID, None), None)
# conflicting
packet = conn.mockGetNamedCalls('answer')[0].getParam(0)
self.assertTrue(unpack('!B8s8s', packet._body)[0])
def test_28_askStoreObject5(self): def test_askStoreObject2(self):
# no conflict => answer # conflict error
conn = Mock({'getUUID': self.app.uuid}) uuid = self.getNewUUID()
self.operation.askStoreObject(conn, INVALID_OID, conn = self._getConnection(uuid=uuid)
INVALID_SERIAL, 0, 0, '', INVALID_TID) tid = self.getNextTID()
t = self.app.transaction_dict.get(INVALID_TID, None) locking_tid = self.getNextTID(tid)
self.assertNotEquals(t, None) def fakeStoreObject(*args):
self.assertEquals(len(t.getObjectList()), 1) raise ConflictError(locking_tid)
object = t.getObjectList()[0] self.app.tm.storeObject = lambda *kw: fakeStoreObject
self.assertEquals(object, (INVALID_OID, 0, 0, '')) oid, serial, comp, checksum, data = self._getObject()
# no conflict self.operation.askStoreObject(conn, oid, serial, comp, checksum,
packet = self.checkAnswerStoreObject(conn) data, tid)
self.assertFalse(unpack('!B8s8s', packet._body)[0]) self.checkAnswerStoreObject(conn)
def test_29_abortTransaction(self): def test_abortTransaction(self):
# remove transaction conn = self._getConnection()
conn = Mock({'getUUID': self.app.uuid}) tid = self.getNextTID()
transaction = Mock({ 'getObjectList': ((0, ), ), }) self.operation.abortTransaction(conn, tid)
self.called = False calls = self.app.tm.mockGetNamedCalls('abort')
def called(): self.assertEqual(len(calls), 1)
self.called = True calls[0].checkArgs(tid)
self.app.executeQueuedEvents = called
self.app.load_lock_dict[0] = object()
self.app.store_lock_dict[0] = object()
self.app.transaction_dict[INVALID_TID] = transaction
self.operation.abortTransaction(conn, INVALID_TID)
self.assertTrue(self.called)
self.assertEquals(len(self.app.load_lock_dict), 0)
self.assertEquals(len(self.app.store_lock_dict), 0)
self.assertEquals(len(self.app.store_lock_dict), 0)
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