Debian Codegraph @

t/smoke/tests/failover/test_broker_failover.py

raw
1 import pytest
2 from pytest_celery import (RABBITMQ_CONTAINER_TIMEOUT, RESULT_TIMEOUT, CeleryBrokerCluster, CeleryTestSetup,
3 RabbitMQContainer, RabbitMQTestBroker)
4 from pytest_docker_tools import container, fxtr
5
6 from t.integration.tasks import identity
7
8 failover_broker = container(
9 image="{default_rabbitmq_broker_image}",
10 ports=fxtr("default_rabbitmq_broker_ports"),
11 environment=fxtr("default_rabbitmq_broker_env"),
12 network="{default_pytest_celery_network.name}",
13 wrapper_class=RabbitMQContainer,
14 timeout=RABBITMQ_CONTAINER_TIMEOUT,
15 )
16
17
18 @pytest.fixture
19 def failover_rabbitmq_broker(failover_broker: RabbitMQContainer) -> RabbitMQTestBroker:
20 broker = RabbitMQTestBroker(failover_broker)
21 yield broker
22 broker.teardown()
23
24
25 @pytest.fixture
26 def celery_broker_cluster(
27 celery_rabbitmq_broker: RabbitMQTestBroker,
28 failover_rabbitmq_broker: RabbitMQTestBroker,
29 ) -> CeleryBrokerCluster:
30 cluster = CeleryBrokerCluster(celery_rabbitmq_broker, failover_rabbitmq_broker)
31 yield cluster
32 cluster.teardown()
33
34
35 class test_broker_failover:
36 def test_killing_first_broker(self, celery_setup: CeleryTestSetup):
37 assert len(celery_setup.broker_cluster) > 1
38 celery_setup.broker.kill()
39 expected = "test_broker_failover"
40 res = identity.s(expected).apply_async(queue=celery_setup.worker.worker_queue)
41 assert res.get(timeout=RESULT_TIMEOUT) == expected
42
43 def test_reconnect_to_main(self, celery_setup: CeleryTestSetup):
44 assert len(celery_setup.broker_cluster) > 1
45 celery_setup.broker_cluster[0].kill()
46 expected = "test_broker_failover"
47 res = identity.s(expected).apply_async(queue=celery_setup.worker.worker_queue)
48 assert res.get(timeout=RESULT_TIMEOUT) == expected
49 celery_setup.broker_cluster[1].kill()
50 celery_setup.broker_cluster[0].restart()
51 res = identity.s(expected).apply_async(queue=celery_setup.worker.worker_queue)
52 assert res.get(timeout=RESULT_TIMEOUT) == expected
53
54 def test_broker_failover_ui(self, celery_setup: CeleryTestSetup):
55 assert len(celery_setup.broker_cluster) > 1
56 celery_setup.broker_cluster[0].kill()
57 celery_setup.worker.assert_log_exists("Will retry using next failover.")
58 celery_setup.worker.assert_log_exists(
59 f"Connected to amqp://guest:**@{celery_setup.broker_cluster[1].hostname()}:5672//"
60 )