Skip to content

Commit fcfaf23

Browse files
committed
Makes sure cache on get does not get shared between test functions
1 parent 653c60b commit fcfaf23

1 file changed

Lines changed: 14 additions & 13 deletions

File tree

tests/shared/fixtures.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -404,35 +404,36 @@ def browser(playwright_browser: Browser) -> Iterator[ExtendedBrowser]:
404404
context.close()
405405

406406

407-
@pytest.fixture(scope="session")
408-
def _redis_url(redis_server: RedisExecutor) -> str:
409-
return f'redis://{redis_server.host}:{redis_server.port}/0'
410-
411407
@pytest.fixture(scope="function")
412408
def redis_pool(
413-
_redis_url: str,
409+
redis_server: RedisExecutor,
414410
monkeypatch: pytest.MonkeyPatch
415411
) -> Iterator[ConnectionPool]:
412+
413+
import onegov.core.cache.redis as redis_cache
414+
415+
url = f'redis://{redis_server.host}:{redis_server.port}/0'
416416
pool = ConnectionPool.from_url(
417-
_redis_url,
417+
url,
418418
# Restore pre 8.0 defaults for these options, since the
419419
# new defaults cause issues in our tests
420420
socket_timeout=None,
421421
socket_connect_timeout=None,
422422
socket_keepalive=None
423423
)
424-
monkeypatch.setattr('onegov.core.cache.redis.get_pool', lambda url: pool)
424+
pool._redis_url = url # type: ignore[attr-defined]
425+
monkeypatch.setattr(redis_cache, 'get_pool', lambda url: pool)
426+
# only cache redis cache region per test case
427+
monkeypatch.setattr(redis_cache, 'get', lru_cache(maxsize=1024)(
428+
redis_cache.get.__wrapped__))
425429
yield pool
426430
pool.close()
427431

428432

429433
@pytest.fixture(scope="function")
430-
def redis_url(
431-
_redis_url: str,
432-
redis_pool: ConnectionPool
433-
) -> Iterator[str]:
434-
yield _redis_url
435-
with Redis(connection_pool=redis_pool) as client:
434+
def redis_url(redis_pool: ConnectionPool) -> Iterator[str]:
435+
yield redis_pool._redis_url # type: ignore[attr-defined]
436+
with Redis.from_pool(redis_pool) as client:
436437
client.flushall()
437438

438439

0 commit comments

Comments
 (0)