Ejemplo n.º 1
0
def test_promise_thread():
    def run(resolve, _):
        sleep(0.25)
        resolve(3)
    run = Mock(wraps=run)
    p = Promise(run, T.THREAD)

    assert p._state == S.PENDING
    assert p._value is None
    assert p._timeout is None
    assert p._thread is not None
    sleep(0.05)
    run.assert_called_once_with(p._resolve, p._reject)
    run.reset_mock()
    assert not p._event.is_set()

    assert not p.wait(0.05)
    assert run.call_count == 0
    assert p._state == S.PENDING
    assert p._value is None
    assert p._timeout is None
    assert p._thread is not None
    assert not p._event.is_set()

    assert p.wait()
    assert run.call_count == 0
    assert p._state == S.RESOLVED
    assert p._value == 3
    assert p._thread is None
    assert p._event.is_set()

    assert p.wait()
Ejemplo n.º 2
0
def test_promise_immediate():
    run = Mock(wraps=lambda resolve, reject: resolve(2))
    p = Promise(run, ptype=T.IMMEDIATE)
    run.assert_called_once_with(p._resolve, p._reject)
    assert p._state == S.RESOLVED
    assert p._value == 2
    assert p._event.is_set()
    assert p.wait()
Ejemplo n.º 3
0
def test_promise_default():
    run = Mock(wraps=lambda resolve, reject: resolve(2))
    p = Promise(run)
    assert p._state == S.PENDING
    assert p._value is None
    assert p._timeout is None
    assert p._thread is None
    assert run.call_count == 0
    assert not p._event.is_set()

    assert p.wait()
    run.assert_called_once_with(p._resolve, p._reject)
    assert p._state == S.RESOLVED
    assert p._value == 2
    assert p._event.is_set()

    run.reset_mock()
    assert p.wait()
    assert run.call_count == 0