|
| 1 | +""" |
| 2 | +Integration tests for the client module. |
| 3 | +
|
| 4 | +These tests verify the functionality of the client module, including |
| 5 | +the singleton client pattern and its thread safety under concurrent access. |
| 6 | +""" |
| 7 | + |
| 8 | +import asyncio |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from holoviz_mcp.client import call_tool |
| 13 | + |
| 14 | + |
| 15 | +class TestClientSingleton: |
| 16 | + """Tests for the singleton client pattern.""" |
| 17 | + |
| 18 | + @pytest.mark.asyncio |
| 19 | + async def test_concurrent_client_initialization(self): |
| 20 | + """Test that concurrent calls to call_tool don't create multiple clients. |
| 21 | +
|
| 22 | + This test verifies that the singleton client pattern handles race |
| 23 | + conditions properly when multiple async tasks call call_tool |
| 24 | + concurrently before the client is initialized. The asyncio.Lock |
| 25 | + ensures only one task initializes the client. |
| 26 | + """ |
| 27 | + # Reset the client state for testing |
| 28 | + # Note: This directly manipulates module state for testing purposes |
| 29 | + import holoviz_mcp.client as client_module |
| 30 | + |
| 31 | + client_module._CLIENT = None |
| 32 | + |
| 33 | + # Create multiple concurrent tasks that all call call_tool |
| 34 | + tasks = [ |
| 35 | + call_tool("panel_list_components", {}), |
| 36 | + call_tool("panel_list_components", {}), |
| 37 | + call_tool("panel_list_components", {}), |
| 38 | + call_tool("panel_list_components", {}), |
| 39 | + call_tool("panel_list_components", {}), |
| 40 | + ] |
| 41 | + |
| 42 | + # Run all tasks concurrently |
| 43 | + results = await asyncio.gather(*tasks) |
| 44 | + |
| 45 | + # All tasks should complete successfully |
| 46 | + assert len(results) == 5 |
| 47 | + for result in results: |
| 48 | + assert result is not None |
| 49 | + # Results should contain component data |
| 50 | + assert isinstance(result.data, list) |
| 51 | + |
| 52 | + @pytest.mark.asyncio |
| 53 | + async def test_client_reuse(self): |
| 54 | + """Test that the client is reused across multiple calls.""" |
| 55 | + # First call |
| 56 | + result1 = await call_tool("panel_list_components", {}) |
| 57 | + assert result1 is not None |
| 58 | + |
| 59 | + # Second call should reuse the same client |
| 60 | + result2 = await call_tool("panel_list_components", {}) |
| 61 | + assert result2 is not None |
| 62 | + |
| 63 | + # Both should return valid data |
| 64 | + assert isinstance(result1.data, list) |
| 65 | + assert isinstance(result2.data, list) |
| 66 | + |
| 67 | + @pytest.mark.asyncio |
| 68 | + async def test_concurrent_tool_calls(self): |
| 69 | + """Test multiple concurrent tool calls using the singleton client.""" |
| 70 | + # Create tasks that call different tools concurrently |
| 71 | + tasks = [ |
| 72 | + call_tool("panel_list_components", {}), |
| 73 | + call_tool("panel_list_packages", {}), |
| 74 | + call_tool("panel_list_components", {}), |
| 75 | + ] |
| 76 | + |
| 77 | + # Run all tasks concurrently |
| 78 | + results = await asyncio.gather(*tasks) |
| 79 | + |
| 80 | + # All tasks should complete successfully |
| 81 | + assert len(results) == 3 |
| 82 | + for result in results: |
| 83 | + assert result is not None |
| 84 | + assert isinstance(result.data, list) |
0 commit comments