Ticket #2675: 2675-r1.patch

File 2675-r1.patch, 9.5 KB (added by djfroofy, 5 years ago)
  • 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
     
    128128         'Path to use as working directory for tests.'],
    129129        ['reporter', None, 'verbose',
    130130         'The reporter to use for this test run.  See --help-reporters for '
    131          'more info.']]
     131         'more info.'],
     132        ['timeout', 't', util.DEFAULT_TIMEOUT_DURATION,
     133         'The default timeout in seconds. You can use the value 0 for no '
     134         'timeout but this may cause your tests to hang.', float]]
    132135
    133136    zsh_actions = {"tbformat":"(plain emacs cgitb)",
    134137                   "reporter":_zshReporterAction}
     
    338341        print 'Running tests shuffled with seed %d\n' % config['random']
    339342    if not config['until-failure']:
    340343        loader.suiteFactory = runner.DestructiveTestSuite
     344    if config['timeout']:
     345        loader.suiteFactory.defaultTimeout = config['timeout']
    341346    return loader
    342347
    343348
  • twisted/trial/test/test_script.py

     
    480480        self.assertDeprecationWarning(self.config.opt_extra,
    481481                                      self.flushWarnings([self.test_xDeprecation]))
    482482
     483
     484class DefaultTimeoutTests(unittest.TestCase):
     485    """
     486    Test for C{--timeout} argument.
     487    """
     488
     489    def test_defaultTimeoutSetOnSuiteFactory(self):
     490        """
     491        Test C{--timeout} argument results in defaultTimeout being set
     492        on the loaded C{suiteFactory}.
     493        """
     494        options = trial.Options()
     495        options.parseOptions(["--timeout", "20"])
     496        loader = trial._getLoader(options)
     497        self.assertEquals(loader.suiteFactory.defaultTimeout, 20)
     498
  • 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/test/test_runner.py

     
    490490        self.assertEqual(['runcall'], debugger._calls)
    491491
    492492
     493    def test_defaultTimeout(self):
     494        self.parseOptions(['--timeout', '25', 'twisted.trial.test.sample'])
     495        my_runner = self.getRunner()
     496        suite = trial._getSuite(self.config)
     497        tests = []
     498        # gather references to any unittest.TestCase in the suite
     499        for destrsuite in suite._tests[0]._tests:
     500            tests.extend([ t for t in destrsuite._tests
     501                           if isinstance(t, unittest.TestCase) ])
     502        first_test = tests[0]
     503        first_test.timeout = 1
     504        second_test = tests[1]
     505        second_test.timeout = 'orange'
     506        tests = tests[2:]
     507        result = my_runner.run(suite)
     508        self.assertEquals(first_test.timeout, 1)
     509        self.assertEquals(second_test.timeout, 'orange')
     510        for test in tests:
     511            self.assertEquals(test.timeout, 25)
     512        self.flushWarnings()
    493513
    494514class RemoveSafelyTests(unittest.TestCase):
    495515    """
  • twisted/trial/test/test_deferred.py

     
    6060
    6161
    6262class TestNeverFire(unittest.TestCase):
    63     def setUp(self):
    64         self._oldTimeout = util.DEFAULT_TIMEOUT_DURATION
    65         util.DEFAULT_TIMEOUT_DURATION = 0.1
    6663
    67     def tearDown(self):
    68         util.DEFAULT_TIMEOUT_DURATION = self._oldTimeout
    69 
    7064    def _loadSuite(self, klass):
    7165        loader = runner.TestLoader()
    7266        r = reporter.TestResult()
    7367        s = loader.loadClass(klass)
     68        s.defaultTimeout = 0.1
    7469        return r, s
    7570
    7671    def test_setUp(self):
     
    8580        self.failUnless(result.errors[0][1].check(defer.TimeoutError))
    8681
    8782
     83
    8884class TestTester(unittest.TestCase):
    8985    def getTest(self, name):
    9086        raise NotImplementedError("must override me")
  • twisted/trial/runner.py

     
    149149            if result.shouldStop:
    150150                break
    151151            test = self._tests.pop(0)
     152            self.suggestTimeout(test)
    152153            test(result)
    153154        return result
    154155
  • twisted/trial/unittest.py

     
    699699                    result.addExpectedFailure(self, f, todo)
    700700                else:
    701701                    result.addError(self, f)
    702         onTimeout = utils.suppressWarnings(
    703             onTimeout, util.suppress(category=DeprecationWarning))
    704702        method = getattr(self, methodName)
    705703        d = defer.maybeDeferred(utils.runWithWarningsSuppressed,
    706704                                self.getSuppress(), method)
    707         call = reactor.callLater(timeout, onTimeout, d)
    708         d.addBoth(lambda x : call.active() and call.cancel() or x)
     705        # If a timeout is not explicitly given we let the test hang
     706        if timeout is not None:
     707            onTimeout = utils.suppressWarnings(
     708                onTimeout, util.suppress(category=DeprecationWarning))
     709            call = reactor.callLater(timeout, onTimeout, d)
     710            d.addBoth(lambda x : call.active() and call.cancel() or x)
    709711        return d
    710712
    711713    def shortDescription(self):
     
    11541156        """
    11551157        Returns the timeout value set on this test. Checks on the instance
    11561158        first, then the class, then the module, then packages. As soon as it
    1157         finds something with a C{timeout} attribute, returns that. Returns
    1158         L{util.DEFAULT_TIMEOUT_DURATION} if it cannot find anything. See
    1159         L{TestCase} docstring for more details.
     1159        finds something with a C{timeout} attribute, returns that. If it
     1160        cannot find any value for timeout, then C{None} is returned which may
     1161        result in a test hang. A default timeout may be set on via the
     1162        L{suggestTimeout} method. See L{TestCase} docstring for more details.
    11601163        """
    1161         timeout =  util.acquireAttribute(self._parents, 'timeout',
    1162                                          util.DEFAULT_TIMEOUT_DURATION)
     1164        timeout =  util.acquireAttribute(self._parents, 'timeout', None)
     1165        if timeout is None:
     1166            return
    11631167        try:
    11641168            return float(timeout)
    11651169        except (ValueError, TypeError):
     
    11691173            # both do this.
    11701174            warnings.warn("'timeout' attribute needs to be a number.",
    11711175                          category=DeprecationWarning)
    1172             return util.DEFAULT_TIMEOUT_DURATION
    11731176
     1177    def suggestTimeout(self, timeout):
     1178        """
     1179        Suggest the timeout for this L{TestCase}. If C{timeout} is C{None}
     1180        this method is a noop. Also, if the current C{TestCase} already
     1181        supplies a non-C{None} timeout value, this method is a noop. Otherwise
     1182        the given C{timeout} will be set on this instance.
     1183        """
     1184        if timeout is None:
     1185            return
     1186        test_timeout = util.acquireAttribute(self._parents, 'timeout', None)
     1187        if test_timeout is None:
     1188            self.timeout = timeout
     1189
    11741190    def getSuppress(self):
    11751191        """
    11761192        Returns any warning suppressions set for this test. Checks on the
     
    13731389class TestSuite(pyunit.TestSuite):
    13741390    """
    13751391    Extend the standard library's C{TestSuite} with support for the visitor
    1376     pattern and a consistently overrideable C{run} method.
     1392    pattern and a consistently overrideable C{run} method. This also supplies
     1393    C{defaultTimeout} which can be applied to instances of
     1394    L{twisted.trial.unittest.TestCase}.
    13771395    """
    13781396
    13791397    visit = suiteVisit
     1398    defaultTimeout = None
    13801399
    13811400    def __call__(self, result):
    13821401        return self.run(result)
     
    13911410        for test in self._tests:
    13921411            if result.shouldStop:
    13931412                break
     1413            if self.defaultTimeout:
     1414                self.suggestTimeout(test)
    13941415            test(result)
    13951416        return result
    13961417
     1418    def suggestTimeout(self, test):
     1419        if isinstance(test, TestCase):
     1420            test.suggestTimeout(self.defaultTimeout)
    13971421
    13981422
     1423
    13991424class TestDecorator(components.proxyForInterface(itrial.ITestCase,
    14001425                                                 "_originalTest")):
    14011426    """