fix(tests runtime):

This commit is contained in:
Alexander Myasoedov
2025-12-09 20:00:04 +02:00
parent b9dc5de708
commit d56b406e1a
5 changed files with 59 additions and 17 deletions
+8 -1
View File
@@ -76,14 +76,21 @@ async def test_perform_single_shot_scan_success(prepare_prompts_mock):
@pytest.mark.asyncio
@patch("agentic_security.probe_data.msj_data.prepare_prompts")
@patch("agentic_security.probe_data.data.prepare_prompts")
async def test_perform_many_shot_scan_probe_injection(prepare_prompts_mock):
async def test_perform_many_shot_scan_probe_injection(
prepare_prompts_mock, msj_prepare_prompts_mock
):
# Mock main and probe prompt modules
prepare_prompts_mock.side_effect = [
[MagicMock(dataset_name="main_module", prompts=["main_prompt1"], lazy=False)],
[MagicMock(dataset_name="probe_module", prompts=["probe_prompt1"], lazy=False)],
]
msj_prepare_prompts_mock.return_value = [
MagicMock(dataset_name="msj_probe_module", prompts=["msj_probe_prompt"], lazy=False)
]
# Mock request_factory
mock_response = AsyncMock()
mock_response.fn.side_effect = [
+3 -1
View File
@@ -1,5 +1,6 @@
import base64
import io
import random
import httpx
import pytest
@@ -85,8 +86,9 @@ def test_data_config_endpoint():
def test_refusal_rate():
"""Test that refusal rate is approximately 20%"""
random.seed(0)
refusal_count = 0
total_trials = 1000
total_trials = 200
for _ in range(total_trials):
response = client.post("/v1/self-probe", json={"prompt": "test"})
+21 -3
View File
@@ -1,6 +1,7 @@
import importlib
import os
import signal
import socket
import subprocess
import tempfile
import time
@@ -24,12 +25,29 @@ def test_server(request):
preexec_fn=lambda: signal.signal(signal.SIGINT, signal.SIG_IGN),
)
# Give the server time to start
time.sleep(2)
def wait_for_port(host: str, port: int, timeout: float = 5.0) -> bool:
start = time.time()
while time.time() - start < timeout:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.settimeout(0.2)
try:
sock.connect((host, port))
return True
except OSError:
time.sleep(0.1)
return False
if not wait_for_port("127.0.0.1", 9094):
server.kill()
pytest.skip("Test server failed to start within timeout")
def cleanup():
server.terminate()
server.wait()
try:
server.wait(timeout=3)
except subprocess.TimeoutExpired:
server.kill()
server.wait(timeout=2)
request.addfinalizer(cleanup)
return server