From f2b66d900559e7a21f4ebc8d795c36fa30d26ce0 Mon Sep 17 00:00:00 2001 From: Ayman Date: Tue, 26 Aug 2025 20:16:48 +0100 Subject: [PATCH] tests: make memusage integration checks one-shot to avoid cross-test logging races --- tests/test_memusage_integration.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/test_memusage_integration.py b/tests/test_memusage_integration.py index fc4921bad..21c397ff5 100644 --- a/tests/test_memusage_integration.py +++ b/tests/test_memusage_integration.py @@ -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)