Exemplo n.º 1
0
def test_immediate_failure(framework):
    exception = RuntimeError("it failed")
    try:
        raise exception
    except:
        f0 = txaio.create_future_error()
        fail = txaio.create_failure()

    errors = []
    results = []
    f1 = txaio.create_future_error(fail)

    def cb(x):
        results.append(x)

    def errback(f):
        errors.append(f)

    txaio.add_callbacks(f0, cb, errback)
    txaio.add_callbacks(f1, cb, errback)

    run_once()
    run_once()
    run_once()

    assert len(results) == 0
    assert len(errors) == 2
    assert isinstance(errors[0], txaio.IFailedFuture)
    assert isinstance(errors[1], txaio.IFailedFuture)
    assert errors[0].value == exception
    assert errors[1].value == exception
    # should be distinct FailedPromise instances
    assert id(errors[0]) != id(errors[1])
Exemplo n.º 2
0
def test_immediate_failure(framework):
    exception = RuntimeError("it failed")
    try:
        raise exception
    except:
        f0 = txaio.create_future_error()
        fail = txaio.create_failure()

    errors = []
    results = []
    f1 = txaio.create_future_error(fail)

    def cb(x):
        results.append(x)

    def errback(f):
        errors.append(f)

    txaio.add_callbacks(f0, cb, errback)
    txaio.add_callbacks(f1, cb, errback)

    run_once()
    run_once()
    run_once()

    assert len(results) == 0
    assert len(errors) == 2
    assert isinstance(errors[0], txaio.IFailedFuture)
    assert isinstance(errors[1], txaio.IFailedFuture)
    assert errors[0].value == exception
    assert errors[1].value == exception
    # should be distinct FailedPromise instances
    assert id(errors[0]) != id(errors[1])
Exemplo n.º 3
0
def test_gather_no_consume(framework):
    '''
    consume_exceptions=False
    '''

    errors = []
    results = []
    calls = []

    f0 = txaio.create_future_error(error=RuntimeError("f0 failed"))
    f1 = txaio.create_future_error(error=RuntimeError("f1 failed"))

    f2 = txaio.gather([f0, f1], consume_exceptions=False)

    def done(arg):
        results.append(arg)

    def error(fail):
        errors.append(fail)
        # fail.printTraceback()
    txaio.add_callbacks(f0, done, error)
    txaio.add_callbacks(f1, done, error)
    txaio.add_callbacks(f2, done, error)

    # FIXME more testing annoyance; the propogated errors are raised
    # out of "run_until_complete()" as well; fix util.py?
    for f in [f0, f1, f2]:
        try:
            _await(f)
        except Exception:
            pass

    assert len(results) == 0
    assert len(errors) == 3
    assert len(calls) == 0
Exemplo n.º 4
0
def test_gather_no_consume(framework):
    '''
    consume_exceptions=False
    '''

    errors = []
    results = []
    calls = []

    f0 = txaio.create_future_error(error=RuntimeError("f0 failed"))
    f1 = txaio.create_future_error(error=RuntimeError("f1 failed"))

    f2 = txaio.gather([f0, f1], consume_exceptions=False)

    def done(arg):
        results.append(arg)

    def error(fail):
        errors.append(fail)
        # fail.printTraceback()
    txaio.add_callbacks(f0, done, error)
    txaio.add_callbacks(f1, done, error)
    txaio.add_callbacks(f2, done, error)

    # FIXME more testing annoyance; the propogated errors are raised
    # out of "run_until_complete()" as well; fix util.py?
    for f in [f0, f1, f2]:
        try:
            await(f)
        except:
            pass

    assert len(results) == 0
    assert len(errors) == 3
    assert len(calls) == 0
Exemplo n.º 5
0
 def create_connection(protocol_factory=None, server_hostname=None, host=None, port=None, ssl=False):
     if actual_protocol[0] is None:
         protocol = protocol_factory()
         actual_protocol[0] = protocol
         protocol.connection_made(fake_transport)
         return txaio.create_future_success((fake_transport, protocol))
     else:
         return txaio.create_future_error(RuntimeError("second connection fails completely"))
Exemplo n.º 6
0
    def callRemote(self, objectPath, methodName,
                   interface        = None,
                   destination      = None,
                   signature        = None,
                   body             = None,
                   expectReply      = True,
                   autoStart        = True,
                   timeout          = None,
                   returnSignature  = _NO_CHECK_RETURN):
        """
        Calls a method on a remote DBus object and returns a deferred to the result.

        @type objectPath: C{string}
        @param objectPath: Path of the remote object

        @type methodName: C{string}
        @param methodName: Name of the method to call

        @type interface: None or C{string}
        @param interface: If specified, this specifies the interface containing the
                          desired method

        @type destination: None or C{string}
        @param destination: If specified, this specifies the bus name containing
                            the remote object

        @type signature: None or C{string}
        @param signature: If specified, this specifies the DBus signature of the
                          body of the DBus MethodCall message. This string must
                          be a valid Signature string as defined by the DBus
                          specification. If arguments are supplied to the method
                          call, this parameter must be provided.

        @type body: C{list}
        @param body: A C{list} of Python objects to encode. The list content must
                     match the content of the signature parameter

        @type expectReply: C{bool}
        @param expectReply: If True (defaults to True) the returned deferred will
                            be called back with the eventual result of the remote
                            call. If False, the deferred will be immediately called
                            back with None.

        @type autoStart: C{bool}
        @param autoStart: If True (defaults to True) DBus will attempt to
                          automatically start a service to handle the method
                          call if a service matching the target object is registered
                          but not yet started.

        @type timeout: None or C{float}
        @param timeout: If specified and the remote call does not return a value
                        before the timeout expires, the returned Deferred will be
                        errbacked with a L{error.TimeOut} instance.

        @type returnSignature: C{string}
        @param returnSignature: If specified, the return values will be validated
                                against the signature string. If the returned values
                                do not mactch, the returned Deferred witl be
                                errbacked with a L{error.RemoteError} instance.
        
        @rtype: L{twisted.internet.defer.Deferred}
        @returns: a Deferred to the result. If expectReply is False, the deferred
                  will be immediately called back with None.
        """

        try:
            mcall = message.MethodCallMessage( objectPath, methodName,
                                               interface    = interface,
                                               destination  = destination,
                                               signature    = signature,
                                               body         = body,
                                               expectReply  = expectReply,
                                               autoStart    = autoStart )
            
            d = self.callRemoteMessage( mcall, timeout )

            txaio.add_callbacks(d, lambda msg: self._cbCvtReply(msg, returnSignature), None)

            return d
        except Exception as e:
            return txaio.create_future_error()