django-constance icon indicating copy to clipboard operation
django-constance copied to clipboard

override_config in multithreaded (parallel) tests execution

Open Sajam opened this issue 5 years ago • 4 comments

Our test suite that utilizes override_config decorator sometimes fails when we execute tests in parallel (value in config in the test body is different than the one defined in override_config).

My theory is that config in test_A gets overridden in the middle of the test execution by test_B that started after override_config from test_A has been applied.

Sajam avatar Sep 24 '19 07:09 Sajam

For parallel testing and overriding, have you considered using another backend that would prevent this from happening (try in-memory for example)? It would also help to know which one you are using in the tests.

SebCorbin avatar Apr 19 '20 18:04 SebCorbin

Which one would work? If they are running on the same machine in parallel, they would still grab the value from the same spot

justmobilize avatar Apr 28 '20 21:04 justmobilize

Recently I've run through a pretty similar issue. In my case using the database backend with different spots for each site it turned out it also used the in-memory cache which affected all the sites and messed up config values. For such case you can either turn the cache off

CONSTANCE_DATABASE_CACHE_BACKEND

or (like I do) set a different cache store prefix for each site

CONSTANCE_DATABASE_PREFIX

rmaciejczyk avatar Apr 29 '20 08:04 rmaciejczyk

Currently trying this (using pytest):

@pytest.fixture(scope='session', autouse=True)
def setup_constance_prefix(worker_id):
    """For parallel test-running, use non-overlapping prefixes for Constance config vars to avoid clobbering."""
    if worker_id:
        settings.CONSTANCE_REDIS_PREFIX = 'constance:{}:'.format(worker_id)

brechin avatar Jul 30 '20 18:07 brechin