changeset 6140:ee88c8c07111

tests: remove sleep hack to expire sql_cache_short A number of tests sleep for 1.5 or 2 seconds to let the beaker cache 'sql_cache_short' expire. This cache is for example used for IP permissions; tests changing such permissions need to make sure they take effect before proceeding, especially when exiting from the test. A much faster method is to effectively invalidating the caches. Because it is difficult and fragile to only invalidate the relevant cache -- difficult to know exactly which cache needs to be invalidated, fragile because the string indicating the cache line is not very nice and might change in the future (in the case of IP permissions for the default user, the cache is referred to with something like "get_user_ips_default_Mapper|UserIpMap|users". This string changes when the permissions are for a different user. Clearing all caches shouldn't be a problem in a test context.
author Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
date Fri, 19 Aug 2016 20:50:26 +0200
parents 569feabd3c9d
children 1ba7e0c0d3b3
files kallithea/tests/__init__.py kallithea/tests/fixture.py kallithea/tests/functional/test_admin_permissions.py kallithea/tests/other/manual_test_vcs_operations.py
diffstat 4 files changed, 23 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/tests/__init__.py	Sun Aug 21 13:43:06 2016 +0200
+++ b/kallithea/tests/__init__.py	Fri Aug 19 20:50:26 2016 +0200
@@ -60,7 +60,7 @@
 
 __all__ = [
     'skipif', 'parametrize', 'environ', 'url', 'TestController',
-    'ldap_lib_installed', 'pam_lib_installed',
+    'ldap_lib_installed', 'pam_lib_installed', 'invalidate_all_caches',
     'TESTS_TMP_PATH', 'HG_REPO', 'GIT_REPO', 'NEW_HG_REPO', 'NEW_GIT_REPO',
     'HG_FORK', 'GIT_FORK', 'TEST_USER_ADMIN_LOGIN', 'TEST_USER_ADMIN_PASS',
     'TEST_USER_ADMIN_EMAIL', 'TEST_USER_REGULAR_LOGIN', 'TEST_USER_REGULAR_PASS',
@@ -142,6 +142,16 @@
 except ImportError:
     pam_lib_installed = False
 
+def invalidate_all_caches():
+    """Invalidate all beaker caches currently configured.
+    Useful when manipulating IP permissions in a test and changes need to take
+    effect immediately.
+    Note: Any use of this function is probably a workaround - it should be
+    replaced with a more specific cache invalidation in code or test."""
+    from beaker.cache import cache_managers
+    for cache in cache_managers.values():
+        cache.clear()
+
 class NullHandler(logging.Handler):
     def emit(self, record):
         pass
--- a/kallithea/tests/fixture.py	Sun Aug 21 13:43:06 2016 +0200
+++ b/kallithea/tests/fixture.py	Fri Aug 19 20:50:26 2016 +0200
@@ -60,7 +60,7 @@
                 anon.active = status
                 Session().add(anon)
                 Session().commit()
-                time.sleep(1.5)  # hack: wait for beaker sql_cache_short to expire
+                invalidate_all_caches()
 
             def __exit__(self, exc_type, exc_val, exc_tb):
                 anon = User.get_default_user()
--- a/kallithea/tests/functional/test_admin_permissions.py	Sun Aug 21 13:43:06 2016 +0200
+++ b/kallithea/tests/functional/test_admin_permissions.py	Fri Aug 19 20:50:26 2016 +0200
@@ -23,8 +23,9 @@
                                  params=dict(new_ip='127.0.0.0/24',
                                  _authentication_token=self.authentication_token()))
 
-        # sleep more than beaker.cache.sql_cache_short.expire to expire user cache
-        time.sleep(1.5)
+        # IP permissions are cached, need to invalidate this cache explicitly
+        invalidate_all_caches()
+
         self.app.get(url('admin_permissions_ips'), status=302)
 
         # REMOTE_ADDR must match 127.0.0.0/24
@@ -43,8 +44,8 @@
                                              _authentication_token=self.authentication_token()),
                                  extra_environ={'REMOTE_ADDR': '127.0.0.1'})
 
-        # sleep more than beaker.cache.sql_cache_short.expire to expire user cache
-        time.sleep(1.5)
+        # IP permissions are cached, need to invalidate this cache explicitly
+        invalidate_all_caches()
 
         response = self.app.get(url('admin_permissions_ips'))
         response.mustcontain('All IP addresses are allowed')
--- a/kallithea/tests/other/manual_test_vcs_operations.py	Sun Aug 21 13:43:06 2016 +0200
+++ b/kallithea/tests/other/manual_test_vcs_operations.py	Fri Aug 19 20:50:26 2016 +0200
@@ -531,7 +531,9 @@
                 UserIpMap.delete(ip.ip_id)
             Session().commit()
 
-        time.sleep(2)
+        # IP permissions are cached, need to invalidate this cache explicitly
+        invalidate_all_caches()
+
         clone_url = _construct_url(HG_REPO)
         stdout, stderr = Command(tempfile.gettempdir()).execute('hg clone', clone_url)
 
@@ -557,7 +559,9 @@
                 UserIpMap.delete(ip.ip_id)
             Session().commit()
 
-        time.sleep(2)
+        # IP permissions are cached, need to invalidate this cache explicitly
+        invalidate_all_caches()
+
         clone_url = _construct_url(GIT_REPO)
         stdout, stderr = Command(tempfile.gettempdir()).execute('git clone', clone_url)