|
3 | 3 |
|
4 | 4 | import pytest |
5 | 5 | from freezegun import freeze_time |
| 6 | +from redis.asyncio import BlockingConnectionPool, Redis |
6 | 7 | from taskiq import ScheduledTask |
7 | 8 |
|
8 | 9 | from taskiq_redis.list_schedule_source import ListRedisScheduleSource |
@@ -179,3 +180,224 @@ async def test_migration(redis_url: str) -> None: |
179 | 180 | for old_schedule in old_schedules: |
180 | 181 | with freeze_time(old_schedule.time): |
181 | 182 | assert await source.get_schedules() == [old_schedule] |
| 183 | + |
| 184 | + |
| 185 | +@pytest.mark.anyio |
| 186 | +@freeze_time("2025-01-01 00:00:00") |
| 187 | +async def test_time_index_populated_on_add(redis_url: str) -> None: |
| 188 | + """Test that adding a time schedule populates the time index sorted set.""" |
| 189 | + prefix = uuid.uuid4().hex |
| 190 | + source = ListRedisScheduleSource(redis_url, prefix=prefix) |
| 191 | + schedule = ScheduledTask( |
| 192 | + task_name="test_task", |
| 193 | + labels={}, |
| 194 | + args=[], |
| 195 | + kwargs={}, |
| 196 | + time=datetime.datetime.now(datetime.timezone.utc) |
| 197 | + + datetime.timedelta(minutes=5), |
| 198 | + ) |
| 199 | + await source.add_schedule(schedule) |
| 200 | + |
| 201 | + # Verify the time index sorted set has an entry. |
| 202 | + async with Redis(connection_pool=source._connection_pool) as redis: |
| 203 | + members = await redis.zrange(source._get_time_index_key(), 0, -1) |
| 204 | + assert len(members) == 1 |
| 205 | + assert members[0].decode() == source._get_time_key(schedule.time) |
| 206 | + |
| 207 | + |
| 208 | +@pytest.mark.anyio |
| 209 | +@freeze_time("2025-01-01 00:00:00") |
| 210 | +async def test_time_index_cleaned_on_delete(redis_url: str) -> None: |
| 211 | + """Test that deleting last schedule from a time key cleans the index.""" |
| 212 | + prefix = uuid.uuid4().hex |
| 213 | + source = ListRedisScheduleSource(redis_url, prefix=prefix) |
| 214 | + schedule = ScheduledTask( |
| 215 | + task_name="test_task", |
| 216 | + labels={}, |
| 217 | + args=[], |
| 218 | + kwargs={}, |
| 219 | + time=datetime.datetime.now(datetime.timezone.utc) |
| 220 | + + datetime.timedelta(minutes=5), |
| 221 | + ) |
| 222 | + await source.add_schedule(schedule) |
| 223 | + |
| 224 | + # Index has 1 entry. |
| 225 | + async with Redis(connection_pool=source._connection_pool) as redis: |
| 226 | + assert await redis.zcard(source._get_time_index_key()) == 1 |
| 227 | + |
| 228 | + await source.delete_schedule(schedule.schedule_id) |
| 229 | + |
| 230 | + # After deletion, the index should be empty. |
| 231 | + async with Redis(connection_pool=source._connection_pool) as redis: |
| 232 | + assert await redis.zcard(source._get_time_index_key()) == 0 |
| 233 | + # The time key list itself should also be deleted. |
| 234 | + assert not await redis.exists(source._get_time_key(schedule.time)) |
| 235 | + |
| 236 | + |
| 237 | +@pytest.mark.anyio |
| 238 | +@freeze_time("2025-01-01 00:00:00") |
| 239 | +async def test_time_index_not_cleaned_when_other_schedules_remain( |
| 240 | + redis_url: str, |
| 241 | +) -> None: |
| 242 | + """Test that deleting one schedule doesn't remove the index entry |
| 243 | + when other schedules still exist at the same time.""" |
| 244 | + prefix = uuid.uuid4().hex |
| 245 | + source = ListRedisScheduleSource(redis_url, prefix=prefix) |
| 246 | + schedule_time = datetime.datetime.now( |
| 247 | + datetime.timezone.utc, |
| 248 | + ) + datetime.timedelta(minutes=5) |
| 249 | + schedule1 = ScheduledTask( |
| 250 | + task_name="test_task_1", |
| 251 | + labels={}, |
| 252 | + args=[], |
| 253 | + kwargs={}, |
| 254 | + time=schedule_time, |
| 255 | + ) |
| 256 | + schedule2 = ScheduledTask( |
| 257 | + task_name="test_task_2", |
| 258 | + labels={}, |
| 259 | + args=[], |
| 260 | + kwargs={}, |
| 261 | + time=schedule_time, |
| 262 | + ) |
| 263 | + await source.add_schedule(schedule1) |
| 264 | + await source.add_schedule(schedule2) |
| 265 | + |
| 266 | + await source.delete_schedule(schedule1.schedule_id) |
| 267 | + |
| 268 | + # Index should still have the entry because schedule2 remains. |
| 269 | + async with Redis(connection_pool=source._connection_pool) as redis: |
| 270 | + assert await redis.zcard(source._get_time_index_key()) == 1 |
| 271 | + |
| 272 | + await source.delete_schedule(schedule2.schedule_id) |
| 273 | + |
| 274 | + # Now the index should be empty. |
| 275 | + async with Redis(connection_pool=source._connection_pool) as redis: |
| 276 | + assert await redis.zcard(source._get_time_index_key()) == 0 |
| 277 | + |
| 278 | + |
| 279 | +@pytest.mark.anyio |
| 280 | +@freeze_time("2025-01-01 00:00:00") |
| 281 | +async def test_past_schedules_found_via_time_index(redis_url: str) -> None: |
| 282 | + """Test that past schedules are discovered via the time index |
| 283 | + instead of a full SCAN.""" |
| 284 | + prefix = uuid.uuid4().hex |
| 285 | + source = ListRedisScheduleSource(redis_url, prefix=prefix) |
| 286 | + past_time = datetime.datetime.now( |
| 287 | + datetime.timezone.utc, |
| 288 | + ) - datetime.timedelta(minutes=5) |
| 289 | + schedule = ScheduledTask( |
| 290 | + task_name="test_task", |
| 291 | + labels={}, |
| 292 | + args=[], |
| 293 | + kwargs={}, |
| 294 | + time=past_time, |
| 295 | + ) |
| 296 | + await source.add_schedule(schedule) |
| 297 | + |
| 298 | + # First call to get_schedules should find the past schedule via time index. |
| 299 | + schedules = await source.get_schedules() |
| 300 | + assert schedules == [schedule] |
| 301 | + |
| 302 | + |
| 303 | +@pytest.mark.anyio |
| 304 | +@freeze_time("2025-01-01 00:00:00") |
| 305 | +async def test_populate_time_index_from_existing_keys(redis_url: str) -> None: |
| 306 | + """Test that populate_time_index=True backfills the sorted set |
| 307 | + from existing time keys created without the index.""" |
| 308 | + prefix = uuid.uuid4().hex |
| 309 | + |
| 310 | + # Simulate old-style data: create time key lists directly in Redis |
| 311 | + # without populating the time index sorted set. |
| 312 | + pool = BlockingConnectionPool.from_url(url=redis_url) |
| 313 | + past_times = [ |
| 314 | + datetime.datetime(2024, 12, 31, 23, 55, tzinfo=datetime.timezone.utc), |
| 315 | + datetime.datetime(2024, 12, 31, 23, 56, tzinfo=datetime.timezone.utc), |
| 316 | + datetime.datetime(2024, 12, 31, 23, 57, tzinfo=datetime.timezone.utc), |
| 317 | + ] |
| 318 | + |
| 319 | + source_for_keys = ListRedisScheduleSource(redis_url, prefix=prefix) |
| 320 | + async with Redis(connection_pool=pool) as redis: |
| 321 | + for t in past_times: |
| 322 | + time_key = source_for_keys._get_time_key(t) |
| 323 | + # Push a dummy schedule ID directly (bypassing add_schedule |
| 324 | + # to simulate old behavior without time index). |
| 325 | + await redis.rpush(time_key, f"sched_{t.minute}") # type: ignore[misc] |
| 326 | + |
| 327 | + # Verify no time index exists yet. |
| 328 | + assert await redis.zcard(source_for_keys._get_time_index_key()) == 0 |
| 329 | + await pool.disconnect() |
| 330 | + |
| 331 | + # Now create a source with populate_time_index=True. |
| 332 | + source = ListRedisScheduleSource( |
| 333 | + redis_url, |
| 334 | + prefix=prefix, |
| 335 | + populate_time_index=True, |
| 336 | + ) |
| 337 | + await source.startup() |
| 338 | + |
| 339 | + # The time index should now be populated. |
| 340 | + async with Redis(connection_pool=source._connection_pool) as redis: |
| 341 | + count = await redis.zcard(source._get_time_index_key()) |
| 342 | + assert count == len(past_times) |
| 343 | + |
| 344 | + |
| 345 | +@pytest.mark.anyio |
| 346 | +@freeze_time("2025-01-01 00:00:00") |
| 347 | +async def test_post_send_cleans_time_index(redis_url: str) -> None: |
| 348 | + """Test that post_send (which calls delete_schedule for time tasks) |
| 349 | + properly cleans up the time index.""" |
| 350 | + prefix = uuid.uuid4().hex |
| 351 | + source = ListRedisScheduleSource(redis_url, prefix=prefix) |
| 352 | + schedule = ScheduledTask( |
| 353 | + task_name="test_task", |
| 354 | + labels={}, |
| 355 | + args=[], |
| 356 | + kwargs={}, |
| 357 | + time=datetime.datetime.now(datetime.timezone.utc) |
| 358 | + - datetime.timedelta(minutes=3), |
| 359 | + ) |
| 360 | + await source.add_schedule(schedule) |
| 361 | + |
| 362 | + # First run picks up past schedules. |
| 363 | + schedules = await source.get_schedules() |
| 364 | + assert schedules == [schedule] |
| 365 | + |
| 366 | + # Simulate sending the task. |
| 367 | + for s in schedules: |
| 368 | + await source.post_send(s) |
| 369 | + |
| 370 | + # Time index should be empty now. |
| 371 | + async with Redis(connection_pool=source._connection_pool) as redis: |
| 372 | + assert await redis.zcard(source._get_time_index_key()) == 0 |
| 373 | + |
| 374 | + # Second run should return nothing. |
| 375 | + schedules = await source.get_schedules() |
| 376 | + assert schedules == [] |
| 377 | + |
| 378 | + |
| 379 | +@pytest.mark.anyio |
| 380 | +@freeze_time("2025-01-01 00:00:00") |
| 381 | +async def test_cron_and_interval_not_in_time_index(redis_url: str) -> None: |
| 382 | + """Test that cron and interval schedules do not affect the time index.""" |
| 383 | + prefix = uuid.uuid4().hex |
| 384 | + source = ListRedisScheduleSource(redis_url, prefix=prefix) |
| 385 | + cron_schedule = ScheduledTask( |
| 386 | + task_name="cron_task", |
| 387 | + labels={}, |
| 388 | + args=[], |
| 389 | + kwargs={}, |
| 390 | + cron="* * * * *", |
| 391 | + ) |
| 392 | + interval_schedule = ScheduledTask( |
| 393 | + task_name="interval_task", |
| 394 | + labels={}, |
| 395 | + args=[], |
| 396 | + kwargs={}, |
| 397 | + interval=datetime.timedelta(seconds=30), |
| 398 | + ) |
| 399 | + await source.add_schedule(cron_schedule) |
| 400 | + await source.add_schedule(interval_schedule) |
| 401 | + |
| 402 | + async with Redis(connection_pool=source._connection_pool) as redis: |
| 403 | + assert await redis.zcard(source._get_time_index_key()) == 0 |
0 commit comments