-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Move test_middleware_wait to non-pytest file, since it raises "…
…twisted.internet.error.ReactorAlreadyRunning"
- Loading branch information
1 parent
55e20c8
commit dbe3b66
Showing
4 changed files
with
46 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
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': { | ||
'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) | ||
|
||
end = time.time() | ||
|
||
assert results == [request] | ||
assert 1 <= end - start <= 1.01 | ||
|
||
|
||
if __name__ == "__main__": | ||
delayed_request_middleware() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters