|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import logging |
3 | 4 | import time |
4 | 5 | from datetime import timedelta |
5 | 6 | from unittest.mock import patch |
@@ -533,3 +534,51 @@ def f2(): |
533 | 534 | # Then a ValueError should be raised |
534 | 535 | assert exc_info.type is ValueError |
535 | 536 | assert str(exc_info.value) == "An actor named 'foo' is already registered." |
| 537 | + |
| 538 | + |
| 539 | +def test_worker_handles_non_numeric_eta_and_logs_warning(stub_broker, stub_worker, caplog): |
| 540 | + # Set the log level to capture warnings |
| 541 | + caplog.set_level(logging.WARNING) |
| 542 | + |
| 543 | + # Given an actor that records when it runs |
| 544 | + run = [] |
| 545 | + |
| 546 | + @dramatiq.actor |
| 547 | + def record(): |
| 548 | + run.append(True) |
| 549 | + |
| 550 | + # If I send it a message with a non-numeric eta manually from another project or manually from rabbitmq UI (not using dramatiq) |
| 551 | + message = record.message_with_options(eta="not-a-number") |
| 552 | + stub_broker.queues[record.queue_name].put(message.encode()) |
| 553 | + |
| 554 | + # Then join on the queue |
| 555 | + stub_broker.join(record.queue_name) |
| 556 | + stub_worker.join() |
| 557 | + |
| 558 | + # I expect the message to have been processed |
| 559 | + assert run |
| 560 | + |
| 561 | + # And a warning should have been logged about the invalid eta |
| 562 | + assert any( |
| 563 | + "Invalid eta value for message" in record.message for record in caplog.records if record.levelname == "WARNING" |
| 564 | + ) |
| 565 | + |
| 566 | + |
| 567 | +def test_worker_handles_none_eta(stub_broker, stub_worker): |
| 568 | + # Given an actor that records when it runs |
| 569 | + run = [] |
| 570 | + |
| 571 | + @dramatiq.actor |
| 572 | + def record(): |
| 573 | + run.append(True) |
| 574 | + |
| 575 | + # If I send it a message with a None eta |
| 576 | + message = record.message_with_options(eta=None) |
| 577 | + stub_broker.queues[record.queue_name].put(message.encode()) |
| 578 | + |
| 579 | + # Then join on the queue |
| 580 | + stub_broker.join(record.queue_name) |
| 581 | + stub_worker.join() |
| 582 | + |
| 583 | + # I expect the message to have been processed |
| 584 | + assert run |
0 commit comments