Skip to content

Commit

Permalink
test: Move test_middleware_wait to non-pytest file, since it raises "…
Browse files Browse the repository at this point in the history
…twisted.internet.error.ReactorAlreadyRunning"
  • Loading branch information
jpmckinney committed Jan 24, 2024
1 parent 55e20c8 commit dbe3b66
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 35 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
TEST_API_URL: http://localhost:${{ job.services.httpbin.ports[80] }}/anything/
# For requests.post() in KingfisherProcessAPI2._post_synchronous().
run: pytest -W error -W ignore::ResourceWarning -rs --cov kingfisher_scrapy
- run: python test_delayed_request_middleware.py
- env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: coveralls --service=github
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/nonlinux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ jobs:
- env:
CI_SKIP: true
run: pytest -W error -rs --cov kingfisher_scrapy
- run: python test_delayed_request_middleware.py
44 changes: 44 additions & 0 deletions test_delayed_request_middleware.py
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()
35 changes: 0 additions & 35 deletions tests/middlewares/test_delayed_request_middleware.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import time

import pytest
from scrapy import Request
from scrapy.core.downloader import DownloaderMiddlewareManager
from twisted.internet.defer import Deferred
from twisted.trial.unittest import TestCase

from kingfisher_scrapy.downloadermiddlewares import DelayedRequestMiddleware
from tests import spider_with_crawler
Expand All @@ -21,34 +17,3 @@ def test_middleware_output(meta, expected):
output = middleware.process_request(request, spider)

assert isinstance(output, expected)


def test_middleware_wait():

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

0 comments on commit dbe3b66

Please sign in to comment.