Test workflow SSE streaming endpoint
(self)
| 398 | |
| 399 | @task |
| 400 | def test_workflow_stream(self) -> None: |
| 401 | """Test workflow SSE streaming endpoint""" |
| 402 | |
| 403 | question = random.choice(self.questions) |
| 404 | self.user_counter += 1 |
| 405 | |
| 406 | headers = { |
| 407 | "Authorization": f"Bearer {self.api_token}", |
| 408 | "Content-Type": "application/json", |
| 409 | "Accept": "text/event-stream", |
| 410 | "Cache-Control": "no-cache", |
| 411 | } |
| 412 | |
| 413 | data = WorkflowRequestData( |
| 414 | inputs=WorkflowInputs(question=question), |
| 415 | response_mode="streaming", |
| 416 | user=f"user_{self.user_counter}", |
| 417 | ) |
| 418 | |
| 419 | start_time = time.time() |
| 420 | first_event_time = None |
| 421 | event_count = 0 |
| 422 | inter_event_times: list[float] = [] |
| 423 | last_event_time = None |
| 424 | ttfe = 0 |
| 425 | request_success = False |
| 426 | bytes_received = 0 |
| 427 | |
| 428 | metrics.connection_started() |
| 429 | |
| 430 | # Use catch_response context manager directly |
| 431 | with self.client.request( |
| 432 | method="POST", |
| 433 | url=WORKFLOW_PATH, |
| 434 | headers=headers, |
| 435 | json=data, |
| 436 | stream=True, |
| 437 | catch_response=True, |
| 438 | timeout=(CONNECT_TIMEOUT, READ_TIMEOUT), |
| 439 | name="/v1/workflows/run", # Name for Locust stats |
| 440 | ) as response: |
| 441 | try: |
| 442 | # Validate response |
| 443 | if response.status_code >= 400: |
| 444 | error_type: ErrorType = "http_4xx" if response.status_code < 500 else "http_5xx" |
| 445 | metrics.record_error(error_type) |
| 446 | response.failure(f"HTTP {response.status_code}") |
| 447 | return |
| 448 | |
| 449 | content_type = response.headers.get("Content-Type", "") |
| 450 | if "text/event-stream" not in content_type and "application/json" not in content_type: |
| 451 | logger.error(f"Expected text/event-stream, got: {content_type}") |
| 452 | metrics.record_error("invalid_response") |
| 453 | response.failure(f"Invalid content type: {content_type}") |
| 454 | return |
| 455 | |
| 456 | # Parse SSE events |
| 457 | parser = SSEParser() |
nothing calls this directly
no test coverage detected