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

TST: Don't hard code server port in tests #1690

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

QuLogic
Copy link
Contributor

@QuLogic QuLogic commented Sep 23, 2024

This allows running tests in parallel without running into "Port already in use" errors.

Copy link
Member

@martindurant martindurant left a comment

Choose a reason for hiding this comment

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

At first glance all looks well here, but I have not yet reviewed in detail.

I have one main comment for you to consider before moving on.

if "give_path" in self.headers:
return self._respond(200, data=json.dumps({"path": self.path}).encode())
if "redirect" in self.headers and file_path != "/index/realfile":
new_url = f"http://127.0.0.1:{port}/index/realfile"
new_url = f"http://127.0.0.1:{self.server.server_port}/index/realfile"
Copy link
Member

Choose a reason for hiding this comment

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

self.server.realfile?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

self.server is the HTTPServer, not the server fixture, and doesn't have a realfile attribute. But this could probably use _make_realfile instead.

httpd = HTTPServer(server_address, HTTPTestHandler)
th = threading.Thread(target=httpd.serve_forever)
th.daemon = True
th.start()
try:
yield f"http://127.0.0.1:{port}"
yield f"http://127.0.0.1:{httpd.server_port}"
Copy link
Member

Choose a reason for hiding this comment

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

Since we are choosing to become port agnostic, this may be a good time to also become server address agnostic. httpd.server_name should be populated here, and might be other than the standard loopback, e.g., for systems that are IPv6 only.

@@ -185,4 +197,5 @@ def serve():
@pytest.fixture(scope="module")
def server():
with serve() as s:
yield s
server = SimpleNamespace(address=s, realfile=_make_realfile(s))
Copy link
Member

Choose a reason for hiding this comment

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

named_tuple or dict? I haven't really seen SimpleNamespace used much. In effect, it's a dictionary here. May as well include server name and other strings that are templated throughout the tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought that might be a bit overkill for tests.

Are there really any other strings that could be put in here? I replaced all the realfiles which seemed like the only common ones.

This allows running tests in parallel without running into "Port already
in use" errors.
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