-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ec9480
commit 53e4a08
Showing
24 changed files
with
512 additions
and
63 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
consumer/tests/test_adapters/test_fetch_filenames/test_rabbitmq/test_close_conn_failed.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from pytest import MonkeyPatch, LogCaptureFixture | ||
import pika | ||
from src.adapters.fetch_filenames.rabbitmq import RabbitMQFetchFilenamesClient | ||
from .utils import random_csv_filenames | ||
|
||
|
||
def test_close_conn_failed( | ||
rabbitmq_fetch_filenames_no_wait_client: RabbitMQFetchFilenamesClient, | ||
raw_rabbitmq_pika_conn_config: tuple[pika.BaseConnection, str], | ||
monkeypatch: MonkeyPatch, | ||
caplog: LogCaptureFixture, | ||
): | ||
conn, _ = raw_rabbitmq_pika_conn_config | ||
|
||
channel = conn.channel() | ||
|
||
channel.queue_declare( | ||
queue=rabbitmq_fetch_filenames_no_wait_client._queue, durable=True | ||
) | ||
|
||
channel.basic_publish( | ||
exchange="", | ||
routing_key=rabbitmq_fetch_filenames_no_wait_client._queue, | ||
body=random_csv_filenames()[0], | ||
properties=pika.BasicProperties(delivery_mode=pika.DeliveryMode.Persistent), | ||
) | ||
|
||
for filename in rabbitmq_fetch_filenames_no_wait_client.fetch(): | ||
assert filename is not None | ||
|
||
assert rabbitmq_fetch_filenames_no_wait_client._conn is not None | ||
|
||
def mock_failed_close( | ||
self, | ||
*args, | ||
**kwargs, | ||
) -> None: | ||
raise Exception("Failed to close!") | ||
|
||
monkeypatch.setattr(pika.BlockingConnection, "close", mock_failed_close) | ||
|
||
with caplog.at_level("ERROR"): | ||
assert not rabbitmq_fetch_filenames_no_wait_client.close() | ||
assert "Failed to close!" in caplog.text |
16 changes: 16 additions & 0 deletions
16
...umer/tests/test_adapters/test_fetch_filenames/test_rabbitmq/test_close_conn_successful.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from src.adapters.fetch_filenames.rabbitmq import RabbitMQFetchFilenamesClient | ||
|
||
|
||
def test_close_conn_successful( | ||
rabbitmq_fetch_filenames_no_wait_client: RabbitMQFetchFilenamesClient, | ||
): | ||
for _ in rabbitmq_fetch_filenames_no_wait_client.fetch(): | ||
pass | ||
assert rabbitmq_fetch_filenames_no_wait_client._conn is not None | ||
assert rabbitmq_fetch_filenames_no_wait_client.close() | ||
|
||
|
||
def test_none_conn_close_successful( | ||
rabbitmq_fetch_filenames_client: RabbitMQFetchFilenamesClient, | ||
): | ||
assert rabbitmq_fetch_filenames_client.close() |
142 changes: 142 additions & 0 deletions
142
consumer/tests/test_adapters/test_fetch_filenames/test_rabbitmq/test_failed_conn.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import pytest | ||
from .utils import random_csv_filenames | ||
from src.adapters.fetch_filenames.rabbitmq import RabbitMQFetchFilenamesClient | ||
from src.deployments.script.config import RabbitMQConfig | ||
import pika | ||
from pytest import MonkeyPatch | ||
|
||
|
||
@pytest.mark.smoke | ||
def test_fetch_failed_conn( | ||
rabbitmq_fetch_filenames_client: RabbitMQFetchFilenamesClient, | ||
monkeypatch: MonkeyPatch, | ||
): | ||
def mocked_failed_conn( | ||
self, | ||
*args, | ||
**kwargs, | ||
) -> None: | ||
raise Exception("Failed to connect") | ||
|
||
monkeypatch.setattr(pika.BlockingConnection, "__init__", mocked_failed_conn) | ||
|
||
monkeypatch.setattr(RabbitMQFetchFilenamesClient, "_reset_conn", mocked_failed_conn) | ||
|
||
with pytest.raises(Exception, match="^Failed to connect$"): | ||
next(rabbitmq_fetch_filenames_client.fetch()) | ||
|
||
monkeypatch.undo() | ||
monkeypatch.undo() | ||
|
||
|
||
@pytest.mark.smoke | ||
def test_fetch_wrong_credentials( | ||
monkeypatch: MonkeyPatch, | ||
): | ||
rabbitmq_fetch_filenames_client = RabbitMQFetchFilenamesClient( | ||
host=RabbitMQConfig.HOST, | ||
port=RabbitMQConfig.PORT, | ||
credentials_service=lambda: ("wrong", "wrong"), | ||
queue=RabbitMQConfig.QUEUE, | ||
polling_timeout=RabbitMQConfig.POLLING_TIMEOUT, | ||
) | ||
|
||
def mocked_failed_conn( | ||
self, | ||
*args, | ||
**kwargs, | ||
) -> None: | ||
raise Exception("Failed to connect") | ||
|
||
monkeypatch.setattr(RabbitMQFetchFilenamesClient, "_reset_conn", mocked_failed_conn) | ||
|
||
with pytest.raises(Exception, match="^Failed to connect$"): | ||
next(rabbitmq_fetch_filenames_client.fetch()) | ||
|
||
monkeypatch.undo() | ||
|
||
|
||
@pytest.mark.slow | ||
@pytest.mark.smoke | ||
def test_publish_single_wrong_host( | ||
monkeypatch: MonkeyPatch, | ||
): | ||
rabbitmq_fetch_filenames_client = RabbitMQFetchFilenamesClient( | ||
host="wrong", | ||
port=RabbitMQConfig.PORT, | ||
credentials_service=lambda: (RabbitMQConfig.USERNAME, RabbitMQConfig.PASSWORD), | ||
queue=RabbitMQConfig.QUEUE, | ||
polling_timeout=RabbitMQConfig.POLLING_TIMEOUT, | ||
) | ||
|
||
def mocked_failed_conn( | ||
self, | ||
*args, | ||
**kwargs, | ||
) -> None: | ||
raise Exception("Failed to connect") | ||
|
||
monkeypatch.setattr(RabbitMQFetchFilenamesClient, "_reset_conn", mocked_failed_conn) | ||
|
||
with pytest.raises(Exception, match="^Failed to connect$") as e: | ||
next(rabbitmq_fetch_filenames_client.fetch()) | ||
|
||
monkeypatch.undo() | ||
|
||
|
||
@pytest.mark.slow | ||
def test_fetch_failed_conn_reset_conn( | ||
rabbitmq_fetch_filenames_no_wait_client: RabbitMQFetchFilenamesClient, | ||
raw_rabbitmq_pika_conn_config: tuple[pika.BaseConnection, str], | ||
monkeypatch: MonkeyPatch, | ||
): | ||
conn, queue = raw_rabbitmq_pika_conn_config | ||
|
||
channel = conn.channel() | ||
|
||
channel.queue_declare(queue=queue, durable=True) | ||
|
||
first_published_filename = random_csv_filenames()[0] | ||
second_published_filename = random_csv_filenames()[1] | ||
|
||
channel.basic_publish( | ||
exchange="", | ||
routing_key=rabbitmq_fetch_filenames_no_wait_client._queue, | ||
body=first_published_filename, | ||
properties=pika.BasicProperties(delivery_mode=pika.DeliveryMode.Persistent), | ||
) | ||
|
||
for i, filename in enumerate(rabbitmq_fetch_filenames_no_wait_client.fetch()): | ||
if i == 0: | ||
assert rabbitmq_fetch_filenames_no_wait_client._conn is not None | ||
conn = rabbitmq_fetch_filenames_no_wait_client._conn | ||
|
||
assert filename == first_published_filename | ||
channel.basic_publish( | ||
exchange="", | ||
routing_key=rabbitmq_fetch_filenames_no_wait_client._queue, | ||
body=second_published_filename, | ||
properties=pika.BasicProperties( | ||
delivery_mode=pika.DeliveryMode.Persistent | ||
), | ||
) | ||
|
||
counter = 0 | ||
|
||
def mock_failed_fetch( | ||
self, | ||
*args, | ||
**kwargs, | ||
) -> None: | ||
nonlocal counter | ||
|
||
if counter == 0: | ||
counter += 1 | ||
monkeypatch.undo() | ||
raise Exception("Failed to fetch!") | ||
|
||
monkeypatch.setattr(pika.channel.Channel, "basic_get", mock_failed_fetch) | ||
if i == 1: | ||
assert filename == second_published_filename | ||
assert rabbitmq_fetch_filenames_no_wait_client._conn is not None | ||
assert rabbitmq_fetch_filenames_no_wait_client._conn != conn |
Oops, something went wrong.