Commit 4e885787 authored by Vincent Pelletier's avatar Vincent Pelletier

Define accessors for App's tid, conflict_serial, txn_finished and txn_voted...

Define accessors for App's tid, conflict_serial, txn_finished and txn_voted (all in one commit since they are closely related in usage).
Use those accessors instead of accessing properties directly.
Update tests.


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@362 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 680e33ae
......@@ -100,19 +100,21 @@ class Storage(BaseStorage.BaseStorage,
data = data, version = version,
transaction = transaction)
except NEOStorageConflictError:
if app.conflict_serial <= app.tid:
conflict_serial = app.getConflictSerial()
tid = app.getTID()
if conflict_serial <= tid:
# Try to resolve conflict only if conflicting serial is older
# than the current transaction ID
new_data = self.tryToResolveConflict(oid,
app.conflict_serial,
conflict_serial,
serial, data)
if new_data is not None:
# Try again after conflict resolution
self.store(oid, app.conflict_serial,
self.store(oid, conflict_serial,
new_data, version, transaction)
return ConflictResolution.ResolvedSerial
raise POSException.ConflictError(oid=oid,
serials=(app.tid,
serials=(tid,
serial),data=data)
def _clear_temp(self):
......
......@@ -212,8 +212,8 @@ class Application(object):
self.txn = None
self.txn_data_dict = {}
self.txn_object_stored = 0
self.txn_voted = 0
self.txn_finished = 0
self.txn_voted = False
self.txn_finished = False
# Internal attribute distinct between thread
self.local_var = local()
# Lock definition :
......@@ -554,12 +554,12 @@ class Application(object):
conn.addPacket(p)
conn.expectMessage(msg_id)
self.dispatcher.register(conn, msg_id, self.getQueue())
self.txn_voted = 0
self.txn_voted = False
finally:
conn.unlock()
self._waitMessage(conn, msg_id)
if self.txn_voted != 1:
if not self.isTransactionVoted():
raise NEOStorageError('tpc_vote failed')
def _clear_txn(self):
......@@ -567,8 +567,8 @@ class Application(object):
self.tid = None
self.txn = None
self.txn_data_dict.clear()
self.txn_voted = 0
self.txn_finished = 0
self.txn_voted = False
self.txn_finished = False
def tpc_abort(self, transaction):
"""Abort current transaction."""
......@@ -651,7 +651,7 @@ class Application(object):
# Wait for answer
self._waitMessage(conn, msg_id)
if self.txn_finished != 1:
if not self.isTransactionFinished():
raise NEOStorageError('tpc_finish failed')
# Update cache
......@@ -1000,3 +1000,24 @@ class Application(object):
def isNodeReady(self):
return self.local_var.node_ready
def setTID(self, value):
self.tid = value
def getTID(self):
return self.tid
def getConflictSerial(self):
return self.conflict_serial
def setTransactionFinished(self):
self.txn_finished = True
def isTransactionFinished(self):
return self.txn_finished
def setTransactionVoted(self):
self.txn_voted = True
def isTransactionVoted(self):
return self.txn_voted
......@@ -401,7 +401,7 @@ class ClientAnswerEventHandler(BaseClientEventHandler):
def handleAnswerNewTID(self, conn, packet, tid):
app = self.app
app.tid = tid
app.setTID(tid)
def handleAnswerNewOIDs(self, conn, packet, oid_list):
app = self.app
......@@ -410,11 +410,8 @@ class ClientAnswerEventHandler(BaseClientEventHandler):
def handleNotifyTransactionFinished(self, conn, packet, tid):
app = self.app
if tid != app.tid:
app.txn_finished = -1
else:
app.txn_finished = 1
if tid == app.getTID():
app.setTransactionFinished()
# Storage node handler
def handleAnswerObject(self, conn, packet, oid, start_serial, end_serial, compression,
......@@ -432,7 +429,7 @@ class ClientAnswerEventHandler(BaseClientEventHandler):
def handleAnswerStoreTransaction(self, conn, packet, tid):
app = self.app
app.txn_voted = 1
app.setTransactionVoted()
def handleAnswerTransactionInformation(self, conn, packet, tid,
user, desc, ext, oid_list):
......
......@@ -830,26 +830,24 @@ class ClientEventHandlerTest(unittest.TestCase):
self.assertEqual(state, test_cell_list[0][2])
def test_AnswerNewTID(self):
class App:
tid = None
app = App()
app = Mock({'setTID': None})
dispatcher = self.getDispatcher()
client_handler = ClientAnswerEventHandler(app, dispatcher)
conn = self.getConnection()
test_tid = 1
client_handler.handleAnswerNewTID(conn, None, test_tid)
self.assertEquals(app.tid, test_tid)
setTID_call_list = app.mockGetNamedCalls('setTID')
self.assertEquals(len(setTID_call_list), 1)
self.assertEquals(setTID_call_list[0].getParam(0), test_tid)
def test_NotifyTransactionFinished(self):
class App:
tid = 1
txn_finished = None
app = App()
test_tid = 1
app = Mock({'getTID': test_tid, 'setTransactionFinished': None})
dispatcher = self.getDispatcher()
client_handler = ClientAnswerEventHandler(app, dispatcher)
conn = self.getConnection()
client_handler.handleNotifyTransactionFinished(conn, None, 1)
self.assertEquals(app.txn_finished, 1)
client_handler.handleNotifyTransactionFinished(conn, None, test_tid)
self.assertEquals(len(app.mockGetNamedCalls('setTransactionFinished')), 1)
# TODO: decide what to do when non-current transaction is notified as finished, and test that behaviour
def test_InvalidateObjects(self):
......@@ -949,16 +947,13 @@ class ClientEventHandlerTest(unittest.TestCase):
self.assertEqual(app.txn_object_stored, (test_oid, test_serial))
def test_AnswerStoreTransaction(self):
class App:
tid = 10
txn_voted = 0
app = App()
test_tid = 10
app = Mock({'getTID': test_tid, 'setTransactionVoted': None})
dispatcher = self.getDispatcher()
client_handler = ClientAnswerEventHandler(app, dispatcher)
conn = self.getConnection()
test_tid = 10
client_handler.handleAnswerStoreTransaction(conn, None, test_tid)
self.assertEquals(app.txn_voted, 1)
self.assertEquals(len(app.mockGetNamedCalls('setTransactionVoted')), 1)
# TODO: test handleAnswerObject with test_tid not matching app.tid (not handled in program)
def test_AnswerTransactionInformation(self):
......
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