@@ -274,6 +274,57 @@ async def test_add_requests_with_forefront(rq: RequestQueue) -> None:
274
274
assert next_request .url == 'https://example.com/priority'
275
275
276
276
277
+ async def test_add_requests_mixed_forefront (rq : RequestQueue ) -> None :
278
+ """Test the ordering when adding requests with mixed forefront values."""
279
+ # Add normal requests
280
+ await rq .add_request ('https://example.com/normal1' )
281
+ await rq .add_request ('https://example.com/normal2' )
282
+
283
+ # Add a batch with forefront=True
284
+ await rq .add_requests (
285
+ ['https://example.com/priority1' , 'https://example.com/priority2' ],
286
+ forefront = True ,
287
+ )
288
+
289
+ # Add another normal request
290
+ await rq .add_request ('https://example.com/normal3' )
291
+
292
+ # Add another priority request
293
+ await rq .add_request ('https://example.com/priority3' , forefront = True )
294
+
295
+ # Wait for background tasks
296
+ await asyncio .sleep (0.1 )
297
+
298
+ # The expected order should be:
299
+ # 1. priority3 (most recent forefront)
300
+ # 2. priority1 (from batch, forefront)
301
+ # 3. priority2 (from batch, forefront)
302
+ # 4. normal1 (oldest normal)
303
+ # 5. normal2
304
+ # 6. normal3 (newest normal)
305
+
306
+ requests = []
307
+ while True :
308
+ req = await rq .fetch_next_request ()
309
+ if req is None :
310
+ break
311
+ requests .append (req )
312
+ await rq .mark_request_as_handled (req )
313
+
314
+ assert len (requests ) == 6
315
+ assert requests [0 ].url == 'https://example.com/priority3'
316
+
317
+ # The next two should be from the forefront batch (exact order within batch may vary)
318
+ batch_urls = {requests [1 ].url , requests [2 ].url }
319
+ assert 'https://example.com/priority1' in batch_urls
320
+ assert 'https://example.com/priority2' in batch_urls
321
+
322
+ # Then the normal requests in order
323
+ assert requests [3 ].url == 'https://example.com/normal1'
324
+ assert requests [4 ].url == 'https://example.com/normal2'
325
+ assert requests [5 ].url == 'https://example.com/normal3'
326
+
327
+
277
328
async def test_fetch_next_request_and_mark_handled (rq : RequestQueue ) -> None :
278
329
"""Test fetching and marking requests as handled."""
279
330
# Add some requests
0 commit comments