mirror of
https://github.com/tiennm99/litellm.git
synced 2026-07-11 19:48:29 +00:00
fix(test): clear tokenizer LRU cache for test isolation
The _select_tokenizer_helper function is decorated with @lru_cache, which causes test failures when tests run sequentially with --dist=loadscope. Previous tests' cached results prevent from_pretrained from being called, causing mock assertions to fail. Implemented triple-layer cache clearing: 1. Module-level clear on import 2. Class-level clear in setUpClass 3. Function-level clear in setUp + pytest fixture This ensures test isolation while allowing --dist=loadscope to provide better overall CI stability (70% pass rate vs 40% without loadscope). Fixes the intermittent failure in TestTokenizerSelection where 'from_pretrained' mock was never called due to cache hits. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -491,7 +491,32 @@ from unittest.mock import MagicMock, patch
|
||||
from litellm.utils import _select_tokenizer_helper, claude_json_str, encoding
|
||||
|
||||
|
||||
# Clear the cache at module load to ensure clean state
|
||||
_select_tokenizer_helper.cache_clear()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="function")
|
||||
def clear_tokenizer_cache_before_test():
|
||||
"""Clear the LRU cache before each test to ensure test isolation.
|
||||
|
||||
The _select_tokenizer_helper function is decorated with @lru_cache,
|
||||
which can cause cache hits from previous tests when running with
|
||||
--dist=loadscope (tests from same file run on same worker).
|
||||
"""
|
||||
# Clear before test
|
||||
_select_tokenizer_helper.cache_clear()
|
||||
yield
|
||||
|
||||
|
||||
class TestTokenizerSelection(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
"""Clear cache before class starts."""
|
||||
_select_tokenizer_helper.cache_clear()
|
||||
|
||||
def setUp(self):
|
||||
"""Clear cache before each test method."""
|
||||
_select_tokenizer_helper.cache_clear()
|
||||
@patch("litellm.utils.Tokenizer.from_pretrained")
|
||||
def test_llama3_tokenizer_api_failure(self, mock_from_pretrained):
|
||||
# Setup mock to raise an error
|
||||
|
||||
Reference in New Issue
Block a user