diff --git a/kallithea/tests/__init__.py b/kallithea/tests/__init__.py --- a/kallithea/tests/__init__.py +++ b/kallithea/tests/__init__.py @@ -60,7 +60,7 @@ parametrize = pytest.mark.parametrize __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 @@ try: 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 diff --git a/kallithea/tests/fixture.py b/kallithea/tests/fixture.py --- a/kallithea/tests/fixture.py +++ b/kallithea/tests/fixture.py @@ -60,7 +60,7 @@ class Fixture(object): 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() diff --git a/kallithea/tests/functional/test_admin_permissions.py b/kallithea/tests/functional/test_admin_permissions.py --- a/kallithea/tests/functional/test_admin_permissions.py +++ b/kallithea/tests/functional/test_admin_permissions.py @@ -23,8 +23,9 @@ class TestAdminPermissionsController(Tes 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 @@ class TestAdminPermissionsController(Tes _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') diff --git a/kallithea/tests/other/manual_test_vcs_operations.py b/kallithea/tests/other/manual_test_vcs_operations.py --- a/kallithea/tests/other/manual_test_vcs_operations.py +++ b/kallithea/tests/other/manual_test_vcs_operations.py @@ -531,7 +531,9 @@ class TestVCSOperations(TestController): 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 @@ class TestVCSOperations(TestController): 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)