Skip to content

Fix DeprecationWarning on closing driver multiple times #964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/neo4j/_async/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,9 @@ def _prepare_session_config(cls, preview_check, config_kwargs):
async def close(self) -> None:
""" Shut down, closing any open connections in the pool.
"""
self._check_state()
# TODO: 6.0 - NOOP if already closed
# if self._closed:
# return
try:
await self._pool.close()
except asyncio.CancelledError:
Expand Down
4 changes: 3 additions & 1 deletion src/neo4j/_sync/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,9 @@ def _prepare_session_config(cls, preview_check, config_kwargs):
def close(self) -> None:
""" Shut down, closing any open connections in the pool.
"""
self._check_state()
# TODO: 6.0 - NOOP if already closed
# if self._closed:
# return
try:
self._pool.close()
except asyncio.CancelledError:
Expand Down
26 changes: 24 additions & 2 deletions tests/unit/async_/test_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import inspect
import ssl
import typing as t
import warnings

import pytest
import typing_extensions as te
Expand Down Expand Up @@ -963,12 +964,11 @@ async def test_supports_session_auth(session_cls_mock) -> None:
("get_server_info", (), {}),
("supports_multi_db", (), {}),
("supports_session_auth", (), {}),
("close", (), {}),

)
)
@mark_async_test
async def test_using_closed_driver_is_deprecated(
async def test_using_closed_driver_where_deprecated(
method_name, args, kwargs, session_cls_mock
) -> None:
driver = AsyncGraphDatabase.driver("bolt://localhost")
Expand All @@ -983,3 +983,25 @@ async def test_using_closed_driver_is_deprecated(
await method(*args, **kwargs)
else:
method(*args, **kwargs)


@pytest.mark.parametrize(
("method_name", "args", "kwargs"),
(
("close", (), {}),
)
)
@mark_async_test
async def test_using_closed_driver_where_not_deprecated(
method_name, args, kwargs, session_cls_mock
) -> None:
driver = AsyncGraphDatabase.driver("bolt://localhost")
await driver.close()

method = getattr(driver, method_name)
with warnings.catch_warnings():
warnings.simplefilter("error")
if inspect.iscoroutinefunction(method):
await method(*args, **kwargs)
else:
method(*args, **kwargs)
26 changes: 24 additions & 2 deletions tests/unit/sync/test_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import inspect
import ssl
import typing as t
import warnings

import pytest
import typing_extensions as te
Expand Down Expand Up @@ -962,12 +963,11 @@ def test_supports_session_auth(session_cls_mock) -> None:
("get_server_info", (), {}),
("supports_multi_db", (), {}),
("supports_session_auth", (), {}),
("close", (), {}),

)
)
@mark_sync_test
def test_using_closed_driver_is_deprecated(
def test_using_closed_driver_where_deprecated(
method_name, args, kwargs, session_cls_mock
) -> None:
driver = GraphDatabase.driver("bolt://localhost")
Expand All @@ -982,3 +982,25 @@ def test_using_closed_driver_is_deprecated(
method(*args, **kwargs)
else:
method(*args, **kwargs)


@pytest.mark.parametrize(
("method_name", "args", "kwargs"),
(
("close", (), {}),
)
)
@mark_sync_test
def test_using_closed_driver_where_not_deprecated(
method_name, args, kwargs, session_cls_mock
) -> None:
driver = GraphDatabase.driver("bolt://localhost")
driver.close()

method = getattr(driver, method_name)
with warnings.catch_warnings():
warnings.simplefilter("error")
if inspect.iscoroutinefunction(method):
method(*args, **kwargs)
else:
method(*args, **kwargs)