Pytest-django: Cache + xdist

Created on 12 Oct 2017  路  4Comments  路  Source: pytest-dev/pytest-django

Has anyone had success running tests that rely on caching with the xdist plugin? I created a fixture, similar to the database fixture, to add a cache key prefix: https://github.com/edx/course-discovery/blob/ad1dca5623b765c6d85d83dcf7e5f75c7b8e1181/conftest.py#L18-L40. However, I still have a couple tests that fail when using memcached (but not when using local memory).

  1. Is there anything obvious that I am missing with my fixture?
  2. If you have had success running cache-related tests in parallel mode, how did you handle cache key collisions?
  3. Are others interested in such a fixture being contributed to this project?
question

Most helpful comment

@clintonb: Your snippet worked almost flawlessly for one of my projects until one day it didn't. I think the prefix prevents collisions without any issues, but (on redis at least) cache.clear() is flushing all cached items, not just the ones with the correct prefix, so any code that relies on the cached value being in the cache may fail if another test starts or finishes between when the cache value is set and when it's read. A local memory cache would likely be immune since flushing it wouldn't affect other processes.

All 4 comments

@clintonb: Your snippet worked almost flawlessly for one of my projects until one day it didn't. I think the prefix prevents collisions without any issues, but (on redis at least) cache.clear() is flushing all cached items, not just the ones with the correct prefix, so any code that relies on the cached value being in the cache may fail if another test starts or finishes between when the cache value is set and when it's read. A local memory cache would likely be immune since flushing it wouldn't affect other processes.

I'm having a (somewhat) similar issue, running tests that use webtest and end up with CSRF error 馃し鈥嶁檪

Any solution to be able to use Redis with this?

I can no longer reach the repo where I did have this working, but IIRC, redis-py provides a method for deleting all keys with a given prefix and I called that, or slightly reworked the cleanup so that it only deleted keys with the correct prefix.

Was this page helpful?
0 / 5 - 0 ratings