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]