Repository navigation
Expand file tree
/
Copy pathtest_client_request_queue.py
More file actions
255 lines (202 loc) · 9.82 KB
/
Copy pathtest_client_request_queue.py
File metadata and controls
255 lines (202 loc) · 9.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
from __future__ import annotations
import gzip
import json
import re
from typing import TYPE_CHECKING
import pytest
from werkzeug.wrappers import Response
from apify_client import ApifyClient, ApifyClientAsync
from apify_client.errors import ApifyApiError
if TYPE_CHECKING:
from collections.abc import Callable
from pytest_httpserver import HTTPServer
from werkzeug.wrappers import Request
from apify_client._typeddicts import RequestDraftDict
# The Apify API limit on the payload size of a batch-add request, which the client's batching must respect.
_API_MAX_PAYLOAD_SIZE_BYTES = 9 * 1024 * 1024
_EMPTY_BATCH_RESPONSE_CONTENT = '{"data": {"processedRequests": [], "unprocessedRequests": []}}'
_PARTIALLY_ADDED_BATCH_RESPONSE_CONTENT = """{
"data": {
"processedRequests": [
{
"requestId": "YiKoxjkaS9gjGTqhF",
"uniqueKey": "http://example.com/1",
"wasAlreadyPresent": true,
"wasAlreadyHandled": false
}
],
"unprocessedRequests": [
{
"uniqueKey": "http://example.com/2",
"url": "http://example.com/2",
"method": "GET"
}
]
}
}"""
async def test_batch_not_processed_raises_exception_async(httpserver: HTTPServer) -> None:
"""Test that client exceptions are not silently ignored"""
server_url = httpserver.url_for('/').removesuffix('/')
client = ApifyClientAsync(
token='placeholder_token',
api_url=server_url,
api_public_url=server_url,
)
httpserver.expect_oneshot_request(re.compile(r'.*'), method='POST').respond_with_data(status=401)
requests: list[RequestDraftDict] = [
{'unique_key': 'http://example.com/1', 'url': 'http://example.com/1', 'method': 'GET'},
{'unique_key': 'http://example.com/2', 'url': 'http://example.com/2', 'method': 'GET'},
]
rq_client = client.request_queue(request_queue_id='whatever')
with pytest.raises(ApifyApiError):
await rq_client.batch_add_requests(requests=requests)
async def test_batch_processed_partially_async(httpserver: HTTPServer) -> None:
server_url = httpserver.url_for('/').removesuffix('/')
client = ApifyClientAsync(
token='placeholder_token',
api_url=server_url,
api_public_url=server_url,
)
httpserver.expect_oneshot_request(re.compile(r'.*'), method='POST').respond_with_data(
status=200, response_data=_PARTIALLY_ADDED_BATCH_RESPONSE_CONTENT
)
requests: list[RequestDraftDict] = [
{'unique_key': 'http://example.com/1', 'url': 'http://example.com/1', 'method': 'GET'},
{'unique_key': 'http://example.com/2', 'url': 'http://example.com/2', 'method': 'GET'},
]
rq_client = client.request_queue(request_queue_id='whatever')
batch_response = await rq_client.batch_add_requests(requests=requests)
assert requests[0]['unique_key'] in {request.unique_key for request in batch_response.processed_requests}
assert len(batch_response.unprocessed_requests) == 1
assert batch_response.unprocessed_requests[0].unique_key == requests[1]['unique_key']
def test_batch_not_processed_raises_exception_sync(httpserver: HTTPServer) -> None:
"""Test that client exceptions are not silently ignored"""
server_url = httpserver.url_for('/').removesuffix('/')
client = ApifyClient(
token='placeholder_token',
api_url=server_url,
api_public_url=server_url,
)
httpserver.expect_oneshot_request(re.compile(r'.*'), method='POST').respond_with_data(status=401)
requests: list[RequestDraftDict] = [
{'unique_key': 'http://example.com/1', 'url': 'http://example.com/1', 'method': 'GET'},
{'unique_key': 'http://example.com/2', 'url': 'http://example.com/2', 'method': 'GET'},
]
rq_client = client.request_queue(request_queue_id='whatever')
with pytest.raises(ApifyApiError):
rq_client.batch_add_requests(requests=requests)
def _make_large_requests() -> list[RequestDraftDict]:
"""Return 3 requests of ~4 MB each, so that all of them together exceed the 9 MB payload limit."""
return [
{
'unique_key': f'http://example.com/{i}',
'url': f'http://example.com/{i}?filler={"x" * (4 * 1024 * 1024)}',
'method': 'GET',
}
for i in range(3)
]
def _payload_capturing_handler(payloads: list[bytes]) -> Callable[[Request], Response]:
"""Return a handler that records each POST body and responds with an empty batch result.
Bodies below the client's compression threshold arrive uncompressed, so the recorded payload is
decompressed only when the request says it was encoded.
"""
def handler(request: Request) -> Response:
body = request.get_data()
payloads.append(gzip.decompress(body) if request.headers.get('Content-Encoding') == 'gzip' else body)
return Response(_EMPTY_BATCH_RESPONSE_CONTENT, status=200, content_type='application/json')
return handler
async def test_batch_add_requests_splits_batches_by_payload_size_async(httpserver: HTTPServer) -> None:
"""Test that batches are split by serialized byte size so no POST payload exceeds the 9 MB API limit."""
server_url = httpserver.url_for('/').removesuffix('/')
client = ApifyClientAsync(
token='placeholder_token',
api_url=server_url,
api_public_url=server_url,
)
payloads = list[bytes]()
httpserver.expect_request(re.compile(r'.*'), method='POST').respond_with_handler(
_payload_capturing_handler(payloads)
)
rq_client = client.request_queue(request_queue_id='whatever')
await rq_client.batch_add_requests(requests=_make_large_requests())
assert len(payloads) > 1
assert all(len(payload) <= _API_MAX_PAYLOAD_SIZE_BYTES for payload in payloads)
assert sum(len(json.loads(payload)) for payload in payloads) == 3
def test_batch_add_requests_splits_batches_by_payload_size_sync(httpserver: HTTPServer) -> None:
"""Test that batches are split by serialized byte size so no POST payload exceeds the 9 MB API limit."""
server_url = httpserver.url_for('/').removesuffix('/')
client = ApifyClient(
token='placeholder_token',
api_url=server_url,
api_public_url=server_url,
)
payloads = list[bytes]()
httpserver.expect_request(re.compile(r'.*'), method='POST').respond_with_handler(
_payload_capturing_handler(payloads)
)
rq_client = client.request_queue(request_queue_id='whatever')
rq_client.batch_add_requests(requests=_make_large_requests())
assert len(payloads) > 1
assert all(len(payload) <= _API_MAX_PAYLOAD_SIZE_BYTES for payload in payloads)
assert sum(len(json.loads(payload)) for payload in payloads) == 3
def _make_oversized_and_small_requests() -> list[RequestDraftDict]:
"""Return a small request plus one whose serialized size alone exceeds the 9 MB payload limit."""
return [
{'unique_key': 'small', 'url': 'http://example.com/small', 'method': 'GET'},
{
'unique_key': 'oversized',
'url': f'http://example.com/oversized?filler={"x" * (10 * 1024 * 1024)}',
'method': 'GET',
},
]
async def test_batch_add_requests_sends_oversized_request_alone_async(httpserver: HTTPServer) -> None:
"""Test that a request exceeding the payload limit is sent in its own batch for the API to judge, not rejected."""
server_url = httpserver.url_for('/').removesuffix('/')
client = ApifyClientAsync(
token='placeholder_token',
api_url=server_url,
api_public_url=server_url,
)
payloads = list[bytes]()
httpserver.expect_request(re.compile(r'.*'), method='POST').respond_with_handler(
_payload_capturing_handler(payloads)
)
rq_client = client.request_queue(request_queue_id='whatever')
await rq_client.batch_add_requests(requests=_make_oversized_and_small_requests())
assert sum(len(json.loads(payload)) for payload in payloads) == 2
assert any(len(payload) > _API_MAX_PAYLOAD_SIZE_BYTES for payload in payloads)
def test_batch_add_requests_sends_oversized_request_alone_sync(httpserver: HTTPServer) -> None:
"""Test that a request exceeding the payload limit is sent in its own batch for the API to judge, not rejected."""
server_url = httpserver.url_for('/').removesuffix('/')
client = ApifyClient(
token='placeholder_token',
api_url=server_url,
api_public_url=server_url,
)
payloads = list[bytes]()
httpserver.expect_request(re.compile(r'.*'), method='POST').respond_with_handler(
_payload_capturing_handler(payloads)
)
rq_client = client.request_queue(request_queue_id='whatever')
rq_client.batch_add_requests(requests=_make_oversized_and_small_requests())
assert sum(len(json.loads(payload)) for payload in payloads) == 2
assert any(len(payload) > _API_MAX_PAYLOAD_SIZE_BYTES for payload in payloads)
def test_batch_processed_partially_sync(httpserver: HTTPServer) -> None:
server_url = httpserver.url_for('/').removesuffix('/')
client = ApifyClient(
token='placeholder_token',
api_url=server_url,
api_public_url=server_url,
)
httpserver.expect_oneshot_request(re.compile(r'.*'), method='POST').respond_with_data(
status=200, response_data=_PARTIALLY_ADDED_BATCH_RESPONSE_CONTENT
)
requests: list[RequestDraftDict] = [
{'unique_key': 'http://example.com/1', 'url': 'http://example.com/1', 'method': 'GET'},
{'unique_key': 'http://example.com/2', 'url': 'http://example.com/2', 'method': 'GET'},
]
rq_client = client.request_queue(request_queue_id='whatever')
batch_response = rq_client.batch_add_requests(requests=requests)
assert requests[0]['unique_key'] in {request.unique_key for request in batch_response.processed_requests}
assert len(batch_response.unprocessed_requests) == 1
assert batch_response.unprocessed_requests[0].unique_key == requests[1]['unique_key']