Solve timing issues with HTTP cache tests? (#7692)

This commit is contained in:
Adrian 2026-06-29 14:21:20 +02:00 committed by GitHub
parent 185d6b9a20
commit 9559cbee1e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import tempfile
import time
from contextlib import contextmanager
from typing import TYPE_CHECKING, Any
from unittest import mock
import pytest
@ -47,7 +48,6 @@ class TestBase:
settings = {
"HTTPCACHE_ENABLED": True,
"HTTPCACHE_DIR": self.tmpdir,
"HTTPCACHE_EXPIRATION_SECS": 1,
"HTTPCACHE_IGNORE_HTTP_CODES": [],
"HTTPCACHE_POLICY": self.policy_class,
"HTTPCACHE_STORAGE": self.storage_class,
@ -94,7 +94,7 @@ class StorageTestMixin:
"""Mixin containing storage-specific test methods."""
def test_storage(self):
with self._storage() as (storage, crawler):
with self._storage(HTTPCACHE_EXPIRATION_SECS=1) as (storage, crawler):
request2 = self.request.copy()
assert storage.retrieve_response(crawler.spider, request2) is None
@ -103,15 +103,17 @@ class StorageTestMixin:
assert isinstance(response2, HtmlResponse) # content-type header
self.assertEqualResponse(self.response, response2)
time.sleep(2) # wait for cache to expire
assert storage.retrieve_response(crawler.spider, request2) is None
expired = time.time() + storage.expiration_secs + 1
with mock.patch("scrapy.extensions.httpcache.time", return_value=expired):
assert storage.retrieve_response(crawler.spider, request2) is None
def test_storage_never_expire(self):
with self._storage(HTTPCACHE_EXPIRATION_SECS=0) as (storage, crawler):
assert storage.retrieve_response(crawler.spider, self.request) is None
storage.store_response(crawler.spider, self.request, self.response)
time.sleep(0.5) # give the chance to expire
assert storage.retrieve_response(crawler.spider, self.request)
future = time.time() + 10**6
with mock.patch("scrapy.extensions.httpcache.time", return_value=future):
assert storage.retrieve_response(crawler.spider, self.request)
def test_storage_no_content_type_header(self):
"""Test that the response body is used to get the right response class