[CalendarServer-changes] [6536] CalendarServer/branches/users/glyph/sharedpool/txdav/common/ datastore/test/util.py

source_changes at macosforge.org source_changes at macosforge.org
Mon Nov 1 14:27:23 PDT 2010


Revision: 6536
          http://trac.macosforge.org/projects/calendarserver/changeset/6536
Author:   glyph at apple.com
Date:     2010-11-01 14:27:20 -0700 (Mon, 01 Nov 2010)
Log Message:
-----------
make test store initialization more consistent

Modified Paths:
--------------
    CalendarServer/branches/users/glyph/sharedpool/txdav/common/datastore/test/util.py

Modified: CalendarServer/branches/users/glyph/sharedpool/txdav/common/datastore/test/util.py
===================================================================
--- CalendarServer/branches/users/glyph/sharedpool/txdav/common/datastore/test/util.py	2010-11-01 21:26:54 UTC (rev 6535)
+++ CalendarServer/branches/users/glyph/sharedpool/txdav/common/datastore/test/util.py	2010-11-01 21:27:20 UTC (rev 6536)
@@ -31,11 +31,14 @@
 from twisted.internet.defer import Deferred, inlineCallbacks
 from twisted.internet.task import deferLater
 from twisted.python import log
+from twisted.application.service import Service
 
 from txdav.common.datastore.sql import CommonDataStore, v1_schema
 from txdav.base.datastore.subpostgres import PostgresService,\
     DiagnosticConnectionWrapper
 from txdav.common.icommondatastore import NoSuchHomeChildError
+from txdav.base.datastore.asyncsqlpool import ConnectionPool
+from twisted.internet.defer import returnValue
 from twistedcaldav.notify import Notifier
 
 
@@ -69,31 +72,15 @@
 
         @return: a L{Deferred} which fires with an L{IDataStore}.
         """
-        currentTestID = testCase.id()
         dbRoot = CachingFilePath(self.SHARED_DB_PATH)
+        attachmentRoot = dbRoot.child("attachments")
         if self.sharedService is None:
             ready = Deferred()
             def getReady(connectionFactory):
-                attachmentRoot = dbRoot.child("attachments")
-                try:
-                    attachmentRoot.createDirectory()
-                except OSError:
-                    pass
-                try:
-                    self.store = CommonDataStore(
-                        self.sharedService.produceLocalTransaction,
-                        notifierFactory,
-                        attachmentRoot
-                    )
-                    self.store.label = currentTestID
-                except:
-                    ready.errback()
-                    raise
-                else:
-                    def readyNow(ignored):
-                        ready.callback(self.store)
-                    return self.cleanDatabase(testCase).addCallback(readyNow)
-                return self.store
+                self.makeAndCleanStore(
+                    testCase, notifierFactory, attachmentRoot
+                ).chainDeferred(ready)
+                return Service()
             self.sharedService = PostgresService(
                 dbRoot, getReady, v1_schema, resetSchema=True,
                 databaseName="caldav",
@@ -108,14 +95,10 @@
                 "before", "shutdown", startStopping)
             result = ready
         else:
-            self.store.notifierFactory = notifierFactory
-            result = self.cleanDatabase(testCase).addCallback(
-                lambda ignored: self.store
+            result = self.makeAndCleanStore(
+                testCase, notifierFactory, attachmentRoot
             )
-
         def cleanUp():
-            # FIXME: clean up any leaked connections and report them with an
-            # immediate test failure.
             def stopit():
                 self.sharedService.pauseMonitor()
             return deferLater(reactor, 0.1, stopit)
@@ -124,8 +107,35 @@
 
 
     @inlineCallbacks
-    def cleanDatabase(self, testCase):
-        cleanupTxn = self.store.sqlTxnFactory(
+    def makeAndCleanStore(self, testCase, notifierFactory, attachmentRoot):
+        """
+        Create a L{CommonDataStore} specific to the given L{TestCase}.
+
+        This also creates a L{ConnectionPool} that gets stopped when the test
+        finishes, to make sure that any test which fails will terminate
+        cleanly.
+
+        @return: a L{Deferred} that fires with a L{CommonDataStore}
+        """
+        try:
+            attachmentRoot.createDirectory()
+        except OSError:
+            pass
+        cp = ConnectionPool(self.sharedService.produceConnection)
+        store = CommonDataStore(
+            cp.connection, notifierFactory, attachmentRoot
+        )
+        currentTestID = testCase.id()
+        store.label = currentTestID
+        cp.startService()
+        testCase.addCleanup(cp.stopService)
+        yield self.cleanStore(testCase, store)
+        returnValue(store)
+
+
+    @inlineCallbacks
+    def cleanStore(self, testCase, storeToClean):
+        cleanupTxn = storeToClean.sqlTxnFactory(
             "%s schema-cleanup" % (testCase.id(),)
         )
         # TODO: should be getting these tables from a declaration of the schema
@@ -232,7 +242,9 @@
         if self.savedStore is None:
             self.savedStore = self.storeUnderTest()
         self.counter += 1
-        txn = self.lastTransaction = self.savedStore.newTransaction(self.id() + " #" + str(self.counter))
+        txn = self.lastTransaction = self.savedStore.newTransaction(
+            self.id() + " #" + str(self.counter)
+        )
         return txn
 
 
@@ -255,11 +267,14 @@
         self.lastTransaction = None
         return result
 
+
     def setUp(self):
         self.counter = 0
         self.notifierFactory = StubNotifierFactory()
+        self.addCleanup(self.commitLast)
 
-    def tearDown(self):
+
+    def commitLast(self):
         if self.lastTransaction is not None:
             return self.commit()
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20101101/2f68971c/attachment-0001.html>


More information about the calendarserver-changes mailing list