Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: testing_support/tests/thread_watcher_test.py

Issue 2067533002: Check for stray threads from tests and fail if found (Closed) Base URL: https://chromium.googlesource.com/infra/testing/testing_support.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2015 The Chromium Authors. All rights reserved.
tandrii(chromium) 2016/06/13 17:03:28 nit 2016
Sergiy Byelozyorov 2016/06/14 06:12:14 Done.
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()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698