class testcase(cls, TestCase): __module__ = cls.__module__ if reactor in cls.skippedReactors: skip = cls.skippedReactors[reactor] try: reactorFactory = namedAny(reactor) except: skip = Failure().getErrorMessage()
def _callProcessExited(self, reason): default = object() processExited = getattr(self.proto, 'processExited', default) if processExited is default: getWarningMethod()( _missingProcessExited % (qual(self.proto.__class__),), DeprecationWarning, stacklevel=0) else: processExited(Failure(reason))
def stop(self): """ Stop further processing of this task. @raise TaskFinished: if this L{CooperativeTask} has previously completed, via C{stop}, completion, or failure. """ self._checkFinish() self._completeWith(TaskStopped(), Failure(TaskStopped()))
def _addTask(self, task): """ Add a L{CooperativeTask} object to this L{Cooperator}. """ if self._stopped: self._tasks.append(task) # XXX silly, I know, but _completeWith # does the inverse task._completeWith(SchedulerStopped(), Failure(SchedulerStopped())) else: self._tasks.append(task) self._reschedule()
def maybeCallProcessEnded(self): """ Call processEnded on protocol after final cleanup. """ if self.proto is not None: reason = self._getReason(self.status) proto = self.proto self.proto = None try: proto.processEnded(Failure(reason)) except: err(None, "unexpected error in processEnded")
def test_emitErrorPriority(self): """ L{SyslogObserver.emit} uses C{LOG_ALERT} if the event represents an error. """ self.observer.emit({ 'message': ('hello, world', ), 'isError': True, 'system': '-', 'failure': Failure(Exception("foo")) }) self.assertEqual(self.events, [(stdsyslog.LOG_ALERT, '[-] hello, world')])
def stop(self): """ Stop scheduling steps. Errback the completion Deferreds of all iterators which have been added and forget about them. """ self._stopped = True for taskObj in self._tasks: taskObj._completeWith(SchedulerStopped(), Failure(SchedulerStopped())) self._tasks = [] if self._delayedCall is not None: self._delayedCall.cancel() self._delayedCall = None
def _cbIdent(self, ident, chatui): if not ident: print 'falsely identified.' return self._ebConnected( Failure(Exception("username or password incorrect"))) print 'Identified!' dl = [] for handlerClass, sname, pname in self.services: d = defer.Deferred() dl.append(d) handler = handlerClass(self, sname, pname, chatui, d) ident.callRemote('attach', sname, pname, handler).addCallback(handler.connected) return defer.DeferredList(dl)
def test_emitCustomPriorityOverridesError(self): """ L{SyslogObserver.emit} uses the value of the C{'syslogPriority'} key if it is specified even if the event dictionary represents an error. """ self.observer.emit({ 'message': ('hello, world', ), 'isError': True, 'system': '-', 'syslogPriority': stdsyslog.LOG_NOTICE, 'failure': Failure(Exception("bar")) }) self.assertEqual(self.events, [(stdsyslog.LOG_NOTICE, '[-] hello, world')])
def test_clientConnectionFailed(self): """ Calls to L{_WrappingFactory.clientConnectionLost} should errback the L{_WrappingFactory._onConnection} L{Deferred} """ wf = endpoints._WrappingFactory(TestFactory(), None) expectedFailure = Failure(error.ConnectError(string="fail")) wf.clientConnectionFailed(None, expectedFailure) errors = [] def gotError(f): errors.append(f) wf._onConnection.addErrback(gotError) self.assertEquals(errors, [expectedFailure])
def _oneWorkUnit(self): """ Perform one unit of work for this task, retrieving one item from its iterator, stopping if there are no further items in the iterator, and pausing if the result was a L{defer.Deferred}. """ try: result = self._iterator.next() except StopIteration: self._completeWith(TaskDone(), self._iterator) except: self._completeWith(TaskFailed(), Failure()) else: if isinstance(result, defer.Deferred): self.pause() def failLater(f): self._completeWith(TaskFailed(), f) result.addCallbacks(lambda result: self.resume(), failLater)
def _cancelConnectFailedTimeoutTest(self, connect): """ Like L{_cancelConnectTest}, but for the case where the L{Deferred} is cancelled after the connection attempt has failed but before it is fired with the resulting failure. """ reactor = MemoryReactorWithConnectorsAndTime() cc = ClientCreator(reactor, Protocol) d, factory = connect(reactor, cc) connector = reactor.connectors.pop() factory.clientConnectionFailed(connector, Failure(Exception("Simulated failure"))) # Sanity check - there is an outstanding delayed call to fire the # Deferred. self.assertEquals(len(reactor.getDelayedCalls()), 1) # Cancel the Deferred, cancelling the delayed call. d.cancel() self.assertEquals(reactor.getDelayedCalls(), []) return self.assertFailure(d, CancelledError)
def buildReactor(self): """ Create and return a reactor using C{self.reactorFactory}. """ try: reactor = self.reactorFactory() except: # Unfortunately, not all errors which result in a reactor # being unusable are detectable without actually # instantiating the reactor. So we catch some more here # and skip the test if necessary. We also log it to aid # with debugging, but flush the logged error so the test # doesn't fail. log.err(None, "Failed to install reactor") self.flushLoggedErrors() raise SkipTest(Failure().getErrorMessage()) else: if self.requiredInterface is not None: if not self.requiredInterface.providedBy(reactor): self.unbuildReactor(reactor) raise SkipTest("%r does not provide %r" % (reactor, self.requiredInterface)) self.addCleanup(self.unbuildReactor, reactor) return reactor
def checkConnLost(self): self.connsLost += 1 if self.connsLost >= 2: self.disconnecting = True self.disconnected = True self.proto.connectionLost(Failure(main.CONNECTION_DONE))