[CalendarServer-changes] [8736] CalendarServer/branches/users/cdaboo/txn-debugging/txdav/common/ datastore

source_changes at macosforge.org source_changes at macosforge.org
Tue Feb 21 09:46:19 PST 2012


Revision: 8736
          http://trac.macosforge.org/projects/calendarserver/changeset/8736
Author:   cdaboo at apple.com
Date:     2012-02-21 09:46:18 -0800 (Tue, 21 Feb 2012)
Log Message:
-----------
Use task.Clock() for testing.

Modified Paths:
--------------
    CalendarServer/branches/users/cdaboo/txn-debugging/txdav/common/datastore/sql.py
    CalendarServer/branches/users/cdaboo/txn-debugging/txdav/common/datastore/test/test_sql.py

Modified: CalendarServer/branches/users/cdaboo/txn-debugging/txdav/common/datastore/sql.py
===================================================================
--- CalendarServer/branches/users/cdaboo/txn-debugging/txdav/common/datastore/sql.py	2012-02-21 16:47:29 UTC (rev 8735)
+++ CalendarServer/branches/users/cdaboo/txn-debugging/txdav/common/datastore/sql.py	2012-02-21 17:46:18 UTC (rev 8736)
@@ -243,6 +243,8 @@
     the transaction.
     """
     
+    callLater = reactor.callLater
+
     def __init__(self, txn, logTimerSeconds, timeoutSeconds):
         self.txn = txn
         self.delayedLog = None
@@ -268,16 +270,16 @@
     def _installLogTimer(self):
         def _logTransactionWait():
             if self.txn is not None:
-                log.error("Transaction wait: %r, IUDs: %d, Statement: %s" % (self.txn, self.txn.iudCount, self.txn.currentStatement if self.txn.currentStatement else "None",))
-                self.delayedLog = reactor.callLater(self.logTimerSeconds, _logTransactionWait)
+                log.error("Transaction wait: %r, Statements: %d, IUDs: %d, Statement: %s" % (self.txn, self.txn.statementCount, self.txn.iudCount, self.txn.currentStatement if self.txn.currentStatement else "None",))
+                self.delayedLog = self.callLater(self.logTimerSeconds, _logTransactionWait)
 
         if self.logTimerSeconds:
-            self.delayedLog = reactor.callLater(self.logTimerSeconds, _logTransactionWait)
+            self.delayedLog = self.callLater(self.logTimerSeconds, _logTransactionWait)
     
     def _installTimeout(self):
         def _forceAbort():
             if self.txn is not None:
-                log.error("Transaction abort too long: %r, IUDs: %d, Statement: %s" % (self.txn, self.txn.iudCount, self.txn.currentStatement if self.txn.currentStatement else "None",))
+                log.error("Transaction abort too long: %r, Statements: %d, IUDs: %d, Statement: %s" % (self.txn, self.txn.statementCount, self.txn.iudCount, self.txn.currentStatement if self.txn.currentStatement else "None",))
                 self.delayedTimeout = None
                 if self.delayedLog:
                     self.delayedLog.cancel()
@@ -285,7 +287,7 @@
                 self.txn.abort()
 
         if self.timeoutSeconds:
-            self.delayedTimeout = reactor.callLater(self.timeoutSeconds, _forceAbort)
+            self.delayedTimeout = self.callLater(self.timeoutSeconds, _forceAbort)
 
 class CommonStoreTransaction(object):
     """
@@ -332,6 +334,7 @@
         self.dialect = sqlTxn.dialect
         
         self._stats = TransactionStatsCollector() if self._store.logStats else None
+        self.statementCount = 0
         self.iudCount = 0
         self.currentStatement = None
 
@@ -615,6 +618,7 @@
         self.currentStatement = a[0]
         if self._store.logTransactionWaits and a[0].split(" ", 1)[0].lower() in ("insert", "update", "delete",):
             self.iudCount += 1
+        self.statementCount += 1
         if self._store.logLabels:
             a = ("-- Label: %s\n" % (self._label.replace("%", "%%"),) + a[0],) + a[1:]
         if self._store.logSQL:

Modified: CalendarServer/branches/users/cdaboo/txn-debugging/txdav/common/datastore/test/test_sql.py
===================================================================
--- CalendarServer/branches/users/cdaboo/txn-debugging/txdav/common/datastore/test/test_sql.py	2012-02-21 16:47:29 UTC (rev 8735)
+++ CalendarServer/branches/users/cdaboo/txn-debugging/txdav/common/datastore/test/test_sql.py	2012-02-21 17:46:18 UTC (rev 8736)
@@ -19,10 +19,10 @@
 """
 
 from twext.enterprise.dal.syntax import Select
-from twisted.internet import reactor
-from twisted.internet.defer import inlineCallbacks, Deferred
+from twisted.internet.defer import inlineCallbacks
+from twisted.internet.task import Clock
 from twisted.trial.unittest import TestCase
-from txdav.common.datastore.sql import log
+from txdav.common.datastore.sql import log, CommonStoreTransactionMonitor
 from txdav.common.datastore.sql_tables import schema
 from txdav.common.datastore.test.util import CommonCommonTests, buildStore
 from txdav.common.icommondatastore import AllRetriesFailed
@@ -76,6 +76,9 @@
         CommonStoreTransactionMonitor logs waiting transactions.
         """
         
+        c = Clock()
+        self.patch(CommonStoreTransactionMonitor, "callLater", c.callLater)
+
         # Patch config to turn on log waits then rebuild the store
         self.patch(self._sqlStore, "logTransactionWaits", 1)
         
@@ -84,25 +87,21 @@
             ctr[0] += 1
         self.patch(log, "error", counter)
 
-        txn = self.transactionUnderTest()
-        
-        def doTest(result):
-            self.assertNotEqual(ctr[0], 0)
-            txn.abort()
-        d = Deferred()
-        d.addCallback(doTest)
+        txn = self.transactionUnderTest()        
+ 
+        c.advance(2)
+        self.assertNotEqual(ctr[0], 0)
+        txn.abort()
 
-        def checkIt():
-            d.callback(None)
-        reactor.callLater(2, checkIt)
 
-        return d
-
     def test_txnTimeout(self):
         """
         CommonStoreTransactionMonitor terminates long transactions.
         """
         
+        c = Clock()
+        self.patch(CommonStoreTransactionMonitor, "callLater", c.callLater)
+
         # Patch config to turn on transaction timeouts then rebuild the store
         self.patch(self._sqlStore, "timeoutTransactions", 1)
         
@@ -112,24 +111,20 @@
         self.patch(log, "error", counter)
 
         txn = self.transactionUnderTest()
-        
-        def doTest(result):
-            self.assertNotEqual(ctr[0], 0)
-            self.assertTrue(txn._sqlTxn._completed)
-        d = Deferred()
-        d.addCallback(doTest)
 
-        def checkIt():
-            d.callback(None)
-        reactor.callLater(2, checkIt)
+        c.advance(2)
+        self.assertNotEqual(ctr[0], 0)
+        self.assertTrue(txn._sqlTxn._completed)
 
-        return d
 
     def test_logWaitsAndTxnTimeout(self):
         """
         CommonStoreTransactionMonitor logs waiting transactions and terminates long transactions.
         """
         
+        c = Clock()
+        self.patch(CommonStoreTransactionMonitor, "callLater", c.callLater)
+
         # Patch config to turn on log waits then rebuild the store
         self.patch(self._sqlStore, "logTransactionWaits", 1)
         self.patch(self._sqlStore, "timeoutTransactions", 2)
@@ -144,19 +139,11 @@
 
         txn = self.transactionUnderTest()
         
-        def doTest(result):
-            self.assertNotEqual(ctr[0], 0)
-            self.assertNotEqual(ctr[1], 0)
-            self.assertTrue(txn._sqlTxn._completed)
-        d = Deferred()
-        d.addCallback(doTest)
+        c.advance(2)
+        self.assertNotEqual(ctr[0], 0)
+        self.assertNotEqual(ctr[1], 0)
+        self.assertTrue(txn._sqlTxn._completed)
 
-        def checkIt():
-            d.callback(None)
-        reactor.callLater(3, checkIt)
-
-        return d
-
     @inlineCallbacks
     def test_subtransactionOK(self):
         """
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20120221/977d9fc1/attachment-0001.html>


More information about the calendarserver-changes mailing list