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

tests: linkcheck: add connection-count measurement #11374

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 37 additions & 1 deletion tests/test_build_linkcheck.py
Expand Up @@ -15,6 +15,7 @@
from unittest import mock

import pytest
from urllib3.poolmanager import PoolManager

from sphinx.builders.linkcheck import HyperlinkAvailabilityCheckWorker, RateLimit
from sphinx.testing.util import strip_escseq
Expand Down Expand Up @@ -60,10 +61,45 @@ def do_GET(self):
self.end_headers()


class ConnectionMeasurement:
"""Measure the number of distinct host connections created during linkchecking"""

def __init__(self):
self.connections = set()
self.urllib3_connection_from_url = PoolManager.connection_from_url
self.patcher = mock.patch.object(
target=PoolManager,
attribute='connection_from_url',
new=self._collect_connections(),
)

def _collect_connections(self):
def connection_collector(obj, url):
connection = self.urllib3_connection_from_url(obj, url)
self.connections.add(connection)
return connection
return connection_collector

def __enter__(self):
self.patcher.start()
return self

def __exit__(self, *args, **kwargs):
for connection in self.connections:
connection.close()
self.patcher.stop()

@property
def connection_count(self):
return len(self.connections)


@pytest.mark.sphinx('linkcheck', testroot='linkcheck', freshenv=True)
def test_defaults(app):
with http_server(DefaultsHandler):
app.build()
with ConnectionMeasurement() as m:
app.build()
assert m.connection_count <= 10

# Text output
assert (app.outdir / 'output.txt').exists()
Expand Down