1 # Copyright 2013 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.
7 class TestCollection(object):
8 """A threadsafe collection of tests.
11 tests: List of tests to put in the collection.
14 def __init__(self
, tests
=None):
17 self
._lock
= threading
.Lock()
19 self
._tests
_in
_progress
= 0
20 # Used to signal that an item is available or all items have been handled.
21 self
._item
_available
_or
_all
_done
= threading
.Event()
26 """Pop a test from the collection.
28 Waits until a test is available or all tests have been handled.
31 A test or None if all tests have been handled.
34 # Wait for a test to be available or all tests to have been handled.
35 self
._item
_available
_or
_all
_done
.wait()
37 # Check which of the two conditions triggered the signal.
38 if self
._tests
_in
_progress
== 0:
41 return self
._tests
.pop(0)
43 # Another thread beat us to the available test, wait again.
44 self
._item
_available
_or
_all
_done
.clear()
47 """Add a test to the collection.
53 self
._tests
.append(test
)
54 self
._item
_available
_or
_all
_done
.set()
55 self
._tests
_in
_progress
+= 1
57 def test_completed(self
):
58 """Indicate that a test has been fully handled."""
60 self
._tests
_in
_progress
-= 1
61 if self
._tests
_in
_progress
== 0:
62 # All tests have been handled, signal all waiting threads.
63 self
._item
_available
_or
_all
_done
.set()
66 """Iterate through tests in the collection until all have been handled."""
74 """Return the number of tests currently in the collection."""
75 return len(self
._tests
)
78 """Return a list of the names of the tests currently in the collection."""
80 return list(t
.test
for t
in self
._tests
)