示例#1
0
def test_CancellableFuture_can_force_cancel_of_embedded_future():
    import threading
    from moler.runner import CancellableFuture
    from concurrent.futures import ThreadPoolExecutor
    from moler.exceptions import MolerException

    executor = ThreadPoolExecutor()
    observer_lock = threading.Lock()
    stop_feeding = threading.Event()
    feed_done = threading.Event()

    def fun_with_future_result(delay, should_stop):
        start_time = time.time()
        while not should_stop.is_set() and (
            (time.time() - start_time) < delay):
            time.sleep(0.01)
        return delay * 2

    connection_observer_future = executor.submit(fun_with_future_result,
                                                 delay=0.5,
                                                 should_stop=stop_feeding)
    c_future = CancellableFuture(connection_observer_future,
                                 observer_lock,
                                 stop_feeding,
                                 feed_done,
                                 stop_timeout=0.2)
    time.sleep(0.1)  # let thread switch happen
    assert "state=running" in str(connection_observer_future)
    with pytest.raises(MolerException) as exc:
        c_future.cancel(no_wait=False)
    time.sleep(0.1)
    assert "state=finished" in str(connection_observer_future)
    assert "Failed to stop thread-running function within 0.2 sec" in str(
        exc.value)
def test_CancellableFuture_can_be_cancelled_while_it_is_running(observer_runner):
    from concurrent.futures import ThreadPoolExecutor, CancelledError
    from moler.runner import CancellableFuture
    # concurrent.futures.Future can't cancel() while it is already running

    stop_running = threading.Event()
    is_done = threading.Event()

    def activity(stop_running, is_done):
        while not stop_running.is_set():
            time.sleep(0.5)
        is_done.set()

    future = ThreadPoolExecutor().submit(activity, stop_running, is_done)
    observer_lock = threading.Lock()
    c_future = CancellableFuture(future, observer_lock, stop_running, is_done)
    try:
        time.sleep(0.1)  # allow threads switch to ensure future running
        assert c_future.running()
        cancelled = c_future.cancel()
        time.sleep(0.1)  # allow threads switch
        assert not c_future.running()
        assert is_done.is_set()
        assert cancelled is True
        assert c_future.cancelled()
        assert c_future.done()
        with pytest.raises(CancelledError):
            c_future.result()
    except AssertionError:
        raise
    finally:
        stop_running.set()
示例#3
0
def test_CancellableFuture_str_casting_shows_embedded_future():
    import threading
    from moler.runner import CancellableFuture
    from concurrent.futures import ThreadPoolExecutor

    def fun_with_future_result(delay):
        time.sleep(delay)
        return delay * 2

    executor = ThreadPoolExecutor()
    observer_lock = threading.Lock()
    stop_feeding = threading.Event()
    feed_done = threading.Event()
    connection_observer_future = executor.submit(fun_with_future_result, delay=0.1)
    c_future = CancellableFuture(connection_observer_future, observer_lock, stop_feeding, feed_done)
    connection_observer_future_as_str = str(connection_observer_future)
    c_future_as_str = str(c_future)
    assert c_future_as_str == "CancellableFuture({})".format(connection_observer_future_as_str)
    executor.shutdown()