mò
Sã¸Ec @ s‚ d Z d k l Z d k l Z d k Z d k l Z d e f d „ ƒ YZ d e f d „ ƒ YZ d f d
„ ƒ YZ
d „ Z d S( s. Test behavior of Connection plus cPickleCache.( s doctest( s
PersistentN( s databaseFromStringt RecalcitrantObjectc B s2 t Z d Z d Z d „ Z d „ Z e e ƒ Z RS( s1 A Persistent object that will not become a ghost.i c C s | i i d 7_ d S( Ni ( t selft __class__t
deactivations( R ( ( t4 /data/zmath/zope/lib/python/ZODB/tests/test_cache.pyt
_p_deactivate s c C s
d | _ d S( Ni ( t clsR ( R ( ( R t init s ( t __name__t
__module__t __doc__R R R t classmethod( ( ( R R s
t
RegularObjectc B s; t Z d Z d Z d „ Z d „ Z d „ Z e e ƒ Z RS( Ni c C s) | i i d 7_ t t | ƒ i ƒ d S( Ni ( R R R t superR R ( R ( ( R R ( s c C s) | i i d 7_ t t | ƒ i ƒ d S( Ni ( R R t
invalidationsR
R t
_p_invalidate( R ( ( R R , s c C s d | _ d | _ d S( Ni ( R R R ( R ( ( R R 0 s ( R R R R R R R R ( ( ( R R # s t
CacheTestsc B s# t Z d „ Z d „ Z d „ Z RS( Nc C s d S( sÉ Test basic cache methods.
Let's start with a clean transaction
>>> transaction.abort()
>>> RegularObject.init()
>>> db = databaseFromString("\n"
... "cache-size 4\n"
... "\n"
... "")
>>> cn = db.open()
>>> r = cn.root()
>>> L = []
>>> for i in range(5):
... o = RegularObject()
... L.append(o)
... r[i] = o
>>> transaction.commit()
After committing a transaction and calling cacheGC(), there
should be cache-size (4) objects in the cache. One of the
RegularObjects was deactivated.
>>> cn._cache.ringlen()
4
>>> RegularObject.deactivations
1
If we explicitly activate the objects again, the ringlen
should go back up to 5.
>>> for o in L:
... o._p_activate()
>>> cn._cache.ringlen()
5
>>> cn.cacheGC()
>>> cn._cache.ringlen()
4
>>> RegularObject.deactivations
2
>>> cn.cacheMinimize()
>>> cn._cache.ringlen()
0
>>> RegularObject.deactivations
6
If we activate all the objects again and mark one as modified,
then the one object should not be deactivated even by a
minimize.
>>> for o in L:
... o._p_activate()
>>> o.attr = 1
>>> cn._cache.ringlen()
5
>>> cn.cacheMinimize()
>>> cn._cache.ringlen()
1
>>> RegularObject.deactivations
10
Clean up
>>> transaction.abort()
N( ( R ( ( R t
test_cache8 s Ec C s d S( sÍ Test that a cacheGC() call will return.
It's possible for a particular object to ignore the
_p_deactivate() call. We want to check several things in this
case. The cache should called the real _p_deactivate() method
not the one provided by Persistent. The cacheGC() call should
also return when it's looked at each item, regardless of whether
it became a ghost.
>>> RecalcitrantObject.init()
>>> db = databaseFromString("\n"
... "cache-size 4\n"
... "\n"
... "")
>>> cn = db.open()
>>> r = cn.root()
>>> L = []
>>> for i in range(5):
... o = RecalcitrantObject()
... L.append(o)
... r[i] = o
>>> transaction.commit()
>>> [o._p_state for o in L]
[0, 0, 0, 0, 0]
The Connection calls cacheGC() after it commits a transaction.
Since the cache will now have more objects that it's target size,
it will call _p_deactivate() on each RecalcitrantObject.
>>> RecalcitrantObject.deactivations
5
>>> [o._p_state for o in L]
[0, 0, 0, 0, 0]
An explicit call to cacheGC() has the same effect.
>>> cn.cacheGC()
>>> RecalcitrantObject.deactivations
10
>>> [o._p_state for o in L]
[0, 0, 0, 0, 0]
N( ( R ( ( R t test_cache_gc_recalcitrant s *c C s d S( sã Test that the cache handles transaction abort correctly.
>>> RegularObject.init()
>>> db = databaseFromString("\n"
... "cache-size 4\n"
... "\n"
... "")
>>> cn = db.open()
>>> r = cn.root()
>>> L = []
>>> for i in range(5):
... o = RegularObject()
... L.append(o)
... r[i] = o
>>> transaction.commit()
>>> RegularObject.deactivations
1
Modify three of the objects and verify that they are
deactivated when the transaction aborts.
>>> for i in range(0, 5, 2):
... L[i].attr = i
>>> [L[i]._p_state for i in range(0, 5, 2)]
[1, 1, 1]
>>> cn._cache.ringlen()
5
>>> transaction.abort()
>>> cn._cache.ringlen()
2
>>> RegularObject.deactivations
4
N( ( R ( ( R t test_cache_on_abort« s "( R R R R R ( ( ( R R 6 s G ,c C s
t i ƒ S( N( t doctestt DocTestSuite( ( ( R t
test_suiteÐ s ( R
t zope.testingR t
persistentt
Persistentt transactiont ZODB.configt databaseFromStringR R R R ( R R R R R R R R ( ( R t ? s
š