|
1 | 1 | """Test the LangfuseResourceManager and get_client() function.""" |
2 | 2 |
|
3 | 3 | import logging |
| 4 | +import threading |
4 | 5 | from queue import Queue |
5 | 6 | from types import SimpleNamespace |
6 | 7 | from typing import Sequence |
7 | 8 | from unittest.mock import Mock |
8 | 9 |
|
| 10 | +import pytest |
9 | 11 | from opentelemetry.sdk.trace import ReadableSpan |
10 | 12 | from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult |
11 | 13 |
|
@@ -471,3 +473,36 @@ def signal_shutdown(self, *, count): |
471 | 473 | ("join", 0), |
472 | 474 | ("join", 1), |
473 | 475 | ] |
| 476 | + |
| 477 | + |
| 478 | +@pytest.mark.filterwarnings("error::pytest.PytestUnhandledThreadExceptionWarning") |
| 479 | +@pytest.mark.parametrize("concurrent", [False, True], ids=["sequential", "concurrent"]) |
| 480 | +def test_flush_returns_after_repeated_shutdown(concurrent): |
| 481 | + client = Langfuse( |
| 482 | + public_key="pk-repeated-shutdown", |
| 483 | + secret_key="sk-repeated-shutdown", |
| 484 | + span_exporter=NoOpSpanExporter(), |
| 485 | + ) |
| 486 | + media_queue = client._resources._media_upload_queue |
| 487 | + shutdowns = [ |
| 488 | + threading.Thread(target=client.shutdown, daemon=True) for _ in range(2) |
| 489 | + ] |
| 490 | + |
| 491 | + for shutdown in shutdowns: |
| 492 | + shutdown.start() |
| 493 | + if not concurrent: |
| 494 | + shutdown.join(timeout=5) |
| 495 | + for shutdown in shutdowns: |
| 496 | + shutdown.join(timeout=5) |
| 497 | + |
| 498 | + flush = threading.Thread(target=client.flush, daemon=True) |
| 499 | + flush.start() |
| 500 | + flush.join(timeout=2) |
| 501 | + hung = [thread for thread in (*shutdowns, flush) if thread.is_alive()] |
| 502 | + # Acknowledge shutdown signals that no consumer took, so a hung thread and |
| 503 | + # the reset() in the autouse fixture can finish. |
| 504 | + while media_queue.unfinished_tasks: |
| 505 | + media_queue.get_nowait() |
| 506 | + media_queue.task_done() |
| 507 | + |
| 508 | + assert not hung |
0 commit comments