forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sliding_window_audit.py
More file actions
76 lines (62 loc) 路 2.61 KB
/
Copy pathtest_sliding_window_audit.py
File metadata and controls
76 lines (62 loc) 路 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Tests for sliding window audit in TelemetryPipeline (Issue #138)."""
from __future__ import annotations
import asyncio
import sqlite3
import tempfile
import time
import unittest
from pathlib import Path
from unittest.mock import patch
from misakanet.tools.telemetry_pipeline import (
TelemetryPipeline,
_ensure_schema,
)
class TestSlidingWindowAudit(unittest.IsolatedAsyncioTestCase):
"""Test sliding window audit runs in consumer task."""
async def asyncSetUp(self):
self.tmpdir = tempfile.mkdtemp()
self.db_path = Path(self.tmpdir) / "test_audit.db"
async def test_audit_breach_does_not_crash_pipeline(self):
"""Audit breach (rate limit) should log warning, not crash pipeline."""
# Insert 10 rapid queries to trigger rate limit
conn = sqlite3.connect(str(self.db_path))
_ensure_schema(conn)
now = time.time()
for i in range(10):
conn.execute(
"INSERT INTO search_telemetry (query, timestamp, latency_ms, cache_hit, query_signature) VALUES (?, ?, ?, ?, ?)",
(f"query_{i}", now + i * 0.1, 100.0, 0, f"sig_{i}"),
)
conn.commit()
conn.close()
# Pipeline should not crash
async with TelemetryPipeline(self.db_path) as pipeline:
await pipeline.emit("new_query", 50.0, cache_hit=False)
await asyncio.sleep(0.1) # Let consumer process
# Verify blacklist entry was created
conn = sqlite3.connect(str(self.db_path))
count = conn.execute("SELECT COUNT(*) FROM local_blacklist").fetchone()[0]
conn.close()
self.assertGreaterEqual(count, 1)
async def test_audit_cooldown_clears_blacklist(self):
"""After cooldown period, blacklist should be cleared."""
# Insert a blacklist entry that's already expired
conn = sqlite3.connect(str(self.db_path))
_ensure_schema(conn)
conn.execute(
"INSERT INTO local_blacklist (blocked_until, reason, hit_count) VALUES (?, 'rate_limit', 1)",
(time.time() - 100,), # Expired 100s ago
)
conn.commit()
conn.close()
# Pipeline should work normally
async with TelemetryPipeline(self.db_path) as pipeline:
await pipeline.emit("test_query", 50.0, cache_hit=True)
await asyncio.sleep(0.1)
# Verify telemetry was recorded
conn = sqlite3.connect(str(self.db_path))
count = conn.execute("SELECT COUNT(*) FROM search_telemetry").fetchone()[0]
conn.close()
self.assertGreaterEqual(count, 1)
if __name__ == "__main__":
unittest.main()