[Zope-Checkins] SVN: Zope/trunk/src/Products/ZCatalog/plan.py Expand test isolation to cover all global state, include default map functionality inspired by experimental.catalogqueryplan

Hanno Schlichting hannosch at hannosch.eu
Mon Aug 2 15:13:04 EDT 2010


Log message for revision 115383:
  Expand test isolation to cover all global state, include default map functionality inspired by experimental.catalogqueryplan
  

Changed:
  U   Zope/trunk/src/Products/ZCatalog/plan.py

-=-
Modified: Zope/trunk/src/Products/ZCatalog/plan.py
===================================================================
--- Zope/trunk/src/Products/ZCatalog/plan.py	2010-08-02 19:06:42 UTC (rev 115382)
+++ Zope/trunk/src/Products/ZCatalog/plan.py	2010-08-02 19:13:04 UTC (rev 115383)
@@ -13,11 +13,14 @@
 
 import time
 from collections import namedtuple
+from logging import getLogger
+from os import environ
 from thread import allocate_lock
 
 from Acquisition import aq_base
 from Acquisition import aq_parent
 from Products.PluginIndexes.interfaces import IUniqueValueIndex
+from zope.dottedname.resolve import resolve
 
 MAX_DISTINCT_VALUES = 10
 REFRESH_RATE = 100
@@ -29,7 +32,9 @@
 RecentQuery = namedtuple('RecentQuery', ['duration', 'details'])
 Report = namedtuple('Report', ['hits', 'duration', 'last'])
 
+logger = getLogger('Products.ZCatalog')
 
+
 class PriorityMap(object):
     """This holds a query key to Benchmark mapping."""
 
@@ -45,7 +50,28 @@
         with cls.lock:
             cls.value[key] = value
 
+    @classmethod
+    def clear(cls):
+        with cls.lock:
+            cls.value = {}
 
+    @classmethod
+    def load_default(cls):
+        location = environ.get('ZCATALOGQUERYPLAN')
+        if location:
+            try:
+                pmap = resolve(location)
+                logger.info('loaded priority %d map(s) from %s',
+                    len(pmap), location)
+                with cls.lock:
+                    cls.value = pmap.copy()
+            except ImportError:
+                logger.warning('could not load priority map from %s', location)
+
+# Load a default map
+PriorityMap.load_default()
+
+
 class Reports(object):
     """This holds a structure of nested dicts.
 
@@ -70,8 +96,9 @@
             cls.value[key] = value
 
     @classmethod
-    def clear(cls, key):
-        cls.set(key, {})
+    def clear(cls):
+        with cls.lock:
+            cls.value = {}
 
     @classmethod
     def get_entry(cls, key, key2):
@@ -88,7 +115,11 @@
         with cls.lock:
             outer[key2] = value
 
+    @classmethod
+    def clear_entry(cls, key):
+        cls.set(key, {})
 
+
 class ValueIndexes(object):
     """Holds a set of index names considered to have an uneven value
     distribution.
@@ -140,11 +171,7 @@
         cls.set(frozenset(value_indexes))
         return value_indexes
 
-from zope.testing.cleanup import addCleanUp
-addCleanUp(ValueIndexes.clear)
-del addCleanUp
 
-
 def make_key(catalog, query):
     if not query:
         return None
@@ -276,7 +303,7 @@
             Reports.set_entry(self.cid, key, Report(1, total, recent))
 
     def reset(self):
-        Reports.clear(self.cid)
+        Reports.clear_entry(self.cid)
 
     def report(self):
         """Returns a statistic report of catalog queries as list of dicts.
@@ -299,3 +326,11 @@
             rval.append(info)
 
         return rval
+
+
+# Make sure we provide test isolation
+from zope.testing.cleanup import addCleanUp
+addCleanUp(PriorityMap.clear)
+addCleanUp(Reports.clear)
+addCleanUp(ValueIndexes.clear)
+del addCleanUp



More information about the Zope-Checkins mailing list