Skip to content

Commit

Permalink
Unit tests: linkcheck: add connection-count measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
jayaddison committed Apr 18, 2023
1 parent acec710 commit e304062
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion tests/test_build_linkcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from queue import Queue
from unittest import mock

from urllib3.poolmanager import PoolManager
import pytest

from sphinx.builders.linkcheck import HyperlinkAvailabilityCheckWorker, RateLimit
Expand Down Expand Up @@ -59,10 +60,46 @@ 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 == 5

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

0 comments on commit e304062

Please sign in to comment.