Python twisted.internet.defer.CancelledError() Examples
The following are 30
code examples of twisted.internet.defer.CancelledError().
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_protocol.py From learn_python3_spider with MIT License | 6 votes |
def _cancelConnectTest(self, connect): """ Helper for implementing a test to verify that cancellation of the L{Deferred} returned by one of L{ClientCreator}'s I{connect} methods is implemented to cancel the underlying connector. @param connect: A function which will be invoked with a L{ClientCreator} instance as an argument and which should call one its I{connect} methods and return the result. @return: A L{Deferred} which fires when the test is complete or fails if there is a problem. """ reactor = MemoryReactorClock() cc = ClientCreator(reactor, Protocol) d = connect(cc) connector = reactor.connectors.pop() self.assertFalse(connector._disconnected) d.cancel() self.assertTrue(connector._disconnected) return self.assertFailure(d, CancelledError)
Example #2
Source File: test_protocol.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def _cancelConnectTest(self, connect): """ Helper for implementing a test to verify that cancellation of the L{Deferred} returned by one of L{ClientCreator}'s I{connect} methods is implemented to cancel the underlying connector. @param connect: A function which will be invoked with a L{ClientCreator} instance as an argument and which should call one its I{connect} methods and return the result. @return: A L{Deferred} which fires when the test is complete or fails if there is a problem. """ reactor = MemoryReactorClock() cc = ClientCreator(reactor, Protocol) d = connect(cc) connector = reactor.connectors.pop() self.assertFalse(connector._disconnected) d.cancel() self.assertTrue(connector._disconnected) return self.assertFailure(d, CancelledError)
Example #3
Source File: test_agent.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_cancelGetConnectionCancelsEndpointConnect(self): """ Cancelling the C{Deferred} returned from L{HTTPConnectionPool.getConnection} cancels the C{Deferred} returned by opening a new connection with the given endpoint. """ self.assertEqual(self.pool._connections, {}) connectionResult = Deferred() class Endpoint: def connect(self, factory): return connectionResult d = self.pool.getConnection(12345, Endpoint()) d.cancel() self.assertEqual(self.failureResultOf(connectionResult).type, CancelledError)
Example #4
Source File: test_producer.py From afkak with Apache License 2.0 | 6 votes |
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 #5
Source File: test_producer.py From afkak with Apache License 2.0 | 6 votes |
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 #6
Source File: test_protocol.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_cancelConnectUNIXTimeout(self): """ L{ClientCreator.connectUNIX} inserts a very short delayed call between the time the connection is established and the time the L{Deferred} returned from one of its connect methods actually fires. If the L{Deferred} is cancelled in this interval, the established connection is closed, the timeout is cancelled, and the L{Deferred} fails with L{CancelledError}. """ def connect(reactor, cc): d = cc.connectUNIX('/foo/bar') address, factory, timeout, bindAddress = reactor.unixClients.pop() protocol = factory.buildProtocol(None) transport = StringTransport() protocol.makeConnection(transport) return d return self._cancelConnectTimeoutTest(connect)
Example #7
Source File: test_protocol.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_cancelConnectTCPTimeout(self): """ L{ClientCreator.connectTCP} inserts a very short delayed call between the time the connection is established and the time the L{Deferred} returned from one of its connect methods actually fires. If the L{Deferred} is cancelled in this interval, the established connection is closed, the timeout is cancelled, and the L{Deferred} fails with L{CancelledError}. """ def connect(reactor, cc): d = cc.connectTCP('example.com', 1234) host, port, factory, timeout, bindAddress = reactor.tcpClients.pop() protocol = factory.buildProtocol(None) transport = StringTransport() protocol.makeConnection(transport) return d return self._cancelConnectTimeoutTest(connect)
Example #8
Source File: test_protocol.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_cancelConnectSSLTimeout(self): """ L{ClientCreator.connectSSL} inserts a very short delayed call between the time the connection is established and the time the L{Deferred} returned from one of its connect methods actually fires. If the L{Deferred} is cancelled in this interval, the established connection is closed, the timeout is cancelled, and the L{Deferred} fails with L{CancelledError}. """ def connect(reactor, cc): d = cc.connectSSL('example.com', 1234, object()) host, port, factory, contextFactory, timeout, bindADdress = reactor.sslClients.pop() protocol = factory.buildProtocol(None) transport = StringTransport() protocol.makeConnection(transport) return d return self._cancelConnectTimeoutTest(connect)
Example #9
Source File: test_internet.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_clientConnectionFailed(self): """ When a client connection fails, the service removes its reference to the protocol and tries again after a timeout. """ clock = Clock() cq, service = self.makeReconnector(fireImmediately=False, clock=clock) self.assertEqual(len(cq.connectQueue), 1) cq.connectQueue[0].errback(Failure(Exception())) whenConnected = service.whenConnected() self.assertNoResult(whenConnected) # Don't fail during test tear-down when service shutdown causes all # waiting connections to fail. whenConnected.addErrback(lambda ignored: ignored.trap(CancelledError)) clock.advance(AT_LEAST_ONE_ATTEMPT) self.assertEqual(len(cq.connectQueue), 2)
Example #10
Source File: test_internet.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_whenConnectedErrbacksOnStopService(self): """ L{ClientService.whenConnected} returns a L{Deferred} that errbacks with L{CancelledError} if L{ClientService.stopService} is called between connection attempts. """ clock = Clock() cq, service = self.makeReconnector(fireImmediately=False, clock=clock) beforeErrbackAndStop = service.whenConnected() # The protocol fails to connect, and the service is waiting to # reconnect. cq.connectQueue[0].errback(Exception("no connection")) service.stopService() afterErrbackAndStop = service.whenConnected() self.assertIsInstance(self.failureResultOf(beforeErrbackAndStop).value, CancelledError) self.assertIsInstance(self.failureResultOf(afterErrbackAndStop).value, CancelledError)
Example #11
Source File: test_smtp.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
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)
Example #12
Source File: test_endpoints.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_execCancelled(self): """ If execution of the command is cancelled via the L{Deferred} returned by L{SSHCommandClientEndpoint.connect}, the connection is closed immediately. """ self.realm.channelLookup[b'session'] = UnsatisfiedExecSession endpoint = self.create() factory = Factory() factory.protocol = Protocol connected = endpoint.connect(factory) server, client, pump = self.finishConnection() connected.cancel() f = self.failureResultOf(connected) f.trap(CancelledError) self.assertClientTransportState(client, True)
Example #13
Source File: test_task.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
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
Example #14
Source File: test_defer.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_cancelDeferredListCallback(self): """ When cancelling an unfired L{defer.DeferredList} without the C{fireOnOneCallback} and C{fireOnOneErrback} flags set, the L{defer.DeferredList} will be callback with a C{list} of (success, result) C{tuple}s. """ deferredOne = defer.Deferred(fakeCallbackCanceller) deferredTwo = defer.Deferred() deferredList = defer.DeferredList([deferredOne, deferredTwo]) deferredList.cancel() self.failureResultOf(deferredTwo, defer.CancelledError) result = self.successResultOf(deferredList) self.assertTrue(result[0][0]) self.assertEqual(result[0][1], "Callback Result") self.assertFalse(result[1][0]) self.assertTrue(result[1][1].check(defer.CancelledError))
Example #15
Source File: producer.py From afkak with Apache License 2.0 | 6 votes |
def _complete_batch_send(self, resp): """Complete the processing of our batch send operation Clear the deferred tracking our current batch processing and reset our retry count and retry interval Return none to eat any errors coming from up the deferred chain """ self._batch_send_d = None self._req_attempts = 0 self._retry_interval = self._init_retry_interval if isinstance(resp, Failure) and not resp.check(tid_CancelledError, CancelledError): log.error( "Failure detected in _complete_batch_send: %r", resp, exc_info=(resp.type, resp.value, resp.getTracebackObject()), ) return
Example #16
Source File: test_defer.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_noCancellerMultipleCancelsAfterCancelAndErrback(self): """ A L{defer.Deferred} without a canceller, when cancelled and then errbacked, ignores multiple cancels thereafter. """ d = defer.Deferred() d.addCallbacks(self._callback, self._errback) d.cancel() self.assertEqual(self.errbackResults.type, defer.CancelledError) currentFailure = self.errbackResults # One errback will be ignored d.errback(GenericError()) # I.e., we should still have a CancelledError. self.assertEqual(self.errbackResults.type, defer.CancelledError) d.cancel() self.assertIs(currentFailure, self.errbackResults)
Example #17
Source File: test_defer.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_cancellerMultipleCancel(self): """ Verify that calling cancel multiple times on a deferred with a canceller that does not errback results in a L{defer.CancelledError} and that subsequent calls to cancel do not cause an error and that after all that, the canceller was only called once. """ def cancel(d): self.cancellerCallCount += 1 d = defer.Deferred(canceller=cancel) d.addCallbacks(self._callback, self._errback) d.cancel() self.assertEqual(self.errbackResults.type, defer.CancelledError) currentFailure = self.errbackResults d.cancel() self.assertIs(currentFailure, self.errbackResults) self.assertEqual(self.cancellerCallCount, 1)
Example #18
Source File: test_defer.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_simpleCanceller(self): """ Verify that a L{defer.Deferred} calls its specified canceller when it is cancelled, and that further call/errbacks raise L{defer.AlreadyCalledError}. """ def cancel(d): self.cancellerCallCount += 1 d = defer.Deferred(canceller=cancel) d.addCallbacks(self._callback, self._errback) d.cancel() self.assertEqual(self.cancellerCallCount, 1) self.assertEqual(self.errbackResults.type, defer.CancelledError) # Test that further call/errbacks are *not* swallowed self.assertRaises(defer.AlreadyCalledError, d.callback, None) self.assertRaises(defer.AlreadyCalledError, d.errback, Exception())
Example #19
Source File: test_defer.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_cancelNestedDeferred(self): """ Verify that a Deferred, a, which is waiting on another Deferred, b, returned from one of its callbacks, will propagate L{defer.CancelledError} when a is cancelled. """ def innerCancel(d): self.cancellerCallCount += 1 def cancel(d): self.assertTrue(False) b = defer.Deferred(canceller=innerCancel) a = defer.Deferred(canceller=cancel) a.callback(None) a.addCallback(lambda data: b) a.cancel() a.addCallbacks(self._callback, self._errback) # The cancel count should be one (the cancellation done by B) self.assertEqual(self.cancellerCallCount, 1) # B's canceller didn't errback, so defer.py will have called errback # with a CancelledError. self.assertEqual(self.errbackResults.type, defer.CancelledError)
Example #20
Source File: test_defer.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_cancelQueueAfterGet(self): """ When canceling a L{Deferred} from a L{DeferredQueue} that does not have a result (i.e., the L{Deferred} has not fired), the cancel causes a L{defer.CancelledError} failure. If the queue has a result later on, it doesn't try to fire the deferred. """ queue = defer.DeferredQueue() d = queue.get() d.cancel() self.assertImmediateFailure(d, defer.CancelledError) def cb(ignore): # If the deferred is still linked with the deferred queue, it will # fail with an AlreadyCalledError queue.put(None) return queue.get().addCallback(self.assertIs, None) d.addCallback(cb) done = [] d.addCallback(done.append) self.assertEqual(len(done), 1)
Example #21
Source File: test_defer.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_errbackAddedBeforeTimeout(self): """ An errback added before a timeout is added errbacks with a L{defer.CancelledError} when the timeout fires. If the errback returns the L{defer.CancelledError}, it is translated to a L{defer.TimeoutError} by the timeout implementation. """ clock = Clock() d = defer.Deferred() dErrbacked = [None] def errback(f): dErrbacked[0] = f return f d.addErrback(errback) d.addTimeout(10, clock) clock.advance(15) self.assertIsInstance(dErrbacked[0], failure.Failure) self.assertIsInstance(dErrbacked[0].value, defer.CancelledError) self.failureResultOf(d, defer.TimeoutError)
Example #22
Source File: test_defer.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_errbackAddedBeforeTimeoutSuppressesCancellation(self): """ An errback added before a timeout is added errbacks with a L{defer.CancelledError} when the timeout fires. If the errback suppresses the L{defer.CancelledError}, the deferred successfully completes. """ clock = Clock() d = defer.Deferred() dErrbacked = [None] def errback(f): dErrbacked[0] = f f.trap(defer.CancelledError) d.addErrback(errback) d.addTimeout(10, clock) clock.advance(15) self.assertIsInstance(dErrbacked[0], failure.Failure) self.assertIsInstance(dErrbacked[0].value, defer.CancelledError) self.successResultOf(d)
Example #23
Source File: test_defer.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_errbackAddedBeforeTimeoutCustom(self): """ An errback added before a timeout is added with a custom timeout function errbacks with a L{defer.CancelledError} when the timeout fires. The timeout function runs if the errback returns the L{defer.CancelledError}. """ clock = Clock() d = defer.Deferred() dErrbacked = [None] def errback(f): dErrbacked[0] = f return f d.addErrback(errback) d.addTimeout(10, clock, _overrideFunc) clock.advance(15) self.assertIsInstance(dErrbacked[0], failure.Failure) self.assertIsInstance(dErrbacked[0].value, defer.CancelledError) self.assertEqual("OVERRIDDEN", self.successResultOf(d))
Example #24
Source File: test_defer.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_errbackAddedBeforeTimeoutSuppressesCancellationCustom(self): """ An errback added before a timeout is added with a custom timeout function errbacks with a L{defer.CancelledError} when the timeout fires. The timeout function runs if the errback suppresses the L{defer.CancelledError}. """ clock = Clock() d = defer.Deferred() dErrbacked = [None] def errback(f): dErrbacked[0] = f d.addErrback(errback) d.addTimeout(10, clock, _overrideFunc) clock.advance(15) self.assertIsInstance(dErrbacked[0], failure.Failure) self.assertIsInstance(dErrbacked[0].value, defer.CancelledError) self.assertEqual("OVERRIDDEN", self.successResultOf(d))
Example #25
Source File: write_request_load.py From flocker with Apache License 2.0 | 6 votes |
def _create_dataset(self, node): """ Creates a dataset in the node given. :param node: node where we want the dataset. :return: A ``Deferred`` that fires when the dataset has been created. :raise DatasetCreationTimeout: if the creation goes wrong. """ self.dataset_node = node creating = self.control_service.create_dataset( primary=node.uuid) def handle_timeout_and_errors(failure): failure.trap(CancelledError) raise DatasetCreationTimeout() timeout(self.reactor, creating, self.timeout) creating.addErrback(handle_timeout_and_errors) return creating
Example #26
Source File: test_agent.py From learn_python3_spider with MIT License | 6 votes |
def test_cancelGetConnectionCancelsEndpointConnect(self): """ Cancelling the C{Deferred} returned from L{HTTPConnectionPool.getConnection} cancels the C{Deferred} returned by opening a new connection with the given endpoint. """ self.assertEqual(self.pool._connections, {}) connectionResult = Deferred() class Endpoint: def connect(self, factory): return connectionResult d = self.pool.getConnection(12345, Endpoint()) d.cancel() self.assertEqual(self.failureResultOf(connectionResult).type, CancelledError)
Example #27
Source File: test_agent.py From learn_python3_spider with MIT License | 6 votes |
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)
Example #28
Source File: test_defer.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_cancelDeferredListWithException(self): """ Cancelling a L{defer.DeferredList} will cancel every L{defer.Deferred} in the list even exceptions raised from the C{cancel} method of the L{defer.Deferred}s. """ def cancellerRaisesException(deferred): """ A L{defer.Deferred} canceller that raises an exception. @param deferred: The cancelled L{defer.Deferred}. """ raise RuntimeError("test") deferredOne = defer.Deferred(cancellerRaisesException) deferredTwo = defer.Deferred() deferredList = defer.DeferredList([deferredOne, deferredTwo]) deferredList.cancel() self.failureResultOf(deferredTwo, defer.CancelledError) errors = self.flushLoggedErrors(RuntimeError) self.assertEqual(len(errors), 1)
Example #29
Source File: test_brokerclient.py From afkak with Apache License 2.0 | 5 votes |
def test_close_while_cancelled_in_flight(self): """ close() may be called when a cancelled request is in flight. The request is cancelled as usual and the close completes successfully. """ req_d = self.brokerClient.makeRequest(2, METADATA_REQUEST_2) conn = self.connections.accept('*') conn.pump.flush() req_d.cancel() close_d = self.brokerClient.close() conn.pump.flush() # Propagate connection loss. self.failureResultOf(req_d, defer.CancelledError) self.assertIs(None, self.successResultOf(close_d))
Example #30
Source File: test_newclient.py From learn_python3_spider with MIT License | 5 votes |
def test_cancelDuringResponse(self): """ The L{Deferred} returned by L{HTTP11ClientProtocol.request} will fire with a L{ResponseFailed} failure containing a L{CancelledError} exception if the request was cancelled before all response headers were received. """ transport = StringTransport() protocol = HTTP11ClientProtocol() protocol.makeConnection(transport) result = protocol.request(Request(b'GET', b'/', _boringHeaders, None)) protocol.dataReceived(b"HTTP/1.1 200 OK\r\n") result.cancel() self.assertTrue(transport.disconnected) return assertResponseFailed(self, result, [CancelledError])