Skip to content

Commit 36446a7

Browse files
authored
chore: improve HttpClient isolating in tests (#1513)
### Description ### Issues - Closes: #1441 - Closes: #1494 - Closes: #1460
1 parent 82d0744 commit 36446a7

File tree

4 files changed

+16
-17
lines changed

4 files changed

+16
-17
lines changed

tests/unit/_utils/test_sitemap.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import base64
22
import gzip
3-
import sys
43
from datetime import datetime
54

6-
import pytest
75
from yarl import URL
86

97
from crawlee._utils.sitemap import Sitemap, SitemapUrl, parse_sitemap
@@ -95,8 +93,6 @@ async def test_gzipped_sitemap(server_url: URL, http_client: HttpClient) -> None
9593
assert set(sitemap.urls) == BASIC_RESULTS
9694

9795

98-
# TODO: Remove this skip when #1460 is resolved.
99-
@pytest.mark.skipif(sys.platform != 'linux', reason='Flaky with Curl on Windows, see #1460.')
10096
async def test_gzipped_sitemap_with_invalid_data(server_url: URL, http_client: HttpClient) -> None:
10197
"""Test loading a invalid gzipped sitemap with correct type and .xml.gz url."""
10298
compress_data = compress_gzip(BASIC_SITEMAP)

tests/unit/conftest.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import logging
77
import os
88
import warnings
9-
from typing import TYPE_CHECKING, cast
9+
from typing import TYPE_CHECKING, Any, cast
1010

1111
import pytest
1212
from curl_cffi import CurlHttpVersion
@@ -205,9 +205,16 @@ def redirect_server_url(redirect_http_server: TestServer) -> URL:
205205
pytest.param('curl', id='curl'),
206206
]
207207
)
208-
async def http_client(request: pytest.FixtureRequest) -> HttpClient:
208+
async def http_client(request: pytest.FixtureRequest) -> AsyncGenerator[HttpClient, None]:
209+
class_client: type[HttpClient]
209210
if request.param == 'curl':
210-
return CurlImpersonateHttpClient(http_version=CurlHttpVersion.V1_1)
211-
if request.param == 'impit':
212-
return ImpitHttpClient(http3=False)
213-
return HttpxHttpClient(http2=False)
211+
class_client = CurlImpersonateHttpClient
212+
kwargs: dict[str, Any] = {'http_version': CurlHttpVersion.V1_1}
213+
elif request.param == 'impit':
214+
class_client = ImpitHttpClient
215+
kwargs = {'http3': False}
216+
else:
217+
class_client = HttpxHttpClient
218+
kwargs = {'http2': True}
219+
async with class_client(**kwargs) as client:
220+
yield client

tests/unit/http_clients/test_http_clients.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import os
4-
import sys
54
from typing import TYPE_CHECKING
65

76
import pytest
@@ -164,10 +163,6 @@ async def test_send_request_allow_redirects_false(custom_http_client: HttpClient
164163

165164

166165
async def test_stream(http_client: HttpClient, server_url: URL) -> None:
167-
# TODO: Remove this skip when #1494 is resolved.
168-
if isinstance(http_client, CurlImpersonateHttpClient) and sys.platform != 'linux':
169-
pytest.skip('Flaky with Curl on Windows, see #1494.')
170-
171166
content_body: bytes = b''
172167

173168
async with http_client.stream(str(server_url)) as response:

tests/unit/storages/test_request_queue.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,9 @@ async def test_add_requests_wait_for_all(
449449
# Immediately after adding, the total count may be less than 15 due to background processing
450450
assert await rq.get_total_count() <= 15
451451

452-
# Wait a 500 milliseconds for background tasks to complete
453-
await asyncio.sleep(0.5)
452+
# Wait for background tasks to complete
453+
while await rq.get_total_count() < 15: # noqa: ASYNC110
454+
await asyncio.sleep(0.1)
454455

455456
# Verify all requests were added
456457
assert await rq.get_total_count() == 15

0 commit comments

Comments
 (0)