import json import pytest pytestmark = pytest.mark.usefixtures('all_day_gpu_policy') from llm_services.store import Settings, atomic_json from llm_services.worker import Worker GPU0 = 'GPU-9efe0d3d-f341-f19a-da75-fcb985e6b96d' GPU1 = 'GPU-84ed9d05-893b-5c7e-0833-b5c34a12ba33' def configured_worker(tmp_path, monkeypatch): path = tmp_path / 'hold.json' monkeypatch.setenv('LLM_QWEN_RESERVATION_PATH', str(path)) monkeypatch.setenv('LLM_QWEN_GPU_UUID', GPU0) worker = Worker(Settings(tmp_path / 'app', tmp_path), gpu_policy=None) return worker, path def hold(path, cutoff=0, token='a' * 32, active=True): atomic_json(path, {'schema_version': 1, 'gpu_uuid': GPU0, 'token': token, 'hold': active, 'queue_seq_cutoff': cutoff}) def scheduler(worker): worker._publish_scheduler('idle') return json.loads((worker.settings.state_dir / 'gpu-scheduler.json').read_text()) def test_missing_configured_reservation_blocks_only_gpu0(tmp_path, monkeypatch): worker, path = configured_worker(tmp_path, monkeypatch) assert hasattr(worker, 'qwen_reservation'), 'worker must coordinate the on-demand Qwen hold' job = worker.store.submit('image.generate', {}, {}) assert not worker.qwen_reservation.allows({'uuid': GPU0}, job) assert worker.qwen_reservation.allows({'uuid': GPU1}, job) path.write_text('{broken') assert not worker.qwen_reservation.allows({'uuid': GPU0}, job) assert scheduler(worker)['qwen_reservation']['quiesced'] is False def test_earlier_fifo_job_runs_before_hold_and_ack_is_sticky(tmp_path, monkeypatch): worker, path = configured_worker(tmp_path, monkeypatch) earlier = worker.store.submit('image.generate', {}, {}) hold(path, earlier['queue_seq']) later = worker.store.submit('image.generate', {}, {}) assert 'qwen_reservation' in scheduler(worker), 'scheduler must return reservation acknowledgement' assert not scheduler(worker)['qwen_reservation']['quiesced'] assert worker.qwen_reservation.allows({'uuid': GPU0}, earlier) assert not worker.qwen_reservation.allows({'uuid': GPU0}, later) claimed = worker.store.claim_next(gpu_uuid=GPU1) assert claimed['id'] == earlier['id'] assert scheduler(worker)['qwen_reservation']['quiesced'] assert not worker.qwen_reservation.allows({'uuid': GPU0}, earlier) assert worker.qwen_reservation.allows({'uuid': GPU1}, later) def test_claim_between_hold_and_ack_must_drain_without_yield(tmp_path, monkeypatch): worker, path = configured_worker(tmp_path, monkeypatch) first = worker.store.submit('image.generate', {}, {}) worker.store.claim_next(gpu_uuid=GPU0) hold(path, first['queue_seq']) assert 'qwen_reservation' in scheduler(worker), 'no acknowledgement can precede a running claim' assert not scheduler(worker)['qwen_reservation']['quiesced'] assert worker._yield_reason({'uuid': GPU0, 'index': 0}) is None worker.store.finish(first['id'], 'succeeded', attempt_count=1) assert scheduler(worker)['qwen_reservation']['quiesced'] hold(path, token='b' * 32) worker._errors.append(RuntimeError('unresolved recovery')) assert not scheduler(worker)['qwen_reservation']['quiesced'] def test_released_reservation_restores_gpu0_and_changed_token_drops_old_ack(tmp_path, monkeypatch): worker, path = configured_worker(tmp_path, monkeypatch) hold(path) assert 'qwen_reservation' in scheduler(worker), 'reservation status is required' assert scheduler(worker)['qwen_reservation']['quiesced'] job = worker.store.submit('image.generate', {}, {}) hold(path, token='b' * 32, cutoff=job['queue_seq']) assert worker.qwen_reservation.allows({'uuid': GPU0}, job) assert not scheduler(worker)['qwen_reservation']['quiesced'] hold(path, active=False) assert worker.qwen_reservation.allows({'uuid': GPU0}, job) def test_dispatch_and_compatibility_entry_skip_reserved_gpu0(tmp_path, monkeypatch): worker, path = configured_worker(tmp_path, monkeypatch) class Policy: def choose(self): return {'uuid': GPU0, 'index': 0} def describe(self): return {'eligible_gpus': [self.choose(), {'uuid': GPU1, 'index': 1}]} worker.gpu_policy = Policy() worker._lock_fd = -1 worker.memory_admission = type('Memory', (), {'check': lambda *args, **kwargs: None})() hold(path) assert scheduler(worker)['qwen_reservation']['quiesced'] jobs = [worker.store.submit('image.generate', {}, {}) for _ in range(2)] executed = [] def finish(job, lease, launched=None): executed.append((job['id'], lease['uuid'])) worker.store.finish(job['id'], 'succeeded', attempt_count=job['attempt_count']) if launched is not None: launched.set() return True monkeypatch.setattr(worker, '_execute', finish) assert worker.run_once() assert worker.dispatch_once() for thread in worker._threads.values(): thread.join(2) assert executed == [(job['id'], GPU1) for job in jobs] def test_same_token_ack_survives_temporary_file_damage(tmp_path, monkeypatch): worker, path = configured_worker(tmp_path, monkeypatch) hold(path, cutoff=1) assert scheduler(worker)['qwen_reservation']['quiesced'] path.write_text('{broken') job = worker.store.submit('image.generate', {}, {}) assert not worker.qwen_reservation.allows({'uuid': GPU0}, job) hold(path, cutoff=1) assert not worker.qwen_reservation.allows({'uuid': GPU0}, job), 'an accepted token stays fenced across read errors'