| OLD | NEW |
| (Empty) | |
| 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 |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import mock |
| 6 import threading |
| 7 import time |
| 8 import unittest |
| 9 |
| 10 from testing_support import thread_watcher |
| 11 |
| 12 class TestNoExtraThreads(thread_watcher.TestCase): |
| 13 def mock_test(self): |
| 14 pass |
| 15 |
| 16 |
| 17 class TestExtraThreads(thread_watcher.TestCase): |
| 18 def _start_thread(self, name='foo'): |
| 19 stop = threading.Event() |
| 20 def thread_func(): |
| 21 while not stop.is_set(): |
| 22 time.sleep(0.01) |
| 23 |
| 24 t = threading.Thread(target=thread_func, name=name) |
| 25 t.start() |
| 26 return t, stop |
| 27 |
| 28 def mock_test(self): |
| 29 self.t1, self.stop1 = self._start_thread('foo') |
| 30 self.t2, self.stop2 = self._start_thread('bar') |
| 31 |
| 32 def stopThreads(self): |
| 33 self.stop1.set() |
| 34 self.stop2.set() |
| 35 self.t1.join() |
| 36 self.t2.join() |
| 37 |
| 38 |
| 39 class TestAutoStubTestCase(thread_watcher.TestCase): |
| 40 @mock.patch('unittest.TestCase.fail') |
| 41 def test_no_extra_threads(self, fail_mock): |
| 42 TestNoExtraThreads('mock_test').run() |
| 43 self.assertFalse(fail_mock.called) |
| 44 |
| 45 @mock.patch('unittest.TestCase.fail') |
| 46 def test_extra_threads(self, fail_mock): |
| 47 t = TestExtraThreads('mock_test') |
| 48 t.run() |
| 49 t.stopThreads() |
| 50 self.assertEqual( |
| 51 fail_mock.call_args_list, |
| 52 [mock.call('Found 2 running thread(s) after the test: foo, bar')]) |
| 53 |
| 54 |
| 55 if __name__ == '__main__': |
| 56 unittest.main() |
| OLD | NEW |