|
1 | | -import os |
2 | 1 | from time import sleep |
3 | 2 | from unittest.mock import Mock, patch |
4 | 3 |
|
5 | 4 | import openai |
6 | 5 | import pytest |
7 | 6 |
|
8 | 7 | from langfuse._client.client import Langfuse |
9 | | -from langfuse._client.environment_variables import ( |
10 | | - LANGFUSE_PROMPT_CACHE_DEFAULT_TTL_SECONDS, |
11 | | -) |
12 | 8 | from langfuse._utils.prompt_cache import ( |
13 | 9 | DEFAULT_PROMPT_CACHE_TTL_SECONDS, |
14 | 10 | PromptCacheItem, |
@@ -1418,90 +1414,3 @@ def test_update_prompt(): |
1418 | 1414 | expected_labels = sorted(["latest", "doe", "production", "john"]) |
1419 | 1415 | assert sorted(fetched_prompt.labels) == expected_labels |
1420 | 1416 | assert sorted(updated_prompt.labels) == expected_labels |
1421 | | - |
1422 | | - |
1423 | | -def test_environment_variable_override_prompt_cache_ttl(): |
1424 | | - """Test that LANGFUSE_PROMPT_CACHE_DEFAULT_TTL_SECONDS environment variable overrides default TTL.""" |
1425 | | - # Set environment variable to override default TTL |
1426 | | - os.environ[LANGFUSE_PROMPT_CACHE_DEFAULT_TTL_SECONDS] = "120" |
1427 | | - |
1428 | | - # Create a new Langfuse instance to pick up the environment variable |
1429 | | - langfuse = Langfuse( |
1430 | | - public_key="test-public-key", |
1431 | | - secret_key="test-secret-key", |
1432 | | - host="https://mock-host.com", |
1433 | | - ) |
1434 | | - langfuse.api = Mock() |
1435 | | - |
1436 | | - prompt_name = "test_env_override_ttl" |
1437 | | - prompt = Prompt_Text( |
1438 | | - name=prompt_name, |
1439 | | - version=1, |
1440 | | - prompt="Test prompt with env override", |
1441 | | - type="text", |
1442 | | - labels=[], |
1443 | | - config={}, |
1444 | | - tags=[], |
1445 | | - ) |
1446 | | - prompt_client = TextPromptClient(prompt) |
1447 | | - |
1448 | | - mock_server_call = langfuse.api.prompts.get |
1449 | | - mock_server_call.return_value = prompt |
1450 | | - |
1451 | | - # Mock time to control cache expiration |
1452 | | - with patch.object(PromptCacheItem, "get_epoch_seconds") as mock_time: |
1453 | | - mock_time.return_value = 0 |
1454 | | - |
1455 | | - # First call - should cache the prompt |
1456 | | - result1 = langfuse.get_prompt(prompt_name) |
1457 | | - assert mock_server_call.call_count == 1 |
1458 | | - assert result1 == prompt_client |
1459 | | - |
1460 | | - # Check that prompt is cached |
1461 | | - cached_item = langfuse._resources.prompt_cache.get( |
1462 | | - langfuse._resources.prompt_cache.generate_cache_key( |
1463 | | - prompt_name, version=None, label=None |
1464 | | - ) |
1465 | | - ) |
1466 | | - assert cached_item is not None |
1467 | | - assert cached_item.value == prompt_client |
1468 | | - |
1469 | | - # Debug: check the cache item's expiry time |
1470 | | - print(f"DEBUG: Cache item expiry: {cached_item._expiry}") |
1471 | | - print(f"DEBUG: Current mock time: {mock_time.return_value}") |
1472 | | - print(f"DEBUG: Is expired? {cached_item.is_expired()}") |
1473 | | - |
1474 | | - # Set time to 60 seconds (before new TTL of 120 seconds) |
1475 | | - mock_time.return_value = 60 |
1476 | | - |
1477 | | - # Second call - should still use cache |
1478 | | - result2 = langfuse.get_prompt(prompt_name) |
1479 | | - assert mock_server_call.call_count == 1 # No new server call |
1480 | | - assert result2 == prompt_client |
1481 | | - |
1482 | | - # Set time to 120 seconds (at TTL expiration) |
1483 | | - mock_time.return_value = 120 |
1484 | | - |
1485 | | - # Third call - should still use cache (stale cache behavior) |
1486 | | - result3 = langfuse.get_prompt(prompt_name) |
1487 | | - assert result3 == prompt_client |
1488 | | - |
1489 | | - # Wait for background refresh to complete |
1490 | | - while True: |
1491 | | - if langfuse._resources.prompt_cache._task_manager.active_tasks() == 0: |
1492 | | - break |
1493 | | - sleep(0.1) |
1494 | | - |
1495 | | - # Should have made a new server call for refresh |
1496 | | - assert mock_server_call.call_count == 2 |
1497 | | - |
1498 | | - # Set time to 121 seconds (after TTL expiration) |
1499 | | - mock_time.return_value = 121 |
1500 | | - |
1501 | | - # Fourth call - should use refreshed cache |
1502 | | - result4 = langfuse.get_prompt(prompt_name) |
1503 | | - assert result4 == prompt_client |
1504 | | - |
1505 | | - # Clean up environment variable |
1506 | | - if LANGFUSE_PROMPT_CACHE_DEFAULT_TTL_SECONDS in os.environ: |
1507 | | - del os.environ[LANGFUSE_PROMPT_CACHE_DEFAULT_TTL_SECONDS] |
0 commit comments