test_intel_serial_barrier.py
python
sha256:545266da3f09ffbfe65a21bfc233bb91fe37f83ab714dd2d07db1fee98c3988b
feat(intel): always-on code intelligence for every reposito…
Sonnet 5.5
minor
⚠ breaking
3 days ago
| 1 | """Intel jobs share the prebuild memory barrier — musehub#8 release gate R1 (bounded, serialised execution). |
| 2 | |
| 3 | ``_SERIAL_JOB_TYPES`` held only ``fetch.mpack.prebuild``: two intel jobs (any repositories, any worker processes) or an intel job and a prebuild could |
| 4 | run at once and add their memory peaks (the #331 failure class). Intel jobs now join the same host-wide barrier, claimed under the same advisory lock. |
| 5 | |
| 6 | Run ONLY this file, against a private Postgres (never the live hub database):: |
| 7 | |
| 8 | TEST_DATABASE_URL=postgresql+asyncpg://musehub:musehub@<private-host>:<port>/musehub_test python -P -m pytest tests/test_intel_serial_barrier.py |
| 9 | |
| 10 | B1 A pending intel job is not claimable while a prebuild is running, and a pending prebuild is not claimable while an intel job is running. |
| 11 | B2 A pending intel job is not claimable while ANOTHER repository's intel job is running (host-wide, not per repository). |
| 12 | B3 Once the running serial job is no longer running the next one is claimable, oldest first. |
| 13 | B4 Non-serial job types are unaffected. |
| 14 | """ |
| 15 | from __future__ import annotations |
| 16 | |
| 17 | import pytest |
| 18 | import pytest_asyncio |
| 19 | from sqlalchemy.ext.asyncio import AsyncSession |
| 20 | |
| 21 | from musehub.services.musehub_jobs import claim_next_job |
| 22 | from tests.factories import create_repo |
| 23 | from tests.test_intel_job_state_machine import add_job, job |
| 24 | |
| 25 | |
| 26 | @pytest_asyncio.fixture |
| 27 | async def two_repos(db_session: AsyncSession) -> tuple[str, str]: |
| 28 | a = await create_repo(db_session, owner="gabriel", slug="serial-a") |
| 29 | b = await create_repo(db_session, owner="gabriel", slug="serial-b") |
| 30 | return a.repo_id, b.repo_id |
| 31 | |
| 32 | |
| 33 | @pytest.mark.asyncio |
| 34 | async def test_b1_intel_and_prebuild_exclude_each_other(db_session: AsyncSession, two_repos) -> None: |
| 35 | a, b = two_repos |
| 36 | running_prebuild = await add_job(db_session, a, "fetch.mpack.prebuild", status="running") |
| 37 | pending_intel = await add_job(db_session, b, "intel.code", "sha256:aa") |
| 38 | assert await claim_next_job(db_session) is None |
| 39 | await db_session.rollback() |
| 40 | assert (await job(db_session, pending_intel)).status == "pending" |
| 41 | |
| 42 | # reverse direction: a running intel job blocks a pending prebuild (and the prebuild's own barrier is unchanged) |
| 43 | r = await job(db_session, running_prebuild) |
| 44 | r.status = "done" |
| 45 | await db_session.commit() |
| 46 | await add_job(db_session, a, "intel.code.coupling", "sha256:aa", status="running") |
| 47 | pending_prebuild = await add_job(db_session, b, "fetch.mpack.prebuild") |
| 48 | claimed = await claim_next_job(db_session) |
| 49 | await db_session.rollback() |
| 50 | assert claimed is None or claimed.job_id != pending_prebuild |
| 51 | |
| 52 | |
| 53 | @pytest.mark.asyncio |
| 54 | async def test_b2_the_barrier_is_host_wide_not_per_repository(db_session: AsyncSession, two_repos) -> None: |
| 55 | a, b = two_repos |
| 56 | await add_job(db_session, a, "intel.code.type", "sha256:aa", status="running") |
| 57 | await add_job(db_session, b, "intel.code", "sha256:bb") |
| 58 | assert await claim_next_job(db_session) is None |
| 59 | |
| 60 | |
| 61 | @pytest.mark.asyncio |
| 62 | async def test_b3_the_next_serial_job_is_claimable_once_nothing_is_running_prebuild_first_then_oldest(db_session: AsyncSession, two_repos) -> None: |
| 63 | """Prebuild priority (always-on intel): a waiting prebuild is claimed before an OLDER intel job; among intel jobs, oldest first.""" |
| 64 | a, b = two_repos |
| 65 | running = await add_job(db_session, a, "intel.code", "sha256:aa", status="running") |
| 66 | older = await add_job(db_session, b, "intel.code", "sha256:bb", age=60) |
| 67 | prebuild = await add_job(db_session, a, "fetch.mpack.prebuild", age=10) |
| 68 | assert await claim_next_job(db_session) is None |
| 69 | await db_session.rollback() |
| 70 | r = await job(db_session, running) |
| 71 | r.status = "done" |
| 72 | await db_session.commit() |
| 73 | claimed = await claim_next_job(db_session) |
| 74 | assert claimed is not None and claimed.job_id == prebuild |
| 75 | await db_session.commit() |
| 76 | await db_session.rollback() |
| 77 | done = await job(db_session, prebuild) |
| 78 | done.status = "done" |
| 79 | await db_session.commit() |
| 80 | claimed = await claim_next_job(db_session) |
| 81 | assert claimed is not None and claimed.job_id == older |
| 82 | |
| 83 | |
| 84 | @pytest.mark.asyncio |
| 85 | async def test_b4_non_serial_job_types_still_claim_while_a_serial_job_runs(db_session: AsyncSession, two_repos) -> None: |
| 86 | a, b = two_repos |
| 87 | await add_job(db_session, a, "intel.code", "sha256:aa", status="running") |
| 88 | gc = await add_job(db_session, b, "gc") |
| 89 | claimed = await claim_next_job(db_session) |
| 90 | assert claimed is not None and claimed.job_id == gc |
File History
2 commits
sha256:545266da3f09ffbfe65a21bfc233bb91fe37f83ab714dd2d07db1fee98c3988b
feat(intel): always-on code intelligence for every reposito…
Sonnet 5.5
minor
⚠
3 days ago
sha256:29f70c9cc76cb0f77c2573efaf7b8bbc64de5a995d17aab676e774332be8f16b
fix(jobs): intel jobs join the host-wide serial memory barr…
Sonnet 5.5
patch
4 days ago