/External.LCA_RESTRICTED/Languages/CPython/27/Lib/unittest/test/test_break.py

http://github.com/IronLanguages/main · Python · 252 lines · 183 code · 60 blank · 9 comment · 18 complexity · adf42246168fff68386126e175fdd43f MD5 · raw file

  1. import gc
  2. import os
  3. import sys
  4. import signal
  5. import weakref
  6. from cStringIO import StringIO
  7. import unittest
  8. @unittest.skipUnless(hasattr(os, 'kill'), "Test requires os.kill")
  9. @unittest.skipIf(sys.platform =="win32", "Test cannot run on Windows")
  10. @unittest.skipIf(sys.platform == 'freebsd6', "Test kills regrtest on freebsd6 "
  11. "if threads have been used")
  12. class TestBreak(unittest.TestCase):
  13. def setUp(self):
  14. self._default_handler = signal.getsignal(signal.SIGINT)
  15. def tearDown(self):
  16. signal.signal(signal.SIGINT, self._default_handler)
  17. unittest.signals._results = weakref.WeakKeyDictionary()
  18. unittest.signals._interrupt_handler = None
  19. def testInstallHandler(self):
  20. default_handler = signal.getsignal(signal.SIGINT)
  21. unittest.installHandler()
  22. self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler)
  23. try:
  24. pid = os.getpid()
  25. os.kill(pid, signal.SIGINT)
  26. except KeyboardInterrupt:
  27. self.fail("KeyboardInterrupt not handled")
  28. self.assertTrue(unittest.signals._interrupt_handler.called)
  29. def testRegisterResult(self):
  30. result = unittest.TestResult()
  31. unittest.registerResult(result)
  32. for ref in unittest.signals._results:
  33. if ref is result:
  34. break
  35. elif ref is not result:
  36. self.fail("odd object in result set")
  37. else:
  38. self.fail("result not found")
  39. def testInterruptCaught(self):
  40. default_handler = signal.getsignal(signal.SIGINT)
  41. result = unittest.TestResult()
  42. unittest.installHandler()
  43. unittest.registerResult(result)
  44. self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler)
  45. def test(result):
  46. pid = os.getpid()
  47. os.kill(pid, signal.SIGINT)
  48. result.breakCaught = True
  49. self.assertTrue(result.shouldStop)
  50. try:
  51. test(result)
  52. except KeyboardInterrupt:
  53. self.fail("KeyboardInterrupt not handled")
  54. self.assertTrue(result.breakCaught)
  55. def testSecondInterrupt(self):
  56. result = unittest.TestResult()
  57. unittest.installHandler()
  58. unittest.registerResult(result)
  59. def test(result):
  60. pid = os.getpid()
  61. os.kill(pid, signal.SIGINT)
  62. result.breakCaught = True
  63. self.assertTrue(result.shouldStop)
  64. os.kill(pid, signal.SIGINT)
  65. self.fail("Second KeyboardInterrupt not raised")
  66. try:
  67. test(result)
  68. except KeyboardInterrupt:
  69. pass
  70. else:
  71. self.fail("Second KeyboardInterrupt not raised")
  72. self.assertTrue(result.breakCaught)
  73. def testTwoResults(self):
  74. unittest.installHandler()
  75. result = unittest.TestResult()
  76. unittest.registerResult(result)
  77. new_handler = signal.getsignal(signal.SIGINT)
  78. result2 = unittest.TestResult()
  79. unittest.registerResult(result2)
  80. self.assertEqual(signal.getsignal(signal.SIGINT), new_handler)
  81. result3 = unittest.TestResult()
  82. def test(result):
  83. pid = os.getpid()
  84. os.kill(pid, signal.SIGINT)
  85. try:
  86. test(result)
  87. except KeyboardInterrupt:
  88. self.fail("KeyboardInterrupt not handled")
  89. self.assertTrue(result.shouldStop)
  90. self.assertTrue(result2.shouldStop)
  91. self.assertFalse(result3.shouldStop)
  92. def testHandlerReplacedButCalled(self):
  93. # If our handler has been replaced (is no longer installed) but is
  94. # called by the *new* handler, then it isn't safe to delay the
  95. # SIGINT and we should immediately delegate to the default handler
  96. unittest.installHandler()
  97. handler = signal.getsignal(signal.SIGINT)
  98. def new_handler(frame, signum):
  99. handler(frame, signum)
  100. signal.signal(signal.SIGINT, new_handler)
  101. try:
  102. pid = os.getpid()
  103. os.kill(pid, signal.SIGINT)
  104. except KeyboardInterrupt:
  105. pass
  106. else:
  107. self.fail("replaced but delegated handler doesn't raise interrupt")
  108. def testRunner(self):
  109. # Creating a TextTestRunner with the appropriate argument should
  110. # register the TextTestResult it creates
  111. runner = unittest.TextTestRunner(stream=StringIO())
  112. result = runner.run(unittest.TestSuite())
  113. self.assertIn(result, unittest.signals._results)
  114. def testWeakReferences(self):
  115. # Calling registerResult on a result should not keep it alive
  116. result = unittest.TestResult()
  117. unittest.registerResult(result)
  118. ref = weakref.ref(result)
  119. del result
  120. # For non-reference counting implementations
  121. gc.collect();gc.collect()
  122. self.assertIsNone(ref())
  123. def testRemoveResult(self):
  124. result = unittest.TestResult()
  125. unittest.registerResult(result)
  126. unittest.installHandler()
  127. self.assertTrue(unittest.removeResult(result))
  128. # Should this raise an error instead?
  129. self.assertFalse(unittest.removeResult(unittest.TestResult()))
  130. try:
  131. pid = os.getpid()
  132. os.kill(pid, signal.SIGINT)
  133. except KeyboardInterrupt:
  134. pass
  135. self.assertFalse(result.shouldStop)
  136. def testMainInstallsHandler(self):
  137. failfast = object()
  138. test = object()
  139. verbosity = object()
  140. result = object()
  141. default_handler = signal.getsignal(signal.SIGINT)
  142. class FakeRunner(object):
  143. initArgs = []
  144. runArgs = []
  145. def __init__(self, *args, **kwargs):
  146. self.initArgs.append((args, kwargs))
  147. def run(self, test):
  148. self.runArgs.append(test)
  149. return result
  150. class Program(unittest.TestProgram):
  151. def __init__(self, catchbreak):
  152. self.exit = False
  153. self.verbosity = verbosity
  154. self.failfast = failfast
  155. self.catchbreak = catchbreak
  156. self.testRunner = FakeRunner
  157. self.test = test
  158. self.result = None
  159. p = Program(False)
  160. p.runTests()
  161. self.assertEqual(FakeRunner.initArgs, [((), {'buffer': None,
  162. 'verbosity': verbosity,
  163. 'failfast': failfast})])
  164. self.assertEqual(FakeRunner.runArgs, [test])
  165. self.assertEqual(p.result, result)
  166. self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)
  167. FakeRunner.initArgs = []
  168. FakeRunner.runArgs = []
  169. p = Program(True)
  170. p.runTests()
  171. self.assertEqual(FakeRunner.initArgs, [((), {'buffer': None,
  172. 'verbosity': verbosity,
  173. 'failfast': failfast})])
  174. self.assertEqual(FakeRunner.runArgs, [test])
  175. self.assertEqual(p.result, result)
  176. self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler)
  177. def testRemoveHandler(self):
  178. default_handler = signal.getsignal(signal.SIGINT)
  179. unittest.installHandler()
  180. unittest.removeHandler()
  181. self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)
  182. # check that calling removeHandler multiple times has no ill-effect
  183. unittest.removeHandler()
  184. self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)
  185. def testRemoveHandlerAsDecorator(self):
  186. default_handler = signal.getsignal(signal.SIGINT)
  187. unittest.installHandler()
  188. @unittest.removeHandler
  189. def test():
  190. self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)
  191. test()
  192. self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler)