tests: make memusage integration checks one-shot to avoid cross-test logging races

This commit is contained in:
Ayman 2025-08-26 20:16:48 +01:00
parent 62a133df9a
commit f2b66d9005
1 changed files with 24 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import pytest
from twisted.internet.defer import inlineCallbacks
from scrapy import signals
from scrapy.extensions import memusage as memusage_mod
from scrapy.extensions.memusage import MemoryUsage
from scrapy.spiders import Spider
from scrapy.utils.test import get_crawler
@ -35,6 +36,23 @@ class _LoopSpider(Spider):
)
class _OneShotLoop:
"""Test stub for create_looping_call: run once immediately, no background task."""
def __init__(self, func):
self.func = func
self.running = False
def start(self, _interval, now: bool = False, **_kw):
self.running = True
if now:
self.func()
return self
def stop(self):
self.running = False
@inlineCallbacks
def test_memusage_limit_closes_spider_with_reason_and_error_log(caplog, monkeypatch):
url = "data:,"
@ -45,6 +63,9 @@ def test_memusage_limit_closes_spider_with_reason_and_error_log(caplog, monkeypa
"LOG_LEVEL": "INFO",
}
# Avoid background LoopingCall that can log after the test finishes.
monkeypatch.setattr(memusage_mod, "create_looping_call", lambda f: _OneShotLoop(f))
MB = 1024 * 1024
state = {"high": False}
@ -80,6 +101,9 @@ def test_memusage_warning_logs_but_allows_normal_finish(caplog, monkeypatch):
"LOG_LEVEL": "INFO",
}
# Avoid background LoopingCall that can log after the test finishes.
monkeypatch.setattr(memusage_mod, "create_looping_call", lambda f: _OneShotLoop(f))
MB = 1024 * 1024
monkeypatch.setattr(MemoryUsage, "get_virtual_size", lambda self: 75 * MB)