Skip to content

Commit

Permalink
make all generated servers pick an arbitrary free port
Browse files Browse the repository at this point in the history
ideally, this should avoid collisions. no clue if this matters at all
  • Loading branch information
cosmicexplorer committed Aug 10, 2024
1 parent 0e9aee8 commit e4b2aaf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
21 changes: 9 additions & 12 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,8 @@ def html_index_with_onetime_server(
"""Serve files from a generated pypi index, erroring if a file is downloaded more
than once.
Provide `-i http://localhost:8000` to pip invocations to point them at this server.
Provide `-i http://localhost:<port>` to pip invocations to point them at
this server.
"""

class InDirectoryServer(http.server.ThreadingHTTPServer):
Expand All @@ -1130,7 +1131,7 @@ def finish_request(self: "Self", request: Any, client_address: Any) -> None:
class Handler(OneTimeDownloadHandler):
_seen_paths: ClassVar[Set[str]] = set()

with InDirectoryServer(("", 8000), Handler) as httpd:
with InDirectoryServer(("", 0), Handler) as httpd:
server_thread = threading.Thread(target=httpd.serve_forever)
server_thread.start()

Expand Down Expand Up @@ -1326,14 +1327,13 @@ def html_index_no_metadata(

HTMLIndexWithRangeServer = Callable[
[RangeHandler],
"AbstractContextManager[Type[ContentRangeDownloadHandler]]",
"AbstractContextManager[Tuple[Type[ContentRangeDownloadHandler], int]]",
]


@pytest.fixture
def html_index_with_range_server(
html_index_no_metadata: Path,
port: int = 8000,
) -> HTMLIndexWithRangeServer:
"""Serve files from a generated pypi index, with support for range requests.
Expand All @@ -1350,7 +1350,7 @@ def finish_request(self, request: Any, client_address: Any) -> None:
@contextmanager
def inner(
range_handler: RangeHandler,
) -> Iterator[Type[ContentRangeDownloadHandler]]:
) -> Iterator[Tuple[Type[ContentRangeDownloadHandler], int]]:
class Handler(ContentRangeDownloadHandler):
@property
def range_handler(self) -> RangeHandler:
Expand All @@ -1362,18 +1362,15 @@ def range_handler(self) -> RangeHandler:
head_request_paths: ClassVar[Set[str]] = set()
ok_response_counts: ClassVar[Dict[str, int]] = {}

with InDirectoryServer(("", port), Handler) as httpd:
with InDirectoryServer(("", 0), Handler) as httpd:
server_thread = threading.Thread(target=httpd.serve_forever)
server_thread.start()

_, server_port = httpd.server_address
try:
yield Handler
yield (Handler, server_port)
finally:
httpd.shutdown()
server_thread.join(timeout=3.0)
if server_thread.is_alive():
raise RuntimeError(
"failed to shutdown http server within 3 seconds"
)
server_thread.join()

return inner
3 changes: 2 additions & 1 deletion tests/functional/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,7 @@ def download_server_html_index(
) -> Callable[..., Tuple[TestPipResult, Path]]:
"""Execute `pip download` against a generated PyPI index."""
download_dir = tmpdir / "download_dir"
_, server_port = html_index_with_onetime_server.server_address

def run_for_generated_index(
args: List[str],
Expand All @@ -1287,7 +1288,7 @@ def run_for_generated_index(
"-d",
str(download_dir),
"-i",
"http://localhost:8000",
f"http://localhost:{server_port}",
*args,
]
result = script.pip(*pip_args, allow_error=allow_error)
Expand Down
9 changes: 5 additions & 4 deletions tests/functional/test_fast_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def test_download_range(
"""Execute `pip download` against a generated PyPI index."""
download_dir = tmpdir / "download_dir"

def run_for_generated_index(args: List[str]) -> TestPipResult:
def run_for_generated_index(server_port: int, args: List[str]) -> TestPipResult:
"""
Produce a PyPI directory structure pointing to the specified packages, then
execute `pip download -i ...` pointing to our generated index.
Expand All @@ -187,14 +187,15 @@ def run_for_generated_index(args: List[str]) -> TestPipResult:
"-d",
str(download_dir),
"-i",
"http://localhost:8000",
f"http://localhost:{server_port}",
*args,
]
return script.pip(*pip_args, allow_stderr_warning=True)

with html_index_with_range_server(range_handler) as handler:
with html_index_with_range_server(range_handler) as (handler, server_port):
run_for_generated_index(
["colander", "compilewheel==2.0", "has-script", "translationstring==0.1"]
server_port,
["colander", "compilewheel==2.0", "has-script", "translationstring==0.1"],
)
generated_files = os.listdir(download_dir)
assert fnmatch.filter(generated_files, "colander*.whl")
Expand Down

0 comments on commit e4b2aaf

Please sign in to comment.