Skip to content

Commit cd6b0a6

Browse files
committed
Forces all tests to use the same connection pool without keepalive
1 parent 71ce719 commit cd6b0a6

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ build-backend = "setuptools.build_meta"
44

55
[tool.pytest.ini_options]
66
log_level = "INFO"
7-
redis_timeout = 0
87
testpaths = ["tests"]
98
python_classes = []
109
python_files = ["test_*.py"]

tests/shared/fixtures.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from playwright.sync_api import sync_playwright
2525
from pytest_localserver.smtp import Server as SmtpServer # type: ignore[import-untyped]
2626
from pytest_redis.factories.proc import redis_proc
27-
from redis import Redis
27+
from redis import ConnectionPool, Redis
2828
from shutil import which
2929
from sqlalchemy import create_engine, exc, text
3030
from sqlalchemy.orm.session import close_all_sessions
@@ -404,17 +404,28 @@ def browser(playwright_browser: Browser) -> Iterator[ExtendedBrowser]:
404404
context.close()
405405

406406

407+
@pytest.fixture(scope="session")
408+
def redis_url_session(redis_server: RedisExecutor) -> str:
409+
return f'redis://{redis_server.host}:{redis_server.port}/0'
410+
411+
@pytest.fixture(scope="session")
412+
def redis_pool(
413+
redis_url_session: str,
414+
monkeysession: pytest.MonkeyPatch
415+
) -> Iterator[ConnectionPool]:
416+
pool = ConnectionPool.from_url(redis_url_session, socket_keepalive=False)
417+
monkeysession.setattr('onegov.core.cache.redis.get_pool', lambda url: pool)
418+
yield pool
419+
pool.close()
420+
421+
407422
@pytest.fixture(scope="function")
408-
def redis_url(redis_server: RedisExecutor) -> Iterator[str]:
409-
url = f'redis://{redis_server.host}:{redis_server.port}/0'
410-
yield url
411-
# disable any potentially resource hungry options and make sure
412-
# we clean up our connection after we're done
413-
with Redis.from_url(
414-
url,
415-
socket_keepalive=False,
416-
single_connection_client=True,
417-
) as client:
423+
def redis_url(
424+
redis_url_session: str,
425+
redis_pool: ConnectionPool
426+
) -> Iterator[str]:
427+
yield redis_url_session
428+
with Redis(connection_pool=redis_pool) as client:
418429
client.flushall()
419430

420431

0 commit comments

Comments
 (0)