[Zope-CVS] CVS: Products/Ape/lib/apelib/tests - testscanner.py:1.1.2.3 teststorage.py:1.4.4.1

Shane Hathaway shane@zope.com
Thu, 24 Jul 2003 21:59:19 -0400


Update of /cvs-repository/Products/Ape/lib/apelib/tests
In directory cvs.zope.org:/tmp/cvs-serv10991/lib/apelib/tests

Modified Files:
      Tag: ape-scan-branch
	testscanner.py teststorage.py 
Log Message:
Closed a hole in the scanner.

Until now, if in some way a connection started using an OID without calling
storage.load(), the scanner would assume that the OID depends on no sources.
This could happen in a ZEO environment and it would result in objects not
getting scanned.  The solution was to give scanners the ability to get the
sources for an OID directly if necessary.  Also enhanced tests.

Periodic scanning now seems to be in good shape.


=== Products/Ape/lib/apelib/tests/testscanner.py 1.1.2.2 => 1.1.2.3 ===
--- Products/Ape/lib/apelib/tests/testscanner.py:1.1.2.2	Thu Jul 24 08:15:39 2003
+++ Products/Ape/lib/apelib/tests/testscanner.py	Thu Jul 24 21:58:43 2003
@@ -1,6 +1,6 @@
 ##############################################################################
 #
-# Copyright (c) 2002 Zope Corporation and Contributors.
+# Copyright (c) 2003 Zope Corporation and Contributors.
 # All Rights Reserved.
 #
 # This software is subject to the provisions of the Zope Public License,
@@ -33,10 +33,18 @@
             if str(location) != location:
                 raise AssertionError, "location is expected to be a string"
             # Always report a change
-            res[source] = time()
+            res[source] = 1001
         return res
 
 
+class FakeStorage:
+
+    repo = FakeRepository()
+
+    def getSources(self, oid):
+        return {(self.repo, oid): 10}
+
+
 class ScanControlTests(unittest.TestCase):
 
     def setUp(self):
@@ -124,6 +132,51 @@
         self.assertEqual(len(self.scanner.future), 1)
         self.assert_(self.scanner.future.has_key(5))
 
+    def testFindNewSources(self):
+        # Verify the scanner calls storage.getSources() and saves the result.
+        storage = FakeStorage()
+        self.scanner.setStorage(storage)
+        self.conn1.setOIDs([5])
+        expect_sources = storage.getSources(5)
+        self.assertEqual(self.scanner.current[5], expect_sources)
+
+    def testUseCachedSources(self):
+        # Verify the scanner uses previously cached sources when available.
+        repo = FakeRepository()
+        sources = {(repo, '999'): -1}
+        self.scanner.setSources(5, sources)
+        self.conn1.setOIDs([5])
+        self.assertEqual(self.scanner.current[5], sources)
+
+    def testUseCommittedSources(self):
+        # Verify the scanner sees sources according to transactions.
+        repo = FakeRepository()
+        sources = {(repo, '999'): -1}
+        self.scanner.setSources(5, sources)
+        self.conn1.setOIDs([5])
+        sources_2 = {(repo, '999'): -2}
+        self.scanner.setUncommittedSources(123, 5, sources_2)
+        # Uncommitted data should have no effect on sources
+        self.assertEqual(self.scanner.current[5], sources)
+        self.scanner.afterCommit(123)
+        # The scanner should have used data from the repo rather
+        # than "-2".
+        final_sources = self.scanner.current[5]
+        self.assertEqual(len(final_sources), 1)
+        self.assertEqual(final_sources.keys()[0], (repo, '999'))
+        self.assertEqual(final_sources.values()[0], 1001)
+
+    def testAbort(self):
+        # Verify the scanner ignores sources on transaction abort.
+        repo = FakeRepository()
+        sources = {(repo, '999'): -2}
+        self.scanner.setUncommittedSources(123, 5, sources)
+        self.assertEqual(self.scanner.uncommitted[123][5], sources)
+        self.scanner.afterAbort(123)
+        self.assert_(not self.scanner.uncommitted.has_key(123))
+        self.assert_(not self.scanner.current.has_key(123))
+        self.assert_(not self.scanner.future.has_key(123))
+        
 
 if __name__ == '__main__':
     unittest.main()


=== Products/Ape/lib/apelib/tests/teststorage.py 1.4 => 1.4.4.1 ===
--- Products/Ape/lib/apelib/tests/teststorage.py:1.4	Mon May 26 15:33:16 2003
+++ Products/Ape/lib/apelib/tests/teststorage.py	Thu Jul 24 21:58:44 2003
@@ -377,6 +377,14 @@
             conn1.close()
 
 
+    def testGetSources(self):
+        sources = self.storage.getSources('\0' * 8)
+        self.assertEqual(sources, {})
+        # The test passed, but check for a false positive.
+        oid = self.storage._oid_encoder.encode(('nonexistent-oid',))
+        self.assertRaises(KeyError, self.storage.getSources, oid)
+
+
 if __name__ == '__main__':
     unittest.main()