mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-09 22:31:41 +00:00
fix(test): improve Langfuse test isolation to prevent flaky failures
Enhances test isolation in TestLangfuseUsageDetails by ensuring the logger instance is completely fresh for each test and properly cleaned up afterward. Changes: - Clear any class-level cached Langfuse clients before creating logger - Reset logger's cached client instances in setUp - Properly clean up logger instance and its state in tearDown Root Cause: The test_log_langfuse_v2_handles_null_usage_values test was failing when run after other tests due to lingering state in the logger instance. While the test passes in isolation, test ordering issues caused it to fail with "Expected 'generation' to have been called once. Called 0 times." This builds on PR #21214 which added sys.modules cleanup, but that wasn't sufficient to prevent all state leakage between tests. Fixes: Test isolation issues in test_log_langfuse_v2_handles_null_usage_values Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
068c994d89
commit
c4fa7e9298
1 changed files with 18 additions and 2 deletions
|
|
@ -76,13 +76,20 @@ class TestLangfuseUsageDetails(unittest.TestCase):
|
|||
sys.modules["langfuse"] = self.mock_langfuse
|
||||
sys.modules["langfuse"].Langfuse = self.mock_langfuse_class
|
||||
|
||||
# Create the logger
|
||||
# Create a fresh logger instance for each test
|
||||
# Force a clean state by clearing any class-level cached state
|
||||
if hasattr(LangFuseLogger, '_langfuse_clients'):
|
||||
LangFuseLogger._langfuse_clients = {}
|
||||
|
||||
self.logger = LangFuseLogger()
|
||||
|
||||
|
||||
# Explicitly set the Langfuse client to our mock
|
||||
self.logger.Langfuse = self.mock_langfuse_client
|
||||
# Ensure langfuse_sdk_version is set correctly for _supports_* methods
|
||||
self.logger.langfuse_sdk_version = "3.0.0"
|
||||
# Reset any cached client instances
|
||||
if hasattr(self.logger, '_langfuse_client_cache'):
|
||||
self.logger._langfuse_client_cache = None
|
||||
|
||||
# Add the log_event_on_langfuse method to the instance
|
||||
def log_event_on_langfuse(
|
||||
|
|
@ -123,6 +130,15 @@ class TestLangfuseUsageDetails(unittest.TestCase):
|
|||
self.logger._is_langfuse_v2 = types.MethodType(mock_is_langfuse_v2, self.logger)
|
||||
|
||||
def tearDown(self):
|
||||
# Clean up logger instance to prevent state leakage
|
||||
if hasattr(self, 'logger'):
|
||||
# Reset logger's Langfuse client
|
||||
self.logger.Langfuse = None
|
||||
# Clear any cached state
|
||||
if hasattr(self.logger, '_langfuse_client_cache'):
|
||||
self.logger._langfuse_client_cache = None
|
||||
del self.logger
|
||||
|
||||
self.env_patcher.stop()
|
||||
self.langfuse_module_patcher.stop() # patch.dict automatically restores sys.modules
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue