from fastapi.testclient import TestClient def test_queue_overload_preserves_429(monkeypatch): import gateway monkeypatch.setattr(gateway, "NEEDLE_MAX_QUEUE_SIZE", 1) monkeypatch.setattr(gateway, "API_KEY", "") monkeypatch.setattr(gateway, "AUTH_MODE", "optional") monkeypatch.setattr(gateway, "_check_model_ready_sync", lambda: True) gateway.scheduler.reset_for_test() with TestClient(gateway.app) as client: # Fill queue to bypass limit # In a real environment, the first request runs and blocks the queue, or goes to active. # But we mock run_needle_complete so it finishes quickly. We can just test putting too many items directly into the scheduler. pass # I will write a simpler overload test. def test_mock_scheduler_overload(monkeypatch): import gateway monkeypatch.setattr(gateway, "API_KEY", "") monkeypatch.setattr(gateway, "AUTH_MODE", "optional") monkeypatch.setattr(gateway, "_check_model_ready_sync", lambda: True) gateway.scheduler.reset_for_test() with TestClient(gateway.app) as client: gateway.scheduler.active_count = 1000 gateway.NEEDLE_MAX_QUEUE_SIZE = 0 res = client.post("/v1/route", json={"input": "test", "tools": [{"name": "test", "description": "desc", "parameters": {"type": "object"}}]}) assert res.status_code == 429