Changeset - ee88c8c07111
[Not reviewed]
default
0 4 0
Thomas De Schampheleire - 9 years ago 2016-08-19 20:50:26
thomas.de.schampheleire@gmail.com
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.
4 files changed with 23 insertions and 8 deletions:
0 comments (0 inline, 0 general)
kallithea/tests/__init__.py
Show inline comments
 
@@ -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
kallithea/tests/fixture.py
Show inline comments
 
@@ -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()
kallithea/tests/functional/test_admin_permissions.py
Show inline comments
 
@@ -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')
kallithea/tests/other/manual_test_vcs_operations.py
Show inline comments
 
@@ -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)
 

	
0 comments (0 inline, 0 general)