Init Project Flexly Estimator
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import asyncio
|
||||
|
||||
import pytest
|
||||
|
||||
from estimator.services.jobs import JobRunner
|
||||
|
||||
|
||||
async def test_concurrency_is_limited():
|
||||
runner = JobRunner(max_concurrent=2)
|
||||
active = peak = 0
|
||||
done = 0
|
||||
|
||||
async def job():
|
||||
nonlocal active, peak, done
|
||||
active += 1
|
||||
peak = max(peak, active)
|
||||
await asyncio.sleep(0.03)
|
||||
active -= 1
|
||||
done += 1
|
||||
|
||||
for i in range(6):
|
||||
runner.submit(job, f"j{i}")
|
||||
await runner.shutdown(grace_s=5)
|
||||
assert done == 6 and peak == 2
|
||||
|
||||
|
||||
async def test_a_crashing_job_does_not_stop_the_others(caplog):
|
||||
runner = JobRunner(max_concurrent=2)
|
||||
ok = []
|
||||
|
||||
async def boom():
|
||||
raise RuntimeError("kaboom")
|
||||
|
||||
async def fine():
|
||||
ok.append(1)
|
||||
|
||||
runner.submit(boom, "boom")
|
||||
runner.submit(fine, "fine")
|
||||
await runner.shutdown(grace_s=5)
|
||||
assert ok == [1] and runner.running == 0
|
||||
assert "boom" in caplog.text
|
||||
|
||||
|
||||
async def test_shutdown_cancels_what_exceeds_the_grace_period():
|
||||
runner = JobRunner(max_concurrent=1)
|
||||
cancelled = asyncio.Event()
|
||||
|
||||
async def forever():
|
||||
try:
|
||||
await asyncio.sleep(60)
|
||||
except asyncio.CancelledError:
|
||||
cancelled.set()
|
||||
raise
|
||||
|
||||
runner.submit(forever, "forever")
|
||||
await asyncio.sleep(0.01)
|
||||
await runner.shutdown(grace_s=0.05)
|
||||
assert cancelled.is_set() and runner.running == 0
|
||||
|
||||
|
||||
async def test_submit_after_shutdown_is_refused():
|
||||
runner = JobRunner(1)
|
||||
await runner.shutdown(0.01)
|
||||
with pytest.raises(RuntimeError):
|
||||
runner.submit(lambda: asyncio.sleep(0), "late")
|
||||
Reference in New Issue
Block a user