|
| 1 | +import queue |
| 2 | + |
1 | 3 | import pytest |
2 | 4 |
|
3 | 5 | from unittest.mock import patch |
@@ -478,3 +480,97 @@ def _get_async_result(data, request_id=None): |
478 | 480 | assert state == telegram.authorization_state == AuthorizationState.READY |
479 | 481 |
|
480 | 482 | assert telegram._tdjson.send.call_count == 0 |
| 483 | + |
| 484 | + |
| 485 | +class TestWorkerExceptionHandling: |
| 486 | + def test_worker_thread_survives_handler_exception(self): |
| 487 | + import time |
| 488 | + from queue import Queue |
| 489 | + from telegram.worker import SimpleWorker |
| 490 | + |
| 491 | + q = Queue() |
| 492 | + worker = SimpleWorker(queue=q) |
| 493 | + worker.run() |
| 494 | + |
| 495 | + results = [] |
| 496 | + |
| 497 | + def bad_handler(update): |
| 498 | + raise RuntimeError("boom") |
| 499 | + |
| 500 | + def good_handler(update): |
| 501 | + results.append(update) |
| 502 | + |
| 503 | + q.put((bad_handler, {"@type": "test"})) |
| 504 | + q.put((good_handler, {"@type": "test"})) |
| 505 | + |
| 506 | + time.sleep(0.5) |
| 507 | + worker.stop() |
| 508 | + |
| 509 | + assert results == [{"@type": "test"}] |
| 510 | + |
| 511 | + |
| 512 | +class TestListenerExceptionHandling: |
| 513 | + def test_listener_survives_receive_exception(self, telegram): |
| 514 | + import threading |
| 515 | + |
| 516 | + telegram._stopped = threading.Event() |
| 517 | + call_count = 0 |
| 518 | + |
| 519 | + def exploding_receive(): |
| 520 | + nonlocal call_count |
| 521 | + call_count += 1 |
| 522 | + if call_count == 1: |
| 523 | + raise RuntimeError("receive failed") |
| 524 | + if call_count == 2: |
| 525 | + return {"@type": "ok", "@extra": {"request_id": "test123"}} |
| 526 | + telegram._stopped.set() |
| 527 | + return None |
| 528 | + |
| 529 | + telegram._tdjson.receive = exploding_receive |
| 530 | + telegram._listen_to_td() |
| 531 | + |
| 532 | + assert call_count == 3 |
| 533 | + |
| 534 | + def test_listener_exits_on_exception_when_stopped(self, telegram): |
| 535 | + import threading |
| 536 | + |
| 537 | + telegram._stopped = threading.Event() |
| 538 | + |
| 539 | + def exploding_receive(): |
| 540 | + telegram._stopped.set() |
| 541 | + raise RuntimeError("error during shutdown") |
| 542 | + |
| 543 | + telegram._tdjson.receive = exploding_receive |
| 544 | + telegram._listen_to_td() |
| 545 | + |
| 546 | + |
| 547 | +class TestRunHandlersQueueFull: |
| 548 | + def test_queue_full_does_not_propagate(self, telegram): |
| 549 | + def my_handler(update): |
| 550 | + pass |
| 551 | + |
| 552 | + telegram.add_update_handler("testUpdate", my_handler) |
| 553 | + |
| 554 | + with patch.object(telegram._workers_queue, "put", side_effect=queue.Full): |
| 555 | + telegram._run_handlers({"@type": "testUpdate"}) |
| 556 | + |
| 557 | + |
| 558 | +class TestSendMessageElementError: |
| 559 | + def test_raises_on_parse_error(self, telegram): |
| 560 | + error_result = AsyncResult(client=telegram) |
| 561 | + error_result.error = True |
| 562 | + error_result.error_info = {"@type": "error", "message": "Bad HTML"} |
| 563 | + error_result._ready.set() |
| 564 | + |
| 565 | + with patch.object(telegram, "parse_text_entities", return_value=error_result): |
| 566 | + with pytest.raises(RuntimeError): |
| 567 | + telegram.send_message(chat_id=1, text=Spoiler("test")) |
| 568 | + |
| 569 | + def test_raises_on_none_update(self, telegram): |
| 570 | + result = AsyncResult(client=telegram) |
| 571 | + result.update = None |
| 572 | + result._ready.set() |
| 573 | + |
| 574 | + with patch.object(telegram, "parse_text_entities", return_value=result): |
| 575 | + with pytest.raises(RuntimeError, match="Failed to parse text entities"): |
| 576 | + telegram.send_message(chat_id=1, text=Spoiler("test")) |
0 commit comments