From f573af6b210aa310235978abe93caa737bb7b87a Mon Sep 17 00:00:00 2001 From: Pastukhov Nikita Date: Wed, 20 Dec 2023 21:52:57 +0300 Subject: [PATCH] fix (#1082): correct NatsTestClient stream publisher (#1083) * fix (#1082): correct NatsTestClient stream publisher * chore: remove anyio restriction * test: add test for ExceptionGroup * test: fix compatibility --- .github/workflows/test.yaml | 36 +++++++++++++++++------------- faststream/__about__.py | 2 +- faststream/nats/test.py | 7 +++++- pyproject.toml | 4 ++-- tests/brokers/nats/test_publish.py | 28 ++++++++++++++++++++++- tests/cli/test_app.py | 17 ++++++++++++++ 6 files changed, 74 insertions(+), 20 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 3718a29498..e0fb4f8545 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -70,13 +70,14 @@ jobs: - name: Test run: bash scripts/test.sh -m "(slow and (not nats and not kafka and not rabbit and not redis)) or (not nats and not kafka and not rabbit and not redis)" env: - COVERAGE_FILE: coverage/.coverage.${{ runner.os }}-py${{ matrix.python-version }} - CONTEXT: ${{ runner.os }}-py${{ matrix.python-version }} + COVERAGE_FILE: coverage/.coverage.${{ runner.os }}-py${{ matrix.python-version }}-${{ matrix.pydantic-version }} + CONTEXT: ${{ runner.os }}-py${{ matrix.python-version }}-${{ matrix.pydantic-version }} - name: Store coverage files - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: - name: coverage + name: .coverage.${{ runner.os }}-py${{ matrix.python-version }}-${{ matrix.pydantic-version }} path: coverage + if-no-files-found: error test-macos-latest: if: github.event.pull_request.draft == false @@ -149,10 +150,11 @@ jobs: COVERAGE_FILE: coverage/.coverage.kafka-py CONTEXT: kafka-py - name: Store coverage files - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: - name: coverage + name: .coverage.kafka-py path: coverage + if-no-files-found: error test-kafka-smoke: if: github.event.pull_request.draft == false @@ -197,10 +199,11 @@ jobs: COVERAGE_FILE: coverage/.coverage.rabbit-py CONTEXT: rabbit-py - name: Store coverage files - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: - name: coverage + name: .coverage.rabbit-py path: coverage + if-no-files-found: error test-rabbit-smoke: if: github.event.pull_request.draft == false @@ -245,10 +248,11 @@ jobs: COVERAGE_FILE: coverage/.coverage.nats-py CONTEXT: nats-py - name: Store coverage files - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: - name: coverage + name: .coverage.nats-py path: coverage + if-no-files-found: error test-nats-smoke: if: github.event.pull_request.draft == false @@ -293,10 +297,11 @@ jobs: COVERAGE_FILE: coverage/.coverage.redis-py CONTEXT: redis-py - name: Store coverage files - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: - name: coverage + name: .coverage.redis-py path: coverage + if-no-files-found: error test-redis-smoke: if: github.event.pull_request.draft == false @@ -335,10 +340,11 @@ jobs: cache-dependency-path: pyproject.toml - name: Get coverage files - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: - name: coverage + pattern: .coverage* path: coverage + merge-multiple: true - run: pip install coverage[toml] @@ -348,7 +354,7 @@ jobs: - run: coverage html --show-contexts --title "FastStream coverage for ${{ github.sha }}" - name: Store coverage html - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: coverage-html path: htmlcov diff --git a/faststream/__about__.py b/faststream/__about__.py index 78ca520a06..bbc353f927 100644 --- a/faststream/__about__.py +++ b/faststream/__about__.py @@ -1,5 +1,5 @@ """Simple and fast framework to create message brokers based microservices""" -__version__ = "0.3.8" +__version__ = "0.3.9" INSTALL_YAML = """ diff --git a/faststream/nats/test.py b/faststream/nats/test.py index 064eaa6e03..52c3b0c2ec 100644 --- a/faststream/nats/test.py +++ b/faststream/nats/test.py @@ -54,8 +54,10 @@ async def publish( # type: ignore[override] subject: str, reply_to: str = "", headers: Optional[Dict[str, str]] = None, - stream: Optional[str] = None, correlation_id: Optional[str] = None, + # NatsJSFastProducer compatibility + timeout: Optional[float] = None, + stream: Optional[str] = None, *, rpc: bool = False, rpc_timeout: Optional[float] = None, @@ -72,6 +74,9 @@ async def publish( # type: ignore[override] for handler in self.broker.handlers.values(): # pragma: no branch call = False + if stream and getattr(handler.stream, "name", None) != stream: + continue + if subject == handler.subject: call = True diff --git a/pyproject.toml b/pyproject.toml index b35947b7d9..65ee7fe044 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -47,10 +47,10 @@ classifiers = [ dynamic = ["version"] dependencies = [ - "anyio>=3.7.1,<4; python_version < '3.11'", - "anyio>=3.7.1,<5; python_version >= '3.11'", + "anyio>=3.7.1,<5", "fast-depends>=2.2.6,<3", "typer>=0.9,<1", + "typing-extensions>=4.8.0", "uvloop>=0.18.0; sys_platform != 'win32' and (sys_platform != 'cygwin' and platform_python_implementation != 'PyPy')", ] diff --git a/tests/brokers/nats/test_publish.py b/tests/brokers/nats/test_publish.py index 019ace16c9..67d7063ec6 100644 --- a/tests/brokers/nats/test_publish.py +++ b/tests/brokers/nats/test_publish.py @@ -5,4 +5,30 @@ @pytest.mark.nats class TestPublish(BrokerPublishTestcase): - pass + @pytest.mark.asyncio + async def test_stream_publish( + self, + queue: str, + test_broker, + ): + @test_broker.subscriber(queue, stream="test") + async def m(): + ... + + await test_broker.start() + await test_broker.publish("Hi!", queue, stream="test") + m.mock.assert_called_once_with("Hi!") + + @pytest.mark.asyncio + async def test_wrong_stream_publish( + self, + queue: str, + test_broker, + ): + @test_broker.subscriber(queue) + async def m(): + ... + + await test_broker.start() + await test_broker.publish("Hi!", queue, stream="test") + assert not m.mock.called diff --git a/tests/cli/test_app.py b/tests/cli/test_app.py index b0195ea9b1..d60a6a4aad 100644 --- a/tests/cli/test_app.py +++ b/tests/cli/test_app.py @@ -133,6 +133,23 @@ async def test_running(async_mock, app: FastStream): async_mock.broker_stopped.assert_called_once() +@pytest.mark.asyncio +async def test_exception_group(async_mock: AsyncMock, app: FastStream): + app._init_async_cycle() + app._stop_event.set() + + async_mock.excp.side_effect = ValueError("Ooops!") + + @app.on_startup + async def raises(): + await async_mock.excp() + + with pytest.raises(ValueError): + with patch.object(app.broker, "start", async_mock.broker_run): + with patch.object(app.broker, "close", async_mock.broker_stopped): + await app.run() + + @pytest.mark.asyncio async def test_running_lifespan_contextmanager(async_mock, mock: Mock, app: FastStream): @asynccontextmanager