Skip to content

tests: select available TCP port#994

Merged
Kludex merged 1 commit into
pydantic:mainfrom
mbeijen:tests-should-not-depend-on-port-8000
May 29, 2026
Merged

tests: select available TCP port#994
Kludex merged 1 commit into
pydantic:mainfrom
mbeijen:tests-should-not-depend-on-port-8000

Conversation

@mbeijen
Copy link
Copy Markdown
Contributor

@mbeijen mbeijen commented May 29, 2026

Summary

If TCP port 8000 is in use on the developers' machine, the tests hang. It is mentioned in the CONTRIBUTING.md but it is not great developer experience.

Let's instead select a random free port by specifying port=0 in uvicorn.

Fixes #681

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 29, 2026

Merging this PR will not alter performance

✅ 15 untouched benchmarks
⏩ 7 skipped benchmarks1


Comparing mbeijen:tests-should-not-depend-on-port-8000 (e11dcfe) with main (fee387a)

Open in CodSpeed

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Copy link
Copy Markdown
Member

@Kludex Kludex left a comment

Choose a reason for hiding this comment

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

Can you try what I commented, please? If it doesn't work, this is fine.

Comment thread tests/httpx2/conftest.py Outdated
@pytest.fixture(scope="session")
def server() -> typing.Iterator[TestServer]:
config = Config(app=app, lifespan="off", loop="asyncio")
config = Config(app=app, lifespan="off", loop="asyncio", port=0)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we can avoid port=0 and the introspection of servers object in Uvicorn if we use free_tcp_port, and then we can use self.config.port.

Suggested change
config = Config(app=app, lifespan="off", loop="asyncio", port=0)
config = Config(app=app, lifespan="off", loop="asyncio", port=free_tcp_port)

If TCP port 8000 is in use on the developers' machine, the tests hang.
It is mentioned in the CONTRIBUTING.md but it is not great developer
experience.

Let's instead select a random free port in uvicorn.

Fixes pydantic#681
@mbeijen mbeijen force-pushed the tests-should-not-depend-on-port-8000 branch from d74afa1 to e11dcfe Compare May 29, 2026 07:56
@mbeijen
Copy link
Copy Markdown
Contributor Author

mbeijen commented May 29, 2026

Can you try what I commented, please? If it doesn't work, this is fine.

I was able to make it work but only using free_tcp_port_factory https://anyio.readthedocs.io/en/stable/testing.html#built-in-utility-fixtures because that one is session-scoped. I updated my PR

Copy link
Copy Markdown
Member

@Kludex Kludex left a comment

Choose a reason for hiding this comment

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

Amazing. Thanks! 🙏

@Kludex Kludex merged commit 999da32 into pydantic:main May 29, 2026
11 checks passed
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.

Testing endless SSE endpoint with AsyncClient hangs forever

2 participants