Skip to content

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

Fix limiting subprocess run time in tests. (#6933)

Andrey Rakhmatullin8d82e7bd2731
diff --git a/tests/test_command_runspider.py b/tests/test_command_runspider.py
index c1a6d9b18..0c12a59f7 100644
--- a/tests/test_command_runspider.py
+++ b/tests/test_command_runspider.py
@@ -112,6 +112,11 @@ class MySpider(scrapy.Spider):
name = 'myspider'
start_urls = ['http://localhost:12345']
+ custom_settings = {
+ "ROBOTSTXT_OBEY": False,
+ "RETRY_ENABLED": False,
+ }
+
def parse(self, response):
return {'test': 'value'}
"""
diff --git a/tests/test_commands.py b/tests/test_commands.py
index 1b36fbb31..a749de9fe 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -9,10 +9,11 @@ from io import StringIO
from pathlib import Path
from shutil import rmtree
from tempfile import TemporaryFile, mkdtemp
-from threading import Timer
from typing import TYPE_CHECKING, Any
from unittest import mock
+import pytest
+
import scrapy
from scrapy.cmdline import _pop_command_name, _print_unknown_command_msg
from scrapy.commands import ScrapyCommand, ScrapyHelpFormatter, view
@@ -100,17 +101,12 @@ class TestProjectBase:
**popen_kwargs,
)
- def kill_proc():
+ try:
+ stdout, stderr = p.communicate(timeout=15)
+ except subprocess.TimeoutExpired:
p.kill()
p.communicate()
- raise AssertionError("Command took too much time to complete")
-
- timer = Timer(15, kill_proc)
- try:
- timer.start()
- stdout, stderr = p.communicate()
- finally:
- timer.cancel()
+ pytest.fail("Command took too much time to complete")
return p, to_unicode(stdout), to_unicode(stderr)
diff --git a/tests/test_engine.py b/tests/test_engine.py
index 9a3075f31..7612c6200 100644
--- a/tests/test_engine.py
+++ b/tests/test_engine.py
@@ -17,7 +17,6 @@ from collections import defaultdict
from dataclasses import dataclass
from logging import DEBUG
from pathlib import Path
-from threading import Timer
from unittest.mock import Mock
from urllib.parse import urlparse
@@ -495,17 +494,12 @@ class TestEngine(TestEngineBase):
stderr=subprocess.PIPE,
)
- def kill_proc():
+ try:
+ _, stderr = p.communicate(timeout=3)
+ except subprocess.TimeoutExpired:
p.kill()
p.communicate()
- raise AssertionError("Command took too much time to complete")
-
- timer = Timer(15, kill_proc)
- try:
- timer.start()
- _, stderr = p.communicate()
- finally:
- timer.cancel()
+ pytest.fail("Command took too much time to complete")
stderr_str = stderr.decode("utf-8")
assert "AttributeError" not in stderr_str, stderr_str