Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | from test import support |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 2 | import unittest |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 3 | import dummy_threading as _threading |
| 4 | import time |
| 5 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 6 | class DummyThreadingTestCase(unittest.TestCase): |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 7 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8 | class TestThread(_threading.Thread): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 9 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 10 | def run(self): |
| 11 | global running |
| 12 | global sema |
| 13 | global mutex |
| 14 | # Uncomment if testing another module, such as the real 'threading' |
| 15 | # module. |
| 16 | #delay = random.random() * 2 |
| 17 | delay = 0 |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 18 | if support.verbose: |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 19 | print('task', self.getName(), 'will run for', delay, 'sec') |
| 20 | sema.acquire() |
| 21 | mutex.acquire() |
| 22 | running += 1 |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 23 | if support.verbose: |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 24 | print(running, 'tasks are running') |
| 25 | mutex.release() |
| 26 | time.sleep(delay) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 27 | if support.verbose: |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 28 | print('task', self.getName(), 'done') |
| 29 | mutex.acquire() |
| 30 | running -= 1 |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 31 | if support.verbose: |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 32 | print(self.getName(), 'is finished.', running, 'tasks are running') |
| 33 | mutex.release() |
| 34 | sema.release() |
| 35 | |
| 36 | def setUp(self): |
| 37 | self.numtasks = 10 |
| 38 | global sema |
| 39 | sema = _threading.BoundedSemaphore(value=3) |
| 40 | global mutex |
| 41 | mutex = _threading.RLock() |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 42 | global running |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 43 | running = 0 |
| 44 | self.threads = [] |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 45 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 46 | def test_tasks(self): |
| 47 | for i in range(self.numtasks): |
| 48 | t = self.TestThread(name="<thread %d>"%i) |
| 49 | self.threads.append(t) |
| 50 | t.start() |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 51 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 52 | if support.verbose: |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 53 | print('waiting for all tasks to complete') |
| 54 | for t in self.threads: |
| 55 | t.join() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 56 | if support.verbose: |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 57 | print('all tasks done') |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 58 | |
| 59 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 60 | support.run_unittest(DummyThreadingTestCase) |
Guido van Rossum | ad50ca9 | 2002-12-30 22:30:22 +0000 | [diff] [blame] | 61 | |
| 62 | |
| 63 | if __name__ == '__main__': |
| 64 | test_main() |