完成渠道模块的单元测试目录搭建,新增多个领域模型、端口、中间件、事件、服务以及基础设施层的单元测试文件,同时补充了conftest.py的环境变量配置,完善测试基础环境。
72 lines
2.3 KiB
Python
72 lines
2.3 KiB
Python
from __future__ import annotations
|
|
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
|
|
from yuxi.channel.interfaces.sse.endpoint import SSEEndpoint
|
|
|
|
|
|
class TestSSEEndpoint:
|
|
@pytest.fixture
|
|
def endpoint(self):
|
|
return SSEEndpoint()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_start(self, endpoint):
|
|
await endpoint.start()
|
|
assert endpoint.is_running is True
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_stop(self, endpoint):
|
|
await endpoint.start()
|
|
await endpoint.stop()
|
|
assert endpoint.is_running is False
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_subscribe(self, endpoint):
|
|
await endpoint.start()
|
|
queue = await endpoint.subscribe("client1")
|
|
assert "client1" in endpoint._clients
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_unsubscribe(self, endpoint):
|
|
await endpoint.start()
|
|
queue = await endpoint.subscribe("client1")
|
|
await endpoint.unsubscribe("client1")
|
|
assert "client1" not in endpoint._clients
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_broadcast(self, endpoint):
|
|
await endpoint.start()
|
|
queue = await endpoint.subscribe("client1")
|
|
await endpoint.broadcast({"type": "message", "data": "test"})
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_broadcast_shutdown(self, endpoint):
|
|
await endpoint.start()
|
|
queue = await endpoint.subscribe("client1")
|
|
await endpoint.broadcast_shutdown()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_send_to_client(self, endpoint):
|
|
await endpoint.start()
|
|
queue = await endpoint.subscribe("client1")
|
|
await endpoint.send_to_client("client1", {"type": "message", "data": "test"})
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_send_to_nonexistent_client(self, endpoint):
|
|
await endpoint.start()
|
|
await endpoint.send_to_client("nonexistent", {"type": "message", "data": "test"})
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_client_queue(self, endpoint):
|
|
await endpoint.start()
|
|
queue = await endpoint.subscribe("client1")
|
|
result = endpoint.get_client_queue("client1")
|
|
assert result is not None
|
|
|
|
def test_get_client_queue_not_found(self, endpoint):
|
|
result = endpoint.get_client_queue("nonexistent")
|
|
assert result is None
|