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

Side by Side Diff: testing_support/thread_watcher.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: Addressed comments 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
« no previous file with comments | « testing_support/tests/thread_watcher_test.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 # found in the LICENSE file.
4
5
6 import sys
7 import threading
8 import traceback
9 import unittest
10
11
12 class ThreadWatcherMixIn(object):
13 def setUp(self):
14 self._pre_test_threads = [t.ident for t in threading.enumerate()]
15
16 def tearDown(self):
17 post_test_threads = threading.enumerate()
18 new_threads = [t for t in post_test_threads
19 if t.ident not in self._pre_test_threads]
20 if new_threads:
21 details = []
22 for th in new_threads:
23 details.append('\nThread %s stacktrace:\n' % th)
24 try:
25 details.extend(traceback.format_stack(sys._current_frames()[t.ident]))
26 except Exception:
27 if t.ident in sys._current_frames():
28 raise
29 # This can happen due to threads starting or stopping concurrently.
30 details.append(' Thread stopped while acquiring stacktrace.\n')
31 details = ''.join(details)
32
33 self.fail('Found %d running thread(s) after the test.\n%s' % (
34 len(new_threads), details))
35
36
37
38 class TestCase(unittest.TestCase, ThreadWatcherMixIn):
39 """Base unittest class that fails on leaked threads after the test."""
40 def setUp(self):
41 super(TestCase, self).setUp()
42 ThreadWatcherMixIn.setUp(self)
43
44 def tearDown(self):
45 ThreadWatcherMixIn.tearDown(self)
46 super(TestCase, self).tearDown()
OLDNEW
« no previous file with comments | « testing_support/tests/thread_watcher_test.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698