Files
aitbc/apps/pool-hub/tests/test_sla_collector.py
aitbc d26e6d3772
Some checks failed
API Endpoint Tests / test-api-endpoints (push) Successful in 22s
Blockchain Synchronization Verification / sync-verification (push) Successful in 3s
CLI Tests / test-cli (push) Failing after 13s
Cross-Chain Functionality Tests / test-cross-chain-sync (push) Failing after 3s
Cross-Chain Functionality Tests / test-cross-chain-transactions (push) Successful in 3s
Cross-Chain Functionality Tests / test-cross-chain-bridge (push) Has been skipped
Cross-Chain Functionality Tests / test-multi-chain-consensus (push) Failing after 3s
Cross-Chain Functionality Tests / aggregate-results (push) Has been skipped
Cross-Node Transaction Testing / transaction-test (push) Successful in 2s
Deploy to Testnet / deploy-testnet (push) Successful in 1m34s
Documentation Validation / validate-docs (push) Failing after 10s
Documentation Validation / validate-policies-strict (push) Successful in 3s
Multi-Node Stress Testing / stress-test (push) Has been cancelled
Node Failover Simulation / failover-test (push) Has been cancelled
Python Tests / test-python (push) Has been cancelled
Integration Tests / test-service-integration (push) Successful in 2m42s
Multi-Chain Island Architecture Tests / test-multi-chain-island (push) Successful in 3s
Multi-Node Blockchain Health Monitoring / health-check (push) Successful in 5s
P2P Network Verification / p2p-verification (push) Successful in 3s
Package Tests / Python package - aitbc-agent-sdk (push) Failing after 33s
Package Tests / Python package - aitbc-core (push) Successful in 17s
Package Tests / Python package - aitbc-crypto (push) Successful in 11s
Security Scanning / security-scan (push) Has been cancelled
Package Tests / Python package - aitbc-sdk (push) Successful in 13s
Package Tests / JavaScript package - aitbc-sdk-js (push) Successful in 9s
Package Tests / JavaScript package - aitbc-token (push) Successful in 17s
Staking Tests / test-staking-service (push) Failing after 6s
Staking Tests / test-staking-integration (push) Has been skipped
Staking Tests / test-staking-contract (push) Has been skipped
Staking Tests / run-staking-test-runner (push) Has been skipped
fix: replace datetime.UTC with timezone.utc for Python 3.12+ compatibility
2026-05-09 12:03:26 +02:00

188 lines
5.8 KiB
Python

"""
Tests for SLA Collector Service
"""
import sys
import pytest
from datetime import datetime, timezone, timedelta
from decimal import Decimal
from sqlalchemy.orm import Session
from poolhub.models import Miner, MinerStatus, SLAMetric, SLAViolation, Feedback, MatchResult
from poolhub.services.sla_collector import SLACollector
@pytest.fixture
def sla_collector(db_session: Session) -> SLACollector:
"""Create SLA collector fixture"""
return SLACollector(db_session)
@pytest.fixture
def sample_miner(db_session: Session) -> Miner:
"""Create sample miner fixture"""
miner = Miner(
miner_id="test_miner_001",
api_key_hash="hash123",
addr="127.0.0.1:8080",
proto="http",
gpu_vram_gb=24.0,
gpu_name="RTX 4090",
cpu_cores=16,
ram_gb=64.0,
max_parallel=4,
base_price=0.50,
)
db_session.add(miner)
db_session.commit()
return miner
@pytest.fixture
def sample_miner_status(db_session: Session, sample_miner: Miner) -> MinerStatus:
"""Create sample miner status fixture"""
status = MinerStatus(
miner_id=sample_miner.miner_id,
queue_len=2,
busy=False,
avg_latency_ms=150,
temp_c=65,
mem_free_gb=32.0,
last_heartbeat_at=datetime.now(timezone.utc),
)
db_session.add(status)
db_session.commit()
return status
@pytest.mark.asyncio
async def test_record_sla_metric(sla_collector: SLACollector, sample_miner: Miner):
"""Test recording an SLA metric"""
metric = await sla_collector.record_sla_metric(
miner_id=sample_miner.miner_id,
metric_type="uptime_pct",
metric_value=98.5,
metadata={"test": "true"},
)
assert metric.miner_id == sample_miner.miner_id
assert metric.metric_type == "uptime_pct"
assert metric.metric_value == 98.5
assert metric.is_violation == False
@pytest.mark.asyncio
async def test_record_sla_metric_violation(sla_collector: SLACollector, sample_miner: Miner):
"""Test recording an SLA metric that violates threshold"""
metric = await sla_collector.record_sla_metric(
miner_id=sample_miner.miner_id,
metric_type="uptime_pct",
metric_value=80.0, # Below threshold of 95%
metadata={"test": "true"},
)
assert metric.is_violation == True
# Check violation was recorded
violations = await sla_collector.get_sla_violations(
miner_id=sample_miner.miner_id, resolved=False
)
assert len(violations) > 0
assert violations[0].violation_type == "uptime_pct"
@pytest.mark.asyncio
async def test_collect_miner_uptime(sla_collector: SLACollector, sample_miner_status: MinerStatus):
"""Test collecting miner uptime"""
uptime = await sla_collector.collect_miner_uptime(sample_miner_status.miner_id)
assert uptime is not None
assert 0 <= uptime <= 100
@pytest.mark.asyncio
async def test_collect_response_time_no_results(sla_collector: SLACollector, sample_miner: Miner):
"""Test collecting response time when no match results exist"""
response_time = await sla_collector.collect_response_time(sample_miner.miner_id)
assert response_time is None
@pytest.mark.asyncio
async def test_collect_completion_rate_no_feedback(sla_collector: SLACollector, sample_miner: Miner):
"""Test collecting completion rate when no feedback exists"""
completion_rate = await sla_collector.collect_completion_rate(sample_miner.miner_id)
assert completion_rate is None
@pytest.mark.asyncio
async def test_collect_capacity_availability(sla_collector: SLACollector):
"""Test collecting capacity availability"""
capacity = await sla_collector.collect_capacity_availability()
assert "total_miners" in capacity
assert "active_miners" in capacity
assert "capacity_availability_pct" in capacity
@pytest.mark.asyncio
async def test_get_sla_metrics(sla_collector: SLACollector, sample_miner: Miner):
"""Test getting SLA metrics"""
# Record a metric first
await sla_collector.record_sla_metric(
miner_id=sample_miner.miner_id,
metric_type="uptime_pct",
metric_value=98.5,
)
metrics = await sla_collector.get_sla_metrics(
miner_id=sample_miner.miner_id, hours=24
)
assert len(metrics) > 0
assert metrics[0].miner_id == sample_miner.miner_id
@pytest.mark.asyncio
async def test_get_sla_violations(sla_collector: SLACollector, sample_miner: Miner):
"""Test getting SLA violations"""
# Record a violation
await sla_collector.record_sla_metric(
miner_id=sample_miner.miner_id,
metric_type="uptime_pct",
metric_value=80.0, # Below threshold
)
violations = await sla_collector.get_sla_violations(
miner_id=sample_miner.miner_id, resolved=False
)
assert len(violations) > 0
def test_check_violation_uptime_below_threshold(sla_collector: SLACollector):
"""Test violation check for uptime below threshold"""
is_violation = sla_collector._check_violation("uptime_pct", 90.0, 95.0)
assert is_violation == True
def test_check_violation_uptime_above_threshold(sla_collector: SLACollector):
"""Test violation check for uptime above threshold"""
is_violation = sla_collector._check_violation("uptime_pct", 98.0, 95.0)
assert is_violation == False
@pytest.mark.asyncio
async def test_check_violation_response_time_above_threshold(sla_collector: SLACollector):
"""Test violation check for response time above threshold"""
is_violation = sla_collector._check_violation("response_time_ms", 2000.0, 1000.0)
assert is_violation == True
@pytest.mark.asyncio
async def test_check_violation_response_time_below_threshold(sla_collector: SLACollector):
"""Test violation check for response time below threshold"""
is_violation = sla_collector._check_violation("response_time_ms", 500.0, 1000.0)
assert is_violation == False