From 0600ccb3241e082016406126014981ccb52e15de Mon Sep 17 00:00:00 2001 From: Tiago Albineli Motta Date: Fri, 10 Feb 2023 12:47:24 -0300 Subject: [PATCH 1/8] Upgrade async worker --- requirements/requirements_base.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements/requirements_base.txt b/requirements/requirements_base.txt index 7e04935..b3447e5 100644 --- a/requirements/requirements_base.txt +++ b/requirements/requirements_base.txt @@ -1,4 +1,4 @@ -async-worker==0.15.1 +async-worker==0.19.2 aioamqp==0.14.0 python-json-logger==2.0.1 jsonschema==3.2.0 \ No newline at end of file From 5cd906fa4efa4c17885ae929a119cf6164fa4170 Mon Sep 17 00:00:00 2001 From: Tiago Albineli Motta Date: Fri, 10 Feb 2023 12:47:51 -0300 Subject: [PATCH 2/8] Avoid intermitent errors on integration test --- Makefile | 2 +- tests_integration/test_barterdude.py | 11 ++++++----- tests_integration/test_rabbitmq_integration.py | 18 ++++++++++-------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index b3edd0b..2e8fecb 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ test: @pytest --ignore="tests_integration" --cov=barterdude integration: - @pytest --ignore="tests_unit" + @pytest --ignore="tests_unit" -vv all-tests: | test integration lint check-sec diff --git a/tests_integration/test_barterdude.py b/tests_integration/test_barterdude.py index 53bdeb0..895c5c8 100644 --- a/tests_integration/test_barterdude.py +++ b/tests_integration/test_barterdude.py @@ -1,7 +1,7 @@ import asyncio import os from asyncio import Event -from random import choices +from random import choices, random from string import ascii_uppercase import aiohttp @@ -21,9 +21,10 @@ class TestBarterDude(TestCase): use_default_loop = True async def setUp(self): - self.input_queue = "test" + suffix = str(int(random()*10000)) + self.input_queue = f"test_{suffix}" self.output_exchange = "test_exchange" - self.output_queue = "test_output" + self.output_queue = f"test_output_{suffix}" self.rabbitmq_host = os.environ.get("RABBITMQ_HOST", "127.0.0.1") self.barterdude_host = os.environ.get("BARTERDUDE_HOST", "127.0.0.1") @@ -189,7 +190,7 @@ async def handler(message): data=self.messages[0] ) await sync_event.wait() - await asyncio.sleep(1) + await asyncio.sleep(0.05) key = self.messages[0]["key"] error_str = repr(error) @@ -227,7 +228,7 @@ async def handler(message): data=self.messages[0] ) await sync_event.wait() - await asyncio.sleep(1) + await asyncio.sleep(0.05) key = self.messages[0]["key"] error_str = repr(error) diff --git a/tests_integration/test_rabbitmq_integration.py b/tests_integration/test_rabbitmq_integration.py index 125d0ad..37ba85b 100644 --- a/tests_integration/test_rabbitmq_integration.py +++ b/tests_integration/test_rabbitmq_integration.py @@ -1,7 +1,7 @@ import asyncio import os from asyncio import Event -from random import choices +from random import choices, random from string import ascii_uppercase from asynctest import TestCase @@ -19,9 +19,10 @@ class RabbitMQConsumerTest(TestCase): use_default_loop = True async def setUp(self): - self.input_queue = "test" + suffix = str(int(random()*10000)) + self.input_queue = f"test_{suffix}" self.output_exchange = "test_exchange" - self.output_queue = "test_output" + self.output_queue = f"test_output_{suffix}" self.rabbitmq_host = os.environ.get("RABBITMQ_HOST", "127.0.0.1") self.barterdude_host = os.environ.get("BARTERDUDE_HOST", "127.0.0.1") @@ -111,7 +112,6 @@ async def handler(message): async def test_process_messages_successfully_even_with_crashed_hook(self): received_messages = set() - sync_event = Event() monitor = Monitor(ErrorHook()) @@ -120,14 +120,16 @@ async def test_process_messages_successfully_even_with_crashed_hook(self): async def handler(message): nonlocal received_messages received_messages.add(message.body["key"]) - sync_event.set() await self.app.startup() await self.send_all_messages() - await sync_event.wait() - for message in self.messages: - self.assertTrue(message["key"] in received_messages) + expected = set([m["key"] for m in self.messages]) + + while len(expected - received_messages) > 0: + await asyncio.sleep(0.05) + + self.assertEquals(expected, received_messages) async def test_process_one_message_and_publish(self): sync_event = Event() From 146a51610e490e937d41afbc02f2f86e24708a58 Mon Sep 17 00:00:00 2001 From: Tiago Albineli Motta Date: Fri, 17 Feb 2023 18:18:10 -0300 Subject: [PATCH 3/8] Using git based async-worker ready for python3.11 --- requirements/requirements_base.txt | 3 +-- requirements/requirements_test.txt | 5 ++--- setup.py | 10 +++++++++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/requirements/requirements_base.txt b/requirements/requirements_base.txt index b3447e5..86cdc0d 100644 --- a/requirements/requirements_base.txt +++ b/requirements/requirements_base.txt @@ -1,4 +1,3 @@ -async-worker==0.19.2 -aioamqp==0.14.0 +git+https://github.com/timotta/async-worker.git@python3.11 python-json-logger==2.0.1 jsonschema==3.2.0 \ No newline at end of file diff --git a/requirements/requirements_test.txt b/requirements/requirements_test.txt index dab12b4..0e7ce8e 100644 --- a/requirements/requirements_test.txt +++ b/requirements/requirements_test.txt @@ -1,8 +1,7 @@ -pytest>5.0.0,<6.0.0 -pytest-cov>2.0.0,<3.0.0 +pytest>=7.0.0,<8.0.0 +pytest-cov>=4.0.0,<5.0.0 coverage>5.0,<6.0 bandit>1.0,<2.0.0 flake8>3.0.0,<4.0.0 -asynctest==0.13.0 freezegun==0.3.14 pydantic==1.7.4 \ No newline at end of file diff --git a/setup.py b/setup.py index 3451066..328f161 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,15 @@ } with open("requirements/requirements_base.txt") as reqs: - requirements = reqs.read().split("\n") + raw_requirements = reqs.read().split("\n") + +requirements = [] +for r in raw_requirements: + if r.startswith("git+"): + name = r.split(".git@")[0].split("/")[-1] + requirements.append(f"{name} @ {r}") + else: + requirements.append(r) for lib in libs: with open(f"requirements/requirements_{lib}.txt") as reqs: From b5ec4d68a71fb7ebb8a82723d687e8b244df664a Mon Sep 17 00:00:00 2001 From: Tiago Albineli Motta Date: Fri, 17 Feb 2023 18:18:19 -0300 Subject: [PATCH 4/8] Fixed tests for python3.11 --- barterdude/__init__.py | 2 +- barterdude/mocks.py | 2 +- tests_integration/test_barterdude.py | 10 ++--- .../test_rabbitmq_integration.py | 9 ++--- tests_unit/test__init__.py | 38 ++++++++++--------- tests_unit/test_conf.py | 6 ++- tests_unit/test_hooks/test__init__.py | 3 +- tests_unit/test_hooks/test_healthcheck.py | 5 ++- tests_unit/test_hooks/test_logging.py | 9 +++-- .../test_prometheus/test__init__.py | 5 ++- .../test_prometheus/test_definitions.py | 3 +- .../test_prometheus/test_metrics.py | 6 ++- tests_unit/test_message.py | 3 +- tests_unit/test_mocks.py | 2 +- tests_unit/test_monitor.py | 23 +++++------ 15 files changed, 68 insertions(+), 58 deletions(-) diff --git a/barterdude/__init__.py b/barterdude/__init__.py index d5bd5fb..9defbad 100644 --- a/barterdude/__init__.py +++ b/barterdude/__init__.py @@ -6,7 +6,7 @@ from asyncworker.options import Options from asyncworker.connections import AMQPConnection from asyncworker.rabbitmq.message import RabbitMQMessage -from collections import MutableMapping +from collections.abc import MutableMapping from typing import Iterable, Optional, Callable, Any, Tuple from barterdude.monitor import Monitor from barterdude.message import MessageValidation, ValidationException diff --git a/barterdude/mocks.py b/barterdude/mocks.py index 5409eaa..548b880 100644 --- a/barterdude/mocks.py +++ b/barterdude/mocks.py @@ -1,6 +1,6 @@ import asyncio from typing import Dict, Any, Iterable, Tuple -from collections import MutableMapping +from collections.abc import MutableMapping from aioamqp.properties import Properties diff --git a/tests_integration/test_barterdude.py b/tests_integration/test_barterdude.py index 895c5c8..b44cf68 100644 --- a/tests_integration/test_barterdude.py +++ b/tests_integration/test_barterdude.py @@ -5,7 +5,7 @@ from string import ascii_uppercase import aiohttp -from asynctest import TestCase +from unittest import IsolatedAsyncioTestCase from asyncworker.connections import AMQPConnection from barterdude import BarterDude from barterdude.hooks import logging as hook_logging @@ -17,10 +17,8 @@ from tests_integration.helpers import ErrorHook -class TestBarterDude(TestCase): - use_default_loop = True - - async def setUp(self): +class TestBarterDude(IsolatedAsyncioTestCase): + async def asyncSetUp(self): suffix = str(int(random()*10000)) self.input_queue = f"test_{suffix}" self.output_exchange = "test_exchange" @@ -59,7 +57,7 @@ async def setUp(self): self.app = BarterDude(hostname=self.rabbitmq_host) - async def tearDown(self): + async def asyncTearDown(self): await self.app.shutdown() await self.queue_manager.connection.channel.queue_delete( self.input_queue diff --git a/tests_integration/test_rabbitmq_integration.py b/tests_integration/test_rabbitmq_integration.py index 37ba85b..128bfe7 100644 --- a/tests_integration/test_rabbitmq_integration.py +++ b/tests_integration/test_rabbitmq_integration.py @@ -4,7 +4,7 @@ from random import choices, random from string import ascii_uppercase -from asynctest import TestCase +from unittest import IsolatedAsyncioTestCase from asyncworker.connections import AMQPConnection from barterdude import BarterDude from barterdude.hooks.logging import Logging @@ -15,10 +15,9 @@ from tests_integration.helpers import ErrorHook -class RabbitMQConsumerTest(TestCase): - use_default_loop = True +class RabbitMQConsumerTest(IsolatedAsyncioTestCase): - async def setUp(self): + async def asyncSetUp(self): suffix = str(int(random()*10000)) self.input_queue = f"test_{suffix}" self.output_exchange = "test_exchange" @@ -57,7 +56,7 @@ async def setUp(self): self.app = BarterDude(hostname=self.rabbitmq_host) - async def tearDown(self): + async def asyncTearDown(self): await self.app.shutdown() await self.queue_manager.connection.channel.queue_delete( self.input_queue diff --git a/tests_unit/test__init__.py b/tests_unit/test__init__.py index 2adcea6..67bed83 100644 --- a/tests_unit/test__init__.py +++ b/tests_unit/test__init__.py @@ -1,19 +1,21 @@ -from asynctest import Mock, TestCase, CoroutineMock, patch, call +from unittest import IsolatedAsyncioTestCase +from unittest.mock import Mock, AsyncMock, patch, call + from asyncworker import Options, RouteTypes from barterdude import BarterDude from barterdude.message import Message from tests_unit.helpers import load_fixture -class TestBarterDude(TestCase): +class TestBarterDude(IsolatedAsyncioTestCase): @patch("barterdude.App") @patch("barterdude.AMQPConnection") def setUp(self, AMQPConnection, App): self.monitor = Mock() - self.monitor.dispatch_before_consume = CoroutineMock() - self.monitor.dispatch_on_success = CoroutineMock() - self.monitor.dispatch_on_fail = CoroutineMock() - self.callback = CoroutineMock() + self.monitor.dispatch_before_consume = AsyncMock() + self.monitor.dispatch_on_success = AsyncMock() + self.monitor.dispatch_on_fail = AsyncMock() + self.callback = AsyncMock() self.messages = [Mock(value=i) for i in range(10)] self.calls = [call(message) for message in self.messages] @@ -21,8 +23,8 @@ def setUp(self, AMQPConnection, App): self.connection = self.AMQPConnection.return_value self.App = App self.app = self.App.return_value - self.app.startup = CoroutineMock() - self.app.shutdown = CoroutineMock() + self.app.startup = AsyncMock() + self.app.shutdown = AsyncMock() self.decorator = self.app.route.return_value self.schema = load_fixture("schema.json") self.barterdude = BarterDude() @@ -41,7 +43,7 @@ def test_should_call_route_when_created(self): monitor = Mock() self.barterdude.consume_amqp( ["queue"], monitor=monitor - )(CoroutineMock()) + )(AsyncMock()) self.app.route.assert_called_once_with( ["queue"], type=RouteTypes.AMQP_RABBITMQ, @@ -85,10 +87,10 @@ async def mock_hook(message, barterdude): await barterdude.publish_amqp(data={'a': 1}) request = Mock() - request.json = CoroutineMock(return_value={'body': {}}) + request.json = AsyncMock(return_value={'body': {}}) service_mock = Mock() service_mock.method_one.return_value = 123 - service_mock.method_two = CoroutineMock(return_value=234) + service_mock.method_two = AsyncMock(return_value=234) dependencies = [(service_mock, 'service')] response = await self.barterdude._call_callback_endpoint( request, mock_hook, dependencies) @@ -111,10 +113,10 @@ async def mock_hook(message, barterdude): await barterdude.publish_amqp(data={'a': 1}) request = Mock() - request.json = CoroutineMock(return_value={}) + request.json = AsyncMock(return_value={}) service_mock = Mock() service_mock.method_one.return_value = 123 - service_mock.method_two = CoroutineMock(return_value=234) + service_mock.method_two = AsyncMock(return_value=234) dependencies = [(service_mock, 'service')] response = await self.barterdude._call_callback_endpoint( request, mock_hook, dependencies) @@ -131,7 +133,7 @@ async def mock_hook(message, barterdude): raise Exception request = Mock() - request.json = CoroutineMock(return_value={'body': {}}) + request.json = AsyncMock(return_value={'body': {}}) service_mock = Mock() dependencies = [(service_mock, 'service')] response = await self.barterdude._call_callback_endpoint( @@ -151,10 +153,10 @@ async def mock_hook(message, barterdude): await barterdude.publish_amqp(data={'a': 1}) request = Mock() - request.json = CoroutineMock() + request.json = AsyncMock() service_mock = Mock() service_mock.method_one.return_value = 123 - service_mock.method_two = CoroutineMock(return_value=234) + service_mock.method_two = AsyncMock(return_value=234) dependencies = [(service_mock, 'service')] response = await self.barterdude._call_callback_endpoint( request, mock_hook, dependencies) @@ -241,7 +243,7 @@ async def test_should_call_monitor_for_each_fail_message(self): async def test_should_call_put_when_publish(self): data = Mock() - self.connection.put = CoroutineMock() + self.connection.put = AsyncMock() await self.barterdude.publish_amqp( 'exchange', data, @@ -267,7 +269,7 @@ def test_should_call_run(self): self.app.run.assert_called_once_with() -class TestAppSharedProperties(TestCase): +class TestAppSharedProperties(IsolatedAsyncioTestCase): def setUp(self): self.barterdude = BarterDude() diff --git a/tests_unit/test_conf.py b/tests_unit/test_conf.py index b7a79b7..bb88466 100644 --- a/tests_unit/test_conf.py +++ b/tests_unit/test_conf.py @@ -1,13 +1,15 @@ import logging from pythonjsonlogger import jsonlogger -from asynctest import TestCase +from unittest import IsolatedAsyncioTestCase +from unittest.mock import Mock, AsyncMock, patch, call + from barterdude.conf import ( getLogger, BARTERDUDE_DEFAULT_APP_NAME, BARTERDUDE_DEFAULT_LOG_LEVEL ) -class TestConf(TestCase): +class TestConf(IsolatedAsyncioTestCase): def setUp(self): self.log_name = BARTERDUDE_DEFAULT_APP_NAME diff --git a/tests_unit/test_hooks/test__init__.py b/tests_unit/test_hooks/test__init__.py index 0245010..125adad 100644 --- a/tests_unit/test_hooks/test__init__.py +++ b/tests_unit/test_hooks/test__init__.py @@ -1,4 +1,5 @@ -from asynctest import TestCase, Mock +from unittest import TestCase +from unittest.mock import Mock from barterdude.hooks import HttpHook diff --git a/tests_unit/test_hooks/test_healthcheck.py b/tests_unit/test_hooks/test_healthcheck.py index eb8d595..5167c73 100644 --- a/tests_unit/test_hooks/test_healthcheck.py +++ b/tests_unit/test_hooks/test_healthcheck.py @@ -1,4 +1,5 @@ -from asynctest import TestCase, MagicMock +from unittest import IsolatedAsyncioTestCase +from unittest.mock import Mock, AsyncMock, patch, call, MagicMock from freezegun import freeze_time from barterdude.hooks.healthcheck import Healthcheck, HealthcheckMonitored @@ -12,7 +13,7 @@ def healthcheck(self): @freeze_time() -class TestHealthcheck(TestCase): +class TestHealthcheck(IsolatedAsyncioTestCase): maxDiff = None def setUp(self): diff --git a/tests_unit/test_hooks/test_logging.py b/tests_unit/test_hooks/test_logging.py index c48559f..0e4a5e3 100644 --- a/tests_unit/test_hooks/test_logging.py +++ b/tests_unit/test_hooks/test_logging.py @@ -1,10 +1,11 @@ import logging -from asynctest import TestCase, Mock, patch +from unittest import IsolatedAsyncioTestCase +from unittest.mock import Mock, patch from barterdude.hooks import logging as hook_log from barterdude.conf import BARTERDUDE_DEFAULT_APP_NAME -class TestLogging(TestCase): +class TestLogging(IsolatedAsyncioTestCase): maxDiff = None def setUp(self): @@ -45,7 +46,7 @@ async def test_should_log_on_connection_fail( }) -class TestLoggingNotRedacted(TestCase): +class TestLoggingNotRedacted(IsolatedAsyncioTestCase): maxDiff = None def setUp(self): @@ -94,7 +95,7 @@ async def test_should_log_on_fail(self, format_tb, repr, dumps, logger): }) -class TestLoggingRedacted(TestCase): +class TestLoggingRedacted(IsolatedAsyncioTestCase): maxDiff = None def setUp(self): diff --git a/tests_unit/test_hooks/test_metrics/test_prometheus/test__init__.py b/tests_unit/test_hooks/test_metrics/test_prometheus/test__init__.py index 4f521c7..508da17 100644 --- a/tests_unit/test_hooks/test_metrics/test_prometheus/test__init__.py +++ b/tests_unit/test_hooks/test_metrics/test_prometheus/test__init__.py @@ -1,5 +1,6 @@ import freezegun -from asynctest import TestCase, Mock, patch +from unittest import IsolatedAsyncioTestCase +from unittest.mock import Mock, AsyncMock, patch, call from barterdude.hooks.metrics.prometheus import Prometheus @@ -13,7 +14,7 @@ ) -class TestPrometheus(TestCase): +class TestPrometheus(IsolatedAsyncioTestCase): def setUp(self): self.registry = Mock() self.labels = {"test": "my_test"} diff --git a/tests_unit/test_hooks/test_metrics/test_prometheus/test_definitions.py b/tests_unit/test_hooks/test_metrics/test_prometheus/test_definitions.py index 9e0ae4d..8ef81e4 100644 --- a/tests_unit/test_hooks/test_metrics/test_prometheus/test_definitions.py +++ b/tests_unit/test_hooks/test_metrics/test_prometheus/test_definitions.py @@ -1,4 +1,5 @@ -from asynctest import TestCase, Mock, MagicMock +from unittest import TestCase +from unittest.mock import Mock, AsyncMock, patch, call, MagicMock from barterdude.hooks.metrics.prometheus.definitions import Definitions from prometheus_client.metrics import Counter, Histogram, REGISTRY diff --git a/tests_unit/test_hooks/test_metrics/test_prometheus/test_metrics.py b/tests_unit/test_hooks/test_metrics/test_prometheus/test_metrics.py index e23df8d..d46950d 100644 --- a/tests_unit/test_hooks/test_metrics/test_prometheus/test_metrics.py +++ b/tests_unit/test_hooks/test_metrics/test_prometheus/test_metrics.py @@ -1,10 +1,12 @@ -from asynctest import TestCase, Mock +from unittest import IsolatedAsyncioTestCase +from unittest.mock import Mock, AsyncMock, patch, call + from prometheus_client.metrics import MetricWrapperBase, REGISTRY from barterdude.hooks.metrics.prometheus.metrics import Metrics -class TestMetrics(TestCase): +class TestMetrics(IsolatedAsyncioTestCase): def test_should_set_and_get_items(self): metrics = Metrics(REGISTRY) metric_mock = Mock(spec=MetricWrapperBase) diff --git a/tests_unit/test_message.py b/tests_unit/test_message.py index ef09241..1026b24 100644 --- a/tests_unit/test_message.py +++ b/tests_unit/test_message.py @@ -1,6 +1,7 @@ import json import pytest -from asynctest import TestCase, Mock +from unittest import TestCase +from unittest.mock import Mock from asyncworker.rabbitmq.message import RabbitMQMessage from asyncworker.easyqueue.message import AMQPMessage from barterdude.message import Message, MessageValidation, ValidationException diff --git a/tests_unit/test_mocks.py b/tests_unit/test_mocks.py index 7a47a28..b0613a8 100644 --- a/tests_unit/test_mocks.py +++ b/tests_unit/test_mocks.py @@ -1,4 +1,4 @@ -from asynctest import TestCase +from unittest import TestCase from barterdude.mocks import BarterdudeMock, RabbitMQMessageMock diff --git a/tests_unit/test_monitor.py b/tests_unit/test_monitor.py index 0675e68..536661a 100644 --- a/tests_unit/test_monitor.py +++ b/tests_unit/test_monitor.py @@ -1,8 +1,9 @@ -from asynctest import TestCase, MagicMock, Mock, CoroutineMock, patch +from unittest import IsolatedAsyncioTestCase +from unittest.mock import Mock, AsyncMock, patch, call, MagicMock from barterdude.monitor import Monitor -class TestMonitor(TestCase): +class TestMonitor(IsolatedAsyncioTestCase): def setUp(self): self.hook1 = Mock() @@ -10,8 +11,8 @@ def setUp(self): self.monitor = Monitor(self.hook1, self.hook2) async def test_should_call_hooks_before_consume(self): - self.hook1.before_consume = CoroutineMock() - self.hook2.before_consume = CoroutineMock() + self.hook1.before_consume = AsyncMock() + self.hook2.before_consume = AsyncMock() await self.monitor.dispatch_before_consume({}) self.hook1.before_consume.assert_called_once() self.hook2.before_consume.assert_called_once() @@ -19,8 +20,8 @@ async def test_should_call_hooks_before_consume(self): self.hook2.before_consume.assert_called_with({}) async def test_should_call_hooks_on_success(self): - self.hook1.on_success = CoroutineMock() - self.hook2.on_success = CoroutineMock() + self.hook1.on_success = AsyncMock() + self.hook2.on_success = AsyncMock() await self.monitor.dispatch_on_success({}) self.hook1.on_success.assert_called_once() self.hook2.on_success.assert_called_once() @@ -29,8 +30,8 @@ async def test_should_call_hooks_on_success(self): async def test_should_call_hooks_on_fail(self): exception = Exception() - self.hook1.on_fail = CoroutineMock() - self.hook2.on_fail = CoroutineMock() + self.hook1.on_fail = AsyncMock() + self.hook2.on_fail = AsyncMock() await self.monitor.dispatch_on_fail({}, exception) self.hook1.on_fail.assert_called_once() self.hook2.on_fail.assert_called_once() @@ -40,8 +41,8 @@ async def test_should_call_hooks_on_fail(self): async def test_should_call_hooks_on_connection_fail(self): exception = Mock() retries = Mock() - self.hook1.on_connection_fail = CoroutineMock() - self.hook2.on_connection_fail = CoroutineMock() + self.hook1.on_connection_fail = AsyncMock() + self.hook2.on_connection_fail = AsyncMock() await self.monitor.dispatch_on_connection_fail(exception, retries) self.hook1.on_connection_fail.assert_called_once() self.hook2.on_connection_fail.assert_called_once() @@ -58,7 +59,7 @@ async def before_consume(msg): raise exception self.monitor._logger = logger self.hook1.before_consume = before_consume - self.hook2.before_consume = CoroutineMock() + self.hook2.before_consume = AsyncMock() await self.monitor.dispatch_before_consume({}) repr.assert_called_once_with(exception) format_tb.assert_called_once_with(exception.__traceback__) From f4a0f69064a0eb7747a0f921eaf9f1182349ad98 Mon Sep 17 00:00:00 2001 From: Tiago Albineli Motta Date: Fri, 17 Feb 2023 18:24:55 -0300 Subject: [PATCH 5/8] Lint fixed --- tests_unit/test_conf.py | 1 - tests_unit/test_hooks/test_healthcheck.py | 2 +- .../test_hooks/test_metrics/test_prometheus/test__init__.py | 2 +- .../test_hooks/test_metrics/test_prometheus/test_definitions.py | 2 +- .../test_hooks/test_metrics/test_prometheus/test_metrics.py | 2 +- tests_unit/test_monitor.py | 2 +- 6 files changed, 5 insertions(+), 6 deletions(-) diff --git a/tests_unit/test_conf.py b/tests_unit/test_conf.py index bb88466..c7470d8 100644 --- a/tests_unit/test_conf.py +++ b/tests_unit/test_conf.py @@ -2,7 +2,6 @@ from pythonjsonlogger import jsonlogger from unittest import IsolatedAsyncioTestCase -from unittest.mock import Mock, AsyncMock, patch, call from barterdude.conf import ( getLogger, BARTERDUDE_DEFAULT_APP_NAME, BARTERDUDE_DEFAULT_LOG_LEVEL diff --git a/tests_unit/test_hooks/test_healthcheck.py b/tests_unit/test_hooks/test_healthcheck.py index 5167c73..5b66850 100644 --- a/tests_unit/test_hooks/test_healthcheck.py +++ b/tests_unit/test_hooks/test_healthcheck.py @@ -1,5 +1,5 @@ from unittest import IsolatedAsyncioTestCase -from unittest.mock import Mock, AsyncMock, patch, call, MagicMock +from unittest.mock import MagicMock from freezegun import freeze_time from barterdude.hooks.healthcheck import Healthcheck, HealthcheckMonitored diff --git a/tests_unit/test_hooks/test_metrics/test_prometheus/test__init__.py b/tests_unit/test_hooks/test_metrics/test_prometheus/test__init__.py index 508da17..b71c78f 100644 --- a/tests_unit/test_hooks/test_metrics/test_prometheus/test__init__.py +++ b/tests_unit/test_hooks/test_metrics/test_prometheus/test__init__.py @@ -1,6 +1,6 @@ import freezegun from unittest import IsolatedAsyncioTestCase -from unittest.mock import Mock, AsyncMock, patch, call +from unittest.mock import Mock, patch from barterdude.hooks.metrics.prometheus import Prometheus diff --git a/tests_unit/test_hooks/test_metrics/test_prometheus/test_definitions.py b/tests_unit/test_hooks/test_metrics/test_prometheus/test_definitions.py index 8ef81e4..df34906 100644 --- a/tests_unit/test_hooks/test_metrics/test_prometheus/test_definitions.py +++ b/tests_unit/test_hooks/test_metrics/test_prometheus/test_definitions.py @@ -1,5 +1,5 @@ from unittest import TestCase -from unittest.mock import Mock, AsyncMock, patch, call, MagicMock +from unittest.mock import Mock, MagicMock from barterdude.hooks.metrics.prometheus.definitions import Definitions from prometheus_client.metrics import Counter, Histogram, REGISTRY diff --git a/tests_unit/test_hooks/test_metrics/test_prometheus/test_metrics.py b/tests_unit/test_hooks/test_metrics/test_prometheus/test_metrics.py index d46950d..63cc734 100644 --- a/tests_unit/test_hooks/test_metrics/test_prometheus/test_metrics.py +++ b/tests_unit/test_hooks/test_metrics/test_prometheus/test_metrics.py @@ -1,5 +1,5 @@ from unittest import IsolatedAsyncioTestCase -from unittest.mock import Mock, AsyncMock, patch, call +from unittest.mock import Mock from prometheus_client.metrics import MetricWrapperBase, REGISTRY diff --git a/tests_unit/test_monitor.py b/tests_unit/test_monitor.py index 536661a..eca31b4 100644 --- a/tests_unit/test_monitor.py +++ b/tests_unit/test_monitor.py @@ -1,5 +1,5 @@ from unittest import IsolatedAsyncioTestCase -from unittest.mock import Mock, AsyncMock, patch, call, MagicMock +from unittest.mock import Mock, AsyncMock, patch, MagicMock from barterdude.monitor import Monitor From 9bbdca95801de0d00f78cab3acf2d283961b28bd Mon Sep 17 00:00:00 2001 From: Tiago Albineli Motta Date: Fri, 17 Feb 2023 18:31:03 -0300 Subject: [PATCH 6/8] Updated python versions --- .github/workflows/main.yml | 12 ++++++------ setup.py | 4 +++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 705a99f..82b4a98 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -18,10 +18,10 @@ jobs: - name: Checkout repository uses: actions/checkout@v2 - - name: Setup python 3.7 + - name: Setup python 3.11 uses: actions/setup-python@v2 with: - python-version: 3.7 + python-version: 3.11 - name: Install project dependencies run: make setup @@ -42,10 +42,10 @@ jobs: - name: Checkout repository uses: actions/checkout@v2 - - name: Setup python 3.7 + - name: Setup python 3.11 uses: actions/setup-python@v2 with: - python-version: 3.7 + python-version: 3.11 - name: Install project dependencies run: make install && python3 -m pip install build @@ -70,10 +70,10 @@ jobs: - name: Checkout repository uses: actions/checkout@v2 - - name: Setup python 3.7 + - name: Setup python 3.11 uses: actions/setup-python@v2 with: - python-version: 3.7 + python-version: 3.11 - name: Install project dependencies run: make install && python3 -m pip install build diff --git a/setup.py b/setup.py index 328f161..edacfe5 100644 --- a/setup.py +++ b/setup.py @@ -41,7 +41,9 @@ install_requires=requirements, extras_require=extra, classifiers=[ - "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", "License :: OSI Approved :: Apache Software License", "Topic :: Software Development :: Libraries :: Python Modules", "Topic :: Software Development :: Libraries :: Application Frameworks" From 17f071c0bf65812912771d2820f54dfde38afee9 Mon Sep 17 00:00:00 2001 From: Tiago Albineli Motta Date: Mon, 17 Apr 2023 09:40:58 -0300 Subject: [PATCH 7/8] Updated async worker version --- requirements/requirements_base.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements/requirements_base.txt b/requirements/requirements_base.txt index 86cdc0d..f3de5ee 100644 --- a/requirements/requirements_base.txt +++ b/requirements/requirements_base.txt @@ -1,3 +1,3 @@ -git+https://github.com/timotta/async-worker.git@python3.11 +git+https://github.com/async-worker/async-worker.git@release/0.20.0 python-json-logger==2.0.1 jsonschema==3.2.0 \ No newline at end of file From ab2c74cfbedaa5ec3f659e5e87781f3e3136f19e Mon Sep 17 00:00:00 2001 From: Tiago Albineli Motta Date: Mon, 17 Apr 2023 17:12:41 -0300 Subject: [PATCH 8/8] Clearing queue instead of randomize the name --- tests_integration/test_barterdude.py | 15 ++++++++++----- tests_integration/test_rabbitmq_integration.py | 15 ++++++++++----- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/tests_integration/test_barterdude.py b/tests_integration/test_barterdude.py index b44cf68..d5b63e2 100644 --- a/tests_integration/test_barterdude.py +++ b/tests_integration/test_barterdude.py @@ -1,7 +1,7 @@ import asyncio import os from asyncio import Event -from random import choices, random +from random import choices from string import ascii_uppercase import aiohttp @@ -19,10 +19,9 @@ class TestBarterDude(IsolatedAsyncioTestCase): async def asyncSetUp(self): - suffix = str(int(random()*10000)) - self.input_queue = f"test_{suffix}" + self.input_queue = "test_input" self.output_exchange = "test_exchange" - self.output_queue = f"test_output_{suffix}" + self.output_queue = "test_output" self.rabbitmq_host = os.environ.get("RABBITMQ_HOST", "127.0.0.1") self.barterdude_host = os.environ.get("BARTERDUDE_HOST", "127.0.0.1") @@ -35,6 +34,9 @@ async def asyncSetUp(self): ) self.queue_manager = self.connection["/"] await self.queue_manager.connection._connect() + + await self.clear() + await self.queue_manager.connection.channel.queue_declare( self.input_queue ) @@ -59,6 +61,10 @@ async def asyncSetUp(self): async def asyncTearDown(self): await self.app.shutdown() + await self.clear() + await self.queue_manager.connection.close() + + async def clear(self): await self.queue_manager.connection.channel.queue_delete( self.input_queue ) @@ -68,7 +74,6 @@ async def asyncTearDown(self): await self.queue_manager.connection.channel.exchange_delete( self.output_exchange ) - await self.queue_manager.connection.close() async def test_obtains_healthcheck(self): monitor = Monitor(Healthcheck(self.app)) diff --git a/tests_integration/test_rabbitmq_integration.py b/tests_integration/test_rabbitmq_integration.py index 128bfe7..e5922ec 100644 --- a/tests_integration/test_rabbitmq_integration.py +++ b/tests_integration/test_rabbitmq_integration.py @@ -1,7 +1,7 @@ import asyncio import os from asyncio import Event -from random import choices, random +from random import choices from string import ascii_uppercase from unittest import IsolatedAsyncioTestCase @@ -18,10 +18,9 @@ class RabbitMQConsumerTest(IsolatedAsyncioTestCase): async def asyncSetUp(self): - suffix = str(int(random()*10000)) - self.input_queue = f"test_{suffix}" + self.input_queue = "test_input" self.output_exchange = "test_exchange" - self.output_queue = f"test_output_{suffix}" + self.output_queue = "test_output" self.rabbitmq_host = os.environ.get("RABBITMQ_HOST", "127.0.0.1") self.barterdude_host = os.environ.get("BARTERDUDE_HOST", "127.0.0.1") @@ -34,6 +33,9 @@ async def asyncSetUp(self): ) self.queue_manager = self.connection["/"] await self.queue_manager.connection._connect() + + await self.clear() + await self.queue_manager.connection.channel.queue_declare( self.input_queue ) @@ -58,6 +60,10 @@ async def asyncSetUp(self): async def asyncTearDown(self): await self.app.shutdown() + await self.clear() + await self.queue_manager.connection.close() + + async def clear(self): await self.queue_manager.connection.channel.queue_delete( self.input_queue ) @@ -67,7 +73,6 @@ async def asyncTearDown(self): await self.queue_manager.connection.channel.exchange_delete( self.output_exchange ) - await self.queue_manager.connection.close() async def send_all_messages(self): futures = []