Index: testing_support/thread_watcher.py |
diff --git a/testing_support/thread_watcher.py b/testing_support/thread_watcher.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6929ffc38e6006ecc10c30b84fe111f072101ede |
--- /dev/null |
+++ b/testing_support/thread_watcher.py |
@@ -0,0 +1,31 @@ |
+# Copyright (c) 2011 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.
|
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+ |
+import threading |
+import unittest |
+ |
+ |
+class ThreadWatcherMixIn(object): |
+ def setUp(self): |
+ self._pre_test_threads = [t.ident for t in threading.enumerate()] |
tandrii(chromium)
2016/06/13 17:03:28
strictly speaking, these numbers can be re-used.
Sergiy Byelozyorov
2016/06/14 06:12:14
Agree, but I am not sure how otherwise we can chec
|
+ |
+ def tearDown(self): |
+ new_threads = [t for t in threading.enumerate() |
+ if t.ident not in self._pre_test_threads] |
+ if new_threads: |
+ self.fail('Found %d running thread(s) after the test: %s' % ( |
+ len(new_threads), ', '.join(t.name for t in new_threads))) |
tandrii(chromium)
2016/06/13 17:03:28
t.name is usually not given, at least in infra lan
Sergiy Byelozyorov
2016/06/14 06:12:14
t.name defaults to Thread-NN, where NN-some unique
tandrii(chromium)
2016/06/14 10:21:43
Yes, and as result both suck for debugging, unless
Sergiy Byelozyorov
2016/06/14 19:47:01
Done.
|
+ |
+ |
+ |
+class TestCase(unittest.TestCase, ThreadWatcherMixIn): |
+ """Base unittest class that cleans off a trial directory in tearDown().""" |
+ def setUp(self): |
+ super(TestCase, self).setUp() |
+ ThreadWatcherMixIn.setUp(self) |
+ |
+ def tearDown(self): |
+ ThreadWatcherMixIn.tearDown(self) |
+ super(TestCase, self).tearDown() |