|
1 | 1 | """Unit tests for streaming query interrupt endpoint.""" |
2 | 2 |
|
3 | 3 | import asyncio |
| 4 | +import threading |
4 | 5 | from collections.abc import Generator |
5 | 6 |
|
6 | 7 | import pytest |
|
9 | 10 | from app.endpoints.stream_interrupt import stream_interrupt_endpoint_handler |
10 | 11 | from models.api.requests import StreamingInterruptRequest |
11 | 12 | from models.api.responses.successful import StreamingInterruptResponse |
12 | | -from utils.stream_interrupts import StreamInterruptRegistry |
| 13 | +from utils.stream_interrupts import CancelStreamResult, StreamInterruptRegistry |
13 | 14 |
|
14 | 15 | REQUEST_ID_SUCCESS = "123e4567-e89b-12d3-a456-426614174000" |
15 | 16 | REQUEST_ID_NOT_FOUND = "123e4567-e89b-12d3-a456-426614174001" |
16 | 17 | REQUEST_ID_WRONG_USER = "123e4567-e89b-12d3-a456-426614174002" |
17 | 18 | REQUEST_ID_ALREADY_COMPLETED = "123e4567-e89b-12d3-a456-426614174004" |
18 | 19 |
|
| 20 | +# CI-friendly sync timeouts for concurrent registry tests. |
| 21 | +_CONCURRENT_BARRIER_TIMEOUT_S = 5.0 |
| 22 | +_CONCURRENT_THREAD_JOIN_TIMEOUT_S = 5.0 |
| 23 | + |
19 | 24 | OWNER_USER_ID = "00000001-0001-0001-0001-000000000001" |
20 | 25 | NON_OWNER_USER_ID = "00000001-0001-0001-0001-000000000999" |
21 | 26 |
|
@@ -148,3 +153,75 @@ async def completed_stream() -> None: |
148 | 153 | assert isinstance(response, StreamingInterruptResponse) |
149 | 154 | assert response.request_id == REQUEST_ID_ALREADY_COMPLETED |
150 | 155 | assert response.interrupted is False |
| 156 | + |
| 157 | + |
| 158 | +@pytest.mark.asyncio |
| 159 | +async def test_stream_interrupt_registry_concurrent_cancel_and_deregister( |
| 160 | + registry: StreamInterruptRegistry, |
| 161 | +) -> None: |
| 162 | + """Concurrent cancel and deregister do not raise under the registry lock.""" |
| 163 | + |
| 164 | + async def pending_stream() -> None: |
| 165 | + await asyncio.sleep(10) |
| 166 | + |
| 167 | + task = asyncio.create_task(pending_stream()) |
| 168 | + registry.register_stream(REQUEST_ID_SUCCESS, OWNER_USER_ID, task) |
| 169 | + |
| 170 | + barrier = threading.Barrier(2) |
| 171 | + errors: list[Exception] = [] |
| 172 | + |
| 173 | + def deregister_in_thread() -> None: |
| 174 | + try: |
| 175 | + barrier.wait(timeout=_CONCURRENT_BARRIER_TIMEOUT_S) |
| 176 | + registry.deregister_stream(REQUEST_ID_SUCCESS) |
| 177 | + except Exception as exc: # pylint: disable=broad-exception-caught |
| 178 | + errors.append(exc) |
| 179 | + |
| 180 | + thread = threading.Thread(target=deregister_in_thread) |
| 181 | + thread.start() |
| 182 | + barrier.wait(timeout=_CONCURRENT_BARRIER_TIMEOUT_S) |
| 183 | + |
| 184 | + result = registry.cancel_stream(REQUEST_ID_SUCCESS, OWNER_USER_ID) |
| 185 | + thread.join(timeout=_CONCURRENT_THREAD_JOIN_TIMEOUT_S) |
| 186 | + assert not thread.is_alive(), "Deregister thread did not complete within timeout" |
| 187 | + |
| 188 | + assert not errors |
| 189 | + assert result in ( |
| 190 | + CancelStreamResult.CANCELLED, |
| 191 | + CancelStreamResult.NOT_FOUND, |
| 192 | + ) |
| 193 | + |
| 194 | + if not task.done(): |
| 195 | + task.cancel() |
| 196 | + with pytest.raises(asyncio.CancelledError): |
| 197 | + await task |
| 198 | + |
| 199 | + |
| 200 | +@pytest.mark.asyncio |
| 201 | +async def test_stream_interrupt_endpoint_double_interrupt( |
| 202 | + registry: StreamInterruptRegistry, |
| 203 | +) -> None: |
| 204 | + """Second interrupt on the same stream returns interrupted=False.""" |
| 205 | + |
| 206 | + async def pending_stream() -> None: |
| 207 | + await asyncio.sleep(10) |
| 208 | + |
| 209 | + task = asyncio.create_task(pending_stream()) |
| 210 | + registry.register_stream(REQUEST_ID_SUCCESS, OWNER_USER_ID, task) |
| 211 | + |
| 212 | + first_response = await stream_interrupt_endpoint_handler( |
| 213 | + interrupt_request=StreamingInterruptRequest(request_id=REQUEST_ID_SUCCESS), |
| 214 | + auth=(OWNER_USER_ID, "mock_username", False, "mock_token"), |
| 215 | + registry=registry, |
| 216 | + ) |
| 217 | + assert first_response.interrupted is True |
| 218 | + |
| 219 | + with pytest.raises(asyncio.CancelledError): |
| 220 | + await task |
| 221 | + |
| 222 | + second_response = await stream_interrupt_endpoint_handler( |
| 223 | + interrupt_request=StreamingInterruptRequest(request_id=REQUEST_ID_SUCCESS), |
| 224 | + auth=(OWNER_USER_ID, "mock_username", False, "mock_token"), |
| 225 | + registry=registry, |
| 226 | + ) |
| 227 | + assert second_response.interrupted is False |
0 commit comments