| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 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 import re | 5 import re |
| 6 import sys | 6 import sys |
| 7 import threading | 7 import threading |
| 8 import unittest | 8 import unittest |
| 9 | 9 |
| 10 from testing_support import thread_watcher | 10 from testing_support import thread_watcher |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 self._start_event.wait() | 21 self._start_event.wait() |
| 22 | 22 |
| 23 def run(self): | 23 def run(self): |
| 24 self._start_event.set() | 24 self._start_event.set() |
| 25 self._stop_event.wait() | 25 self._stop_event.wait() |
| 26 | 26 |
| 27 def stop(self): | 27 def stop(self): |
| 28 self._stop_event.set() | 28 self._stop_event.set() |
| 29 self.join() | 29 self.join() |
| 30 | 30 |
| 31 class _ResultFake(object): |
| 32 def __init__(self, value): |
| 33 self.value = value |
| 34 |
| 35 def wasSuccessful(self): |
| 36 return self.value |
| 37 |
| 31 | 38 |
| 32 class ThreadWatcherTestCase(thread_watcher.TestCase): | 39 class ThreadWatcherTestCase(thread_watcher.TestCase): |
| 33 def setUp(self): | 40 def setUp(self): |
| 34 self._fail_called = [] | 41 self._fail_called = [] |
| 35 self.watcher = thread_watcher.ThreadWatcherMixIn() | 42 self.watcher = thread_watcher.ThreadWatcherMixIn() |
| 36 self.watcher.fail = lambda x: self._fail_called.append(x) | 43 self.watcher.fail = lambda x: self._fail_called.append(x) |
| 37 self._threads = [] | 44 self._threads = [] |
| 38 | 45 |
| 39 def tearDown(self): | 46 def tearDown(self): |
| 40 for t in self._threads: | 47 for t in self._threads: |
| (...skipping 23 matching lines...) Expand all Loading... |
| 64 re.compile( | 71 re.compile( |
| 65 '^Found 2 running thread\(s\) after the test.\n\n' | 72 '^Found 2 running thread\(s\) after the test.\n\n' |
| 66 'Thread <_PuppetThread\(foo, started daemon \d+\)> stacktrace:\n' | 73 'Thread <_PuppetThread\(foo, started daemon \d+\)> stacktrace:\n' |
| 67 ' .*\n\n' | 74 ' .*\n\n' |
| 68 'Thread <_PuppetThread\(bar, started daemon \d+\)> stacktrace:\n' | 75 'Thread <_PuppetThread\(bar, started daemon \d+\)> stacktrace:\n' |
| 69 ' .*\n$', re.DOTALL) | 76 ' .*\n$', re.DOTALL) |
| 70 ) | 77 ) |
| 71 self.assertNotIn(' Thread stopped while acquiring stacktrace.\n', | 78 self.assertNotIn(' Thread stopped while acquiring stacktrace.\n', |
| 72 error_message) | 79 error_message) |
| 73 | 80 |
| 81 def test_extra_threads_unittest_pass(self): |
| 82 # Test succeeded, so thread_watcher must cause failure. |
| 83 self.watcher._resultForDoCleanups = _ResultFake(True) |
| 84 self.watcher.setUp() |
| 85 self._threads.append(_PuppetThread('foo')) |
| 86 self.watcher.tearDown() |
| 87 self.assertEqual(len(self._fail_called), 1) |
| 88 |
| 89 def test_extra_threads_unittest_fail(self): |
| 90 # Test failed already, so thread_watcher must ignore result. |
| 91 self.watcher._resultForDoCleanups = _ResultFake(False) |
| 92 self.watcher.setUp() |
| 93 self._threads.append(_PuppetThread('foo')) |
| 94 self.watcher.tearDown() |
| 95 self.assertEqual(self._fail_called, []) |
| 96 |
| 97 def test_extra_threads_expect_tests_pass(self): |
| 98 # Test succeeded, so thread_watcher must cause failure. |
| 99 self.watcher._test_failed_with_exception = False |
| 100 self.watcher.setUp() |
| 101 self._threads.append(_PuppetThread('foo')) |
| 102 self.watcher.tearDown() |
| 103 self.assertEqual(len(self._fail_called), 1) |
| 104 |
| 105 def test_extra_threads_expect_tests_fail(self): |
| 106 # Test failed already, so thread_watcher must ignore result. |
| 107 self.watcher._test_failed_with_exception = True |
| 108 self.watcher.setUp() |
| 109 self._threads.append(_PuppetThread('foo')) |
| 110 self.watcher.tearDown() |
| 111 self.assertEqual(self._fail_called, []) |
| 112 |
| 74 def test_fail_get_stacktrace(self): | 113 def test_fail_get_stacktrace(self): |
| 75 self.watcher.setUp() | 114 self.watcher.setUp() |
| 76 self._threads.append(_PuppetThread('foo')) | 115 self._threads.append(_PuppetThread('foo')) |
| 77 try: | 116 try: |
| 78 old = sys._current_frames | 117 old = sys._current_frames |
| 79 sys._current_frames = lambda: {} | 118 sys._current_frames = lambda: {} |
| 80 self.watcher.tearDown() | 119 self.watcher.tearDown() |
| 81 finally: | 120 finally: |
| 82 sys._current_frames = old | 121 sys._current_frames = old |
| 83 | 122 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 101 self._threads.append(_PuppetThread(name)) | 140 self._threads.append(_PuppetThread(name)) |
| 102 | 141 |
| 103 def test_ok(self): | 142 def test_ok(self): |
| 104 pass | 143 pass |
| 105 | 144 |
| 106 def test_ok_with_threads(self): | 145 def test_ok_with_threads(self): |
| 107 self._threads.append(_PuppetThread('ok')) | 146 self._threads.append(_PuppetThread('ok')) |
| 108 self._threads[-1].stop() | 147 self._threads[-1].stop() |
| 109 | 148 |
| 110 | 149 |
| 111 | |
| 112 if __name__ == '__main__': | 150 if __name__ == '__main__': |
| 113 unittest.main() | 151 unittest.main() |
| OLD | NEW |