Skip to content

Commit

Permalink
Add a connection-count measurement to the linkcheck tests (#11374)
Browse files Browse the repository at this point in the history
  • Loading branch information
jayaddison committed Jul 23, 2023
1 parent 71db08c commit 2b33326
Showing 1 changed file with 37 additions and 1 deletion.
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

0 comments on commit 2b33326

Please sign in to comment.