|
| 1 | +"""Test environment setting on Langfuse get_client() function.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from langfuse import Langfuse |
| 5 | +from langfuse._client.get_client import get_client |
| 6 | + |
| 7 | + |
| 8 | +def test_get_client_preserves_environment(): |
| 9 | + """Test that get_client() preserves the environment when returning existing clients.""" |
| 10 | + test_env = "production-test-env" |
| 11 | + original_client = Langfuse(environment=test_env) |
| 12 | + |
| 13 | + # Verify the original client has the correct environment |
| 14 | + assert original_client._environment == test_env, ( |
| 15 | + f"original client environment should be '{test_env}', got '{original_client._environment}'" |
| 16 | + ) |
| 17 | + |
| 18 | + # call get_client() should return a client with the same environment |
| 19 | + retrieved_client = get_client() |
| 20 | + |
| 21 | + assert retrieved_client._environment == test_env, ( |
| 22 | + f"get_client() should return client with environment '{test_env}', got '{retrieved_client._environment}'" |
| 23 | + ) |
| 24 | + |
| 25 | + original_client.shutdown() |
| 26 | + |
| 27 | + |
| 28 | +def test_get_client_with_multiple_environments(): |
| 29 | + """Test get_client() behavior with multiple clients having different environments.""" |
| 30 | + env_a = "environment-a" |
| 31 | + env_b = "environment-b" |
| 32 | + |
| 33 | + client_a = Langfuse(public_key="pk-a", secret_key="sk-a", environment=env_a) |
| 34 | + client_b = Langfuse(public_key="pk-b", secret_key="sk-b", environment=env_b) |
| 35 | + |
| 36 | + # original clients should have correct environments |
| 37 | + assert client_a._environment == env_a |
| 38 | + assert client_b._environment == env_b |
| 39 | + |
| 40 | + # Get clients using get_client() with specific public keys |
| 41 | + retrieved_a = get_client(public_key="pk-a") |
| 42 | + retrieved_b = get_client(public_key="pk-b") |
| 43 | + |
| 44 | + # should have the same environments as the originals |
| 45 | + assert retrieved_a._environment == env_a, ( |
| 46 | + f"Expected client A environment to be '{env_a}', got '{retrieved_a._environment}'" |
| 47 | + ) |
| 48 | + assert retrieved_b._environment == env_b, ( |
| 49 | + f"Expected client B environment to be '{env_b}', got '{retrieved_b._environment}'" |
| 50 | + ) |
| 51 | + |
| 52 | + client_a.shutdown() |
| 53 | + client_b.shutdown() |
| 54 | + |
| 55 | + |
| 56 | +def test_get_client_single_client_environment(): |
| 57 | + """Test that get_client() preserves environment in single-client scenario.""" |
| 58 | + # Clean state - ensure no existing clients |
| 59 | + from langfuse._client.resource_manager import LangfuseResourceManager |
| 60 | + |
| 61 | + with LangfuseResourceManager._lock: |
| 62 | + LangfuseResourceManager._instances.clear() |
| 63 | + |
| 64 | + test_env = "single-client-env" |
| 65 | + |
| 66 | + client = Langfuse(environment=test_env) |
| 67 | + assert client._environment == test_env |
| 68 | + |
| 69 | + # get_client() should return a client with the same environment |
| 70 | + retrieved = get_client() |
| 71 | + |
| 72 | + # This assertion demonstrates the bug |
| 73 | + assert retrieved._environment == test_env, ( |
| 74 | + f"Expected single client scenario to preserve environment '{test_env}', got '{retrieved._environment}'" |
| 75 | + ) |
| 76 | + |
| 77 | + client.shutdown() |
0 commit comments