Python twisted.internet.defer.fail() Examples

The following are 30 code examples of twisted.internet.defer.fail(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module twisted.internet.defer , or try the search function .
Example #1
Source File: test_group.py    From afkak with Apache License 2.0 6 votes vote down vote up
def test_get_coordinator_retry(self):
        """
        fail to retrieve a coordinator and retry
        """
        client = self.mock_client([
            defer.succeed(Mock(error_code=0)),
        ])
        coord = self.make_coordinator(client)
        client._get_coordinator_for_group.return_value = defer.fail(
            RequestTimedOutError())
        client._get_brokerclient.return_value = None
        de = coord.get_coordinator_broker()
        self.successResultOf(de)
        assert_delayed_calls(1, client)  # Heartbeat scheduled.
        client._get_coordinator_for_group.return_value = defer.fail(UnknownError())
        de = coord.get_coordinator_broker()
        self.successResultOf(de) 
Example #2
Source File: endpoints.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def listen(self, factory):
        """
        Implement L{IStreamServerEndpoint.listen} to start listening on, and
        then close, C{self._fileno}.
        """
        if self._used:
            return defer.fail(error.AlreadyListened())
        self._used = True

        try:
            self._setNonBlocking(self.fileno)
            port = self.reactor.adoptStreamPort(
                self.fileno, self.addressFamily, factory)
            self._close(self.fileno)
        except:
            return defer.fail()
        return defer.succeed(port) 
Example #3
Source File: endpoints.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def connect(self, protocolFactory):
        """
        Implement L{IStreamClientEndpoint.connect} to launch a child process
        and connect it to a protocol created by C{protocolFactory}.

        @param protocolFactory: A factory for an L{IProtocol} provider which
            will be notified of all events related to the created process.
        """
        proto = protocolFactory.buildProtocol(_ProcessAddress())
        try:
            self._spawnProcess(
                _WrapIProtocol(proto, self._executable, self._errFlag),
                self._executable, self._args, self._env, self._path, self._uid,
                self._gid, self._usePTY, self._childFDs)
        except:
            return defer.fail()
        else:
            return defer.succeed(proto) 
Example #4
Source File: cred.py    From bitmask-dev with GNU General Public License v3.0 6 votes vote down vote up
def checkSoledadToken(self, username, password, service):
        soledad = self._soledad_sessions.get(username)
        if not soledad:
            return defer.fail(Exception("No soledad"))

        def match_token(token):
            if token is None:
                raise RuntimeError('no token')
            if token == password:
                return username
            else:
                raise RuntimeError('bad token')

        d = soledad.get_or_create_service_token(service)
        d.addCallback(match_token)
        return d 
Example #5
Source File: test_incoming_mail.py    From bitmask-dev with GNU General Public License v3.0 6 votes vote down vote up
def testExtractInvalidAttachedKey(self):
        KEY = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n..."

        message = MIMEMultipart()
        message.add_header("from", ADDRESS_2)
        key = MIMEApplication("", "pgp-keys")
        key.set_payload(KEY)
        message.attach(key)
        self.fetcher._keymanager.put_raw_key = Mock(
            return_value=defer.fail(KeyAddressMismatch()))

        def put_raw_key_called(_):
            self.fetcher._keymanager.put_raw_key.assert_called_once_with(
                KEY, address=ADDRESS_2)

        d = self._do_fetch(message.as_string())
        d.addCallback(put_raw_key_called)
        d.addErrback(log.err)
        return d 
Example #6
Source File: test_newclient.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_failedWriteTo(self):
        """
        If the L{Deferred} returned by L{Request.writeTo} fires with a
        L{Failure}, L{HTTP11ClientProtocol.request} disconnects its transport
        and returns a L{Deferred} which fires with a L{Failure} of
        L{RequestGenerationFailed} wrapping the underlying failure.
        """
        class BrokenRequest:
            persistent = False
            def writeTo(self, transport):
                return fail(ArbitraryException())

        d = self.protocol.request(BrokenRequest())
        def cbFailed(ignored):
            self.assertTrue(self.transport.disconnecting)
            # Simulate what would happen if the protocol had a real transport
            # and make sure no exception is raised.
            self.protocol.connectionLost(
                Failure(ConnectionDone(u"you asked for it")))
        d = assertRequestGenerationFailed(self, d, [ArbitraryException])
        d.addCallback(cbFailed)
        return d 
Example #7
Source File: test_incoming_mail.py    From bitmask-dev with GNU General Public License v3.0 6 votes vote down vote up
def testExtractOpenPGPHeaderIfInvalidAttachedKey(self):
        KEY = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n..."
        KEYURL = "https://leap.se/key.txt"
        OpenPGP = "id=12345678; url=\"%s\"; preference=signencrypt" % (KEYURL,)

        message = MIMEMultipart()
        message.add_header("from", ADDRESS_2)
        message.add_header("OpenPGP", OpenPGP)
        key = MIMEApplication("", "pgp-keys")
        key.set_payload(KEY)
        message.attach(key)

        self.fetcher._keymanager.put_raw_key = Mock(
            return_value=defer.fail(KeyAddressMismatch()))
        self.fetcher._keymanager.fetch_key = Mock()

        def put_raw_key_called(_):
            self.fetcher._keymanager.put_raw_key.assert_called_once_with(
                KEY, address=ADDRESS_2)
            self.fetcher._keymanager.fetch_key.assert_called_once_with(
                ADDRESS_2, KEYURL)

        d = self._do_fetch(message.as_string())
        d.addCallback(put_raw_key_called)
        return d 
Example #8
Source File: test_incoming_mail.py    From bitmask-dev with GNU General Public License v3.0 6 votes vote down vote up
def testLogErrorIfDecryptFails(self):

        def assert_failure(_):
            mock_logger_error.assert_any_call('_decrypt_doc: '
                                              'Error decrypting document with '
                                              'ID 1')

        with patch.object(Logger, 'error') as mock_logger_error:
            doc = SoledadDocument()
            doc.doc_id = '1'
            doc.content = {'_enc_json': ''}

            self.fetcher._process_decrypted_doc = Mock()
            self.km.decrypt = Mock(
                return_value=defer.fail(Exception()))

            d = self.fetcher._decrypt_doc(doc)
            d.addCallback(assert_failure)
            return d 
Example #9
Source File: thimble.py    From kotori with GNU Affero General Public License v3.0 6 votes vote down vote up
def _deferToThreadPool(self, f, *args, **kwargs):
        """Defer execution of ``f(*args, **kwargs)`` to the thread pool.
        This returns a deferred which will callback with the result of
        that expression, or errback with a failure wrapping the raised
        exception.
        """
        if self._pool.joined:
            return fail(
                ReactorNotRunning("This thimble's threadpool already stopped.")
            )
        if not self._pool.started:
            self._pool.start()
            self._reactor.addSystemEventTrigger(
                'during', 'shutdown', self._pool.stop)

        return deferToThreadPool(self._reactor, self._pool, f, *args, **kwargs) 
Example #10
Source File: test_xmlrpc.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_doubleEncodingError(self):
        """
        If it is not possible to encode a response to the request (for example,
        because L{xmlrpclib.dumps} raises an exception when encoding a
        L{Fault}) the exception which prevents the response from being
        generated is logged and the request object is finished anyway.
        """
        d = self.proxy().callRemote("echo", "")

        # *Now* break xmlrpclib.dumps.  Hopefully the client already used it.
        def fakeDumps(*args, **kwargs):
            raise RuntimeError("Cannot encode anything at all!")
        self.patch(xmlrpclib, 'dumps', fakeDumps)

        # It doesn't matter how it fails, so long as it does.  Also, it happens
        # to fail with an implementation detail exception right now, not
        # something suitable as part of a public interface.
        d = self.assertFailure(d, Exception)

        def cbFailed(ignored):
            # The fakeDumps exception should have been logged.
            self.assertEqual(len(self.flushLoggedErrors(RuntimeError)), 1)
        d.addCallback(cbFailed)
        return d 
Example #11
Source File: _protocol.py    From afkak with Apache License 2.0 6 votes vote down vote up
def request(self, request):
        """
        Send a request to the Kafka broker.

        :param bytes request:
            The bytes of a Kafka `RequestMessage`_ structure. It must have
            a unique (to this connection) correlation ID.

        :returns:
            `Deferred` which will:

              - Succeed with the bytes of a Kafka `ResponseMessage`_
              - Fail when the connection terminates

        .. _RequestMessage:: https://kafka.apache.org/protocol.html#protocol_messages

        """
        if self._failed is not None:
            return fail(self._failed)
        correlation_id = request[4:8]
        assert correlation_id not in self._pending
        d = Deferred()
        self.sendString(request)
        self._pending[correlation_id] = d
        return d 
Example #12
Source File: test_util.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_renderNoFailure(self):
        """
        If the L{Deferred} fails, L{DeferredResource} reports the failure via
        C{processingFailed}, and does not cause an unhandled error to be
        logged.
        """
        request = DummyRequest([])
        d = request.notifyFinish()
        failure = Failure(RuntimeError())
        deferredResource = DeferredResource(defer.fail(failure))
        deferredResource.render(request)
        self.assertEqual(self.failureResultOf(d), failure)
        del deferredResource
        gc.collect()
        errors = self.flushLoggedErrors(RuntimeError)
        self.assertEqual(errors, []) 
Example #13
Source File: client.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def request(self, method, uri, headers=None, bodyProducer=None):
        """
        Issue a request to the server indicated by the given C{uri}.

        An existing connection from the connection pool may be used or a new
        one may be created.

        I{HTTP} and I{HTTPS} schemes are supported in C{uri}.

        @see: L{twisted.web.iweb.IAgent.request}
        """
        parsedURI = URI.fromBytes(uri)
        try:
            endpoint = self._getEndpoint(parsedURI)
        except SchemeNotSupported:
            return defer.fail(Failure())
        key = (parsedURI.scheme, parsedURI.host, parsedURI.port)
        return self._requestWithEndpoint(key, endpoint, method, parsedURI,
                                         headers, bodyProducer,
                                         parsedURI.originForm) 
Example #14
Source File: test_producer.py    From afkak with Apache License 2.0 6 votes vote down vote up
def test_producer_stop_waiting_to_retry(self):
        """
        Test stopping producer while it's waiting to retry a request
        """
        clock = MemoryReactorClock()
        client = Mock(reactor=clock)
        f = Failure(BrokerNotAvailableError())
        ret = [fail(f)]
        client.send_produce_request.side_effect = ret
        client.topic_partitions = {self.topic: [0, 1, 2, 3]}
        client.metadata_error_for_topic.return_value = False
        msgs = [self.msg("one"), self.msg("two")]
        batch_n = 2

        producer = Producer(client, batch_every_n=batch_n, batch_send=True)
        d = producer.send_messages(self.topic, msgs=msgs)
        # At first, there's no result. Have to retry due to first failure
        self.assertNoResult(d)
        # Advance the clock, some, but not enough to retry
        clock.advance(producer._retry_interval / 2)
        # Stop the producer before the retry
        producer.stop()
        self.failureResultOf(d, tid_CancelledError) 
Example #15
Source File: test_producer.py    From afkak with Apache License 2.0 6 votes vote down vote up
def test_producer_stop_during_request(self):
        """
        Test stopping producer while it's waiting for reply from client
        """
        clock = MemoryReactorClock()
        client = Mock(reactor=clock)
        f = Failure(BrokerNotAvailableError())
        ret = [fail(f), Deferred()]
        client.send_produce_request.side_effect = ret
        client.topic_partitions = {self.topic: [0, 1, 2, 3]}
        client.metadata_error_for_topic.return_value = False
        msgs = [self.msg("one"), self.msg("two")]
        batch_n = 2

        producer = Producer(client, batch_every_n=batch_n, batch_send=True)
        d = producer.send_messages(self.topic, msgs=msgs)
        # At first, there's no result. Have to retry due to first failure
        self.assertNoResult(d)
        clock.advance(producer._retry_interval)

        producer.stop()
        self.failureResultOf(d, tid_CancelledError) 
Example #16
Source File: test_producer.py    From afkak with Apache License 2.0 6 votes vote down vote up
def test_producer_send_messages_no_retry_fail(self):
        client = Mock(reactor=MemoryReactorClock())
        f = Failure(BrokerNotAvailableError())
        client.send_produce_request.side_effect = [fail(f)]
        client.topic_partitions = {self.topic: [0, 1, 2, 3]}
        client.metadata_error_for_topic.return_value = False
        msgs = [self.msg("one"), self.msg("two")]

        producer = Producer(client, max_req_attempts=1)
        d = producer.send_messages(self.topic, msgs=msgs)
        # Check the expected request was sent
        msgSet = create_message_set(
            make_send_requests(msgs), producer.codec)
        req = ProduceRequest(self.topic, 0, msgSet)
        client.send_produce_request.assert_called_once_with(
            [req], acks=producer.req_acks, timeout=producer.ack_timeout,
            fail_on_error=False)
        self.failureResultOf(d, BrokerNotAvailableError)

        producer.stop() 
Example #17
Source File: test_group.py    From afkak with Apache License 2.0 6 votes vote down vote up
def test_join_error(self):
        """
            Get an error when joining and retry
        """
        client = self.mock_client([
            defer.fail(RebalanceInProgress()),
            self.join_response(),
            self.sync_response(),
        ])
        coord = self.make_coordinator(client)
        de = coord.join_and_sync()
        self.successResultOf(de)
        self.assertEqual(coord._rejoin_needed, True)
        assert_delayed_calls(1, client)
        de = coord.join_and_sync()
        self.successResultOf(de)
        self.assertEqual(coord._rejoin_needed, False) 
Example #18
Source File: test_group.py    From afkak with Apache License 2.0 6 votes vote down vote up
def test_sync_error(self):
        """
            Get an error when syncing and retry
        """
        client = self.mock_client([
            self.join_response(),
            defer.fail(RebalanceInProgress()),
            self.join_response(),
            self.sync_response(),
        ])
        coord = self.make_coordinator(client)
        de = coord.join_and_sync()
        self.successResultOf(de)
        self.assertEqual(coord._rejoin_needed, True)
        assert_delayed_calls(1, client)
        de = coord.join_and_sync()
        self.successResultOf(de)
        self.assertEqual(coord._rejoin_needed, False) 
Example #19
Source File: endpoints.py    From afkak with Apache License 2.0 5 votes vote down vote up
def fail(self, host_pattern, reason):
        """
        Fail a pending connection request.

        :param str host_pattern:
            :func:`fnmatch.fnmatchcase` pattern against which the connection
            request must match.

        :param reason: `twisted.python.failure.Failure`
        """
        host, port, protocolFactory, d = self._match(host_pattern)
        d.errback(reason) 
Example #20
Source File: test_producer.py    From afkak with Apache License 2.0 5 votes vote down vote up
def test_producer_send_messages_batched(self):
        clock = MemoryReactorClock()
        client = Mock(reactor=clock)
        f = Failure(BrokerNotAvailableError())
        ret = [fail(f), succeed([ProduceResponse(self.topic, 0, 0, 10)])]
        client.send_produce_request.side_effect = ret
        client.topic_partitions = {self.topic: [0, 1, 2, 3]}
        client.metadata_error_for_topic.return_value = False
        msgs = [self.msg("one"), self.msg("two")]
        batch_n = 2

        producer = Producer(client, batch_every_n=batch_n, batch_send=True)
        d = producer.send_messages(self.topic, msgs=msgs)
        # Check the expected request was sent
        msgSet = create_message_set(
            make_send_requests(msgs), producer.codec)
        req = ProduceRequest(self.topic, ANY, msgSet)
        client.send_produce_request.assert_called_once_with(
            [req], acks=producer.req_acks, timeout=producer.ack_timeout,
            fail_on_error=False)
        # At first, there's no result. Have to retry due to first failure
        self.assertNoResult(d)
        clock.advance(producer._retry_interval)
        self.successResultOf(d)

        producer.stop() 
Example #21
Source File: _asynctest.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def _run(self, methodName, result):
        from twisted.internet import reactor
        timeout = self.getTimeout()
        def onTimeout(d):
            e = defer.TimeoutError("%r (%s) still running at %s secs"
                % (self, methodName, timeout))
            f = failure.Failure(e)
            # try to errback the deferred that the test returns (for no gorram
            # reason) (see issue1005 and test_errorPropagation in
            # test_deferred)
            try:
                d.errback(f)
            except defer.AlreadyCalledError:
                # if the deferred has been called already but the *back chain
                # is still unfinished, crash the reactor and report timeout
                # error ourself.
                reactor.crash()
                self._timedOut = True # see self._wait
                todo = self.getTodo()
                if todo is not None and todo.expected(f):
                    result.addExpectedFailure(self, f, todo)
                else:
                    result.addError(self, f)
        onTimeout = utils.suppressWarnings(
            onTimeout, util.suppress(category=DeprecationWarning))
        method = getattr(self, methodName)
        if inspect.isgeneratorfunction(method):
            exc = TypeError(
                '%r is a generator function and therefore will never run' % (
                    method,))
            return defer.fail(exc)
        d = defer.maybeDeferred(
            utils.runWithWarningsSuppressed, self._getSuppress(), method)
        call = reactor.callLater(timeout, onTimeout, d)
        d.addBoth(lambda x : call.active() and call.cancel() or x)
        return d 
Example #22
Source File: test_xmlrpc.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def xmlrpc_deferFail(self):
        return defer.fail(TestValueError())

    # don't add a doc string, it's part of the test 
Example #23
Source File: test_group.py    From afkak with Apache License 2.0 5 votes vote down vote up
def test_get_coordinator_fatal(self):
        """
        A non-retriable failure when retrieving the coordinator broker leaves
        the coordinator in a quiescent state.
        """
        client = self.mock_client([])
        client._get_coordinator_for_group.return_value = defer.fail(AttributeError())
        coord = self.make_coordinator(client)

        self.failureResultOf(coord.get_coordinator_broker()).check(AttributeError)

        # No heartbeat scheduled.
        self.assertEqual([], client.reactor.getDelayedCalls()) 
Example #24
Source File: openpgp.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def put_raw_key(self, key_data, address):
        """
        Put key contained in C{key_data} in local storage.

        :param key_data: The key data to be stored.
        :type key_data: str or unicode
        :param address: address for which this key will be active
        :type address: str

        :return: A Deferred which fires when the OpenPGPKey is in the storage.
        :rtype: Deferred
        """
        leap_assert_type(key_data, (str, unicode))

        openpgp_privkey = None
        try:
            openpgp_pubkey, openpgp_privkey = self.parse_key(
                key_data, address)
        except (errors.KeyAddressMismatch, errors.KeyFingerprintMismatch) as e:
            return defer.fail(e)

        def put_key(_, key):
            return self.put_key(key)

        d = defer.succeed(None)
        if openpgp_pubkey is not None:
            d.addCallback(put_key, openpgp_pubkey)
        if openpgp_privkey is not None:
            d.addCallback(put_key, openpgp_privkey)
        return d 
Example #25
Source File: endpoints.py    From afkak with Apache License 2.0 5 votes vote down vote up
def connect(self, protocolFactory):
        e = IndentationError('failing to connect {}'.format(protocolFactory))
        return defer.fail(e) 
Example #26
Source File: test_unix.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def waitForDescriptor(self):
        """
        Return a L{Deferred} which will fire with the next file descriptor
        received, or with a failure if the connection is or has already been
        lost.
        """
        if self.reason is None:
            self.waiting = Deferred()
            return self.waiting
        else:
            return fail(self.reason) 
Example #27
Source File: endpoints.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def connect(self, protocolFactory):
        """
        Implement L{IStreamClientEndpoint.connect} to connect via a
        UNIX Socket
        """
        try:
            wf = _WrappingFactory(protocolFactory)
            self._reactor.connectUNIX(
                self._path, wf,
                timeout=self._timeout,
                checkPID=self._checkPID)
            return wf._onConnection
        except:
            return defer.fail() 
Example #28
Source File: _newclient.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def _connectionLost_WAITING(self, reason):
        """
        Disconnect the response parser so that it can propagate the event as
        necessary (for example, to call an application protocol's
        C{connectionLost} method, or to fail a request L{Deferred}) and move
        to the C{'CONNECTION_LOST'} state.
        """
        self._disconnectParser(reason)
        self._state = 'CONNECTION_LOST' 
Example #29
Source File: endpoints.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def connect(self, protocolFactory):
        """
        Implement L{IStreamClientEndpoint.connect} to connect with SSL over
        TCP.
        """
        try:
            wf = _WrappingFactory(protocolFactory)
            self._reactor.connectSSL(
                self._host, self._port, wf, self._sslContextFactory,
                timeout=self._timeout, bindAddress=self._bindAddress)
            return wf._onConnection
        except:
            return defer.fail() 
Example #30
Source File: checkers.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def requestAvatarId(self, credentials):
        if credentials.username in self.users:
            return defer.maybeDeferred(
                credentials.checkPassword,
                self.users[credentials.username]).addCallback(
                self._cbPasswordMatch, credentials.username)
        else:
            return defer.fail(error.UnauthorizedLogin())