예제 #1
0
def evaluate_impl(expression, params=None):
    '''Implementation of CalculatorImpl::evaluate(), also shared by
    FunctionImpl::call().  In the latter case, `params` are the parameter
    values passed to the function; in the former case, `params` is just an
    empty list.'''

    which = expression.which()

    if which == 'literal':
        return capnp.Promise(expression.literal)
    elif which == 'previousResult':
        return read_value(expression.previousResult)
    elif which == 'parameter':
        assert expression.parameter < len(params)
        return capnp.Promise(params[expression.parameter])
    elif which == 'call':
        call = expression.call
        func = call.function

        # Evaluate each parameter.
        paramPromises = [evaluate_impl(param, params) for param in call.params]

        joinedParams = capnp.join_promises(paramPromises)
        # When the parameters are complete, call the function.
        ret = (joinedParams.then(lambda vals: func.call(vals)).then(
            lambda result: result.value))

        return ret
    else:
        raise ValueError("Unknown expression type: " + which)
예제 #2
0
def test_then_args():
    capnp.Promise(0).then(lambda x: 1)

    with pytest.raises(Exception):
        capnp.Promise(0).then(lambda: 1)

    with pytest.raises(Exception):
        capnp.Promise(0).then(lambda x, y: 1)

    capnp.getTimer().after_delay(1).then(lambda: 1)  # after_delay is a VoidPromise

    with pytest.raises(Exception):
        capnp.getTimer().after_delay(1).then(lambda x: 1)

    client = capability.TestInterface._new_client(Server())

    client.foo(i=5).then(lambda x: 1)

    with pytest.raises(Exception):
        client.foo(i=5).then(lambda: 1)

    with pytest.raises(Exception):
        client.foo(i=5).then(lambda x, y: 1)
예제 #3
0
def test_timer():
    global test_timer_var
    test_timer_var = False

    def set_timer_var():
        global test_timer_var
        test_timer_var = True

    capnp.getTimer().after_delay(1).then(set_timer_var).wait()

    assert test_timer_var is True

    test_timer_var = False
    promise = capnp.Promise(0).then(lambda x: time.sleep(.1)).then(
        lambda x: time.sleep(.1)).then(lambda x: set_timer_var())

    canceller = capnp.getTimer().after_delay(1).then(lambda: promise.cancel())

    joined = capnp.join_promises([canceller, promise])
    joined.wait()