[z3-checkins] r25074 - in z3/sqlos/trunk/src/sqlos: . ftests

jinty at codespeak.net jinty at codespeak.net
Tue Mar 28 12:25:20 CEST 2006


Author: jinty
Date: Tue Mar 28 12:25:12 2006
New Revision: 25074

Modified:
   z3/sqlos/trunk/src/sqlos/_transaction.py
   z3/sqlos/trunk/src/sqlos/ftests/test_transaction.py
Log:
The sych to clear the cache was not registered in all threads. A test and bugfix.

Modified: z3/sqlos/trunk/src/sqlos/_transaction.py
==============================================================================
--- z3/sqlos/trunk/src/sqlos/_transaction.py	(original)
+++ z3/sqlos/trunk/src/sqlos/_transaction.py	Tue Mar 28 12:25:12 2006
@@ -33,6 +33,9 @@
     if c is None:
         cache.cacheset = CacheSet()
         c = cache.cacheset
+        cache.synch = CacheSynchronizer()   # we need to keep a solid reference, because the
+                                            # transaction manager only keeps weak ones
+        transaction.manager.registerSynch(cache.synch)
     return c
 
 class CacheSynchronizer:
@@ -60,10 +63,6 @@
             cache.clear() # Blech, not optimal, don't know what is.
                           # expireAll, doesn't expire the objects.
 
-_synch = CacheSynchronizer() # we need to keep a solid reference, because the
-                             # transaction manager only keeps weak ones
-transaction.manager.registerSynch(_synch)
-
 def beforeCommitHook(obj):
     """Called before transactions are started.
 

Modified: z3/sqlos/trunk/src/sqlos/ftests/test_transaction.py
==============================================================================
--- z3/sqlos/trunk/src/sqlos/ftests/test_transaction.py	(original)
+++ z3/sqlos/trunk/src/sqlos/ftests/test_transaction.py	Tue Mar 28 12:25:12 2006
@@ -9,9 +9,11 @@
 """
 $Id$
 """
+
 import unittest
-from transaction import get, begin
+import threading
 
+from transaction import get, begin
 from zope.app.testing.functional import BrowserTestCase
 from zope.app import zapi
 from zope.app.rdb.interfaces import IZopeDatabaseAdapter
@@ -112,7 +114,6 @@
             person1.fullname = 'Another Person'
             person1.username = 'notsidnei'
             person1.password = 'nottest'
-        import threading
         thread = threading.Thread(target=changePerson)
         thread.start()
         thread.join()
@@ -121,6 +122,13 @@
         self.assertEqual(person2.password, 'test')
 
     def testCleanCacheOnOverTransaction(self):
+        """Test that the cache is being cleared on every transaction.
+
+        We test this in a different thread to the main one to make sure that
+        whatever needs to happen to clean out the cache over transactions will
+        happen even if the thread is not the main thread. (Just in case some
+        dodo only registers the cache clearer in the main thread.)
+        """
         ut = zapi.getUtility(IConnectionName)
         adapter = zapi.queryUtility(IZopeDatabaseAdapter, ut.name)
         if adapter.getDSN() == 'dbi://:memory:':
@@ -128,32 +136,39 @@
             warnings.warn('Warning, not testing Cache Isolation')
             return # this test is NOT going to work against the default test
                    # database
-        # warm up the cache by getting an instance
-        person2 = SamplePerson.get(self.personid)
-        self.assertEqual(person2.fullname, 'Sidnei da Silva')
-        self.assertEqual(person2.username, 'sidnei')
-        self.assertEqual(person2.password, 'test')
-        # comit this transaction "just in case"
-        get().commit()
-        # get change and commit the person in another thread
-        def changePerson():
-            person1 = SamplePerson.get(self.personid)
-            person1.fullname = 'Another Person'
-            person1.username = 'notsidnei'
-            person1.password = 'nottest'
+        log = {}
+        def testCache():
+            # warm up the cache by getting an instance
+            person2 = SamplePerson.get(self.personid)
+            self.assertEqual(person2.fullname, 'Sidnei da Silva')
+            self.assertEqual(person2.username, 'sidnei')
+            self.assertEqual(person2.password, 'test')
+            # comit this transaction "just in case"
             get().commit()
-        import threading
-        thread = threading.Thread(target=changePerson)
+            # get change and commit the person in another thread
+            def changePerson():
+                person1 = SamplePerson.get(self.personid)
+                person1.fullname = 'Another Person'
+                person1.username = 'notsidnei'
+                person1.password = 'nottest'
+                get().commit()
+            thread = threading.Thread(target=changePerson)
+            thread.start()
+            thread.join()
+            # start a new transaction in this thread
+            get().commit()
+            begin()
+            # If we get a new person, we should see the changed person
+            person3 = SamplePerson.get(self.personid)
+            log['fullname'] = person3.fullname
+            log['username'] = person3.username
+            log['password'] = person3.password
+        thread = threading.Thread(target=testCache)
         thread.start()
         thread.join()
-        # start a new transaction in this thread
-        get().commit()
-        begin()
-        # If we get a new person, we should see the changed person
-        person3 = SamplePerson.get(self.personid)
-        self.assertEqual(person3.fullname, 'Another Person')
-        self.assertEqual(person3.username, 'notsidnei')
-        self.assertEqual(person3.password, 'nottest')
+        self.assertEqual(log['fullname'], 'Another Person')
+        self.assertEqual(log['username'], 'notsidnei')
+        self.assertEqual(log['password'], 'nottest')
 
     def tearDown(self):
         person = SamplePerson.get(self.personid)


More information about the z3-checkins mailing list