Debian Codegraph @

t/unit/test_loops.py

raw
1 from unittest.mock import Mock, patch
2
3 import pytest
4
5 from celery import bootsteps
6 from celery.worker.loops import synloop
7
8
9 def test_synloop_perform_pending_operations_on_system_exit():
10 # Mock dependencies
11 obj = Mock()
12 connection = Mock()
13 consumer = Mock()
14 blueprint = Mock()
15 hub = Mock()
16 qos = Mock()
17 heartbeat = Mock()
18 clock = Mock()
19
20 # Set up the necessary attributes
21 obj.create_task_handler.return_value = Mock()
22 obj.perform_pending_operations = Mock()
23 obj.on_ready = Mock()
24 obj.pool.is_green = False
25 obj.connection = True
26
27 blueprint.state = bootsteps.RUN # Simulate RUN state
28
29 qos.prev = qos.value = Mock()
30
31 # Mock state.maybe_shutdown to raise SystemExit
32 with patch("celery.worker.loops.state") as mock_state:
33 mock_state.maybe_shutdown.side_effect = SystemExit
34
35 # Call synloop and expect SystemExit to be raised
36 with pytest.raises(SystemExit):
37 synloop(
38 obj,
39 connection,
40 consumer,
41 blueprint,
42 hub,
43 qos,
44 heartbeat,
45 clock,
46 hbrate=2.0,
47 )
48
49 # Assert that perform_pending_operations was called even after SystemExit
50 obj.perform_pending_operations.assert_called_once()
51
52 # Assert that connection.drain_events was called
53 connection.drain_events.assert_called_with(timeout=2.0)
54
55 # Assert other important method calls
56 obj.on_ready.assert_called_once()
57 consumer.consume.assert_called_once()