twisted.internet.defer.CancelledError

Here are the examples of the python api twisted.internet.defer.CancelledError taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

35 Examples 7

3 Source : internet.py
with MIT License
from autofelix

    def _cancelConnectWaiters(self):
        """
        Notify all pending requests for a connection that no more connections
        are expected.
        """
        self._unawait(Failure(CancelledError()))

    @_machine.output()

3 Source : internet.py
with MIT License
from autofelix

    def _ignoreAndCancelConnectWaiters(self, f):
        """
        Notify all pending requests for a connection that no more connections
        are expected, after ignoring the Failure passed in.
        """
        self._unawait(Failure(CancelledError()))


    @_machine.output()

3 Source : internet.py
with MIT License
from autofelix

    def _noConnection(self, failAfterFailures=None):
        """
        Notify the caller that no connection is expected.

        @return: L{Deferred} that is fired with L{CancelledError}.
        """
        return fail(CancelledError())


    @_machine.output()

3 Source : test_smtp.py
with MIT License
from autofelix

    def test_cancelBeforeConnectionMade(self):
        """
        When a user cancels L{twisted.mail.smtp.sendmail} before the connection
        is made, the connection is closed by
        L{twisted.internet.interfaces.IConnector.disconnect}.
        """
        reactor = MemoryReactor()
        d = smtp.sendmail("localhost", "source@address", "recipient@address",
                          b"message", reactor=reactor)
        d.cancel()
        self.assertEqual(reactor.connectors[0]._disconnected, True)
        failure = self.failureResultOf(d)
        failure.trap(defer.CancelledError)


    def test_cancelAfterConnectionMade(self):

3 Source : test_smtp.py
with MIT License
from autofelix

    def test_cancelAfterConnectionMade(self):
        """
        When a user cancels L{twisted.mail.smtp.sendmail} after the connection
        is made, the connection is closed by
        L{twisted.internet.interfaces.ITransport.abortConnection}.
        """
        reactor = MemoryReactor()
        transport = AbortableStringTransport()
        d = smtp.sendmail("localhost", "source@address", "recipient@address",
                          b"message", reactor=reactor)
        factory = reactor.tcpClients[0][2]
        p = factory.buildProtocol(None)
        p.makeConnection(transport)
        d.cancel()
        self.assertEqual(transport.aborting, True)
        self.assertEqual(transport.disconnecting, True)
        failure = self.failureResultOf(d)
        failure.trap(defer.CancelledError)

3 Source : test_defer.py
with MIT License
from autofelix

    def test_cancelDeferUntilLocked(self):
        """
        When cancelling a L{defer.Deferred} returned by
        L{defer.DeferredFilesystemLock.deferUntilLocked}, the
        L{defer.DeferredFilesystemLock._tryLockCall} is cancelled.
        """
        self.lock.lock()
        deferred = self.lock.deferUntilLocked()
        tryLockCall = self.lock._tryLockCall
        deferred.cancel()
        self.assertFalse(tryLockCall.active())
        self.assertIsNone(self.lock._tryLockCall)
        self.failureResultOf(deferred, defer.CancelledError)


    def test_cancelDeferUntilLockedWithTimeout(self):

3 Source : test_defer.py
with MIT License
from autofelix

    def test_cancelDeferUntilLockedWithTimeout(self):
        """
        When cancel a L{defer.Deferred} returned by
        L{defer.DeferredFilesystemLock.deferUntilLocked}, if the timeout is
        set, the timeout call will be cancelled.
        """
        self.lock.lock()
        deferred = self.lock.deferUntilLocked(timeout=1)
        timeoutCall = self.lock._timeoutCall
        deferred.cancel()
        self.assertFalse(timeoutCall.active())
        self.assertIsNone(self.lock._timeoutCall)
        self.failureResultOf(deferred, defer.CancelledError)



def _overrideFunc(v, t):

3 Source : test_agent.py
with MIT License
from autofelix

    def test_dontRetryIfFailedDueToCancel(self):
        """
        If a request failed due to the operation being cancelled,
        C{_shouldRetry} returns C{False} to indicate the request should not be
        retried.
        """
        pool = client.HTTPConnectionPool(None)
        connection = client._RetryingHTTP11ClientProtocol(None, pool)
        exception = ResponseNeverReceived([Failure(defer.CancelledError())])
        self.assertFalse(connection._shouldRetry(b"GET", exception, None))


    def test_retryIfFailedDueToNonCancelException(self):

3 Source : test_agent.py
with MIT License
from autofelix

    def test_cancel(self):
        """
        When cancelling the L{Deferred} returned by L{client.readBody}, the
        connection to the server will be aborted.
        """
        response = DummyResponse()
        deferred = client.readBody(response)
        deferred.cancel()
        self.failureResultOf(deferred, defer.CancelledError)
        self.assertTrue(response.transport.aborting)


    def test_withPotentialDataLoss(self):

3 Source : test_agent.py
with MIT License
from autofelix

    def test_deprecatedTransport(self):
        """
        Calling L{client.readBody} with a transport that does not implement
        L{twisted.internet.interfaces.ITCPTransport} produces a deprecation
        warning, but no exception when cancelling.
        """
        response = DummyResponse(transportFactory=StringTransport)
        response.transport.abortConnection = None
        d = self.assertWarns(
            DeprecationWarning,
            'Using readBody with a transport that does not have an '
            'abortConnection method',
            __file__,
            lambda: client.readBody(response))
        d.cancel()
        self.failureResultOf(d, defer.CancelledError)


    def test_deprecatedTransportNoWarning(self):

3 Source : test_xmlrpc.py
with MIT License
from autofelix

    def test_cancel(self):
        """
        A deferred from the Proxy can be cancelled, disconnecting
        the L{twisted.internet.interfaces.IConnector}.
        """
        def factory(*args, **kw):
            factory.f = TestQueryFactoryCancel(*args, **kw)
            return factory.f
        d = self.proxy(factory).callRemote('add', 2, 3)
        self.assertNotEqual(factory.f.connector.state, "disconnected")
        d.cancel()
        self.assertEqual(factory.f.connector.state, "disconnected")
        d = self.assertFailure(d, defer.CancelledError)
        return d


    def test_errorGet(self):

3 Source : test_smtp.py
with MIT License
from fbla-competitive-events

    def test_cancelBeforeConnectionMade(self):
        """
        When a user cancels L{twisted.mail.smtp.sendmail} before the connection
        is made, the connection is closed by
        L{twisted.internet.interfaces.IConnector.disconnect}.
        """
        reactor = MemoryReactor()
        d = smtp.sendmail("localhost", "source@address", "recipient@address",
                          "message", reactor=reactor)
        d.cancel()
        self.assertEqual(reactor.connectors[0]._disconnected, True)
        failure = self.failureResultOf(d)
        failure.trap(defer.CancelledError)


    def test_cancelAfterConnectionMade(self):

3 Source : test_smtp.py
with MIT License
from fbla-competitive-events

    def test_cancelAfterConnectionMade(self):
        """
        When a user cancels L{twisted.mail.smtp.sendmail} after the connection
        is made, the connection is closed by
        L{twisted.internet.interfaces.ITransport.abortConnection}.
        """
        reactor = MemoryReactor()
        transport = AbortableStringTransport()
        d = smtp.sendmail("localhost", "source@address", "recipient@address",
                          "message", reactor=reactor)
        factory = reactor.tcpClients[0][2]
        p = factory.buildProtocol(None)
        p.makeConnection(transport)
        d.cancel()
        self.assertEqual(transport.aborting, True)
        self.assertEqual(transport.disconnecting, True)
        failure = self.failureResultOf(d)
        failure.trap(defer.CancelledError)

3 Source : test_defer.py
with MIT License
from fbla-competitive-events

    def test_cancelDeferUntilLockedWithTimeout(self):
        """
        When cancel a L{defer.Deferred} returned by
        L{defer.DeferredFilesystemLock.deferUntilLocked}, if the timeout is
        set, the timeout call will be cancelled.
        """
        self.lock.lock()
        deferred = self.lock.deferUntilLocked(timeout=1)
        timeoutCall = self.lock._timeoutCall
        deferred.cancel()
        self.assertFalse(timeoutCall.active())
        self.assertIsNone(self.lock._timeoutCall)
        self.failureResultOf(deferred, defer.CancelledError)

3 Source : api.py
with GNU General Public License v2.0
from fedora-infra

def _twisted_publish(message, exchange):
    """
    Wrapper to provide a synchronous API for publishing messages via Twisted.

    Returns:
        defer.Deferred: A deferred that fires when a message has been published
            and confirmed by the broker.
    """
    _init_twisted_service()
    try:
        yield _twisted_service._service.factory.publish(message, exchange=exchange)
    except defer.CancelledError:
        _log.debug("Canceled publish of %r to %s due to timeout", message, exchange)


def publish(message, exchange=None, timeout=30):

3 Source : test_consumer.py
with GNU General Public License v2.0
from fedora-infra

    def _queue_contents(self, values):
        yield from values
        self.consumer._running = False
        yield defer.CancelledError()

    def _call_read_one(self, topic, headers, body):

3 Source : test_misc.py
with GNU General Public License v2.0
from flathub

    def test_timeout_succeeds(self):
        d = misc.cancelAfter(10, self.d, self.reactor)
        self.assertFalse(d.called)
        self.reactor.advance(11)
        d.callback("result")  # ignored
        self.assertTrue(d.called)
        yield self.assertFailure(d, defer.CancelledError)

    @defer.inlineCallbacks

3 Source : test_misc.py
with GNU General Public License v2.0
from flathub

    def test_timeout_fails(self):
        d = misc.cancelAfter(10, self.d, self.reactor)
        self.assertFalse(d.called)
        self.reactor.advance(11)
        self.d.errback(RuntimeError("oh noes"))  # ignored
        self.assertTrue(d.called)
        yield self.assertFailure(d, defer.CancelledError)

3 Source : send_tokens.py
with Apache License 2.0
from HathorNetwork

    def _render_POST_thread(self, tx: Transaction, request: Request) -> Transaction:
        """ Method called in a thread to solve tx pow without stratum
        """
        # TODO Tx should be resolved in the frontend
        def _should_stop():
            return request.should_stop_mining_thread
        tx.start_mining(sleep_seconds=self.sleep_seconds, should_stop=_should_stop)
        if request.should_stop_mining_thread:
            raise CancelledError()
        tx.update_hash()
        tx.verify()
        return tx

    def _cb_tx_resolve(self, tx, request):

3 Source : test_proxy.py
with MIT License
from jesopo

    def testConnectCancelled(self):
        d = self.checker.check(self.scan, self.env)
        host, port, factory, timeout, bindAddress = self.reactor.tcpClients[0]

        d.cancel()

        # XXX this should test the connector actually had disconnect called,
        # but MemoryReactor does not conveniently allow it

        return self.assertFailure(d, defer.CancelledError)

    def testConnectSucceeded(self):

0 Source : http11.py
with MIT License
from autofelix

    def _cb_bodyready(self, txresponse, request):
        # deliverBody hangs for responses without body
        if txresponse.length == 0:
            return txresponse, b'', None

        maxsize = request.meta.get('download_maxsize', self._maxsize)
        warnsize = request.meta.get('download_warnsize', self._warnsize)
        expected_size = txresponse.length if txresponse.length != UNKNOWN_LENGTH else -1
        fail_on_dataloss = request.meta.get('download_fail_on_dataloss', self._fail_on_dataloss)

        if maxsize and expected_size > maxsize:
            error_msg = ("Cancelling download of %(url)s: expected response "
                         "size (%(size)s) larger than download max size (%(maxsize)s).")
            error_args = {'url': request.url, 'size': expected_size, 'maxsize': maxsize}

            logger.error(error_msg, error_args)
            txresponse._transport._producer.loseConnection()
            raise defer.CancelledError(error_msg % error_args)

        if warnsize and expected_size > warnsize:
            logger.warning("Expected response size (%(size)s) larger than "
                           "download warn size (%(warnsize)s) in request %(request)s.",
                           {'size': expected_size, 'warnsize': warnsize, 'request': request})

        def _cancel(_):
            # Abort connection immediately.
            txresponse._transport._producer.abortConnection()

        d = defer.Deferred(_cancel)
        txresponse.deliverBody(_ResponseReader(
            d, txresponse, request, maxsize, warnsize, fail_on_dataloss))

        # save response for timeouts
        self._txresponse = txresponse

        return d

    def _cb_bodydone(self, result, request, url):

0 Source : test_task.py
with MIT License
from autofelix

    def test_cancel(self):
        """
        The L{Deferred} returned by L{task.deferLater} can be
        cancelled to prevent the call from actually being performed.
        """
        called = []
        clock = task.Clock()
        d = task.deferLater(clock, 1, called.append, None)
        d.cancel()
        def cbCancelled(ignored):
            # Make sure there are no calls outstanding.
            self.assertEqual([], clock.getDelayedCalls())
            # And make sure the call didn't somehow happen already.
            self.assertFalse(called)
        self.assertFailure(d, defer.CancelledError)
        d.addCallback(cbCancelled)
        return d


    def test_noCallback(self):

0 Source : client.py
with MIT License
from autofelix

    def _shouldRetry(self, method, exception, bodyProducer):
        """
        Indicate whether request should be retried.

        Only returns C{True} if method is idempotent, no response was
        received, the reason for the failed request was not due to
        user-requested cancellation, and no body was sent. The latter
        requirement may be relaxed in the future, and PUT added to approved
        method list.

        @param method: The method of the request.
        @type method: L{bytes}
        """
        if method not in (b"GET", b"HEAD", b"OPTIONS", b"DELETE", b"TRACE"):
            return False
        if not isinstance(exception, (RequestNotSent,
                                      RequestTransmissionFailed,
                                      ResponseNeverReceived)):
            return False
        if isinstance(exception, _WrapperException):
            for aFailure in exception.reasons:
                if aFailure.check(defer.CancelledError):
                    return False
        if bodyProducer is not None:
            return False
        return True


    def request(self, request):

0 Source : _newclient.py
with MIT License
from autofelix

    def request(self, request):
        """
        Issue C{request} over C{self.transport} and return a L{Deferred} which
        will fire with a L{Response} instance or an error.

        @param request: The object defining the parameters of the request to
           issue.
        @type request: L{Request}

        @rtype: L{Deferred}
        @return: The deferred may errback with L{RequestGenerationFailed} if
            the request was not fully written to the transport due to a local
            error.  It may errback with L{RequestTransmissionFailed} if it was
            not fully written to the transport due to a network error.  It may
            errback with L{ResponseFailed} if the request was sent (not
            necessarily received) but some or all of the response was lost.  It
            may errback with L{RequestNotSent} if it is not possible to send
            any more requests using this L{HTTP11ClientProtocol}.
        """
        if self._state != 'QUIESCENT':
            return fail(RequestNotSent())

        self._state = 'TRANSMITTING'
        _requestDeferred = maybeDeferred(request.writeTo, self.transport)

        def cancelRequest(ign):
            # Explicitly cancel the request's deferred if it's still trying to
            # write when this request is cancelled.
            if self._state in (
                    'TRANSMITTING', 'TRANSMITTING_AFTER_RECEIVING_RESPONSE'):
                _requestDeferred.cancel()
            else:
                self.transport.abortConnection()
                self._disconnectParser(Failure(CancelledError()))
        self._finishedRequest = Deferred(cancelRequest)

        # Keep track of the Request object in case we need to call stopWriting
        # on it.
        self._currentRequest = request

        self._transportProxy = TransportProxyProducer(self.transport)
        self._parser = HTTPClientParser(request, self._finishResponse)
        self._parser.makeConnection(self._transportProxy)
        self._responseDeferred = self._parser._responseDeferred

        def cbRequestWritten(ignored):
            if self._state == 'TRANSMITTING':
                self._state = 'WAITING'
                self._responseDeferred.chainDeferred(self._finishedRequest)

        def ebRequestWriting(err):
            if self._state == 'TRANSMITTING':
                self._state = 'GENERATION_FAILED'
                self.transport.abortConnection()
                self._finishedRequest.errback(
                    Failure(RequestGenerationFailed([err])))
            else:
                self._log.failure(
                    u'Error writing request, but not in valid state '
                    u'to finalize request: {state}',
                    failure=err,
                    state=self._state
                )

        _requestDeferred.addCallbacks(cbRequestWritten, ebRequestWriting)

        return self._finishedRequest


    def _finishResponse(self, rest):

0 Source : test_task.py
with The Unlicense
from dspray95

    def test_cancel(self):
        """
        The L{Deferred} returned by L{task.deferLater} can be
        cancelled to prevent the call from actually being performed.
        """
        called = []
        clock = task.Clock()
        d = task.deferLater(clock, 1, called.append, None)
        d.cancel()
        def cbCancelled(ignored):
            # Make sure there are no calls outstanding.
            self.assertEqual([], clock.getDelayedCalls())
            # And make sure the call didn't somehow happen already.
            self.assertFalse(called)
        self.assertFailure(d, defer.CancelledError)
        d.addCallback(cbCancelled)
        return d



class _FakeReactor(object):

0 Source : internet.py
with MIT License
from fbla-competitive-events

    def whenConnected(self):
        """
        Retrieve the currently-connected L{Protocol}, or the next one to
        connect.

        @return: a Deferred that fires with a protocol produced by the factory
            passed to C{__init__}
        @rtype: L{Deferred} firing with L{IProtocol} or failing with
            L{CancelledError} the service is stopped.
        """
        if self._currentConnection is not None:
            return succeed(self._currentConnection)
        elif self._stopped:
            return fail(CancelledError())
        else:
            result = Deferred()
            self._awaitingConnected.append(result)
            return result


    def _unawait(self, value):

0 Source : internet.py
with MIT License
from fbla-competitive-events

    def stopService(self):
        """
        Stop attempting to reconnect and close any existing connections.

        @return: a L{Deferred} that fires when all outstanding connections are
            closed and all in-progress connection attempts halted.
        """
        super(ClientService, self).stopService()
        self._stopRetry()
        self._stopRetry = _noop
        self._connectionInProgress.cancel()
        self._loseConnection()
        self._currentConnection = None
        def finishStopping(result):
            if not self.running:
                self._stopped = True
                self._unawait(Failure(CancelledError()))
            return None
        return (gatherResults([self._connectionInProgress, self._lostDeferred])
                .addBoth(finishStopping))



__all__ = (['TimerService', 'CooperatorService', 'MulticastServer',

0 Source : _newclient.py
with MIT License
from fbla-competitive-events

    def request(self, request):
        """
        Issue C{request} over C{self.transport} and return a L{Deferred} which
        will fire with a L{Response} instance or an error.

        @param request: The object defining the parameters of the request to
           issue.
        @type request: L{Request}

        @rtype: L{Deferred}
        @return: The deferred may errback with L{RequestGenerationFailed} if
            the request was not fully written to the transport due to a local
            error.  It may errback with L{RequestTransmissionFailed} if it was
            not fully written to the transport due to a network error.  It may
            errback with L{ResponseFailed} if the request was sent (not
            necessarily received) but some or all of the response was lost.  It
            may errback with L{RequestNotSent} if it is not possible to send
            any more requests using this L{HTTP11ClientProtocol}.
        """
        if self._state != 'QUIESCENT':
            return fail(RequestNotSent())

        self._state = 'TRANSMITTING'
        _requestDeferred = maybeDeferred(request.writeTo, self.transport)

        def cancelRequest(ign):
            # Explicitly cancel the request's deferred if it's still trying to
            # write when this request is cancelled.
            if self._state in (
                    'TRANSMITTING', 'TRANSMITTING_AFTER_RECEIVING_RESPONSE'):
                _requestDeferred.cancel()
            else:
                self.transport.abortConnection()
                self._disconnectParser(Failure(CancelledError()))
        self._finishedRequest = Deferred(cancelRequest)

        # Keep track of the Request object in case we need to call stopWriting
        # on it.
        self._currentRequest = request

        self._transportProxy = TransportProxyProducer(self.transport)
        self._parser = HTTPClientParser(request, self._finishResponse)
        self._parser.makeConnection(self._transportProxy)
        self._responseDeferred = self._parser._responseDeferred

        def cbRequestWritten(ignored):
            if self._state == 'TRANSMITTING':
                self._state = 'WAITING'
                self._responseDeferred.chainDeferred(self._finishedRequest)

        def ebRequestWriting(err):
            if self._state == 'TRANSMITTING':
                self._state = 'GENERATION_FAILED'
                self.transport.abortConnection()
                self._finishedRequest.errback(
                    Failure(RequestGenerationFailed([err])))
            else:
                log.err(err, u'Error writing request, but not in valid state '
                             u'to finalize request: %s' % self._state)

        _requestDeferred.addCallbacks(cbRequestWritten, ebRequestWriting)

        return self._finishedRequest


    def _finishResponse(self, rest):

0 Source : factory.py
with GNU General Public License v2.0
from fedora-infra

    def when_connected(self):
        """
        Retrieve the currently-connected Protocol, or the next one to connect.

        Returns:
            defer.Deferred: A Deferred that fires with a connected
                :class:`FedoraMessagingProtocolV2` instance. This is similar to
                the whenConnected method from the Twisted endpoints APIs, which
                is sadly isn't available before 16.1.0, which isn't available
                in EL7.
        """
        if self._client and not self._client.is_closed:
            _std_log.debug("Already connected with %r", self._client)
        else:
            self._client = None
            _std_log.debug(
                "Waiting for %r to fire with new connection", self._client_deferred
            )
            try:
                yield self._client_deferred
            except defer.CancelledError:
                # Renew the deferred to handle future connections.
                self._client_deferred = defer.Deferred()
                raise
        defer.returnValue(self._client)

    @defer.inlineCallbacks

0 Source : manager.py
with GNU General Public License v2.0
from flathub

    def newConnection(self, conn, workerName):
        if workerName in self.connections:
            log.msg(("Got duplication connection from '{}'"
                     " starting arbitration procedure").format(workerName))
            old_conn = self.connections[workerName]
            try:
                yield misc.cancelAfter(self.PING_TIMEOUT,
                                       old_conn.remotePrint("master got a duplicate connection"))
                # if we get here then old connection is still alive, and new
                # should be rejected
                raise RuntimeError("rejecting duplicate worker")
            except defer.CancelledError:
                old_conn.loseConnection()
                log.msg("Connected worker '{}' ping timed out after {} seconds".format(workerName,
                        self.PING_TIMEOUT))
            except RuntimeError:
                raise
            except Exception as e:
                old_conn.loseConnection()
                log.msg("Got error while trying to ping connected worker {}:{}".format(workerName,
                                                                                       e))
            log.msg("Old connection for '{}' was lost, accepting new".format(workerName))

        try:
            yield conn.remotePrint(message="attached")
            info = yield conn.remoteGetWorkerInfo()
            log.msg("Got workerinfo from '{}'".format(workerName))
        except Exception as e:
            log.msg("Failed to communicate with worker '{}'\n{}".format(workerName, e))
            raise

        conn.info = info
        self.connections[workerName] = conn

        def remove():
            del self.connections[workerName]
        conn.notifyOnDisconnect(remove)

        # accept the connection
        return True

0 Source : scanner.py
with MIT License
from jesopo

    def addCheck(self, check, pool, scanset, *args):
        if self.finished:
            return

        if (check, scanset.timeout) in self.checks:
            return

        self.checks.add((check, scanset.timeout))

        d = pool.run(check, self, *args)
        self.running[(check, scanset.timeout)] = d

        @d.addBoth
        def killRunning(result):
            del self.running[(check, scanset.timeout)]
            return result

        def checked(result):
            if result is not None:
                # We found something:
                self._setResult((scanset, result))

                # Stop all other checks:
                # (list(...) because it'll change size during iteration)
                running = list(self.running.values())
                for d in running:
                    d.cancel()
                assert not self.running

        def failed(fail):
            fail.trap(defer.CancelledError)

        d.addCallbacks(checked, failed)
        @d.addErrback
        def report(fail):
            for errhandler in self.errhandlers:
                errhandler(fail)

        @d.addBoth
        def maybeFinished(ignored):
            if self.started and not self.running and not self.result:
                # We were the last pending check to finish. We're clean.
                self._setResult(None)
            return ignored

        # TODO: might be worth it performance-wise to batch our timeouts.

        # We do not bother cancelling these timeouts: cancelling an
        # already cancelled deferred is a noop.
        self.clock.callLater(scanset.timeout, d.cancel)

    def start(self):

0 Source : test_proxy.py
with MIT License
from jesopo

    def testConnectionCancelled(self):
        d = self.checker.check(self.scan, self.env)
        host, port, factory, timeout, bindAddress = self.reactor.tcpClients[0]

        # HACK: this makes some assumptions about how ClientCreator works.
        factory.reactor = self.clock

        transport = proto_helpers.StringTransport()

        proto = factory.buildProtocol(None)
        proto.transport = transport
        proto.connectionMade()

        self.clock.advance(0)

        d.cancel()
        self.failUnless(transport.disconnecting)
        return self.assertFailure(d, defer.CancelledError)


class LineProtocolTest(unittest.TestCase):

0 Source : http11.py
with Apache License 2.0
from lynings

    def _cb_bodyready(self, txresponse, request):
        # deliverBody hangs for responses without body
        if txresponse.length == 0:
            return txresponse, b'', None

        maxsize = request.meta.get('download_maxsize', self._maxsize)
        warnsize = request.meta.get('download_warnsize', self._warnsize)
        expected_size = txresponse.length if txresponse.length != UNKNOWN_LENGTH else -1
        fail_on_dataloss = request.meta.get('download_fail_on_dataloss', self._fail_on_dataloss)

        if maxsize and expected_size > maxsize:
            error_msg = ("Cancelling download of %(url)s: expected response "
                         "size (%(size)s) larger than download max size (%(maxsize)s).")
            error_args = {'url': request.url, 'size': expected_size, 'maxsize': maxsize}

            logger.error(error_msg, error_args)
            txresponse._transport._producer.loseConnection()
            raise defer.CancelledError(error_msg % error_args)

        if warnsize and expected_size > warnsize:
            logger.warning("Expected response size (%(size)s) larger than "
                           "download warn size (%(warnsize)s) in request %(request)s.",
                           {'size': expected_size, 'warnsize': warnsize, 'request': request})

        def _cancel(_):
            # Abort connection inmediately.
            txresponse._transport._producer.abortConnection()

        d = defer.Deferred(_cancel)
        txresponse.deliverBody(_ResponseReader(
            d, txresponse, request, maxsize, warnsize, fail_on_dataloss))

        # save response for timeouts
        self._txresponse = txresponse

        return d

    def _cb_bodydone(self, result, request, url):

0 Source : well_known_resolver.py
with Apache License 2.0
from matrix-org

    async def _fetch_well_known(self, server_name: bytes) -> Tuple[bytes, float]:
        """Actually fetch and parse a .well-known, without checking the cache

        Args:
            server_name: name of the server, from the requested url

        Raises:
            _FetchWellKnownFailure if we fail to lookup a result

        Returns:
            The lookup result and cache period.
        """

        had_valid_well_known = self._had_valid_well_known_cache.get(server_name, False)

        # We do this in two steps to differentiate between possibly transient
        # errors (e.g. can't connect to host, 503 response) and more permanent
        # errors (such as getting a 404 response).
        response, body = await self._make_well_known_request(
            server_name, retry=had_valid_well_known
        )

        try:
            if response.code != 200:
                raise Exception("Non-200 response %s" % (response.code,))

            parsed_body = json_decoder.decode(body.decode("utf-8"))
            logger.info("Response from .well-known: %s", parsed_body)

            result = parsed_body["m.server"].encode("ascii")
        except defer.CancelledError:
            # Bail if we've been cancelled
            raise
        except Exception as e:
            logger.info("Error parsing well-known for %s: %s", server_name, e)
            raise _FetchWellKnownFailure(temporary=False)

        cache_period = _cache_period_from_headers(
            response.headers, time_now=self._reactor.seconds
        )
        if cache_period is None:
            cache_period = WELL_KNOWN_DEFAULT_CACHE_PERIOD
            # add some randomness to the TTL to avoid a stampeding herd every 24 hours
            # after startup
            cache_period *= random.uniform(
                1 - WELL_KNOWN_DEFAULT_CACHE_PERIOD_JITTER,
                1 + WELL_KNOWN_DEFAULT_CACHE_PERIOD_JITTER,
            )
        else:
            cache_period = min(cache_period, WELL_KNOWN_MAX_CACHE_PERIOD)
            cache_period = max(cache_period, WELL_KNOWN_MIN_CACHE_PERIOD)

        # We got a success, mark as such in the cache
        self._had_valid_well_known_cache.set(
            server_name,
            bool(result),
            cache_period + WELL_KNOWN_REMEMBER_DOMAIN_HAD_VALID,
        )

        return result, cache_period

    async def _make_well_known_request(

0 Source : well_known_resolver.py
with Apache License 2.0
from matrix-org

    async def _make_well_known_request(
        self, server_name: bytes, retry: bool
    ) -> Tuple[IResponse, bytes]:
        """Make the well known request.

        This will retry the request if requested and it fails (with unable
        to connect or receives a 5xx error).

        Args:
            server_name: name of the server, from the requested url
            retry: Whether to retry the request if it fails.

        Raises:
            _FetchWellKnownFailure if we fail to lookup a result

        Returns:
            Returns the response object and body. Response may be a non-200 response.
        """
        uri = b"https://%s/.well-known/matrix/server" % (server_name,)
        uri_str = uri.decode("ascii")

        headers = {
            b"User-Agent": [self.user_agent],
        }

        i = 0
        while True:
            i += 1

            logger.info("Fetching %s", uri_str)
            try:
                response = await make_deferred_yieldable(
                    self._well_known_agent.request(
                        b"GET", uri, headers=Headers(headers)
                    )
                )
                body_stream = BytesIO()
                await make_deferred_yieldable(
                    read_body_with_max_size(response, body_stream, WELL_KNOWN_MAX_SIZE)
                )
                body = body_stream.getvalue()

                if 500   <  = response.code  <  600:
                    raise Exception("Non-200 response %s" % (response.code,))

                return response, body
            except defer.CancelledError:
                # Bail if we've been cancelled
                raise
            except BodyExceededMaxSize:
                # If the well-known file was too large, do not keep attempting
                # to download it, but consider it a temporary error.
                logger.warning(
                    "Requested .well-known file for %s is too large > %r bytes",
                    server_name.decode("ascii"),
                    WELL_KNOWN_MAX_SIZE,
                )
                raise _FetchWellKnownFailure(temporary=True)
            except Exception as e:
                if not retry or i >= WELL_KNOWN_RETRY_ATTEMPTS:
                    logger.info("Error fetching %s: %s", uri_str, e)
                    raise _FetchWellKnownFailure(temporary=True)

                logger.info("Error fetching %s: %s. Retrying", uri_str, e)

            # Sleep briefly in the hopes that they come back up
            await self._clock.sleep(0.5)


def _cache_period_from_headers(