Repository navigation
Deprecate unused TCP and UDP port fixtures #1615
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
Closed
agustin18
wants to merge
3
commits into
pytest-dev:main
from
agustin18:deprecate-unused-port-fixtures
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| The ``unused_tcp_port``, ``unused_tcp_port_factory``, ``unused_udp_port``, and ``unused_udp_port_factory`` fixtures are deprecated because the returned port can be claimed by another process before the test binds it. Bind to port 0 and read the assigned port from the server instead. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| Avoid the unused port fixtures | ||
| ============================== | ||
|
|
||
| The ``unused_tcp_port``, ``unused_tcp_port_factory``, ``unused_udp_port``, and | ||
| ``unused_udp_port_factory`` fixtures are deprecated because the port they | ||
| return can be claimed by another process before the test binds it. | ||
|
|
||
| Bind to port 0 instead and read the port assigned by the operating system. | ||
|
|
||
| Migration | ||
| --------- | ||
|
|
||
| Replace the fixtures with the patterns below: | ||
|
|
||
| - ``unused_tcp_port`` / ``unused_udp_port``: bind the server or datagram | ||
| endpoint to port 0 and read the port assigned by the operating system. | ||
| The examples below show the full pattern for TCP and UDP. | ||
| - ``unused_tcp_port_factory`` / ``unused_udp_port_factory``: create each | ||
| server or datagram endpoint on port 0 and read its assigned port. If the | ||
| port must be passed to another process before the server starts, bind the | ||
| socket in the test and pass the listening socket (or use a | ||
| ``socketpair``/pipe) instead of a bare port number; a bare port can always | ||
| be claimed before the other process binds it. | ||
|
|
||
| The deprecated fixtures remain available and keep working, but they emit a | ||
| ``DeprecationWarning``. The warning and this guide describe the same | ||
| migration; the guide exists because the fixtures are public API and the | ||
| warning alone does not show the replacement pattern. | ||
|
|
||
| TCP | ||
| --- | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| import asyncio | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| async def handler(reader, writer): | ||
| writer.close() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_server(): | ||
| server = await asyncio.start_server(handler, host="127.0.0.1", port=0) | ||
| port = server.sockets[0].getsockname()[1] | ||
| try: | ||
| reader, writer = await asyncio.open_connection("127.0.0.1", port) | ||
| writer.close() | ||
| await writer.wait_closed() | ||
| finally: | ||
| server.close() | ||
| await server.wait_closed() | ||
|
|
||
| UDP | ||
| --- | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| import asyncio | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| class Protocol(asyncio.DatagramProtocol): | ||
| pass | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_datagram_endpoint(): | ||
| loop = asyncio.get_running_loop() | ||
| transport, _ = await loop.create_datagram_endpoint( | ||
| Protocol, local_addr=("127.0.0.1", 0) | ||
| ) | ||
| port = transport.get_extra_info("sockname")[1] | ||
| try: | ||
| transport.sendto(b"ping", ("127.0.0.1", port)) | ||
| finally: | ||
| transport.close() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from textwrap import dedent | ||
|
|
||
| import pytest | ||
| from pytest import Pytester | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "fixture_name", | ||
| [ | ||
| "unused_tcp_port", | ||
| "unused_udp_port", | ||
| "unused_tcp_port_factory", | ||
| "unused_udp_port_factory", | ||
| ], | ||
| ) | ||
| def test_port_fixtures_are_deprecated(pytester: Pytester, fixture_name: str) -> None: | ||
| pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") | ||
| pytester.makepyfile(dedent(f"""\ | ||
| import pytest | ||
|
|
||
| def test_deprecated(request): | ||
| with pytest.warns( | ||
| DeprecationWarning, | ||
| match="The {fixture_name} fixture is deprecated", | ||
| ): | ||
| request.getfixturevalue("{fixture_name}") | ||
| """)) | ||
|
|
||
| result = pytester.runpytest() | ||
|
|
||
| result.assert_outcomes(passed=1) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This document lists calls to various asyncio functions, but it doesn't contain any information or recommendation on what current users of the deprecated fixtures should do or which functionality they should use instead. Can you make a proposal?
On another note, why do you think a how-to guide is warranted for the deprecation?