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

Unified Diff: tests/threading_utils_test.py

Issue 25478010: Add ThreadPool.abort() to stop processing early. (Closed) Base URL: https://chromium.googlesource.com/a/chromium/tools/swarm_client@master
Patch Set: Add TODO Created 7 years, 2 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « googletest/trace_test_cases.py ('k') | tools/isolateserver_load_test.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/threading_utils_test.py
diff --git a/tests/threading_utils_test.py b/tests/threading_utils_test.py
index 723d4db4ac95e867994bdf2cae341f1e73d57359..e06dc65e2efb8237364e96c232792fb7f96a4570 100755
--- a/tests/threading_utils_test.py
+++ b/tests/threading_utils_test.py
@@ -266,6 +266,34 @@ class ThreadPoolTest(unittest.TestCase):
actual = pool.join()
self.assertEqual(['a', 'c', 'b'], actual)
+ @timeout(2)
+ def test_abort(self):
+ # Trigger a ridiculous amount of tasks, and abort the remaining.
+ with threading_utils.ThreadPool(2, 2, 0) as pool:
+ # Allow 10 tasks to run initially.
+ sem = threading.Semaphore(10)
+
+ def grab_and_return(x):
+ sem.acquire()
+ return x
+
+ for i in range(100):
+ pool.add_task(0, grab_and_return, i)
+
+ # Running at 11 would hang.
+ results = [pool.get_one_result() for _ in xrange(10)]
+ # At that point, there's 10 completed tasks and 2 tasks hanging, 88
+ # pending.
+ self.assertEqual(88, pool.abort())
+ # Calling .join() before these 2 .release() would hang.
+ sem.release()
+ sem.release()
+ results.extend(pool.join())
+ # The results *may* be out of order. Even if the calls are processed
+ # strictly in FIFO mode, a thread may preempt another one when returning the
+ # values.
+ self.assertEqual(range(12), sorted(results))
+
class AutoRetryThreadPoolTest(unittest.TestCase):
def test_bad_class(self):
« no previous file with comments | « googletest/trace_test_cases.py ('k') | tools/isolateserver_load_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698