Repository navigation
Bound standalone urllib response reads by wall clock #1776
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,10 +4,13 @@ | |
| import http.client | ||
| import io | ||
| import json | ||
| import signal | ||
| import socket | ||
| import ssl | ||
| import subprocess | ||
| import sys | ||
| import traceback | ||
| from types import SimpleNamespace | ||
| import urllib.error | ||
| import urllib.request | ||
|
|
||
|
|
@@ -250,3 +253,74 @@ def fail(_request): | |
| with pytest.raises(mergify.MergifyError, match="transport failed") as info: | ||
| mergify.api_get("acme/widgets", "/merge-queue/status") | ||
| assert str(reason) in str(info.value) and sleeps == expected_sleeps | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("phase", ["headers", "body"]) | ||
| def test_request_deadline_covers_headers_and_body(monkeypatch, phase): | ||
| handler = signal.getsignal(signal.SIGALRM) | ||
|
|
||
| def expire(): | ||
| assert 0 < signal.getitimer(signal.ITIMER_REAL)[0] <= 30 | ||
| signal.raise_signal(signal.SIGALRM) | ||
|
|
||
| def open_(*args, **kwargs): | ||
| if phase == "headers": | ||
| expire() | ||
| return io.BytesIO(b"{}") | ||
|
|
||
| monkeypatch.setattr(mergify.urllib.request, "build_opener", lambda *_: SimpleNamespace(open=open_)) | ||
| with ( | ||
| pytest.raises(TimeoutError, match="Mergify HTTP request timed out"), | ||
| mergify._open(urllib.request.Request("https://example.invalid")), | ||
| ): | ||
| expire() | ||
| assert signal.getsignal(signal.SIGALRM) is handler | ||
| assert signal.getitimer(signal.ITIMER_REAL) == (0, 0) | ||
|
|
||
|
|
||
|
|
||
| def test_request_deadline_stops_address_fallback(monkeypatch): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Exercise both deadline regressions through the account helper The address-fallback and interrupted-disarm tests both exercise |
||
| addresses = [("192.0.2.1", 443), ("192.0.2.2", 443)] | ||
| attempted = [] | ||
|
|
||
| def connect(address): | ||
| attempted.append(address) | ||
| if len(attempted) == 1: | ||
| signal.raise_signal(signal.SIGALRM) | ||
|
|
||
| def open_(*args, **kwargs): | ||
| socket.create_connection(("example.invalid", 443)) | ||
| return io.BytesIO(b"{}") | ||
|
|
||
| monkeypatch.setattr(socket, "getaddrinfo", lambda *_: [ | ||
| (socket.AF_INET, socket.SOCK_STREAM, 0, "", address) for address in addresses | ||
| ]) | ||
| monkeypatch.setattr(socket, "socket", lambda *_: SimpleNamespace(connect=connect, close=lambda: None)) | ||
| monkeypatch.setattr(mergify.urllib.request, "build_opener", lambda *_: SimpleNamespace(open=open_)) | ||
| with ( | ||
| pytest.raises(TimeoutError, match="Mergify HTTP request timed out"), | ||
| mergify._open(urllib.request.Request("https://example.invalid")), | ||
| ): | ||
| pytest.fail("address fallback swallowed the deadline") | ||
| assert attempted == addresses[:1] | ||
|
|
||
|
|
||
| def test_request_deadline_restores_handler_when_disarming_is_interrupted(monkeypatch): | ||
| handler = signal.getsignal(signal.SIGALRM) | ||
| setitimer = signal.setitimer | ||
|
|
||
| def interrupt(timer, seconds): | ||
| result = setitimer(timer, seconds) | ||
| if seconds == 0: | ||
| signal.raise_signal(signal.SIGALRM) | ||
| return result | ||
|
|
||
| monkeypatch.setattr(signal, "setitimer", interrupt) | ||
| monkeypatch.setattr(mergify.urllib.request, "build_opener", | ||
| lambda *_: SimpleNamespace(open=lambda *_, **__: io.BytesIO(b"{}"))) | ||
| with ( | ||
| pytest.raises(TimeoutError, match="Mergify HTTP request timed out"), | ||
| mergify._open(urllib.request.Request("https://example.invalid")), | ||
| ): | ||
| pass | ||
| assert signal.getsignal(signal.SIGALRM) is handler | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Exercise the success-body read through
api_getThe body case raises the signal directly inside
_openwithout reading the response. It would still pass ifapi_getmovedjson.loadoutside the deadline. Have the fake response raise the signal fromread()and callapi_getwith retry sleeps stubbed out. That also checks the transport error seen by the caller.