Skip to content
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

chore: do not close connections at the end of pytest #3811

Merged
merged 6 commits into from
Sep 30, 2024
Merged

chore: do not close connections at the end of pytest #3811

merged 6 commits into from
Sep 30, 2024

Conversation

kostasrim
Copy link
Contributor

@kostasrim kostasrim commented Sep 27, 2024

A common case is that we need to clean up a connection before we exit a test via .close() method. This is needed because otherwise the connection will raise a warning that it is left unclosed. However, remembering to call .close() at each connection at the end of the test is cumbersome! Luckily, fixtures in python can be marked as async which allow us to:

  • cache all clients created by DflyInstance.client()
  • clean them all at the end of the fixture in one go

This PR adds this behaviour and cleans up the code in pytests. Bonus point is that no warnings are now generated.

@kostasrim kostasrim self-assigned this Sep 27, 2024
@pytest.fixture(scope="function", params=[{}])
def df_factory(request, tmp_dir, test_env) -> DflyInstanceFactory:
@pytest_asyncio.fixture(scope="function", params=[{}])
async def df_factory(request, tmp_dir, test_env) -> DflyInstanceFactory:
Copy link
Contributor Author

@kostasrim kostasrim Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the bread and butter of the PR. Fixtures can be async! This allows us to:

  1. Save all clients we create on df instance
  2. Clean them at the end of the fixture (since we can now await close())

Copy link
Contributor

@dranikpg dranikpg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing how much boilerplate code we delete in this brings me tears of joy

@kostasrim kostasrim merged commit b19f722 into main Sep 30, 2024
12 checks passed
@kostasrim kostasrim deleted the kpr2 branch September 30, 2024 06:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants