-
Notifications
You must be signed in to change notification settings - Fork 12
/
test_delayed_request_middleware.py
45 lines (32 loc) · 1.44 KB
/
test_delayed_request_middleware.py
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
import time
from scrapy import Request
from scrapy.core.downloader import DownloaderMiddlewareManager
from twisted.internet.defer import Deferred
from twisted.trial.unittest import TestCase
from tests import spider_with_crawler
# Running this as a pytest test raises "twisted.internet.error.ReactorAlreadyRunning".
def delayed_request_middleware():
def download_func(spider, request):
return request
spider = spider_with_crawler(settings={
'DOWNLOADER_MIDDLEWARES': {
'scrapy.downloadermiddlewares.offsite.OffsiteMiddleware': None,
'kingfisher_scrapy.downloadermiddlewares.DelayedRequestMiddleware': 543,
},
})
request = Request('http://example.com', meta={'wait_time': 1})
# We send the request to all the downloader middlewares, including the delayed request middleware.
manager = DownloaderMiddlewareManager.from_crawler(spider.crawler)
downloaded = manager.download(download_func, request, spider)
assert isinstance(downloaded, Deferred)
start = time.time()
# https://github.com/scrapy/scrapy/blob/28262d4b241744aa7c090702db9a89411e3bbf9a/tests/test_downloadermiddleware.py#L36
results = []
downloaded.addBoth(results.append)
test = TestCase()
test._wait(downloaded) # noqa: SLF001
spent = time.time() - start
assert results == [request], results
assert 1 <= spent <= 1.5, spent
if __name__ == "__main__":
delayed_request_middleware()