예제 #1
0
def test_promise_chain_length(length, reject_i, error):
    i = 0
    def run(_=None):
        nonlocal i
        if i == reject_i:
            if error:
                raise error()
            return Promise.reject(-i)
        i += 1
        return i
    run = Mock(wraps=run)
    chain = [Promise.resolve(0)]
    for i_ in range(length):
        chain.append(chain[i_].then(run))
    assert i == 0
    assert run.call_count == 0
    assert [p._state for p in chain[1:]] == [S.PENDING] * length
    assert chain[len(chain) - 1].wait(0.2)
    assert i == reject_i
    assert run.call_count == i if reject_i >= length else i + 1
    assert [p._state for p in chain[1:i + 1]] == [S.RESOLVED] * reject_i
    assert [p._state for p in chain[i + 1:]] == [S.REJECTED] * (length - reject_i)
    assert [p._value for p in chain[1:i + 1]] == list(range(1, reject_i + 1))
    reject_n = length - reject_i
    if error is not None:
        assert [type(p._value) for p in chain[i + 1:]] == [error] * reject_n
    else:
        assert [p._value for p in chain[i + 1:]] == [-i] * reject_n
예제 #2
0
def test_promise_resolve():
    p = Promise.resolve(0)
    assert p._state == S.RESOLVED
    assert p._value == 0
    assert p._timeout is None
    assert p._thread is None
    assert p._event.is_set()
예제 #3
0
def test_promise_on_reject_2():
    on_resolve = Mock(wraps=lambda _: int('x'))
    on_reject = Mock(wraps=lambda _: Promise.reject(2))
    p = Promise.resolve(0).then(on_resolve, on_reject)
    p.wait()
    on_resolve.assert_called_once_with(0)
    assert on_reject.call_count == 0
    assert isinstance(p._value, Exception)
    assert p._state == S.REJECTED
예제 #4
0
 def run(_=None):
     nonlocal i
     if i == reject_i:
         if error:
             raise error()
         return Promise.reject(-i)
     i += 1
     if i < length:
         return Promise.resolve(i).then(run)
     return i
예제 #5
0
def test_promise_chain_depth(length, reject_i, error):
    i = 0
    def run(_=None):
        nonlocal i
        if i == reject_i:
            if error:
                raise error()
            return Promise.reject(-i)
        i += 1
        if i < length:
            return Promise.resolve(i).then(run)
        return i
    run = Mock(wraps=run)
    start = Promise.resolve(0)
    chain = start.then(run)
    end = chain.then(lambda _: 10)
    assert i == 0
    assert run.call_count == 0
    assert start._state == S.RESOLVED
    assert chain._state == S.PENDING
    assert end._state == S.PENDING
    assert end.wait(0.2)
    if reject_i >= length:
        assert chain._value == length
        assert end._value == 10
        assert chain._state == S.RESOLVED
        assert end._state == S.RESOLVED
    else:
        if error is not None:
            assert isinstance(chain._value, error)
            assert isinstance(chain._value, error)
        else:
            assert chain._value == -reject_i
            assert end._value == -reject_i
        assert chain._state == S.REJECTED
        assert end._state == S.REJECTED
    assert i == reject_i
    assert run.call_count == i if reject_i >= length else i + 1
    assert start._state == S.RESOLVED