Skip to content

fix: StreamSchedule loses messages #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ test = [

"coverage[toml]>=7.2.0,<8.0.0",
"pytest>=7.4.0,<9",
"freezegun>=1.2.2"
]

dev = [
Expand Down
8 changes: 0 additions & 8 deletions taskiq_faststream/kicker.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
from typing import Any

from taskiq.kicker import AsyncKicker, _FuncParams, _ReturnType
from taskiq.message import TaskiqMessage


class LabelRespectKicker(AsyncKicker[_FuncParams, _ReturnType]):
"""Patched kicker doesn't cast labels to str."""

def _prepare_message(self, *args: Any, **kwargs: Any) -> TaskiqMessage:
msg = super()._prepare_message(*args, **kwargs)
msg.labels = self.labels
return msg
54 changes: 52 additions & 2 deletions tests/testcase.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import asyncio
from datetime import datetime, timezone
from datetime import datetime, timedelta, timezone
from typing import Any
from unittest.mock import MagicMock

import pytest
from faststream.utils.functions import timeout_scope
from freezegun import freeze_time
from taskiq import AsyncBroker, TaskiqScheduler
from taskiq.cli.scheduler.args import SchedulerArgs
from taskiq.cli.scheduler.run import run_scheduler
from taskiq.schedule_sources import LabelScheduleSource

from taskiq_faststream import BrokerWrapper
from taskiq_faststream import BrokerWrapper, StreamScheduler


@pytest.mark.anyio
Expand Down Expand Up @@ -67,3 +68,52 @@ async def handler(msg: str) -> None:

mock.assert_called_once_with("Hi!")
task.cancel()

async def test_task_multiple_schedules_by_cron(
self,
subject: str,
broker: Any,
event: asyncio.Event,
) -> None:
"""Test cron runs twice via StreamScheduler."""
received_message = []

@broker.subscriber(subject)
async def handler(msg: str) -> None:
received_message.append(msg)
event.set()

taskiq_broker = self.build_taskiq_broker(broker)

taskiq_broker.task(
"Hi!",
**{self.subj_name: subject},
schedule=[
{
"cron": "* * * * *",
},
],
)

async with self.test_class(broker):
with freeze_time("00:00:00", tick=True) as frozen_datetime:
task = asyncio.create_task(
run_scheduler(
SchedulerArgs(
scheduler=StreamScheduler(
broker=taskiq_broker,
sources=[LabelScheduleSource(taskiq_broker)],
),
modules=[],
),
),
)

await asyncio.wait_for(event.wait(), 2.0)
event.clear()
frozen_datetime.tick(timedelta(minutes=2))
await asyncio.wait_for(event.wait(), 2.0)

task.cancel()

assert received_message == ["Hi!", "Hi!"], received_message