Example #1
0
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)
Example #2
0
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
Example #3
0
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]
Example #4
0
def test_stop_stops_all_workers(pool: ThreadPool):
    pool.stop()
    for w in pool.workers:
        w.join()
Example #5
0
def test_initial_state_of_workers(pool: ThreadPool):
    assert pool.get_states() == ["waiting"] * NUM_WORKERS
Example #6
0
def pool():
    return ThreadPool(NUM_WORKERS)
Example #7
0
 def setupClass(cls):
     cls.thread_pool = ThreadPool(num_workers=4)
Example #8
0
def pool():
    p = ThreadPool(num_workers=4)
    yield p
    p.stop()