Skip to content

Commit 1252e62

Browse files
authored
test: try reduce flakiness of test_ssl (#2735)
test: replace badssl.com with google.com/generate_204 in SSL test Switch to a more reliable HTTPS endpoint and add retry logic with exponential backoff to reduce flaky test failures.
1 parent 42d1cae commit 1252e62

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

test/test_ssl.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import socket
21
import textwrap
32

43
import pytest
@@ -8,14 +7,25 @@
87
project_with_ssl_tests = test_projects.new_c_project(
98
setup_py_add=textwrap.dedent(
109
r"""
11-
import ssl
10+
import ssl, time, urllib.request, urllib.error
1211
13-
from urllib.request import urlopen
12+
def check_https(context=None):
13+
# google hosts this endpoint that returns a 204 No Content, it's used for
14+
# connectivity checks in Android & Chrome
15+
url = "https://google.com/generate_204"
1416
15-
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
16-
# badssl.com is a HTTPS test server that can be used to test SSL connections
17-
data = urlopen("https://tls-v1-2.badssl.com", context=context)
18-
data = urlopen("https://tls-v1-2.badssl.com")
17+
for i in range(5):
18+
try:
19+
urllib.request.urlopen(url, context=context, timeout=5)
20+
return
21+
except (OSError, urllib.error.URLError) as e:
22+
print(f"Attempt {i+1}: Could not connect to {url}: {e}")
23+
time.sleep(2 ** i) # Backoff: 1s, 2s, 4s, 8s...
24+
25+
raise ConnectionError(f"Could not connect to {url} after retries.")
26+
27+
check_https()
28+
check_https(context=ssl.SSLContext(ssl.PROTOCOL_TLSv1_2))
1929
"""
2030
)
2131
)
@@ -28,9 +38,6 @@ def test(tmp_path):
2838
project_dir = tmp_path / "project"
2939
project_with_ssl_tests.generate(project_dir)
3040

31-
# warm up connection
32-
socket.getaddrinfo("tls-v1-2.badssl.com", 443)
33-
3441
actual_wheels = utils.cibuildwheel_run(project_dir)
3542

3643
expected_wheels = utils.expected_wheels("spam", "0.1.0")

0 commit comments

Comments
 (0)