Ticket #2675: 2675.patch

File 2675.patch, 8.7 KB (added by djfroofy, 5 years ago)
  • twisted/scripts/trial.py

    === modified file 'twisted/scripts/trial.py'
     
    1313from twisted import plugin
    1414from twisted.python.util import spewer
    1515from twisted.python.compat import set
    16 from twisted.trial import runner, itrial, reporter
     16from twisted.trial import runner, itrial, reporter, util
    1717
    1818
    1919# Yea, this is stupid.  Leave it for for command-line compatibility for a
     
    124124         'Path to use as working directory for tests.'],
    125125        ['reporter', None, 'verbose',
    126126         'The reporter to use for this test run.  See --help-reporters for '
    127          'more info.']]
     127         'more info.'],
     128        ['timeout', 't', util.DEFAULT_TIMEOUT_DURATION,
     129         'The default timeout in seconds. You can use the value 0 for no '
     130         'timeout but this may cause your tests to hang.', float]]
    128131
    129132    zsh_actions = {"tbformat":"(plain emacs cgitb)",
    130133                   "reporter":_zshReporterAction}
     
    302305        print 'Running tests shuffled with seed %d\n' % config['random']
    303306    if not config['until-failure']:
    304307        loader.suiteFactory = runner.DestructiveTestSuite
     308    if config['timeout']:
     309        loader.suiteFactory.defaultTimeout = config['timeout']
    305310    return loader
    306311
    307312
  • twisted/trial/runner.py

    === modified file 'twisted/trial/runner.py'
     
    135135            if result.shouldStop:
    136136                break
    137137            test = self._tests.pop(0)
     138            self.suggestTimeout(test)
    138139            test(result)
    139140        return result
    140141
  • twisted/trial/test/test_deferred.py

    === modified file 'twisted/trial/test/test_deferred.py'
     
    8585        self.failUnless(result.errors[0][1].check(defer.TimeoutError))
    8686
    8787
     88    test_setUp.skip = ("I don't think this test is relevant as an "
     89                       "outcome of #2675. No?")
     90
    8891class TestTester(unittest.TestCase):
    8992    def getTest(self, name):
    9093        raise NotImplementedError("must override me")
  • twisted/trial/test/test_runner.py

    === modified file 'twisted/trial/test/test_runner.py'
     
    487487        self.assertEqual(['runcall'], debugger._calls)
    488488
    489489
     490    def test_defaultTimeout(self):
     491        self.parseOptions(['--timeout', '25', 'twisted.trial.test.sample'])
     492        my_runner = self.getRunner()
     493        suite = trial._getSuite(self.config)
     494        tests = []
     495        # gather references to any unittest.TestCase in the suite
     496        for destrsuite in suite._tests[0]._tests:
     497            tests.extend([ t for t in destrsuite._tests
     498                           if isinstance(t, unittest.TestCase) ])
     499        first_test = tests[0]
     500        first_test.timeout = 1
     501        second_test = tests[1]
     502        second_test.timeout = 'orange'
     503        tests = tests[2:]
     504        result = my_runner.run(suite)
     505        self.assertEquals(first_test.timeout, 1)
     506        self.assertEquals(second_test.timeout, 'orange')
     507        for test in tests:
     508            self.assertEquals(test.timeout, 25)
     509        self.flushWarnings()
    490510
    491511class RemoveSafelyTests(unittest.TestCase):
    492512    """
  • twisted/trial/test/test_script.py

    === modified file 'twisted/trial/test/test_script.py'
     
    414414        options = trial.Options()
    415415        options.parseOptions(["--coverage"])
    416416        self.assertEquals(sys.gettrace(), options.tracer.globaltrace)
     417
     418
     419class DefaultTimeoutTests(unittest.TestCase):
     420    """
     421    Test for C{--timeout} argument.
     422    """
     423
     424    def test_defaultTimeoutSetOnSuiteFactory(self):
     425        """
     426        Test C{--timeout} argument results in defaultTimeout being set
     427        on the loaded C{suiteFactory}.
     428        """
     429        options = trial.Options()
     430        options.parseOptions(["--timeout", "20"])
     431        loader = trial._getLoader(options)
     432        self.assertEquals(loader.suiteFactory.defaultTimeout, 20)
     433
  • twisted/trial/test/test_warning.py

    === modified file 'twisted/trial/test/test_warning.py'
     
    176176        originalWarnings = warnings.filters[:]
    177177        try:
    178178            warnings.simplefilter('error')
     179            case.suggestTimeout(0.1)
    179180            case.run(result)
    180181            self.assertEqual(len(result.errors), 1)
    181182            self.assertIdentical(result.errors[0][0], case)
     
    200201        originalWarnings = warnings.filters[:]
    201202        try:
    202203            warnings.simplefilter('error')
     204            case.suggestTimeout(0.1)
    203205            case.run(result)
    204206            self.assertEqual(result.errors, [])
    205207        finally:
  • twisted/trial/unittest.py

    === modified file 'twisted/trial/unittest.py'
     
    726726                    result.addExpectedFailure(self, f, todo)
    727727                else:
    728728                    result.addError(self, f)
    729         onTimeout = utils.suppressWarnings(
    730             onTimeout, util.suppress(category=DeprecationWarning))
    731729        method = getattr(self, methodName)
    732730        d = defer.maybeDeferred(utils.runWithWarningsSuppressed,
    733731                                self.getSuppress(), method)
    734         call = reactor.callLater(timeout, onTimeout, d)
    735         d.addBoth(lambda x : call.active() and call.cancel() or x)
     732        # If a timeout is not explicitly given we let the test hang
     733        if timeout is not None:
     734            onTimeout = utils.suppressWarnings(
     735                onTimeout, util.suppress(category=DeprecationWarning))
     736            call = reactor.callLater(timeout, onTimeout, d)
     737            d.addBoth(lambda x : call.active() and call.cancel() or x)
    736738        return d
    737739
    738740    def shortDescription(self):
     
    11501152        """
    11511153        Returns the timeout value set on this test. Checks on the instance
    11521154        first, then the class, then the module, then packages. As soon as it
    1153         finds something with a C{timeout} attribute, returns that. Returns
    1154         L{util.DEFAULT_TIMEOUT_DURATION} if it cannot find anything. See
    1155         L{TestCase} docstring for more details.
     1155        finds something with a C{timeout} attribute, returns that. If it
     1156        cannot find any value for timeout, then C{None} is returned which may
     1157        result in a test hang. A default timeout may be set on via the
     1158        L{suggestTimeout} method. See L{TestCase} docstring for more details.
    11561159        """
    1157         timeout =  util.acquireAttribute(self._parents, 'timeout',
    1158                                          util.DEFAULT_TIMEOUT_DURATION)
     1160        timeout =  util.acquireAttribute(self._parents, 'timeout', None)
    11591161        try:
    11601162            return float(timeout)
    11611163        except (ValueError, TypeError):
     
    11651167            # both do this.
    11661168            warnings.warn("'timeout' attribute needs to be a number.",
    11671169                          category=DeprecationWarning)
    1168             return util.DEFAULT_TIMEOUT_DURATION
     1170
     1171    def suggestTimeout(self, timeout):
     1172        """
     1173        Suggest the timeout for this L{TestCase}. If C{timeout} is C{None}
     1174        this method is a noop. Also, if the current C{TestCase} already
     1175        supplies a non-C{None} timeout value, this method is a noop. Otherwise
     1176        the given C{timeout} will be set on this instance.
     1177        """
     1178        if timeout is None:
     1179            return
     1180        test_timeout = util.acquireAttribute(self._parents, 'timeout', None)
     1181        if test_timeout is None:
     1182            self.timeout = timeout
    11691183
    11701184    def getSuppress(self):
    11711185        """
     
    13691383class TestSuite(pyunit.TestSuite):
    13701384    """
    13711385    Extend the standard library's C{TestSuite} with support for the visitor
    1372     pattern and a consistently overrideable C{run} method.
     1386    pattern and a consistently overrideable C{run} method. This also supplies
     1387    C{defaultTimeout} which can be applied to instances of
     1388    L{twisted.trial.unittest.TestCase}.
    13731389    """
    13741390
    13751391    visit = suiteVisit
     1392    defaultTimeout = None
    13761393
    13771394    def __call__(self, result):
    13781395        return self.run(result)
     
    13871404        for test in self._tests:
    13881405            if result.shouldStop:
    13891406                break
     1407            self.suggestTimeout(test)
    13901408            test(result)
    13911409        return result
    13921410
     1411    def suggestTimeout(self, test):
     1412        if isinstance(test, TestCase):
     1413            test.suggestTimeout(self.defaultTimeout)
     1414
    13931415
    13941416
    13951417class TestDecorator(components.proxyForInterface(itrial.ITestCase,