Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Add a test function that uses asyncio.Queue().

Andrey Rakhmatullin2b9254c2bde7
diff --git a/scrapy/utils/test.py b/scrapy/utils/test.py
index 307c25352..0f4cf8091 100644
--- a/scrapy/utils/test.py
+++ b/scrapy/utils/test.py
@@ -1,7 +1,7 @@
"""
This module contains some assorted functions used in tests
"""
-
+import asyncio
import os
from importlib import import_module
@@ -96,3 +96,10 @@ def assert_samelines(testcase, text1, text2, msg=None):
line endings between platforms
"""
testcase.assertEqual(text1.splitlines(), text2.splitlines(), msg)
+
+
+def get_from_asyncio_queue(value):
+ q = asyncio.Queue()
+ getter = q.get()
+ q.put_nowait(value)
+ return getter
diff --git a/tests/test_downloadermiddleware.py b/tests/test_downloadermiddleware.py
index 3943cecf7..3dd4f2351 100644
--- a/tests/test_downloadermiddleware.py
+++ b/tests/test_downloadermiddleware.py
@@ -11,7 +11,7 @@ from scrapy.http import Request, Response
from scrapy.spiders import Spider
from scrapy.exceptions import _InvalidOutput
from scrapy.core.downloader.middleware import DownloaderMiddlewareManager
-from scrapy.utils.test import get_crawler
+from scrapy.utils.test import get_crawler, get_from_asyncio_queue
from scrapy.utils.python import to_bytes
@@ -240,7 +240,8 @@ class MiddlewareUsingCoro(ManagerTestCase):
class CoroMiddleware:
async def process_request(self, request, spider):
await asyncio.sleep(0.1)
- return resp
+ result = await get_from_asyncio_queue(resp)
+ return result
self.mwman._add_middleware(CoroMiddleware())
req = Request('http://example.com/index.html')