Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 | 5 |
| 6 import logging | |
| 6 import sys | 7 import sys |
| 7 import threading | 8 import threading |
| 8 import traceback | 9 import traceback |
| 9 import unittest | 10 import unittest |
| 10 | 11 |
| 11 | 12 |
| 12 class ThreadWatcherMixIn(object): | 13 class ThreadWatcherMixIn(object): |
| 13 def setUp(self): | 14 def setUp(self): |
| 14 self._pre_test_threads = [t.ident for t in threading.enumerate()] | 15 self._pre_test_threads = [t.ident for t in threading.enumerate()] |
| 15 | 16 |
| 16 def tearDown(self): | 17 def tearDown(self): |
| 17 post_test_threads = threading.enumerate() | 18 post_test_threads = threading.enumerate() |
| 18 new_threads = [t for t in post_test_threads | 19 new_threads = [t for t in post_test_threads |
| 19 if t.ident not in self._pre_test_threads] | 20 if t.ident not in self._pre_test_threads] |
| 20 if new_threads: | 21 if new_threads: |
| 21 details = [] | 22 details = [] |
| 22 for th in new_threads: | 23 for th in new_threads: |
| 23 details.append('\nThread %s stacktrace:\n' % th) | 24 details.append('\nThread %s stacktrace:\n' % th) |
| 24 try: | 25 try: |
| 25 details.extend(traceback.format_stack(sys._current_frames()[t.ident])) | 26 details.extend(traceback.format_stack(sys._current_frames()[t.ident])) |
| 26 except Exception: | 27 except Exception: |
| 27 if t.ident in sys._current_frames(): | 28 if t.ident in sys._current_frames(): |
| 28 raise | 29 raise |
| 29 # This can happen due to threads starting or stopping concurrently. | 30 # This can happen due to threads starting or stopping concurrently. |
| 30 details.append(' Thread stopped while acquiring stacktrace.\n') | 31 details.append(' Thread stopped while acquiring stacktrace.\n') |
| 31 details = ''.join(details) | 32 details = ''.join(details) |
| 32 | 33 |
| 33 self.fail('Found %d running thread(s) after the test.\n%s' % ( | 34 msg = 'Found %d running thread(s) after the test.\n%s' % ( |
| 34 len(new_threads), details)) | 35 len(new_threads), details) |
| 36 if self._should_fail(): | |
| 37 self.fail(msg) # This raises exception. | |
| 38 # Test failed before this check, so don't raise another exception | |
|
Sergiy Byelozyorov
2016/07/07 07:05:01
IMHO this comment should be before line 36 and IMH
tandrii(chromium)
2016/07/07 11:47:05
Done.
| |
| 39 # overwriting the original one, and making it harder to debug. | |
| 40 # Besides, that exception might be the reason cleanup didn't happen. | |
| 41 logging.warn(msg) | |
| 42 logging.warn('Note: extra running threads might be due to your test ' | |
| 43 'failure, which prevented *your cleanup code* from ' | |
| 44 'executing.') | |
| 35 | 45 |
| 46 def _should_fail(self): | |
|
Sergiy Byelozyorov
2016/07/07 07:05:01
suggestion: rename to _test_likely_passed(self):
tandrii(chromium)
2016/07/07 11:47:05
Done.
| |
| 47 """Conservatively returns True if current test has likely not failed yet.""" | |
|
Sergiy Byelozyorov
2016/07/07 07:05:01
How about this?
"""Conservatively returns True if
tandrii(chromium)
2016/07/07 11:47:05
Done.
| |
| 48 # When run using unittest runner, | |
| 49 # self._resultForDoCleanups is set to internal unittest object. | |
| 50 if hasattr(self, '_resultForDoCleanups') and self._resultForDoCleanups: | |
| 51 return self._resultForDoCleanups.wasSuccessful() | |
| 52 # expect_tests runner does so differently. | |
| 53 if hasattr(self, '_test_failed_with_exception'): | |
| 54 return not self._test_failed_with_exception | |
| 55 # Default case - be conservative. | |
| 56 return True | |
| 36 | 57 |
| 37 | 58 |
| 38 class TestCase(unittest.TestCase, ThreadWatcherMixIn): | 59 class TestCase(unittest.TestCase, ThreadWatcherMixIn): |
| 39 """Base unittest class that fails on leaked threads after the test.""" | 60 """Base unittest class that fails on leaked threads after the test.""" |
| 40 def setUp(self): | 61 def setUp(self): |
| 41 super(TestCase, self).setUp() | 62 super(TestCase, self).setUp() |
| 42 ThreadWatcherMixIn.setUp(self) | 63 ThreadWatcherMixIn.setUp(self) |
| 43 | 64 |
| 44 def tearDown(self): | 65 def tearDown(self): |
| 45 ThreadWatcherMixIn.tearDown(self) | 66 ThreadWatcherMixIn.tearDown(self) |
| 46 super(TestCase, self).tearDown() | 67 super(TestCase, self).tearDown() |
| OLD | NEW |