Skip to content

Commit

Permalink
Add client connection-pool contention coverage to the linkcheck t…
Browse files Browse the repository at this point in the history
…ests (#11402)

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
  • Loading branch information
jayaddison and AA-Turner authored Jul 23, 2023
1 parent 2b33326 commit c583e0f
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion tests/test_build_linkcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
import pytest
from urllib3.poolmanager import PoolManager

from sphinx.builders.linkcheck import HyperlinkAvailabilityCheckWorker, RateLimit
from sphinx.builders.linkcheck import (
CheckRequest,
Hyperlink,
HyperlinkAvailabilityCheckWorker,
RateLimit,
)
from sphinx.testing.util import strip_escseq
from sphinx.util import requests
from sphinx.util.console import strip_colors
Expand Down Expand Up @@ -800,6 +805,47 @@ def test_limit_rate_bails_out_after_waiting_max_time(app):
assert next_check is None


@mock.patch('sphinx.util.requests.requests.Session.get_adapter')
def test_connection_contention(get_adapter, app, capsys):
# Create a shared, but limited-size, connection pool
import requests
get_adapter.return_value = requests.adapters.HTTPAdapter(pool_maxsize=1)

# Set an upper-bound on socket timeouts globally
import socket
socket.setdefaulttimeout(5)

# Place a workload into the linkcheck queue
link_count = 10
rqueue, wqueue = Queue(), Queue()
for _ in range(link_count):
wqueue.put(CheckRequest(0, Hyperlink("http://localhost:7777", "test", "test.rst", 1)))

# Create parallel consumer threads
with http_server(make_redirect_handler(support_head=True)):
begin, checked = time.time(), []
threads = [
HyperlinkAvailabilityCheckWorker(
config=app.config,
rqueue=rqueue,
wqueue=wqueue,
rate_limits={},
)
for _ in range(10)
]
for thread in threads:
thread.start()
while time.time() < begin + 5 and len(checked) < link_count:
checked.append(rqueue.get(timeout=5))
for thread in threads:
thread.join(timeout=0)

# Ensure that all items were consumed within the time limit
_, stderr = capsys.readouterr()
assert len(checked) == link_count
assert "TimeoutError" not in stderr


class ConnectionResetHandler(http.server.BaseHTTPRequestHandler):
protocol_version = "HTTP/1.1"

Expand Down

0 comments on commit c583e0f

Please sign in to comment.