|
1 | 1 | """Unit tests for health endpoints handlers."""
|
2 | 2 |
|
| 3 | +import time |
3 | 4 | from unittest.mock import patch
|
4 | 5 |
|
5 | 6 | import pytest
|
@@ -68,6 +69,52 @@ def test_readiness_probe_llm_check__state_cache(mocked_load_llm):
|
68 | 69 | assert mocked_load_llm.call_count == 1
|
69 | 70 |
|
70 | 71 |
|
| 72 | +@patch("ols.app.endpoints.health.llm_is_ready_persistent_state", new=False) |
| 73 | +@patch("ols.app.endpoints.health.load_llm") |
| 74 | +def test_readiness_probe_llm_check__state_cache_not_expired(mocked_load_llm): |
| 75 | + """Test the scenario with cache not expired - LLM check is done only once.""" |
| 76 | + try: |
| 77 | + # Set cache expiration time to 1 sec. |
| 78 | + config.ols_config.expire_llm_is_ready_persistent_state = 1 |
| 79 | + mocked_load_llm.return_value = MockedLLM(invoke_return="message") |
| 80 | + assert llm_is_ready() |
| 81 | + assert mocked_load_llm.call_count == 1 |
| 82 | + |
| 83 | + response = readiness_probe_get_method() |
| 84 | + assert response == ReadinessResponse(ready=True, reason="service is ready") |
| 85 | + |
| 86 | + # try again and check if the llm function was invoked again - it shouldn't |
| 87 | + llm_is_ready() |
| 88 | + assert mocked_load_llm.call_count == 1 |
| 89 | + finally: |
| 90 | + # Reset the expire_llm_is_ready_persistent_state option. |
| 91 | + config.ols_config.expire_llm_is_ready_persistent_state = -1 |
| 92 | + |
| 93 | + |
| 94 | +@patch("ols.app.endpoints.health.llm_is_ready_persistent_state", new=False) |
| 95 | +@patch("ols.app.endpoints.health.load_llm") |
| 96 | +def test_readiness_probe_llm_check__state_cache_expired(mocked_load_llm): |
| 97 | + """Test the scenario with cache expired - LLM check is done twice.""" |
| 98 | + try: |
| 99 | + # Set cache expiration time to 1 sec. |
| 100 | + config.ols_config.expire_llm_is_ready_persistent_state = 1 |
| 101 | + mocked_load_llm.return_value = MockedLLM(invoke_return="message") |
| 102 | + assert llm_is_ready() |
| 103 | + assert mocked_load_llm.call_count == 1 |
| 104 | + |
| 105 | + response = readiness_probe_get_method() |
| 106 | + assert response == ReadinessResponse(ready=True, reason="service is ready") |
| 107 | + # Wait for 1.5 secs and let the cache get expired. |
| 108 | + time.sleep(1.5) |
| 109 | + |
| 110 | + # try again and check if the llm function was invoked again - it should. |
| 111 | + llm_is_ready() |
| 112 | + assert mocked_load_llm.call_count == 2 |
| 113 | + finally: |
| 114 | + # Reset the expire_llm_is_ready_persistent_state option. |
| 115 | + config.ols_config.expire_llm_is_ready_persistent_state = -1 |
| 116 | + |
| 117 | + |
71 | 118 | @patch("ols.app.endpoints.health.llm_is_ready_persistent_state", new=False)
|
72 | 119 | @patch("ols.app.endpoints.health.load_llm")
|
73 | 120 | def test_readiness_probe_llm_check__llm_raise(mocked_load_llm):
|
|
0 commit comments