|
| 1 | +import datetime as dt |
| 2 | +import uuid |
| 3 | +from typing import List |
| 4 | + |
| 5 | +import pytest |
| 6 | +from taskiq import ScheduledTask |
| 7 | + |
| 8 | +from taskiq_nats import NATSKeyValueScheduleSource |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.anyio |
| 12 | +async def test_set_schedule(nats_urls: List[str]) -> None: |
| 13 | + prefix = uuid.uuid4().hex |
| 14 | + source = NATSKeyValueScheduleSource(servers=nats_urls, prefix=prefix) |
| 15 | + await source.startup() |
| 16 | + schedule = ScheduledTask( |
| 17 | + task_name="test_task", |
| 18 | + labels={}, |
| 19 | + args=[], |
| 20 | + kwargs={}, |
| 21 | + cron="* * * * *", |
| 22 | + ) |
| 23 | + await source.add_schedule(schedule) |
| 24 | + schedules = await source.get_schedules() |
| 25 | + assert schedules == [schedule] |
| 26 | + await source.shutdown() |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.anyio |
| 30 | +async def test_delete_schedule(nats_urls: List[str]) -> None: |
| 31 | + prefix = uuid.uuid4().hex |
| 32 | + source = NATSKeyValueScheduleSource(servers=nats_urls, prefix=prefix) |
| 33 | + await source.startup() |
| 34 | + schedule = ScheduledTask( |
| 35 | + task_name="test_task", |
| 36 | + labels={}, |
| 37 | + args=[], |
| 38 | + kwargs={}, |
| 39 | + cron="* * * * *", |
| 40 | + ) |
| 41 | + await source.add_schedule(schedule) |
| 42 | + schedules = await source.get_schedules() |
| 43 | + assert schedules == [schedule] |
| 44 | + await source.delete_schedule(schedule.schedule_id) |
| 45 | + schedules = await source.get_schedules() |
| 46 | + # Schedules are empty. |
| 47 | + assert not schedules |
| 48 | + await source.shutdown() |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.anyio |
| 52 | +async def test_post_run_cron(nats_urls: List[str]) -> None: |
| 53 | + prefix = uuid.uuid4().hex |
| 54 | + source = NATSKeyValueScheduleSource(servers=nats_urls, prefix=prefix) |
| 55 | + await source.startup() |
| 56 | + schedule = ScheduledTask( |
| 57 | + task_name="test_task", |
| 58 | + labels={}, |
| 59 | + args=[], |
| 60 | + kwargs={}, |
| 61 | + cron="* * * * *", |
| 62 | + ) |
| 63 | + await source.add_schedule(schedule) |
| 64 | + assert await source.get_schedules() == [schedule] |
| 65 | + await source.post_send(schedule) |
| 66 | + assert await source.get_schedules() == [schedule] |
| 67 | + await source.shutdown() |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.anyio |
| 71 | +async def test_post_run_time(nats_urls: List[str]) -> None: |
| 72 | + prefix = uuid.uuid4().hex |
| 73 | + source = NATSKeyValueScheduleSource(servers=nats_urls, prefix=prefix) |
| 74 | + await source.startup() |
| 75 | + schedule = ScheduledTask( |
| 76 | + task_name="test_task", |
| 77 | + labels={}, |
| 78 | + args=[], |
| 79 | + kwargs={}, |
| 80 | + time=dt.datetime(2000, 1, 1), |
| 81 | + ) |
| 82 | + await source.add_schedule(schedule) |
| 83 | + assert await source.get_schedules() == [schedule] |
| 84 | + await source.post_send(schedule) |
| 85 | + assert await source.get_schedules() == [] |
| 86 | + await source.shutdown() |
0 commit comments