ForcePilot/backend/test/unit/channel/startup/test_prometheus_metrics.py
Kris 6c95dc006a test: 新增渠道模块全链路单元测试用例与目录结构
完成渠道模块的单元测试目录搭建,新增多个领域模型、端口、中间件、事件、服务以及基础设施层的单元测试文件,同时补充了conftest.py的环境变量配置,完善测试基础环境。
2026-05-30 21:55:35 +08:00

738 lines
24 KiB
Python

from __future__ import annotations
from unittest.mock import MagicMock
import pytest
from yuxi.channel.infrastructure.metrics.prometheus_metrics import PrometheusMetrics
class TestPrometheusMetrics:
@pytest.fixture
def metrics(self):
return PrometheusMetrics()
@pytest.mark.asyncio
async def test_record_inbound_received(self, metrics):
await metrics.record_inbound_received("web")
@pytest.mark.asyncio
async def test_record_inbound_blocked(self, metrics):
await metrics.record_inbound_blocked("web", "rate_limit")
@pytest.mark.asyncio
async def test_record_outbound_sent(self, metrics):
await metrics.record_outbound_sent("web")
@pytest.mark.asyncio
async def test_record_outbox_enqueued(self, metrics):
await metrics.record_outbox_enqueued()
@pytest.mark.asyncio
async def test_record_outbox_delivered(self, metrics):
await metrics.record_outbox_delivered()
@pytest.mark.asyncio
async def test_record_outbox_failed(self, metrics):
await metrics.record_outbox_failed()
@pytest.mark.asyncio
async def test_record_worker_dispatch_total(self, metrics):
await metrics.record_worker_dispatch_total()
@pytest.mark.asyncio
async def test_record_worker_dispatch_success(self, metrics):
await metrics.record_worker_dispatch_success()
@pytest.mark.asyncio
async def test_record_worker_dispatch_failure(self, metrics):
await metrics.record_worker_dispatch_failure()
@pytest.mark.asyncio
async def test_record_ws_connection(self, metrics):
await metrics.record_ws_connection("feishu")
@pytest.mark.asyncio
async def test_record_ws_disconnection(self, metrics):
await metrics.record_ws_disconnection("feishu")
@pytest.mark.asyncio
async def test_record_ws_message(self, metrics):
await metrics.record_ws_message("feishu")
@pytest.mark.asyncio
async def test_record_ws_error(self, metrics):
await metrics.record_ws_error("feishu")
@pytest.mark.asyncio
async def test_record_sse_connection(self, metrics):
await metrics.record_sse_connection()
@pytest.mark.asyncio
async def test_record_sse_disconnection(self, metrics):
await metrics.record_sse_disconnection()
@pytest.mark.asyncio
async def test_record_sse_message(self, metrics):
await metrics.record_sse_message()
@pytest.mark.asyncio
async def test_record_sse_error(self, metrics):
await metrics.record_sse_error()
@pytest.mark.asyncio
async def test_record_auth_success(self, metrics):
await metrics.record_auth_success()
@pytest.mark.asyncio
async def test_record_auth_failure(self, metrics):
await metrics.record_auth_failure()
@pytest.mark.asyncio
async def test_record_session_created(self, metrics):
await metrics.record_session_created()
@pytest.mark.asyncio
async def test_record_session_resolved(self, metrics):
await metrics.record_session_resolved()
@pytest.mark.asyncio
async def test_record_message_saved(self, metrics):
await metrics.record_message_saved()
@pytest.mark.asyncio
async def test_record_message_log_created(self, metrics):
await metrics.record_message_log_created()
@pytest.mark.asyncio
async def test_record_binding_created(self, metrics):
await metrics.record_binding_created()
@pytest.mark.asyncio
async def test_record_binding_deleted(self, metrics):
await metrics.record_binding_deleted()
@pytest.mark.asyncio
async def test_record_config_reload(self, metrics):
await metrics.record_config_reload()
@pytest.mark.asyncio
async def test_record_config_reload_error(self, metrics):
await metrics.record_config_reload_error()
@pytest.mark.asyncio
async def test_record_content_filter_blocked(self, metrics):
await metrics.record_content_filter_blocked()
@pytest.mark.asyncio
async def test_record_content_filter_passed(self, metrics):
await metrics.record_content_filter_passed()
@pytest.mark.asyncio
async def test_record_rate_limit_hit(self, metrics):
await metrics.record_rate_limit_hit()
@pytest.mark.asyncio
async def test_record_circuit_breaker_open(self, metrics):
await metrics.record_circuit_breaker_open()
@pytest.mark.asyncio
async def test_record_circuit_breaker_close(self, metrics):
await metrics.record_circuit_breaker_close()
@pytest.mark.asyncio
async def test_record_bot_loop_detected(self, metrics):
await metrics.record_bot_loop_detected()
@pytest.mark.asyncio
async def test_record_signature_verify_success(self, metrics):
await metrics.record_signature_verify_success()
@pytest.mark.asyncio
async def test_record_signature_verify_failure(self, metrics):
await metrics.record_signature_verify_failure()
@pytest.mark.asyncio
async def test_record_keyword_match(self, metrics):
await metrics.record_keyword_match()
@pytest.mark.asyncio
async def test_record_keyword_no_match(self, metrics):
await metrics.record_keyword_no_match()
@pytest.mark.asyncio
async def test_record_mention_gate_passed(self, metrics):
await metrics.record_mention_gate_passed()
@pytest.mark.asyncio
async def test_record_mention_gate_blocked(self, metrics):
await metrics.record_mention_gate_blocked()
@pytest.mark.asyncio
async def test_record_access_policy_allowed(self, metrics):
await metrics.record_access_policy_allowed()
@pytest.mark.asyncio
async def test_record_access_policy_denied(self, metrics):
await metrics.record_access_policy_denied()
@pytest.mark.asyncio
async def test_record_dedup_hit(self, metrics):
await metrics.record_dedup_hit()
@pytest.mark.asyncio
async def test_record_dedup_miss(self, metrics):
await metrics.record_dedup_miss()
@pytest.mark.asyncio
async def test_record_adapter_open(self, metrics):
await metrics.record_adapter_open("web")
@pytest.mark.asyncio
async def test_record_adapter_close(self, metrics):
await metrics.record_adapter_close("web")
@pytest.mark.asyncio
async def test_record_adapter_error(self, metrics):
await metrics.record_adapter_error("web")
@pytest.mark.asyncio
async def test_record_queue_enqueue(self, metrics):
await metrics.record_queue_enqueue()
@pytest.mark.asyncio
async def test_record_queue_dequeue(self, metrics):
await metrics.record_queue_dequeue()
@pytest.mark.asyncio
async def test_record_queue_ack(self, metrics):
await metrics.record_queue_ack()
@pytest.mark.asyncio
async def test_record_cache_hit(self, metrics):
await metrics.record_cache_hit()
@pytest.mark.asyncio
async def test_record_cache_miss(self, metrics):
await metrics.record_cache_miss()
@pytest.mark.asyncio
async def test_record_pubsub_publish(self, metrics):
await metrics.record_pubsub_publish()
@pytest.mark.asyncio
async def test_record_pubsub_receive(self, metrics):
await metrics.record_pubsub_receive()
@pytest.mark.asyncio
async def test_record_agent_request(self, metrics):
await metrics.record_agent_request()
@pytest.mark.asyncio
async def test_record_agent_response(self, metrics):
await metrics.record_agent_response()
@pytest.mark.asyncio
async def test_record_agent_error(self, metrics):
await metrics.record_agent_error()
@pytest.mark.asyncio
async def test_record_agent_timeout(self, metrics):
await metrics.record_agent_timeout()
@pytest.mark.asyncio
async def test_record_agent_stream_chunk(self, metrics):
await metrics.record_agent_stream_chunk()
@pytest.mark.asyncio
async def test_record_agent_stream_complete(self, metrics):
await metrics.record_agent_stream_complete()
@pytest.mark.asyncio
async def test_record_agent_stream_error(self, metrics):
await metrics.record_agent_stream_error()
@pytest.mark.asyncio
async def test_record_http_request(self, metrics):
await metrics.record_http_request("GET", "/test")
@pytest.mark.asyncio
async def test_record_http_response(self, metrics):
await metrics.record_http_response("GET", "/test", 200)
@pytest.mark.asyncio
async def test_record_http_error(self, metrics):
await metrics.record_http_error("GET", "/test", 500)
@pytest.mark.asyncio
async def test_record_http_duration(self, metrics):
await metrics.record_http_duration("GET", "/test", 0.5)
@pytest.mark.asyncio
async def test_record_db_query(self, metrics):
await metrics.record_db_query("SELECT")
@pytest.mark.asyncio
async def test_record_db_error(self, metrics):
await metrics.record_db_error("SELECT")
@pytest.mark.asyncio
async def test_record_db_duration(self, metrics):
await metrics.record_db_duration("SELECT", 0.1)
@pytest.mark.asyncio
async def test_record_redis_command(self, metrics):
await metrics.record_redis_command("GET")
@pytest.mark.asyncio
async def test_record_redis_error(self, metrics):
await metrics.record_redis_error("GET")
@pytest.mark.asyncio
async def test_record_redis_duration(self, metrics):
await metrics.record_redis_duration("GET", 0.01)
@pytest.mark.asyncio
async def test_record_startup_phase(self, metrics):
await metrics.record_startup_phase("config", 0.1)
@pytest.mark.asyncio
async def test_record_shutdown_phase(self, metrics):
await metrics.record_shutdown_phase("cleanup", 0.1)
@pytest.mark.asyncio
async def test_record_goroutine_count(self, metrics):
await metrics.record_goroutine_count(10)
@pytest.mark.asyncio
async def test_record_memory_usage(self, metrics):
await metrics.record_memory_usage(100)
@pytest.mark.asyncio
async def test_record_cpu_usage(self, metrics):
await metrics.record_cpu_usage(50.0)
@pytest.mark.asyncio
async def test_record_gc_duration(self, metrics):
await metrics.record_gc_duration(0.01)
@pytest.mark.asyncio
async def test_record_gc_runs(self, metrics):
await metrics.record_gc_runs(1)
@pytest.mark.asyncio
async def test_record_heap_alloc(self, metrics):
await metrics.record_heap_alloc(1000)
@pytest.mark.asyncio
async def test_record_heap_sys(self, metrics):
await metrics.record_heap_sys(2000)
@pytest.mark.asyncio
async def test_record_heap_idle(self, metrics):
await metrics.record_heap_idle(500)
@pytest.mark.asyncio
async def test_record_heap_inuse(self, metrics):
await metrics.record_heap_inuse(1500)
@pytest.mark.asyncio
async def test_record_stack_inuse(self, metrics):
await metrics.record_stack_inuse(100)
@pytest.mark.asyncio
async def test_record_stack_sys(self, metrics):
await metrics.record_stack_sys(200)
@pytest.mark.asyncio
async def test_record_mspan_inuse(self, metrics):
await metrics.record_mspan_inuse(10)
@pytest.mark.asyncio
async def test_record_mspan_sys(self, metrics):
await metrics.record_mspan_sys(20)
@pytest.mark.asyncio
async def test_record_mcache_inuse(self, metrics):
await metrics.record_mcache_inuse(5)
@pytest.mark.asyncio
async def test_record_mcache_sys(self, metrics):
await metrics.record_mcache_sys(10)
@pytest.mark.asyncio
async def test_record_buck_hash_sys(self, metrics):
await metrics.record_buck_hash_sys(1)
@pytest.mark.asyncio
async def test_record_gcsys(self, metrics):
await metrics.record_gcsys(2)
@pytest.mark.asyncio
async def test_record_other_sys(self, metrics):
await metrics.record_other_sys(3)
@pytest.mark.asyncio
async def test_record_next_gc(self, metrics):
await metrics.record_next_gc(5000)
@pytest.mark.asyncio
async def test_record_last_gc(self, metrics):
await metrics.record_last_gc(1000)
@pytest.mark.asyncio
async def test_record_pause_total_ns(self, metrics):
await metrics.record_pause_total_ns(100)
@pytest.mark.asyncio
async def test_record_num_gc(self, metrics):
await metrics.record_num_gc(5)
@pytest.mark.asyncio
async def test_record_enable_gc(self, metrics):
await metrics.record_enable_gc(True)
@pytest.mark.asyncio
async def test_record_debug_gc(self, metrics):
await metrics.record_debug_gc(False)
@pytest.mark.asyncio
async def test_record_num_forced_gc(self, metrics):
await metrics.record_num_forced_gc(0)
@pytest.mark.asyncio
async def test_record_heap_objects(self, metrics):
await metrics.record_heap_objects(100)
@pytest.mark.asyncio
async def test_record_total_alloc(self, metrics):
await metrics.record_total_alloc(10000)
@pytest.mark.asyncio
async def test_record_sys(self, metrics):
await metrics.record_sys(5000)
@pytest.mark.asyncio
async def test_record_lookups(self, metrics):
await metrics.record_lookups(10)
@pytest.mark.asyncio
async def test_record_mallocs(self, metrics):
await metrics.record_mallocs(1000)
@pytest.mark.asyncio
async def test_record_frees(self, metrics):
await metrics.record_frees(500)
@pytest.mark.asyncio
async def test_record_heap_released(self, metrics):
await metrics.record_heap_released(200)
@pytest.mark.asyncio
async def test_record_heap_fragments(self, metrics):
await metrics.record_heap_fragments(5)
@pytest.mark.asyncio
async def test_record_live_objects(self, metrics):
await metrics.record_live_objects(500)
@pytest.mark.asyncio
async def test_record_gc_cpu_fraction(self, metrics):
await metrics.record_gc_cpu_fraction(0.1)
@pytest.mark.asyncio
async def test_record_gc_scan_duration(self, metrics):
await metrics.record_gc_scan_duration(0.01)
@pytest.mark.asyncio
async def test_record_gc_mark_duration(self, metrics):
await metrics.record_gc_mark_duration(0.02)
@pytest.mark.asyncio
async def test_record_gc_sweep_duration(self, metrics):
await metrics.record_gc_sweep_duration(0.005)
@pytest.mark.asyncio
async def test_record_gc_pause_duration(self, metrics):
await metrics.record_gc_pause_duration(0.001)
@pytest.mark.asyncio
async def test_record_gc_heap_live(self, metrics):
await metrics.record_gc_heap_live(1000)
@pytest.mark.asyncio
async def test_record_gc_heap_dead(self, metrics):
await metrics.record_gc_heap_dead(500)
@pytest.mark.asyncio
async def test_record_gc_heap_reachable(self, metrics):
await metrics.record_gc_heap_reachable(1500)
@pytest.mark.asyncio
async def test_record_gc_heap_unreachable(self, metrics):
await metrics.record_gc_heap_unreachable(200)
@pytest.mark.asyncio
async def test_record_gc_heap_fragmentation(self, metrics):
await metrics.record_gc_heap_fragmentation(0.1)
@pytest.mark.asyncio
async def test_record_gc_heap_utilization(self, metrics):
await metrics.record_gc_heap_utilization(0.8)
@pytest.mark.asyncio
async def test_record_gc_heap_growth(self, metrics):
await metrics.record_gc_heap_growth(100)
@pytest.mark.asyncio
async def test_record_gc_heap_shrink(self, metrics):
await metrics.record_gc_heap_shrink(50)
@pytest.mark.asyncio
async def test_record_gc_heap_target(self, metrics):
await metrics.record_gc_heap_target(2000)
@pytest.mark.asyncio
async def test_record_gc_heap_trigger(self, metrics):
await metrics.record_gc_heap_trigger(1500)
@pytest.mark.asyncio
async def test_record_gc_heap_live_objects(self, metrics):
await metrics.record_gc_heap_live_objects(100)
@pytest.mark.asyncio
async def test_record_gc_heap_dead_objects(self, metrics):
await metrics.record_gc_heap_dead_objects(50)
@pytest.mark.asyncio
async def test_record_gc_heap_reachable_objects(self, metrics):
await metrics.record_gc_heap_reachable_objects(150)
@pytest.mark.asyncio
async def test_record_gc_heap_unreachable_objects(self, metrics):
await metrics.record_gc_heap_unreachable_objects(20)
@pytest.mark.asyncio
async def test_record_gc_heap_fragmentation_objects(self, metrics):
await metrics.record_gc_heap_fragmentation_objects(5)
@pytest.mark.asyncio
async def test_record_gc_heap_utilization_objects(self, metrics):
await metrics.record_gc_heap_utilization_objects(0.75)
@pytest.mark.asyncio
async def test_record_gc_heap_growth_objects(self, metrics):
await metrics.record_gc_heap_growth_objects(10)
@pytest.mark.asyncio
async def test_record_gc_heap_shrink_objects(self, metrics):
await metrics.record_gc_heap_shrink_objects(5)
@pytest.mark.asyncio
async def test_record_gc_heap_target_objects(self, metrics):
await metrics.record_gc_heap_target_objects(200)
@pytest.mark.asyncio
async def test_record_gc_heap_trigger_objects(self, metrics):
await metrics.record_gc_heap_trigger_objects(150)
@pytest.mark.asyncio
async def test_record_gc_cpu_fraction_objects(self, metrics):
await metrics.record_gc_cpu_fraction_objects(0.05)
@pytest.mark.asyncio
async def test_record_gc_scan_duration_objects(self, metrics):
await metrics.record_gc_scan_duration_objects(0.005)
@pytest.mark.asyncio
async def test_record_gc_mark_duration_objects(self, metrics):
await metrics.record_gc_mark_duration_objects(0.01)
@pytest.mark.asyncio
async def test_record_gc_sweep_duration_objects(self, metrics):
await metrics.record_gc_sweep_duration_objects(0.002)
@pytest.mark.asyncio
async def test_record_gc_pause_duration_objects(self, metrics):
await metrics.record_gc_pause_duration_objects(0.0005)
@pytest.mark.asyncio
async def test_record_gc_heap_live_bytes(self, metrics):
await metrics.record_gc_heap_live_bytes(1000)
@pytest.mark.asyncio
async def test_record_gc_heap_dead_bytes(self, metrics):
await metrics.record_gc_heap_dead_bytes(500)
@pytest.mark.asyncio
async def test_record_gc_heap_reachable_bytes(self, metrics):
await metrics.record_gc_heap_reachable_bytes(1500)
@pytest.mark.asyncio
async def test_record_gc_heap_unreachable_bytes(self, metrics):
await metrics.record_gc_heap_unreachable_bytes(200)
@pytest.mark.asyncio
async def test_record_gc_heap_fragmentation_bytes(self, metrics):
await metrics.record_gc_heap_fragmentation_bytes(50)
@pytest.mark.asyncio
async def test_record_gc_heap_utilization_bytes(self, metrics):
await metrics.record_gc_heap_utilization_bytes(0.8)
@pytest.mark.asyncio
async def test_record_gc_heap_growth_bytes(self, metrics):
await metrics.record_gc_heap_growth_bytes(100)
@pytest.mark.asyncio
async def test_record_gc_heap_shrink_bytes(self, metrics):
await metrics.record_gc_heap_shrink_bytes(50)
@pytest.mark.asyncio
async def test_record_gc_heap_target_bytes(self, metrics):
await metrics.record_gc_heap_target_bytes(2000)
@pytest.mark.asyncio
async def test_record_gc_heap_trigger_bytes(self, metrics):
await metrics.record_gc_heap_trigger_bytes(1500)
@pytest.mark.asyncio
async def test_record_gc_cpu_fraction_bytes(self, metrics):
await metrics.record_gc_cpu_fraction_bytes(0.1)
@pytest.mark.asyncio
async def test_record_gc_scan_duration_bytes(self, metrics):
await metrics.record_gc_scan_duration_bytes(0.01)
@pytest.mark.asyncio
async def test_record_gc_mark_duration_bytes(self, metrics):
await metrics.record_gc_mark_duration_bytes(0.02)
@pytest.mark.asyncio
async def test_record_gc_sweep_duration_bytes(self, metrics):
await metrics.record_gc_sweep_duration_bytes(0.005)
@pytest.mark.asyncio
async def test_record_gc_pause_duration_bytes(self, metrics):
await metrics.record_gc_pause_duration_bytes(0.001)
@pytest.mark.asyncio
async def test_record_gc_heap_live_count(self, metrics):
await metrics.record_gc_heap_live_count(100)
@pytest.mark.asyncio
async def test_record_gc_heap_dead_count(self, metrics):
await metrics.record_gc_heap_dead_count(50)
@pytest.mark.asyncio
async def test_record_gc_heap_reachable_count(self, metrics):
await metrics.record_gc_heap_reachable_count(150)
@pytest.mark.asyncio
async def test_record_gc_heap_unreachable_count(self, metrics):
await metrics.record_gc_heap_unreachable_count(20)
@pytest.mark.asyncio
async def test_record_gc_heap_fragmentation_count(self, metrics):
await metrics.record_gc_heap_fragmentation_count(5)
@pytest.mark.asyncio
async def test_record_gc_heap_utilization_count(self, metrics):
await metrics.record_gc_heap_utilization_count(0.75)
@pytest.mark.asyncio
async def test_record_gc_heap_growth_count(self, metrics):
await metrics.record_gc_heap_growth_count(10)
@pytest.mark.asyncio
async def test_record_gc_heap_shrink_count(self, metrics):
await metrics.record_gc_heap_shrink_count(5)
@pytest.mark.asyncio
async def test_record_gc_heap_target_count(self, metrics):
await metrics.record_gc_heap_target_count(200)
@pytest.mark.asyncio
async def test_record_gc_heap_trigger_count(self, metrics):
await metrics.record_gc_heap_trigger_count(150)
@pytest.mark.asyncio
async def test_record_gc_cpu_fraction_count(self, metrics):
await metrics.record_gc_cpu_fraction_count(0.05)
@pytest.mark.asyncio
async def test_record_gc_scan_duration_count(self, metrics):
await metrics.record_gc_scan_duration_count(0.005)
@pytest.mark.asyncio
async def test_record_gc_mark_duration_count(self, metrics):
await metrics.record_gc_mark_duration_count(0.01)
@pytest.mark.asyncio
async def test_record_gc_sweep_duration_count(self, metrics):
await metrics.record_gc_sweep_duration_count(0.002)
@pytest.mark.asyncio
async def test_record_gc_pause_duration_count(self, metrics):
await metrics.record_gc_pause_duration_count(0.0005)
@pytest.mark.asyncio
async def test_record_gc_heap_live_size(self, metrics):
await metrics.record_gc_heap_live_size(1000)
@pytest.mark.asyncio
async def test_record_gc_heap_dead_size(self, metrics):
await metrics.record_gc_heap_dead_size(500)
@pytest.mark.asyncio
async def test_record_gc_heap_reachable_size(self, metrics):
await metrics.record_gc_heap_reachable_size(1500)
@pytest.mark.asyncio
async def test_record_gc_heap_unreachable_size(self, metrics):
await metrics.record_gc_heap_unreachable_size(200)
@pytest.mark.asyncio
async def test_record_gc_heap_fragmentation_size(self, metrics):
await metrics.record_gc_heap_fragmentation_size(50)
@pytest.mark.asyncio
async def test_record_gc_heap_utilization_size(self, metrics):
await metrics.record_gc_heap_utilization_size(0.8)
@pytest.mark.asyncio
async def test_record_gc_heap_growth_size(self, metrics):
await metrics.record_gc_heap_growth_size(100)
@pytest.mark.asyncio
async def test_record_gc_heap_shrink_size(self, metrics):
await metrics.record_gc_heap_shrink_size(50)
@pytest.mark.asyncio
async def test_record_gc_heap_target_size(self, metrics):
await metrics.record_gc_heap_target_size(2000)
@pytest.mark.asyncio
async def test_record_gc_heap_trigger_size(self, metrics):
await metrics.record_gc_heap_trigger_size(1500)
@pytest.mark.asyncio
async def test_record_gc_cpu_fraction_size(self, metrics):
await metrics.record_gc_cpu_fraction_size(0.1)
@pytest.mark.asyncio
async def test_record_gc_scan_duration_size(self, metrics):
await metrics.record_gc_scan_duration_size(0.01)
@pytest.mark.asyncio
async def test_record_gc_mark_duration_size(self, metrics):
await metrics.record_gc_mark_duration_size(0.02)
@pytest.mark.asyncio
async def test_record_gc_sweep_duration_size(self, metrics):
await metrics.record_gc_sweep_duration_size(0.005)
@pytest.mark.asyncio
async def test_record_gc_pause_duration_size(self, metrics):
await metrics.record_gc_pause_duration_size(0.001)