"""Chrome 프로필 슬롯 배정 — 프로세스가 여러 개여도 같은 프로필을 잡으면 안 된다.""" import os from services.search import profile_slot from services.search.profile_slot import claim_profile_slot def test_second_claim_gets_a_different_slot(tmp_path): """같은 소스·같은 워커 인덱스라도 두 번째 요청(=다른 프로세스)은 다른 슬롯을 받아야 한다.""" a = claim_profile_slot(str(tmp_path), "coupang", worker_index=0) b = claim_profile_slot(str(tmp_path), "coupang", worker_index=0) assert a != b and os.path.isdir(a) and os.path.isdir(b) def test_sources_and_workers_are_separate(tmp_path): c = claim_profile_slot(str(tmp_path), "coupang", worker_index=0) n = claim_profile_slot(str(tmp_path), "naver", worker_index=0) w1 = claim_profile_slot(str(tmp_path), "coupang", worker_index=1) assert len({c, n, w1}) == 3 def test_slot_is_reused_after_release(tmp_path): """프로세스가 죽으면 OS 가 락을 풀고 같은 슬롯이 재사용된다 — 웜 쿠키를 버리지 않기 위함.""" a = claim_profile_slot(str(tmp_path), "coupang", worker_index=0) for fh in list(profile_slot._HELD): # 프로세스 종료를 흉내 fh.close() profile_slot._HELD.remove(fh) assert claim_profile_slot(str(tmp_path), "coupang", worker_index=0) == a def test_exhausted_slots_fall_back_without_raising(tmp_path): paths = [claim_profile_slot(str(tmp_path), "coupang", worker_index=0, max_slots=2) for _ in range(3)] assert paths[2] == paths[1] # 마지막 슬롯 공유(검색 중단보다 낫다)