Skip to content

Cache name resolutions#369

Merged
miketheman merged 7 commits into
miketheman:mainfrom
twm:cache-hostname-lookups
Sep 7, 2024
Merged

Cache name resolutions#369
miketheman merged 7 commits into
miketheman:mainfrom
twm:cache-hostname-lookups

Conversation

@twm
Copy link
Copy Markdown
Contributor

@twm twm commented Aug 20, 2024

To avoid resolving the allowed hostnames once per test, cache the results in a shared dict. This should result in one set of resolutions per pytest run (or test worker, under pytest-xdist). I did this in a backward-compatible manner by introducing an optional parameter to the socket_allow_hosts() and normalize_allowed_hosts() functions as they appear to be public API, albeit undocumented.

I also refactored the plugin to use Stash, which was introduced in pytest 7.0.0, rather than set attributes on Config. If you want to keep support for pytest>=6.2.5 I can back that bit out, I just thought it improved clarity.

@twm twm force-pushed the cache-hostname-lookups branch from 34366ac to 76ed414 Compare August 20, 2024 22:47
twm added 2 commits August 20, 2024 15:48
To avoid resolving the allowed hostnames once per test, cache the
results in a shared dict. This should result in one set of resolutions
per pytest run (or test worker, under pytest-xdist).

I also refactored the plugin to use Stash, which was introduced in
pytest 7.0.0, rather than set attributes on config.
@twm twm force-pushed the cache-hostname-lookups branch from 53acc15 to 2bcdf8c Compare August 20, 2024 22:48
Copy link
Copy Markdown
Owner

@miketheman miketheman left a comment

Choose a reason for hiding this comment

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

Very cool stuff - I didn't know about stash, that's much nicer than the __... stuff I was doing before.

Can you conceive of a test to add that confirms the resolution cache is working?

Comment thread pyproject.toml
Comment thread pytest_socket.py Outdated
Comment thread pytest_socket.py
@miketheman miketheman added the enhancement New feature or request label Aug 21, 2024
@twm twm force-pushed the cache-hostname-lookups branch from b025860 to 0ed6819 Compare August 23, 2024 00:20
@qlty-cloud-legacy
Copy link
Copy Markdown

Code Climate has analyzed commit f2704d5 and detected 0 issues on this pull request.

View more on Code Climate.

@twm twm force-pushed the cache-hostname-lookups branch from f2704d5 to 0ed6819 Compare August 23, 2024 00:32
@twm
Copy link
Copy Markdown
Contributor Author

twm commented Aug 23, 2024

@miketheman I think I've addressed all your feedback. I settled on monkey patching socket.getaddrinfo() to count the number of calls to ensure the cache works.

Interestingly, it looks like pre-commit.ci's autocommit bypasses workflow approval. (Sorry about the many notifications...)

Copy link
Copy Markdown
Owner

@miketheman miketheman left a comment

Choose a reason for hiding this comment

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

@twm Thanks for putting in the effort!

@miketheman miketheman merged commit 60286e4 into miketheman:main Sep 7, 2024
@twm twm deleted the cache-hostname-lookups branch September 7, 2024 00:04
@twm
Copy link
Copy Markdown
Contributor Author

twm commented Sep 7, 2024

Thank you for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants