[Zope3-checkins] CVS: ZODB4/src/zodb/zeo/tests - test_cache.py:1.3.30.1
Jeremy Hylton
jeremy@zope.com
Wed, 18 Jun 2003 16:55:09 -0400
Update of /cvs-repository/ZODB4/src/zodb/zeo/tests
In directory cvs.zope.org:/tmp/cvs-serv28292/tests
Modified Files:
Tag: ZODB3-2-merge
test_cache.py
Log Message:
Backport the new zeo cache code with some small improvements.
=== ZODB4/src/zodb/zeo/tests/test_cache.py 1.3 => 1.3.30.1 ===
--- ZODB4/src/zodb/zeo/tests/test_cache.py:1.3 Tue Feb 25 13:55:05 2003
+++ ZODB4/src/zodb/zeo/tests/test_cache.py Wed Jun 18 16:54:39 2003
@@ -26,6 +26,10 @@
class ClientCacheTests(unittest.TestCase):
+ _oid = 'abcdefgh'
+ _oid2 = 'bcdefghi'
+ _oid3 = 'cdefghij'
+
def setUp(self):
unittest.TestCase.setUp(self)
self.cachesize = 10*1000*1000
@@ -41,94 +45,87 @@
def testStoreLoad(self):
cache = self.cache
- oid = 'abcdefgh'
data = '1234'*100
serial = 'ABCDEFGH'
- cache.store(oid, data, serial, '', '', '')
- loaded = cache.load(oid, '')
+ cache.store(self._oid, data, serial, '', '', '')
+ loaded = cache.load(self._oid, '')
self.assertEqual(loaded, (data, serial))
def testMissingLoad(self):
cache = self.cache
- oid = 'abcdefgh'
data = '1234'*100
serial = 'ABCDEFGH'
- cache.store(oid, data, serial, '', '', '')
+ cache.store(self._oid, data, serial, '', '', '')
loaded = cache.load('garbage1', '')
self.assertEqual(loaded, None)
def testInvalidate(self):
cache = self.cache
- oid = 'abcdefgh'
data = '1234'*100
serial = 'ABCDEFGH'
- cache.store(oid, data, serial, '', '', '')
- loaded = cache.load(oid, '')
+ cache.store(self._oid, data, serial, '', '', '')
+ loaded = cache.load(self._oid, '')
self.assertEqual(loaded, (data, serial))
- cache.invalidate(oid, '')
- loaded = cache.load(oid, '')
+ cache.invalidate(self._oid, '')
+ loaded = cache.load(self._oid, '')
self.assertEqual(loaded, None)
def testVersion(self):
cache = self.cache
- oid = 'abcdefgh'
data = '1234'*100
serial = 'ABCDEFGH'
vname = 'myversion'
vdata = '5678'*200
vserial = 'IJKLMNOP'
- cache.store(oid, data, serial, vname, vdata, vserial)
- loaded = cache.load(oid, '')
+ cache.store(self._oid, data, serial, vname, vdata, vserial)
+ loaded = cache.load(self._oid, '')
self.assertEqual(loaded, (data, serial))
- vloaded = cache.load(oid, vname)
+ vloaded = cache.load(self._oid, vname)
self.assertEqual(vloaded, (vdata, vserial))
def testVersionOnly(self):
cache = self.cache
- oid = 'abcdefgh'
data = ''
serial = ''
vname = 'myversion'
vdata = '5678'*200
vserial = 'IJKLMNOP'
- cache.store(oid, data, serial, vname, vdata, vserial)
- loaded = cache.load(oid, '')
+ cache.store(self._oid, data, serial, vname, vdata, vserial)
+ loaded = cache.load(self._oid, '')
self.assertEqual(loaded, None)
- vloaded = cache.load(oid, vname)
+ vloaded = cache.load(self._oid, vname)
self.assertEqual(vloaded, (vdata, vserial))
def testInvalidateNonVersion(self):
cache = self.cache
- oid = 'abcdefgh'
data = '1234'*100
serial = 'ABCDEFGH'
vname = 'myversion'
vdata = '5678'*200
vserial = 'IJKLMNOP'
- cache.store(oid, data, serial, vname, vdata, vserial)
- loaded = cache.load(oid, '')
+ cache.store(self._oid, data, serial, vname, vdata, vserial)
+ loaded = cache.load(self._oid, '')
self.assertEqual(loaded, (data, serial))
- vloaded = cache.load(oid, vname)
+ vloaded = cache.load(self._oid, vname)
self.assertEqual(vloaded, (vdata, vserial))
- cache.invalidate(oid, '')
- loaded = cache.load(oid, '')
+ cache.invalidate(self._oid, '')
+ loaded = cache.load(self._oid, '')
self.assertEqual(loaded, None)
# The version data is also invalidated at this point
- vloaded = cache.load(oid, vname)
+ vloaded = cache.load(self._oid, vname)
self.assertEqual(vloaded, None)
def testInvalidateVersion(self):
# Invalidating a version should not invalidate the non-version data.
# (This tests for the same bug as testInvalidatePersists below.)
cache = self.cache
- oid = 'abcdefgh'
data = '1234'*100
serial = 'ABCDEFGH'
- cache.store(oid, data, serial, '', '', '')
- loaded = cache.load(oid, '')
+ cache.store(self._oid, data, serial, '', '', '')
+ loaded = cache.load(self._oid, '')
self.assertEqual(loaded, (data, serial))
- cache.invalidate(oid, 'bogus')
- loaded = cache.load(oid, '')
+ cache.invalidate(self._oid, 'bogus')
+ loaded = cache.load(self._oid, '')
self.assertEqual(loaded, (data, serial))
def testVerify(self):
@@ -138,30 +135,27 @@
results.append((oid, serial, vserial))
cache.verify(verifier)
self.assertEqual(results, [])
- oid = 'abcdefgh'
data = '1234'*100
serial = 'ABCDEFGH'
- cache.store(oid, data, serial, '', '', '')
+ cache.store(self._oid, data, serial, '', '', '')
results = []
cache.verify(verifier)
- self.assertEqual(results, [(oid, serial, None)])
+ self.assertEqual(results, [(self._oid, serial, None)])
def testCheckSize(self):
# Make sure that cache._index[oid] is erased for oids that are
# stored in the cache file that's rewritten after a flip.
cache = self.cache
- oid = 'abcdefgh'
data = '1234'*100
serial = 'ABCDEFGH'
- cache.store(oid, data, serial, '', '', '')
+ cache.store(self._oid, data, serial, '', '', '')
cache.checkSize(10*self.cachesize) # Force a file flip
- oid2 = 'abcdefgz'
data2 = '1234'*10
serial2 = 'ABCDEFGZ'
- cache.store(oid2, data2, serial2, '', '', '')
+ cache.store(self._oid2, data2, serial2, '', '', '')
cache.checkSize(10*self.cachesize) # Force another file flip
- self.assertNotEqual(cache._index.get(oid2), None)
- self.assertEqual(cache._index.get(oid), None)
+ self.assertNotEqual(cache._index.get(self._oid2), None)
+ self.assertEqual(cache._index.get(self._oid), None)
def testCopyToCurrent(self):
# - write some objects to cache file 0
@@ -177,91 +171,105 @@
cache = self.cache
# Create some objects
- oid1 = 'abcdefgh'
data1 = '1234' * 100
serial1 = 'ABCDEFGH'
- oid2 = 'bcdefghi'
data2 = '2345' * 200
serial2 = 'BCDEFGHI'
version2 = 'myversion'
nonversion = 'nada'
vdata2 = '5432' * 250
vserial2 = 'IHGFEDCB'
- oid3 = 'cdefghij'
data3 = '3456' * 300
serial3 = 'CDEFGHIJ'
# Store them in the cache
- cache.store(oid1, data1, serial1, '', '', '')
- cache.store(oid2, data2, serial2, version2, vdata2, vserial2)
- cache.store(oid3, data3, serial3, '', '', '')
+ cache.store(self._oid, data1, serial1, '', '', '')
+ cache.store(self._oid2, data2, serial2, version2, vdata2, vserial2)
+ cache.store(self._oid3, data3, serial3, '', '', '')
# Verify that they are in file 0
- self.assert_(None is not cache._index.get(oid1) > 0)
- self.assert_(None is not cache._index.get(oid2) > 0)
- self.assert_(None is not cache._index.get(oid3) > 0)
+ self.assert_(None is not cache._index.get(self._oid) > 0)
+ self.assert_(None is not cache._index.get(self._oid2) > 0)
+ self.assert_(None is not cache._index.get(self._oid3) > 0)
# Load them and verify that the loads return correct data
- self.assertEqual(cache.load(oid1, ''), (data1, serial1))
- self.assertEqual(cache.load(oid2, ''), (data2, serial2))
- self.assertEqual(cache.load(oid2, nonversion), (data2, serial2))
- self.assertEqual(cache.load(oid2, version2), (vdata2, vserial2))
- self.assertEqual(cache.load(oid3, ''), (data3, serial3))
+ self.assertEqual(cache.load(self._oid, ''), (data1, serial1))
+ self.assertEqual(cache.load(self._oid2, ''), (data2, serial2))
+ self.assertEqual(cache.load(self._oid2, nonversion), (data2, serial2))
+ self.assertEqual(cache.load(self._oid2, version2), (vdata2, vserial2))
+ self.assertEqual(cache.load(self._oid3, ''), (data3, serial3))
# Verify that they are still in file 0
- self.assert_(None is not cache._index.get(oid1) > 0)
- self.assert_(None is not cache._index.get(oid2) > 0)
- self.assert_(None is not cache._index.get(oid3) > 0)
+ self.assert_(None is not cache._index.get(self._oid) > 0)
+ self.assert_(None is not cache._index.get(self._oid2) > 0)
+ self.assert_(None is not cache._index.get(self._oid3) > 0)
# Cause a cache flip
cache.checkSize(10*self.cachesize)
# Load o1, o2, o4 again and verify that the loads return correct data
- self.assertEqual(cache.load(oid1, ''), (data1, serial1))
- self.assertEqual(cache.load(oid2, version2), (vdata2, vserial2))
- self.assertEqual(cache.load(oid2, nonversion), (data2, serial2))
- self.assertEqual(cache.load(oid2, ''), (data2, serial2))
+ self.assertEqual(cache.load(self._oid, ''), (data1, serial1))
+ self.assertEqual(cache.load(self._oid2, version2), (vdata2, vserial2))
+ self.assertEqual(cache.load(self._oid2, nonversion), (data2, serial2))
+ self.assertEqual(cache.load(self._oid2, ''), (data2, serial2))
# Verify that o1, o2, 04 are now in file 1, o3 still in file 0
- self.assert_(None is not cache._index.get(oid1) < 0)
- self.assert_(None is not cache._index.get(oid2) < 0)
- self.assert_(None is not cache._index.get(oid3) > 0)
+ self.assert_(None is not cache._index.get(self._oid) < 0)
+ self.assert_(None is not cache._index.get(self._oid2) < 0)
+ self.assert_(None is not cache._index.get(self._oid3) > 0)
# Cause another cache flip
cache.checkSize(10*self.cachesize)
# Load o1 and o2 again and verify that the loads return correct data
- self.assertEqual(cache.load(oid1, ''), (data1, serial1))
- self.assertEqual(cache.load(oid2, nonversion), (data2, serial2))
- self.assertEqual(cache.load(oid2, version2), (vdata2, vserial2))
- self.assertEqual(cache.load(oid2, ''), (data2, serial2))
+ self.assertEqual(cache.load(self._oid, ''), (data1, serial1))
+ self.assertEqual(cache.load(self._oid2, nonversion), (data2, serial2))
+ self.assertEqual(cache.load(self._oid2, version2), (vdata2, vserial2))
+ self.assertEqual(cache.load(self._oid2, ''), (data2, serial2))
# Verify that o1 and o2 are now back in file 0, o3 is lost
- self.assert_(None is not cache._index.get(oid1) > 0)
- self.assert_(None is not cache._index.get(oid2) > 0)
- self.assert_(None is cache._index.get(oid3))
+ self.assert_(None is not cache._index.get(self._oid) > 0)
+ self.assert_(None is not cache._index.get(self._oid2) > 0)
+ self.assert_(None is cache._index.get(self._oid3))
# Invalidate version data for o2
- cache.invalidate(oid2, nonversion)
- self.assertEqual(cache.load(oid2, ''), (data2, serial2))
- self.assertEqual(cache.load(oid2, nonversion), None)
- self.assertEqual(cache.load(oid2, version2), None)
+ cache.invalidate(self._oid2, nonversion)
+ self.assertEqual(cache.load(self._oid2, ''), (data2, serial2))
+ self.assertEqual(cache.load(self._oid2, nonversion), None)
+ self.assertEqual(cache.load(self._oid2, version2), None)
# Cause another cache flip
cache.checkSize(10*self.cachesize)
# Load o1 and o2 again and verify that the loads return correct data
- self.assertEqual(cache.load(oid1, ''), (data1, serial1))
- self.assertEqual(cache.load(oid2, version2), None)
- self.assertEqual(cache.load(oid2, nonversion), None)
- self.assertEqual(cache.load(oid2, ''), (data2, serial2))
+ self.assertEqual(cache.load(self._oid, ''), (data1, serial1))
+ self.assertEqual(cache.load(self._oid2, version2), None)
+ self.assertEqual(cache.load(self._oid2, nonversion), None)
+ self.assertEqual(cache.load(self._oid2, ''), (data2, serial2))
# Verify that o1 and o2 are now in file 1
- self.assert_(None is not cache._index.get(oid1) < 0)
- self.assert_(None is not cache._index.get(oid2) < 0)
+ self.assert_(None is not cache._index.get(self._oid) < 0)
+ self.assert_(None is not cache._index.get(self._oid2) < 0)
+
+ def testLastTid(self):
+ cache = self.cache
+ self.failUnless(cache.getLastTid() is None)
+ ltid = 'pqrstuvw'
+ cache.setLastTid(ltid)
+ self.assertEqual(cache.getLastTid(), ltid)
+ cache.checkSize(10*self.cachesize) # Force a file flip
+ self.assertEqual(cache.getLastTid(), ltid)
+ cache.setLastTid(None)
+ self.failUnless(cache.getLastTid() is None)
+ cache.checkSize(10*self.cachesize) # Force a file flip
+ self.failUnless(cache.getLastTid() is None)
class PersistentClientCacheTests(unittest.TestCase):
+ _oid = 'abcdefgh'
+ _oid2 = 'bcdefghi'
+ _oid3 = 'cdefghij'
+
def setUp(self):
unittest.TestCase.setUp(self)
self.vardir = os.getcwd() # Don't use /tmp, it's a security risk
@@ -309,18 +317,18 @@
# 'current' file when a persistent cache was opened.
cache = self.cache
self.assertEqual(cache._current, 0) # Check that file 0 is current
- oid = 'abcdefgh'
data = '1234'
serial = 'ABCDEFGH'
- cache.store(oid, data, serial, '', '', '')
+ cache.store(self._oid, data, serial, '', '', '')
+ cache.setLastTid(serial)
cache.checkSize(10*self.cachesize) # Force a file flip
self.assertEqual(cache._current, 1) # Check that the flip worked
- oid = 'abcdefgh'
data = '123'
serial = 'ABCDEFGZ'
- cache.store(oid, data, serial, '', '', '')
+ cache.store(self._oid, data, serial, '', '', '')
+ cache.setLastTid(serial)
cache = self.reopenCache()
- loaded = cache.load(oid, '')
+ loaded = cache.load(self._oid, '')
# Check that we got the most recent data:
self.assertEqual(loaded, (data, serial))
self.assertEqual(cache._current, 1) # Double check that 1 is current
@@ -334,23 +342,50 @@
cache = self.cache
magicsize = (ord('i') + 1) << 16
cache = self.cache
- oid = 'abcdefgh'
data = '!'*magicsize
serial = 'ABCDEFGH'
- cache.store(oid, data, serial, '', '', '')
- loaded = cache.load(oid, '')
+ cache.store(self._oid, data, serial, '', '', '')
+ loaded = cache.load(self._oid, '')
self.assertEqual(loaded, (data, serial))
- cache.invalidate(oid, '')
+ cache.invalidate(self._oid, '')
cache = self.reopenCache()
- loaded = cache.load(oid, '')
+ loaded = cache.load(self._oid, '')
if loaded != None:
self.fail("invalidated data resurrected, size %d, was %d" %
(len(loaded[0]), len(data)))
+ def testPersistentLastTid(self):
+ cache = self.cache
+ self.failUnless(cache.getLastTid() is None)
+ ltid = 'pqrstuvw'
+ cache.setLastTid(ltid)
+ self.assertEqual(cache.getLastTid(), ltid)
+ data = '1234'
+ serial = 'ABCDEFGH'
+ cache.store(self._oid, data, serial, '', '', '')
+ self.assertEqual(cache.getLastTid(), ltid)
+ cache.checkSize(10*self.cachesize) # Force a file flip
+ self.assertEqual(cache.getLastTid(), ltid)
+ cache = self.reopenCache()
+ self.assertEqual(cache.getLastTid(), ltid)
+ cache.setLastTid(None)
+ self.failUnless(cache.getLastTid() is None)
+ cache.checkSize(10*self.cachesize) # Force a file flip
+ self.failUnless(cache.getLastTid() is None)
+
+class ClientCacheLongOIDTests(ClientCacheTests):
+ _oid = 'abcdefghijklmnop' * 2
+ _oid2 = 'bcdefghijklmnopq' * 2
+ _oid3 = 'cdefghijklmnopqr' * 2
+
+class PersistentClientCacheLongOIDTests(PersistentClientCacheTests):
+ _oid = 'abcdefghijklmnop' * 2
+
def test_suite():
suite = unittest.TestSuite()
- suite.addTest(unittest.makeSuite(ClientCacheTests))
- suite.addTest(unittest.makeSuite(PersistentClientCacheTests))
+ for klass in (ClientCacheTests, PersistentClientCacheTests,
+ ClientCacheLongOIDTests, PersistentClientCacheLongOIDTests):
+ suite.addTest(unittest.makeSuite(klass))
return suite
if __name__ == '__main__':