Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_dummy_threading.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 024b2b984866 |
| children |
| rev | line source |
|---|---|
|
45816
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
1 from test import test_support |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
2 import unittest |
|
26890
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 import dummy_threading as _threading |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 import time |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 |
|
45816
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
6 class DummyThreadingTestCase(unittest.TestCase): |
|
26890
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 |
|
45816
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
8 class TestThread(_threading.Thread): |
|
27963
226463445697
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
26890
diff
changeset
|
9 |
|
45816
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
10 def run(self): |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
11 global running |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
12 global sema |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
13 global mutex |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
14 # Uncomment if testing another module, such as the real 'threading' |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
15 # module. |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
16 #delay = random.random() * 2 |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
17 delay = 0 |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
18 if test_support.verbose: |
|
48289
024b2b984866
change a few uses of the threading APIs
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
19 print 'task', self.name, 'will run for', delay, 'sec' |
|
45816
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
20 sema.acquire() |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
21 mutex.acquire() |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
22 running += 1 |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
23 if test_support.verbose: |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
24 print running, 'tasks are running' |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
25 mutex.release() |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
26 time.sleep(delay) |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
27 if test_support.verbose: |
|
48289
024b2b984866
change a few uses of the threading APIs
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
28 print 'task', self.name, 'done' |
|
45816
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
29 mutex.acquire() |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
30 running -= 1 |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
31 if test_support.verbose: |
|
48289
024b2b984866
change a few uses of the threading APIs
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
32 print self.name, 'is finished.', running, 'tasks are running' |
|
45816
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
33 mutex.release() |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
34 sema.release() |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
35 |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
36 def setUp(self): |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
37 self.numtasks = 10 |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
38 global sema |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
39 sema = _threading.BoundedSemaphore(value=3) |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
40 global mutex |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
41 mutex = _threading.RLock() |
|
26890
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 global running |
|
45816
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
43 running = 0 |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
44 self.threads = [] |
|
26890
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 |
|
45816
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
46 def test_tasks(self): |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
47 for i in range(self.numtasks): |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
48 t = self.TestThread(name="<thread %d>"%i) |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
49 self.threads.append(t) |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
50 t.start() |
|
26890
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 |
|
45816
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
52 if test_support.verbose: |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
53 print 'waiting for all tasks to complete' |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
54 for t in self.threads: |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
55 t.join() |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
56 if test_support.verbose: |
|
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
57 print 'all tasks done' |
|
26890
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 def test_main(): |
|
45816
3745fdb9932f
Convert test_dummy_threading and test_dbm to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
60 test_support.run_unittest(DummyThreadingTestCase) |
|
26890
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 if __name__ == '__main__': |
|
8053bc0b8bb5
Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 test_main() |
