diff --git a/.env b/.env index b96d9fe..7bcfa59 100644 --- a/.env +++ b/.env @@ -12,5 +12,5 @@ POSTGRES_USER=postgres POSTGRES_PASSWORD=postgres #Redis -REDIS_HOST= redis +REDIS_HOST= localhost REDIS_PORT= 6379 \ No newline at end of file diff --git a/task/main.py b/task/main.py index ab88075..76efdc5 100644 --- a/task/main.py +++ b/task/main.py @@ -31,8 +31,12 @@ async def lifespan(_: FastAPI) -> AsyncIterator[None]: logging.info("Cache initialized successfully.") except Exception as e: logging.error(f"Error initializing cache: {e}") + yield - yield + finally: + # Clean up resources if needed + await redis.close() + logging.info("Redis connection closed.") app = FastAPI( diff --git a/task/tests/conftest.py b/task/tests/conftest.py index a3f33c0..d118aa8 100644 --- a/task/tests/conftest.py +++ b/task/tests/conftest.py @@ -16,7 +16,10 @@ def session(): table_registry.metadata.create_all(engine) with Session(engine) as session: - yield session + try: + yield session + finally: + session.close() table_registry.metadata.drop_all(engine) @@ -24,7 +27,10 @@ def session(): @pytest.fixture def client(session): def get_session_override(): - return session + try: + yield session + finally: + session.close() with TestClient(app) as client: app.dependency_overrides[get_session] = get_session_override