Ticket #2675: timeouts-rev2-2675.patch
File timeouts-rev2-2675.patch, 9.2 KB (added by , 7 years ago) |
---|
-
twisted/topfiles/2675.bugfix
1 Allow setting default timeout for trial TestCases via -t/--timeout argument to trial. -
twisted/scripts/trial.py
13 13 from twisted import plugin 14 14 from twisted.python.util import spewer 15 15 from twisted.python.compat import set 16 from twisted.trial import runner, itrial, reporter 16 from twisted.trial import runner, itrial, reporter, util 17 17 18 18 19 19 # Yea, this is stupid. Leave it for for command-line compatibility for a … … 128 128 'Path to use as working directory for tests.'], 129 129 ['reporter', None, 'verbose', 130 130 '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]] 132 135 133 136 zsh_actions = {"tbformat":"(plain emacs cgitb)", 134 137 "reporter":_zshReporterAction} … … 338 341 print 'Running tests shuffled with seed %d\n' % config['random'] 339 342 if not config['until-failure']: 340 343 loader.suiteFactory = runner.DestructiveTestSuite 344 if config['timeout']: 345 loader.suiteFactory.defaultTimeout = config['timeout'] 341 346 return loader 342 347 343 348 -
twisted/trial/test/test_script.py
480 480 self.assertDeprecationWarning(self.config.opt_extra, 481 481 self.flushWarnings([self.test_xDeprecation])) 482 482 483 484 class 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_runner.py
490 490 self.assertEqual(['runcall'], debugger._calls) 491 491 492 492 493 def test_defaultTimeout(self): 494 """ 495 The integral value passed by --timeout argument to trial is used 496 to set the default timeout value on loaded tests that do not 497 explicitly set a timeout otherwise. 498 """ 499 self.parseOptions(['--timeout', '25', 'twisted.trial.test.sample']) 500 myRunner = self.getRunner() 501 suite = trial._getSuite(self.config) 502 # Gather up non-stdlib unittests (i.e. trial.unittest.TestCases 503 # instances only) 504 tests = [test for test in unittest._iterateTests(suite) 505 if isinstance(test, unittest.TestCase)] 506 firstTest = tests[0] 507 firstTest.timeout = 1 508 secondTest = tests[1] 509 secondTest.timeout = 2 510 # Remaining tests in suit that do not explicity set a timeout 511 tests = tests[2:] 512 result = myRunner.run(suite) 513 self.assertEquals(firstTest.timeout, 1) 514 self.assertEquals(secondTest.timeout, 2) 515 for test in tests: 516 self.assertEquals(test.timeout, 25) 517 self.flushWarnings() 493 518 494 519 class RemoveSafelyTests(unittest.TestCase): 495 520 """ -
twisted/trial/test/test_deferred.py
60 60 61 61 62 62 class TestNeverFire(unittest.TestCase): 63 def setUp(self):64 self._oldTimeout = util.DEFAULT_TIMEOUT_DURATION65 util.DEFAULT_TIMEOUT_DURATION = 0.166 63 67 def tearDown(self):68 util.DEFAULT_TIMEOUT_DURATION = self._oldTimeout69 70 64 def _loadSuite(self, klass): 71 65 loader = runner.TestLoader() 72 66 r = reporter.TestResult() 73 67 s = loader.loadClass(klass) 68 s.defaultTimeout = 0.1 74 69 return r, s 75 70 76 71 def test_setUp(self): … … 85 80 self.failUnless(result.errors[0][1].check(defer.TimeoutError)) 86 81 87 82 83 88 84 class TestTester(unittest.TestCase): 89 85 def getTest(self, name): 90 86 raise NotImplementedError("must override me") -
twisted/trial/runner.py
149 149 if result.shouldStop: 150 150 break 151 151 test = self._tests.pop(0) 152 if self.defaultTimeout: 153 if isinstance(test, unittest.TestCase): 154 self._suggestTimeout(test, self.defaultTimeout) 152 155 test(result) 153 156 return result 154 157 -
twisted/trial/unittest.py
710 710 result.addExpectedFailure(self, f, todo) 711 711 else: 712 712 result.addError(self, f) 713 onTimeout = utils.suppressWarnings(714 onTimeout, util.suppress(category=DeprecationWarning))715 713 method = getattr(self, methodName) 716 714 d = defer.maybeDeferred(utils.runWithWarningsSuppressed, 717 715 self.getSuppress(), method) 718 call = reactor.callLater(timeout, onTimeout, d) 719 d.addBoth(lambda x : call.active() and call.cancel() or x) 716 # If a timeout is not explicitly given we let the test hang 717 if timeout is not None: 718 onTimeout = utils.suppressWarnings( 719 onTimeout, util.suppress(category=DeprecationWarning)) 720 call = reactor.callLater(timeout, onTimeout, d) 721 d.addBoth(lambda x : call.active() and call.cancel() or x) 720 722 return d 721 723 722 724 def shortDescription(self): … … 1165 1167 """ 1166 1168 Returns the timeout value set on this test. Checks on the instance 1167 1169 first, then the class, then the module, then packages. As soon as it 1168 finds something with a C{timeout} attribute, returns that. Returns1169 L{util.DEFAULT_TIMEOUT_DURATION} if it cannot find anything. See1170 L{TestCase} docstring for more details.1170 finds something with a C{timeout} attribute, returns that. If it 1171 cannot find any value for timeout, then C{None} is returned which may 1172 result in a test hang. 1171 1173 """ 1172 timeout = util.acquireAttribute(self._parents, 'timeout', 1173 util.DEFAULT_TIMEOUT_DURATION) 1174 timeout = util.acquireAttribute(self._parents, 'timeout', None) 1175 if timeout is None: 1176 return None 1174 1177 try: 1175 1178 return float(timeout) 1176 1179 except (ValueError, TypeError): … … 1180 1183 # both do this. 1181 1184 warnings.warn("'timeout' attribute needs to be a number.", 1182 1185 category=DeprecationWarning) 1183 return util.DEFAULT_TIMEOUT_DURATION1184 1186 1185 1187 def getSuppress(self): 1186 1188 """ … … 1384 1386 class TestSuite(pyunit.TestSuite): 1385 1387 """ 1386 1388 Extend the standard library's C{TestSuite} with support for the visitor 1387 pattern and a consistently overrideable C{run} method. 1389 pattern and a consistently overrideable C{run} method. This also supplies 1390 C{defaultTimeout} which can be applied to instances of 1391 L{twisted.trial.unittest.TestCase}. 1388 1392 """ 1389 1393 1390 1394 visit = suiteVisit 1395 defaultTimeout = None 1391 1396 1392 1397 def __call__(self, result): 1393 1398 return self.run(result) … … 1402 1407 for test in self._tests: 1403 1408 if result.shouldStop: 1404 1409 break 1410 if self.defaultTimeout: 1411 if isinstance(test, TestCase): 1412 self._suggestTimeout(test, self.defaultTimeout) 1405 1413 test(result) 1406 1414 return result 1407 1415 1408 1416 1417 def _suggestTimeout(self, test, timeout): 1418 """ 1419 Suggest the timeout for the L{TestCase}. If C{timeout} is C{None} 1420 this method is a noop. Also, if the current C{TestCase} already 1421 supplies a non-C{None} timeout value, this method is a noop. Otherwise 1422 the given C{timeout} will be set on this test instance. 1423 """ 1424 if timeout is None: 1425 return 1426 testTimeout = util.acquireAttribute(test._parents, 'timeout', None) 1427 if testTimeout is None: 1428 test.timeout = timeout 1409 1429 1430 1410 1431 class TestDecorator(components.proxyForInterface(itrial.ITestCase, 1411 1432 "_originalTest")): 1412 1433 """