test_step_claim.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from __future__ import annotations
  2. from datetime import timedelta
  3. from sqlalchemy import create_engine
  4. from sqlalchemy.orm import Session
  5. from supply_infra.db.models.pipeline_lock import PipelineLock
  6. from supply_infra.db.models.pipeline_run import PipelineRun
  7. from supply_infra.db.models.pipeline_step_run import PipelineStepRun
  8. from supply_infra.db.repositories.pipeline_step_run_repo import (
  9. PipelineStepRunRepository,
  10. )
  11. from supply_infra.pipeline.dates import china_now
  12. def _session() -> Session:
  13. engine = create_engine("sqlite+pysqlite:///:memory:")
  14. PipelineRun.__table__.create(engine)
  15. PipelineStepRun.__table__.create(engine)
  16. PipelineLock.__table__.create(engine)
  17. return Session(engine, expire_on_commit=False)
  18. def _run(run_id: str) -> PipelineRun:
  19. now = china_now()
  20. return PipelineRun(
  21. run_id=run_id,
  22. dedupe_key=f"supply_pipeline:20260727:{run_id}",
  23. pipeline_key="supply_pipeline",
  24. biz_dt="20260727",
  25. trigger_type="test",
  26. run_mode="full",
  27. dry_run=True,
  28. status="queued",
  29. deadline_at=now + timedelta(days=1),
  30. config_snapshot_json={},
  31. date_snapshot_json={},
  32. )
  33. def _step(
  34. *,
  35. step_run_id: str,
  36. run_id: str,
  37. status: str,
  38. order: int,
  39. ) -> PipelineStepRun:
  40. return PipelineStepRun(
  41. step_run_id=step_run_id,
  42. run_id=run_id,
  43. step_key=f"step-{step_run_id}",
  44. step_order=order,
  45. attempt=1,
  46. status=status,
  47. critical=True,
  48. dependency_snapshot_json=[],
  49. input_snapshot_json={},
  50. timeout_seconds=10,
  51. max_attempts=1,
  52. retryable=False,
  53. )
  54. def test_claim_respects_global_active_step_cap() -> None:
  55. with _session() as session:
  56. session.add_all(
  57. [
  58. _run("run-1"),
  59. _run("run-2"),
  60. _step(
  61. step_run_id="running",
  62. run_id="run-1",
  63. status="running",
  64. order=1,
  65. ),
  66. _step(
  67. step_run_id="ready",
  68. run_id="run-2",
  69. status="ready",
  70. order=1,
  71. ),
  72. ]
  73. )
  74. session.flush()
  75. claimed = PipelineStepRunRepository(session).claim_next_ready(
  76. owner="worker-2",
  77. lease_seconds=120,
  78. max_active_steps=1,
  79. now=china_now(),
  80. )
  81. assert claimed is None
  82. ready = session.get(PipelineStepRun, "ready")
  83. assert ready is not None
  84. assert ready.status == "ready"
  85. def test_claim_does_not_overlap_a_different_pipeline_run() -> None:
  86. with _session() as session:
  87. session.add_all(
  88. [
  89. _run("run-1"),
  90. _run("run-2"),
  91. _step(
  92. step_run_id="running",
  93. run_id="run-1",
  94. status="running",
  95. order=1,
  96. ),
  97. _step(
  98. step_run_id="ready",
  99. run_id="run-2",
  100. status="ready",
  101. order=1,
  102. ),
  103. ]
  104. )
  105. session.flush()
  106. claimed = PipelineStepRunRepository(session).claim_next_ready(
  107. owner="worker-2",
  108. lease_seconds=120,
  109. max_active_steps=2,
  110. now=china_now(),
  111. )
  112. assert claimed is None