[CMF-checkins] CVS: CMF/CMFCore/tests - test_CachingPolicyManager.py:1.6

Tres Seaver tseaver@zope.com
Thu, 4 Apr 2002 17:44:43 -0500


Update of /cvs-repository/CMF/CMFCore/tests
In directory cvs.zope.org:/tmp/cvs-serv8324/CMFCore/tests

Modified Files:
	test_CachingPolicyManager.py 
Log Message:
 - More tweaking to get rid of races.

=== CMF/CMFCore/tests/test_CachingPolicyManager.py 1.5 => 1.6 ===
     __allow_access_to_unprotected_subobjects__ = 1
 
-    def __init__(self, EPOCH):
-        self.modified = EPOCH
+    def __init__(self, modified ):
+        self.modified = modified 
 
     def Type( self ):
         return 'Dummy'
@@ -23,7 +23,7 @@
 class CachingPolicyTests( unittest.TestCase ):
 
     def setUp(self):
-        self.EPOCH = DateTime( '1970/01/01' )
+        self._epoch = DateTime( '1970/01/01' )
 
     def _makePolicy( self, policy_id, **kw ):
 
@@ -34,8 +34,8 @@
 
         from Products.CMFCore.CachingPolicyManager import createCPContext
         from Products.CMFCore.CachingPolicyManager import createCPContext
-        return createCPContext( DummyContent(self.EPOCH)
-                              , 'foo_view', kw, self.EPOCH )
+        return createCPContext( DummyContent(self._epoch)
+                              , 'foo_view', kw, self._epoch )
         
     def test_interface( self ):
         from Products.CMFCore.interfaces.CachingPolicyManager \
@@ -53,8 +53,9 @@
         headers = policy.getHeaders( context )
 
         self.assertEqual( len( headers ), 1 )
-        self.assertEqual( headers[0][0] , 'Last-modified' )
-        self.assertEqual( headers[0][1] , rfc1123_date(self.EPOCH.timeTime()) )
+        self.assertEqual( headers[0][0], 'Last-modified' )
+        self.assertEqual( headers[0][1]
+                        , rfc1123_date(self._epoch.timeTime()) )
 
     def test_noPassPredicate( self ):
 
@@ -73,12 +74,12 @@
 
         self.assertEqual( len( headers ), 1 )
         self.assertEqual( headers[0][0] , 'Last-modified' )
-        self.assertEqual( headers[0][1] , rfc1123_date(self.EPOCH.timeTime()) )
+        self.assertEqual( headers[0][1] , rfc1123_date(self._epoch.timeTime()) )
 
     def test_typePredicateMiss( self ):
 
         policy = self._makePolicy( 'typePredicate'
-                           , predicate='python:content.Type() == "Foolish"' )
+                        , predicate='python:content.Type() == "Foolish"' )
         context = self._makeContext()
         headers = policy.getHeaders( context )
 
@@ -87,13 +88,14 @@
     def test_viewPredicate( self ):
 
         policy = self._makePolicy( 'viewPredicate'
-                           , predicate='python:view == "foo_view"' )
+                                 , predicate='python:view == "foo_view"' )
         context = self._makeContext()
         headers = policy.getHeaders( context )
 
         self.assertEqual( len( headers ), 1 )
-        self.assertEqual( headers[0][0] , 'Last-modified' )
-        self.assertEqual( headers[0][1] , rfc1123_date(self.EPOCH.timeTime()) )
+        self.assertEqual( headers[0][0], 'Last-modified' )
+        self.assertEqual( headers[0][1]
+                        , rfc1123_date(self._epoch.timeTime()) )
 
     def test_viewPredicateMiss( self ):
 
@@ -107,18 +109,19 @@
     def test_kwPredicate( self ):
 
         policy = self._makePolicy( 'kwPredicate'
-                           , predicate='python:"foo" in keywords.keys()' )
+                                 , predicate='python:"foo" in keywords.keys()' )
         context = self._makeContext( foo=1 )
         headers = policy.getHeaders( context )
 
         self.assertEqual( len( headers ), 1 )
-        self.assertEqual( headers[0][0] , 'Last-modified' )
-        self.assertEqual( headers[0][1] , rfc1123_date(self.EPOCH.timeTime()) )
+        self.assertEqual( headers[0][0], 'Last-modified' )
+        self.assertEqual( headers[0][1]
+                        , rfc1123_date(self._epoch.timeTime()) )
 
     def test_kwPredicateMiss( self ):
 
         policy = self._makePolicy( 'kwPredicateMiss'
-                           , predicate='python:"foo" in keywords.keys()' )
+                                 , predicate='python:"foo" in keywords.keys()' )
         context = self._makeContext( bar=1 )
         headers = policy.getHeaders( context )
 
@@ -131,13 +134,15 @@
         
     def test_mtimeFunc( self ):
 
-        policy = self._makePolicy( 'mtimeFunc', mtime_func='string:2001/01/01' )
+        policy = self._makePolicy( 'mtimeFunc'
+                                 , mtime_func='string:2001/01/01' )
         context = self._makeContext()
         headers = policy.getHeaders( context )
 
         self.assertEqual( len( headers ), 1 )
-        self.assertEqual( headers[0][0] , 'Last-modified' )
-        self.assertEqual( headers[0][1] , rfc1123_date(ACCLARK.timeTime()) )
+        self.assertEqual( headers[0][0], 'Last-modified' )
+        self.assertEqual( headers[0][1]
+                        , rfc1123_date(ACCLARK.timeTime()) )
         
     def test_mtimeFuncNone( self ):
 
@@ -157,10 +162,10 @@
         self.assertEqual( len( headers ), 3 )
         self.assertEqual( headers[0][0].lower() , 'last-modified' )
         self.assertEqual( headers[0][1]
-                        , rfc1123_date(self.EPOCH.timeTime()) )
+                        , rfc1123_date(self._epoch.timeTime()) )
         self.assertEqual( headers[1][0].lower() , 'expires' )
         self.assertEqual( headers[1][1]
-                        , rfc1123_date((self.EPOCH+1).timeTime()) )
+                        , rfc1123_date((self._epoch+1).timeTime()) )
         self.assertEqual( headers[2][0].lower() , 'cache-control' )
         self.assertEqual( headers[2][1] , 'max-age=86400' )
         
@@ -172,7 +177,8 @@
 
         self.assertEqual( len( headers ), 2 )
         self.assertEqual( headers[0][0].lower() , 'last-modified' )
-        self.assertEqual( headers[0][1] , rfc1123_date(self.EPOCH.timeTime()) )
+        self.assertEqual( headers[0][1]
+                        , rfc1123_date(self._epoch.timeTime()) )
         self.assertEqual( headers[1][0].lower() , 'cache-control' )
         self.assertEqual( headers[1][1] , 'no-cache' )
         
@@ -184,7 +190,8 @@
 
         self.assertEqual( len( headers ), 2 )
         self.assertEqual( headers[0][0].lower() , 'last-modified' )
-        self.assertEqual( headers[0][1] , rfc1123_date(self.EPOCH.timeTime()) )
+        self.assertEqual( headers[0][1]
+                        , rfc1123_date(self._epoch.timeTime()) )
         self.assertEqual( headers[1][0].lower() , 'cache-control' )
         self.assertEqual( headers[1][1] , 'no-store' )
         
@@ -196,7 +203,8 @@
 
         self.assertEqual( len( headers ), 2 )
         self.assertEqual( headers[0][0].lower() , 'last-modified' )
-        self.assertEqual( headers[0][1] , rfc1123_date(self.EPOCH.timeTime()) )
+        self.assertEqual( headers[0][1]
+                        , rfc1123_date(self._epoch.timeTime()) )
         self.assertEqual( headers[1][0].lower() , 'cache-control' )
         self.assertEqual( headers[1][1] , 'must-revalidate' )
         
@@ -208,7 +216,8 @@
 
         self.assertEqual( len( headers ), 2 )
         self.assertEqual( headers[0][0].lower() , 'last-modified' )
-        self.assertEqual( headers[0][1] , rfc1123_date(self.EPOCH.timeTime()) )
+        self.assertEqual( headers[0][1]
+                        , rfc1123_date(self._epoch.timeTime()) )
         self.assertEqual( headers[1][0].lower() , 'cache-control' )
         self.assertEqual( headers[1][1] , 'no-cache, no-store' )
 
@@ -216,11 +225,16 @@
 class CachingPolicyManagerTests( unittest.TestCase ):
 
     def setUp(self):
-        self.EPOCH = DateTime()
+
+        self._epoch = DateTime()
+
     def _makeOne( self ):
         from Products.CMFCore.CachingPolicyManager import CachingPolicyManager
         return CachingPolicyManager()
 
+    def assertEqualDelta( self, lhs, rhs, delta ):
+        self.failUnless( abs( lhs - rhs ) <= delta )
+
     def test_interface( self ):
         from Products.CMFCore.CachingPolicyManager import CachingPolicyManager
         from Products.CMFCore.interfaces.CachingPolicyManager \
@@ -236,7 +250,7 @@
         mgr = self._makeOne()
 
         self.assertEqual( len( mgr.listPolicies() ), 0 )
-        headers = mgr.getHTTPCachingHeaders( content=DummyContent(self.EPOCH)
+        headers = mgr.getHTTPCachingHeaders( content=DummyContent(self._epoch)
                                            , view_method='foo_view'
                                            , keywords={}
                                            )
@@ -251,17 +265,19 @@
 
         mgr = self._makeOne()
         mgr._addPolicy( 'first', 'python:1', None, 0, 0, 0, 0 )
-        headers = mgr.getHTTPCachingHeaders( content=DummyContent(self.EPOCH)
+        headers = mgr.getHTTPCachingHeaders( content=DummyContent(self._epoch)
                                            , view_method='foo_view'
                                            , keywords={}
                                            )
         self.assertEqual( len( headers ), 3 )
         self.assertEqual( headers[0][0].lower() , 'last-modified' )
-        self.assertEqual( headers[0][1] , rfc1123_date(self.EPOCH.timeTime()) )
+        self.assertEqual( headers[0][1]
+                        , rfc1123_date(self._epoch.timeTime()) )
         self.assertEqual( headers[1][0].lower() , 'expires' )
-        self.assertEqual( headers[1][1] , rfc1123_date(self.EPOCH.timeTime()) )
+        self.assertEqual( headers[1][1]
+                        , rfc1123_date(self._epoch.timeTime()) )
         self.assertEqual( headers[2][0].lower() , 'cache-control' )
-        self.assertEqual( headers[2][1] , 'max-age=0' )
+        self.assertEqual( headers[2][1], 'max-age=0' )
 
     def test_reorder( self ):
 
@@ -302,7 +318,7 @@
     def test_lookupNoMatch( self ):
 
         mgr = self._makeOneWithPolicies()
-        headers = mgr.getHTTPCachingHeaders( content=DummyContent(self.EPOCH)
+        headers = mgr.getHTTPCachingHeaders( content=DummyContent(self._epoch)
                                            , view_method='foo_view'
                                            , keywords={}
                                            )
@@ -311,43 +327,51 @@
     def test_lookupMatchFoo( self ):
 
         mgr = self._makeOneWithPolicies()
-        headers = mgr.getHTTPCachingHeaders( content=DummyContent(self.EPOCH)
+        headers = mgr.getHTTPCachingHeaders( content=DummyContent(self._epoch)
                                            , view_method='foo_view'
                                            , keywords={ 'foo' : 1 }
                                            )
         self.assertEqual( len( headers ), 1 )
-        self.assertEqual( headers[0][0].lower() , 'last-modified' )
-        self.assertEqual( headers[0][1] , rfc1123_date(self.EPOCH.timeTime()) )
+        self.assertEqual( headers[0][0].lower(), 'last-modified' )
+        self.assertEqual( headers[0][1]
+                        , rfc1123_date(self._epoch.timeTime()) )
 
 
     def test_lookupMatchBar( self ):
 
         mgr = self._makeOneWithPolicies()
-        headers = mgr.getHTTPCachingHeaders( content=DummyContent(self.EPOCH)
+        headers = mgr.getHTTPCachingHeaders( content=DummyContent(self._epoch)
                                            , view_method='foo_view'
                                            , keywords={ 'bar' : 1 }
                                            )
         self.assertEqual( len( headers ), 3 )
         self.assertEqual( headers[0][0].lower() , 'last-modified' )
-        self.assertEqual( headers[0][1] , rfc1123_date(self.EPOCH.timeTime()) )
+        self.assertEqual( headers[0][1]
+                        , rfc1123_date(self._epoch.timeTime()) )
         self.assertEqual( headers[1][0].lower() , 'expires' )
-        self.assertEqual( headers[1][1] , rfc1123_date(self.EPOCH.timeTime()) )
+        self.assertEqual( headers[1][1]
+                        , rfc1123_date(self._epoch.timeTime()) )
         self.assertEqual( headers[2][0].lower() , 'cache-control' )
-        self.assertEqual( headers[2][1] , 'max-age=0' )
+        self.assertEqual( headers[2][1], 'max-age=0' )
 
 
     def test_lookupMatchBaz( self ):
 
         mgr = self._makeOneWithPolicies()
-        headers = mgr.getHTTPCachingHeaders( content=DummyContent(self.EPOCH)
+        headers = mgr.getHTTPCachingHeaders( content=DummyContent(self._epoch)
                                            , view_method='foo_view'
                                            , keywords={ 'baz' : 1 }
                                            )
         self.assertEqual( len( headers ), 3 )
         self.assertEqual( headers[0][0].lower() , 'last-modified' )
-        self.assertEqual( headers[0][1] , rfc1123_date(self.EPOCH.timeTime()) )
+        self.assertEqual( headers[0][1]
+                        , rfc1123_date(self._epoch.timeTime()) )
         self.assertEqual( headers[1][0].lower() , 'expires' )
-        self.assertEqual( headers[1][1] , rfc1123_date((self.EPOCH+(1.0/24.0)).timeTime()) )
+
+        exp_time = DateTime( headers[1][1] )
+        target = self._epoch + ( 1.0 / 24.0 )
+        self.assertEqualDelta( exp_time, target, 0.01 )
+
         self.assertEqual( headers[2][0].lower() , 'cache-control' )
         self.assertEqual( headers[2][1] , 'max-age=3600' )
 
@@ -355,15 +379,20 @@
     def test_lookupMatchQux( self ):
 
         mgr = self._makeOneWithPolicies()
-        headers = mgr.getHTTPCachingHeaders( content=DummyContent(self.EPOCH)
+        headers = mgr.getHTTPCachingHeaders( content=DummyContent(self._epoch)
                                            , view_method='foo_view'
                                            , keywords={ 'qux' : 1 }
                                            )
         self.assertEqual( len( headers ), 3 )
         self.assertEqual( headers[0][0].lower() , 'last-modified' )
-        self.assertEqual( headers[0][1] , rfc1123_date(self.EPOCH.timeTime()) )
+        self.assertEqual( headers[0][1]
+                        , rfc1123_date(self._epoch.timeTime()) )
         self.assertEqual( headers[1][0].lower() , 'expires' )
-        self.assertEqual( headers[1][1] , rfc1123_date((self.EPOCH+1).timeTime()) )
+
+        exp_time = DateTime( headers[1][1] )
+        target = self._epoch + 1.0
+        self.assertEqualDelta( exp_time, target, 0.01 )
+
         self.assertEqual( headers[2][0].lower() , 'cache-control' )
         self.assertEqual( headers[2][1] , 'max-age=86400' )