def test_wake_task_executes_task_on_idle_worker(pool: ThreadPool): start = threading.Event() stop = threading.Event() def task(): start.set() time.sleep(0.2) stop.set() pool.wake_up(task) assert start.wait(timeout=1) assert pool.get_states().count("running task") == 1 assert stop.wait(timeout=1)
def test_wake_task_executes_task_on_assigned_worker(pool: ThreadPool): stop = threading.Event() worker = None def task(): nonlocal worker worker = threading.current_thread() time.sleep(0.2) stop.set() task.assigned_worker = random.choice(list(pool.workers)) pool.wake_up(task) assert stop.wait(timeout=1) assert worker == task.assigned_worker
def test_exception_does_not_kill_worker(): pool = ThreadPool(1) stop = threading.Event() order = [] def task1(): order.append(1) raise Exception() def task2(): order.append(2) stop.set() pool.wake_up(Task(task1)) pool.wake_up(Task(task2)) assert stop.wait(timeout=1) assert order == [1, 2]
def test_stop_stops_all_workers(pool: ThreadPool): pool.stop() for w in pool.workers: w.join()
def test_initial_state_of_workers(pool: ThreadPool): assert pool.get_states() == ["waiting"] * NUM_WORKERS
def pool(): return ThreadPool(NUM_WORKERS)
def setupClass(cls): cls.thread_pool = ThreadPool(num_workers=4)
def pool(): p = ThreadPool(num_workers=4) yield p p.stop()