def _cleanUp(self, result): try: if self.forceGarbageCollection: gc.collect() util._Janitor().postCaseCleanup() except util.FailureError, e: result.addError(self, e.original) self._passed = False
def test_cleanReactorKillsProcesses(self): """ The Janitor will kill processes during reactor cleanup. """ @implementer(IProcessTransport) class StubProcessTransport(object): """ A stub L{IProcessTransport} provider which records signals. @ivar signals: The signals passed to L{signalProcess}. """ def __init__(self): self.signals = [] def signalProcess(self, signal): """ Append C{signal} to C{self.signals}. """ self.signals.append(signal) pt = StubProcessTransport() reactor = StubReactor([], [pt]) jan = _Janitor(None, None, reactor=reactor) jan._cleanReactor() self.assertEqual(pt.signals, ["KILL"])
def test_cleanReactorKillsProcesses(self): """ The Janitor will kill processes during reactor cleanup. """ class StubProcessTransport(object): """ A stub L{IProcessTransport} provider which records signals. @ivar signals: The signals passed to L{signalProcess}. """ implements(IProcessTransport) def __init__(self): self.signals = [] def signalProcess(self, signal): """ Append C{signal} to C{self.signals}. """ self.signals.append(signal) pt = StubProcessTransport() reactor = StubReactor([], [pt]) jan = _Janitor(None, None, reactor=reactor) jan._cleanReactor() self.assertEqual(pt.signals, ["KILL"])
def test_cleanReactorRemovesSelectables(self): """ The Janitor will remove selectables during reactor cleanup. """ reactor = StubReactor([]) jan = _Janitor(None, None, reactor=reactor) jan._cleanReactor() self.assertEqual(reactor.removeAllCalled, 1)
def testBenchmark(self): from twisted.trial.test.common import BogusReporter from twisted import trial suite = runner.TestSuite(BogusReporter(), util._Janitor(), benchmark=True) suite.addTestClass(self.Benchmark) suite.run() stats = pickle.load(file('test.stats', 'rb')) failUnlessEqual(stats, {reflect.qual(self.Benchmark.benchmarkValues): statdatum})
def test_postCaseCleanupNoErrors(self): """ The post-case cleanup method will return True and not call C{addError} on the result if there are no pending calls. """ reactor = StubReactor([]) test = object() reporter = StubErrorReporter() jan = _Janitor(test, reporter, reactor=reactor) self.assertTrue(jan.postCaseCleanup()) self.assertEqual(reporter.errors, [])
def test_cleanPendingSpinsReactor(self): """ During pending-call cleanup, the reactor will be spun twice with an instant timeout. This is not a requirement, it is only a test for current behavior. Hopefully Trial will eventually not do this kind of reactor stuff. """ reactor = StubReactor([]) jan = _Janitor(None, None, reactor=reactor) jan._cleanPending() self.assertEqual(reactor.iterations, [0, 0])
def test_postClassCleanupNoErrors(self): """ The post-class cleanup method will not call C{addError} on the result if there are no pending calls or selectables. """ reactor = StubReactor([]) test = object() reporter = StubErrorReporter() jan = _Janitor(test, reporter, reactor=reactor) jan.postClassCleanup() self.assertEqual(reporter.errors, [])
def test_cleanPendingCancelsCalls(self): """ During pending-call cleanup, the janitor cancels pending timed calls. """ def func(): return "Lulz" cancelled = [] delayedCall = DelayedCall(300, func, (), {}, cancelled.append, lambda x: None) reactor = StubReactor([delayedCall]) jan = _Janitor(None, None, reactor=reactor) jan._cleanPending() self.assertEquals(cancelled, [delayedCall])
def test_postClassCleanupWithSelectableErrors(self): """ The post-class cleanup method call C{addError} on the result with a L{DirtyReactorAggregateError} Failure if there are selectables. """ selectable = "SELECTABLE HERE" reactor = StubReactor([], [selectable]) test = object() reporter = StubErrorReporter() jan = _Janitor(test, reporter, reactor=reactor) jan.postClassCleanup() self.assertEqual(len(reporter.errors), 1) self.assertEqual(reporter.errors[0][1].value.selectables, [repr(selectable)])
def test_cleanPendingCancelsCalls(self): """ During pending-call cleanup, the janitor cancels pending timed calls. """ def func(): return "Lulz" cancelled = [] delayedCall = DelayedCall(300, func, (), {}, cancelled.append, lambda x: None) reactor = StubReactor([delayedCall]) jan = _Janitor(None, None, reactor=reactor) jan._cleanPending() self.assertEqual(cancelled, [delayedCall])
def test_cleanPendingReturnsDelayedCallStrings(self): """ The Janitor produces string representations of delayed calls from the delayed call cleanup method. It gets the string representations *before* cancelling the calls; this is important because cancelling the call removes critical debugging information from the string representation. """ delayedCall = DelayedCall(300, lambda: None, (), {}, lambda x: None, lambda x: None, seconds=lambda: 0) delayedCallString = str(delayedCall) reactor = StubReactor([delayedCall]) jan = _Janitor(None, None, reactor=reactor) strings = jan._cleanPending() self.assertEqual(strings, [delayedCallString])
def test_postClassCleanupWithPendingCallErrors(self): """ The post-class cleanup method call C{addError} on the result with a L{DirtyReactorAggregateError} Failure if there are pending calls. """ delayedCall = DelayedCall(300, lambda: None, (), {}, lambda x: None, lambda x: None, seconds=lambda: 0) delayedCallString = str(delayedCall) reactor = StubReactor([delayedCall], []) test = object() reporter = StubErrorReporter() jan = _Janitor(test, reporter, reactor=reactor) jan.postClassCleanup() self.assertEqual(len(reporter.errors), 1) self.assertEqual(reporter.errors[0][1].value.delayedCalls, [delayedCallString])
def testMethods(self): from twisted.trial.test.common import BogusReporter for klass in (self.Tests, self.TestSkipClassAttr, self.TestTodoClassAttr): suite = runner.TestSuite(BogusReporter(), util._Janitor()) suite.addTestClass(klass) suite.run() for method in suite.methods: try: self.checkResults(method) except unittest.FailTest: raise
def _cleanUp(self, result): try: clean = util._Janitor(self, result).postCaseCleanup() if not clean: self._passed = False except BaseException: result.addError(self, failure.Failure()) self._passed = False for error in self._observer.getErrors(): result.addError(self, error) self._passed = False self.flushLoggedErrors() self._removeObserver() if self._passed: result.addSuccess(self)
def _cleanUp(self, result): try: clean = util._Janitor(self, result).postCaseCleanup() if not clean: self._passed = False except: result.addError(self, failure.Failure()) self._passed = False for error in self._observer.getErrors(): result.addError(self, error) self._passed = False self.flushLoggedErrors() self._removeObserver() if self._passed: result.addSuccess(self)
def test_cleanReactorReturnsSelectableStrings(self): """ The Janitor returns string representations of the selectables that it cleaned up from the reactor cleanup method. """ class Selectable(object): """ A stub Selectable which only has an interesting string representation. """ def __repr__(self): return "(SELECTABLE!)" reactor = StubReactor([], [Selectable()]) jan = _Janitor(None, None, reactor=reactor) self.assertEqual(jan._cleanReactor(), ["(SELECTABLE!)"])
def test_cleanPendingReturnsDelayedCallStrings(self): """ The Janitor produces string representations of delayed calls from the delayed call cleanup method. It gets the string representations *before* cancelling the calls; this is important because cancelling the call removes critical debugging information from the string representation. """ delayedCall = DelayedCall( 300, lambda: None, (), {}, lambda x: None, lambda x: None, seconds=lambda: 0 ) delayedCallString = str(delayedCall) reactor = StubReactor([delayedCall]) jan = _Janitor(None, None, reactor=reactor) strings = jan._cleanPending() self.assertEqual(strings, [delayedCallString])
def test_postClassCleanupWithPendingCallErrors(self): """ The post-class cleanup method call C{addError} on the result with a L{DirtyReactorAggregateError} Failure if there are pending calls. """ delayedCall = DelayedCall( 300, lambda: None, (), {}, lambda x: None, lambda x: None, seconds=lambda: 0 ) delayedCallString = str(delayedCall) reactor = StubReactor([delayedCall], []) test = object() reporter = StubErrorReporter() jan = _Janitor(test, reporter, reactor=reactor) jan.postClassCleanup() self.assertEqual(len(reporter.errors), 1) self.assertEqual(reporter.errors[0][1].value.delayedCalls, [delayedCallString])
def setUp(self): self.janitor = util._Janitor()
def setUpClass(self): self.janitor = util._Janitor()
def _cleanUp(self, result): try: util._Janitor().postCaseCleanup() except util.FailureError, e: result.addError(self, e.original) self._passed = False
def _classCleanUp(self, result): try: util._Janitor(self, result).postClassCleanup() except: result.addError(self, failure.Failure())
def _classCleanUp(self, result): try: util._Janitor(self, result).postClassCleanup() except BaseException: result.addError(self, failure.Failure())
def _getJanitor(config=None): j = util._Janitor() return j
def _classCleanUp(self, result): try: util._Janitor().postClassCleanup() except util.FailureError, e: result.cleanupErrors(e.original)