コード例 #1
0
def test_postpone_performance():
    executed = finished = False

    def add(a, b, c=None):
        nonlocal executed, finished
        executed = True
        time.sleep(PERFORMANCE_SLEEP_TIME)
        finished = True
        return a + b + c

    assert not executed
    assert not finished

    postponement = pbatch.postpone(add, 1, 2, c=100)

    assert executed
    assert not finished

    time.sleep(PERFORMANCE_SLEEP_TIME / 2)

    start = time.time()
    assert postponement.wait() == 103
    end = time.time()

    duration = end - start
    assert duration < PERFORMANCE_SLEEP_TIME

    assert executed
    assert finished

    assert postponement.wait() == 103
コード例 #2
0
def test_cancel():
    def to_cancel():
        return 0

    postponement = pbatch.postpone(to_cancel)
    postponement.cancel()

    with pytest.raises(asyncio.CancelledError):
        postponement.wait()
コード例 #3
0
def test_exception():
    def raises_exception():
        raise ValueError("Raised exception")

    postponement = pbatch.postpone(raises_exception)

    for _ in range(2):
        with pytest.raises(ValueError) as info:
            postponement.wait()

        assert info.value.args == ("Raised exception", )
コード例 #4
0
def test_postpone():
    executed = finished = False

    def add(a, b, c=None):
        nonlocal executed, finished
        executed = True
        return a + b + c

    assert not executed

    postponement = pbatch.postpone(add, 1, 2, c=100)
    assert postponement.wait() == 103
コード例 #5
0
def test_cancel():
    def long_function():
        time.sleep(PERFORMANCE_SLEEP_TIME)

    start = time.time()

    postponement = pbatch.postpone(long_function)
    postponement.cancel()

    with pytest.raises(asyncio.CancelledError):
        postponement.wait()

    end = time.time()
    duration = end - start
    assert duration < PERFORMANCE_SLEEP_TIME